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