001 /*****************************************************************************
002 * Copyright (c) PicoContainer Organization. All rights reserved. *
003 * ------------------------------------------------------------------------- *
004 * The software in this package is published under the terms of the BSD *
005 * style license a copy of which has been included with this distribution in *
006 * the LICENSE.txt file. *
007 * *
008 * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
009 *****************************************************************************/
010
011 package org.picocontainer.defaults;
012
013 import static org.junit.Assert.assertEquals;
014 import static org.junit.Assert.assertNotNull;
015 import static org.junit.Assert.assertNotSame;
016 import static org.junit.Assert.fail;
017
018 import org.junit.Before;
019 import org.junit.Test;
020 import org.picocontainer.DefaultPicoContainer;
021 import org.picocontainer.MutablePicoContainer;
022 import org.picocontainer.PicoCompositionException;
023 import org.picocontainer.injectors.AbstractInjector;
024 import org.picocontainer.testmodel.DependsOnTouchable;
025 import org.picocontainer.testmodel.SimpleTouchable;
026
027 public class DelegatingPicoContainerTestCase {
028 private MutablePicoContainer parent;
029 private DefaultPicoContainer child;
030
031 @Before
032 public void setUp() throws PicoCompositionException {
033 parent = new DefaultPicoContainer();
034 child = new DefaultPicoContainer(parent);
035 }
036
037 @Test public void testChildGetsFromParent() {
038 parent.addComponent(SimpleTouchable.class);
039 child.addComponent(DependsOnTouchable.class);
040 DependsOnTouchable dependsOnTouchable = child.getComponent(DependsOnTouchable.class);
041
042 assertNotNull(dependsOnTouchable);
043 }
044
045 @Test public void testParentDoesntGetFromChild() {
046 child.addComponent(SimpleTouchable.class);
047 parent.addComponent(DependsOnTouchable.class);
048 try {
049 parent.getComponent(DependsOnTouchable.class);
050 fail();
051 } catch (AbstractInjector.UnsatisfiableDependenciesException e) {
052 }
053 }
054
055 @Test public void testChildOverridesParent() {
056 parent.addComponent(SimpleTouchable.class);
057 child.addComponent(SimpleTouchable.class);
058
059 SimpleTouchable parentTouchable = parent.getComponent(SimpleTouchable.class);
060 SimpleTouchable childTouchable = child.getComponent(SimpleTouchable.class);
061 assertEquals(1, child.getComponents().size());
062 assertNotSame(parentTouchable, childTouchable);
063 }
064 }