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 junit.framework.TestCase;
014 import org.picocontainer.MutablePicoContainer;
015 import org.picocontainer.PicoCompositionException;
016 import org.picocontainer.DefaultPicoContainer;
017 import org.picocontainer.injectors.AbstractInjector;
018 import org.picocontainer.testmodel.DependsOnTouchable;
019 import org.picocontainer.testmodel.SimpleTouchable;
020
021 public class DelegatingPicoContainerTestCase extends TestCase {
022 private MutablePicoContainer parent;
023 private DefaultPicoContainer child;
024
025 public void setUp() throws PicoCompositionException {
026 parent = new DefaultPicoContainer();
027 child = new DefaultPicoContainer(parent);
028 }
029
030 public void testChildGetsFromParent() {
031 parent.addComponent(SimpleTouchable.class);
032 child.addComponent(DependsOnTouchable.class);
033 DependsOnTouchable dependsOnTouchable = child.getComponent(DependsOnTouchable.class);
034
035 assertNotNull(dependsOnTouchable);
036 }
037
038 public void testParentDoesntGetFromChild() {
039 child.addComponent(SimpleTouchable.class);
040 parent.addComponent(DependsOnTouchable.class);
041 try {
042 parent.getComponent(DependsOnTouchable.class);
043 fail();
044 } catch (AbstractInjector.UnsatisfiableDependenciesException e) {
045 }
046 }
047
048 public void testChildOverridesParent() {
049 parent.addComponent(SimpleTouchable.class);
050 child.addComponent(SimpleTouchable.class);
051
052 SimpleTouchable parentTouchable = parent.getComponent(SimpleTouchable.class);
053 SimpleTouchable childTouchable = child.getComponent(SimpleTouchable.class);
054 assertEquals(1, child.getComponents().size());
055 assertNotSame(parentTouchable, childTouchable);
056 }
057 }