KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > defaults > DelegatingPicoContainerTestCase


1 /*****************************************************************************
2  * Copyright (c) PicoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
9  *****************************************************************************/

10
11 package org.picocontainer.defaults;
12
13 import junit.framework.TestCase;
14 import org.picocontainer.MutablePicoContainer;
15 import org.picocontainer.PicoInitializationException;
16 import org.picocontainer.PicoRegistrationException;
17 import org.picocontainer.testmodel.DependsOnTouchable;
18 import org.picocontainer.testmodel.SimpleTouchable;
19
20 public class DelegatingPicoContainerTestCase extends TestCase {
21     private MutablePicoContainer parent;
22     private DefaultPicoContainer child;
23
24     public void setUp() throws PicoRegistrationException, PicoInitializationException {
25         parent = new DefaultPicoContainer();
26         child = new DefaultPicoContainer(parent);
27     }
28
29     public void testChildGetsFromParent() {
30         parent.registerComponentImplementation(SimpleTouchable.class);
31         child.registerComponentImplementation(DependsOnTouchable.class);
32         DependsOnTouchable dependsOnTouchable = (DependsOnTouchable) child.getComponentInstance(DependsOnTouchable.class);
33
34         assertNotNull(dependsOnTouchable);
35     }
36
37     public void testParentDoesntGetFromChild() {
38         child.registerComponentImplementation(SimpleTouchable.class);
39         parent.registerComponentImplementation(DependsOnTouchable.class);
40         try {
41             parent.getComponentInstance(DependsOnTouchable.class);
42             fail();
43         } catch (UnsatisfiableDependenciesException e) {
44         }
45     }
46
47     public void testChildOverridesParent() {
48         parent.registerComponentImplementation(SimpleTouchable.class);
49         child.registerComponentImplementation(SimpleTouchable.class);
50
51         SimpleTouchable parentTouchable = (SimpleTouchable) parent.getComponentInstance(SimpleTouchable.class);
52         SimpleTouchable childTouchable = (SimpleTouchable) child.getComponentInstance(SimpleTouchable.class);
53         assertEquals(1, child.getComponentInstances().size());
54         assertNotSame(parentTouchable, childTouchable);
55     }
56 }
57
Popular Tags