KickJava   Java API By Example, From Geeks To Geeks.

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


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.Parameter;
14 import org.picocontainer.testmodel.DecoratedTouchable;
15 import org.picocontainer.testmodel.SimpleTouchable;
16 import org.picocontainer.testmodel.Touchable;
17 import org.picocontainer.testmodel.DependsOnTouchable;
18
19 import java.util.Collections JavaDoc;
20
21 /**
22  * @author Thomas Heller
23  * @author Aslak Hellesøy
24  * @version $Revision: 2306 $
25  */

26 public class ComponentKeysTestCase extends TestCase {
27     public void testComponensRegisteredWithClassKeyTakePrecedenceOverOthersWhenThereAreMultipleImplementations() throws Exception JavaDoc {
28         DefaultPicoContainer pico = new DefaultPicoContainer();
29         pico.registerComponentImplementation("default", SimpleTouchable.class);
30
31         /**
32          * By using a class as key, this should take precedence over the other Touchable
33          */

34         pico.registerComponentImplementation(Touchable.class, DecoratedTouchable.class, new Parameter[]{
35                             new ComponentParameter("default")
36                     });
37
38         Touchable touchable = (Touchable) pico.getComponentInstanceOfType(Touchable.class);
39         assertEquals(DecoratedTouchable.class, touchable.getClass());
40     }
41
42     public void testComponentAdapterResolutionIsFirstLookedForByClassKeyToTheTopOfTheContainerHierarchy() {
43         DefaultPicoContainer pico = new DefaultPicoContainer();
44         pico.registerComponentImplementation("default", SimpleTouchable.class);
45
46         // Use the List variant instead, so we get better test coverage.
47
pico.registerComponentImplementation(Touchable.class, DecoratedTouchable.class, Collections.singletonList(new ComponentParameter("default")));
48
49         DefaultPicoContainer grandChild = new DefaultPicoContainer(new DefaultPicoContainer(new DefaultPicoContainer(pico)));
50
51         Touchable touchable = (Touchable) grandChild.getComponentInstanceOfType(Touchable.class);
52         assertEquals(DecoratedTouchable.class, touchable.getClass());
53
54     }
55
56     public void testComponentKeysFromParentCannotConfuseTheChild() throws Exception JavaDoc {
57         DefaultPicoContainer pico = new DefaultPicoContainer();
58         pico.registerComponentImplementation("test", SimpleTouchable.class);
59
60         DefaultPicoContainer child = new DefaultPicoContainer(pico);
61
62         child.registerComponentImplementation("test", DependsOnTouchable.class);
63
64         DependsOnTouchable dot = (DependsOnTouchable) child.getComponentInstance("test");
65
66         assertNotNull(dot);
67     }
68
69 }
70
Popular Tags