KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Original code by *
9  *****************************************************************************/

10 package org.picocontainer.defaults;
11
12 import junit.framework.TestCase;
13 import org.picocontainer.MutablePicoContainer;
14 import org.picocontainer.Parameter;
15 import org.picocontainer.testmodel.AlternativeTouchable;
16 import org.picocontainer.testmodel.DependsOnTouchable;
17 import org.picocontainer.testmodel.SimpleTouchable;
18 import org.picocontainer.testmodel.Touchable;
19
20 /**
21  * @author Paul Hammant
22  * @author Aslak Hellesøy
23  * @author Rafal Krzewski
24  * @version $Revision: 2286 $
25  */

26 public class ChildContainerTestCase extends TestCase {
27
28     public void testParentContainerWithComponentWithEqualKeyShouldBeShadowedByChild() throws Exception JavaDoc {
29         DefaultPicoContainer parent = new DefaultPicoContainer();
30         DefaultPicoContainer child = new DefaultPicoContainer(parent);
31
32         parent.registerComponentImplementation("key", AlternativeTouchable.class);
33         child.registerComponentImplementation("key", SimpleTouchable.class);
34         child.registerComponentImplementation(DependsOnTouchable.class);
35
36         DependsOnTouchable dot = (DependsOnTouchable) child.getComponentInstanceOfType(DependsOnTouchable.class);
37         assertEquals(SimpleTouchable.class, dot.getTouchable().getClass());
38     }
39
40     public void testParentComponentRegisteredAsClassShouldBePreffered() throws Exception JavaDoc {
41         DefaultPicoContainer parent = new DefaultPicoContainer();
42         DefaultPicoContainer child = new DefaultPicoContainer(parent);
43
44         parent.registerComponentImplementation(Touchable.class, AlternativeTouchable.class);
45         child.registerComponentImplementation("key", SimpleTouchable.class);
46         child.registerComponentImplementation(DependsOnTouchable.class);
47
48         DependsOnTouchable dot = (DependsOnTouchable) child.getComponentInstanceOfType(DependsOnTouchable.class);
49         assertEquals(AlternativeTouchable.class, dot.getTouchable().getClass());
50     }
51
52     public void testResolveFromParentByType() {
53         MutablePicoContainer parent = new DefaultPicoContainer();
54         parent.registerComponentImplementation(Touchable.class, SimpleTouchable.class);
55
56         MutablePicoContainer child = new DefaultPicoContainer(parent);
57         child.registerComponentImplementation(DependsOnTouchable.class);
58
59         assertNotNull(child.getComponentInstance(DependsOnTouchable.class));
60     }
61
62     public void testResolveFromParentByKey() {
63         MutablePicoContainer parent = new DefaultPicoContainer();
64         parent.registerComponentImplementation(Touchable.class, SimpleTouchable.class);
65
66         MutablePicoContainer child = new DefaultPicoContainer(parent);
67         child.registerComponentImplementation(DependsOnTouchable.class, DependsOnTouchable.class,
68                 new Parameter[]{new ComponentParameter(Touchable.class)});
69
70         assertNotNull(child.getComponentInstance(DependsOnTouchable.class));
71     }
72
73     public void testResolveFromGrandParentByType() {
74         MutablePicoContainer grandParent = new DefaultPicoContainer();
75         grandParent.registerComponentImplementation(Touchable.class, SimpleTouchable.class);
76
77         MutablePicoContainer parent = new DefaultPicoContainer(grandParent);
78
79         MutablePicoContainer child = new DefaultPicoContainer(parent);
80         child.registerComponentImplementation(DependsOnTouchable.class);
81
82         assertNotNull(child.getComponentInstance(DependsOnTouchable.class));
83     }
84
85     public void testResolveFromGrandParentByKey() {
86         MutablePicoContainer grandParent = new DefaultPicoContainer();
87         grandParent.registerComponentImplementation(Touchable.class, SimpleTouchable.class);
88
89         MutablePicoContainer parent = new DefaultPicoContainer(grandParent);
90
91         MutablePicoContainer child = new DefaultPicoContainer(parent);
92         child.registerComponentImplementation(DependsOnTouchable.class, DependsOnTouchable.class,
93                 new Parameter[]{new ComponentParameter(Touchable.class)});
94
95         assertNotNull(child.getComponentInstance(DependsOnTouchable.class));
96     }
97 }
98
Popular Tags