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.extras;
12
13 import junit.framework.TestCase;
14 import org.picocontainer.PicoInitializationException;
15 import org.picocontainer.PicoRegistrationException;
16 import org.picocontainer.defaults.DefaultComponentRegistry;
17 import org.picocontainer.defaults.DefaultPicoContainer;
18 import org.picocontainer.testmodel.DependsOnTouchable;
19 import org.picocontainer.testmodel.SimpleTouchable;
20 import org.picocontainer.testmodel.Touchable;
21
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
29 public class HierarchicalComponentRegistryTestCase extends TestCase {
30
31 public void testBasicContainerAsserts() {
32 try {
33 new HierarchicalComponentRegistry.WithChildRegistry(null, new DefaultComponentRegistry());
34 fail("Should have had NPE)");
35 } catch (NullPointerException npe) {
36 // expected
37 }
38 try {
39 new HierarchicalComponentRegistry.WithChildRegistry(new DefaultComponentRegistry(), null);
40 fail("Should have had NPE)");
41 } catch (NullPointerException npe) {
42 // expected
43 }
44
45 }
46
47 public void testBasicRegAndStart() throws PicoInitializationException, PicoRegistrationException {
48 DefaultComponentRegistry dcr = new DefaultComponentRegistry();
49 DefaultPicoContainer dpc = new DefaultPicoContainer.WithComponentRegistry(dcr);
50 HierarchicalComponentRegistry hcr = new HierarchicalComponentRegistry.Default(dcr);
51 DefaultPicoContainer dpc2 = new DefaultPicoContainer.WithComponentRegistry(hcr);
52
53 dpc.registerComponentByClass(DependsOnTouchable.class);
54 dpc.registerComponentByClass(SimpleTouchable.class);
55 HashMap hashMap = new HashMap();
56 HashSet hashSet = new HashSet();
57 dpc.registerComponent(Map.class, hashMap);
58 dpc2.registerComponent(Set.class, hashSet);
59
60 dpc.instantiateComponents();
61 dpc2.instantiateComponents();
62
63 Set set = hcr.getComponentInstanceKeys();
64 assertTrue(set.contains(DependsOnTouchable.class));
65 assertTrue(set.contains(SimpleTouchable.class));
66 assertTrue(set.contains(Map.class));
67 assertTrue(set.contains(Set.class));
68
69 List list = hcr.getOrderedComponents();
70 assertTrue(list.contains(hashMap));
71 assertTrue(list.contains(hashSet));
72
73 assertEquals("There should be two comps in the internals", 4, hcr.getComponentInstances().size());
74
75 assertTrue("There should have been a Fred in the registry", hcr.hasComponentInstance(DependsOnTouchable.class));
76 assertTrue("There should have been a Touchable in the registry", hcr.hasComponentInstance(SimpleTouchable.class));
77 assertTrue("There should have been a Map in the registry", hcr.hasComponentInstance(Map.class));
78 assertTrue("There should have been a Set in the registry", hcr.hasComponentInstance(Set.class));
79
80 }
81
82 public static class DerivedTouchable extends SimpleTouchable {
83 public DerivedTouchable() {
84 }
85 }
86
87 public void testRegisterComponentWithObject() throws PicoRegistrationException, PicoInitializationException {
88 DefaultComponentRegistry parentReg = new DefaultComponentRegistry();
89 DefaultPicoContainer parent = new DefaultPicoContainer.WithComponentRegistry(parentReg);
90 HierarchicalComponentRegistry hcr = new HierarchicalComponentRegistry.Default(parentReg);
91 DefaultPicoContainer child = new DefaultPicoContainer.WithComponentRegistry(hcr);
92
93 parent.registerComponentByClass(DependsOnTouchable.class);
94 parent.registerComponentByInstance(new SimpleTouchable());
95
96 parent.instantiateComponents();
97
98 assertTrue("There should have been a Fred in the internals", child.hasComponent(DependsOnTouchable.class));
99 assertTrue("There should have been a Touchable in the internals", child.hasComponent(SimpleTouchable.class));
100 }
101
102 public void testGetComponentTypes() throws PicoRegistrationException, PicoInitializationException {
103
104 DefaultComponentRegistry parentReg = new DefaultComponentRegistry();
105 DefaultPicoContainer parent = new DefaultPicoContainer.WithComponentRegistry(parentReg);
106 HierarchicalComponentRegistry hcr = new HierarchicalComponentRegistry.Default(parentReg);
107 DefaultPicoContainer child = new DefaultPicoContainer.WithComponentRegistry(hcr);
108
109 parent.registerComponentByClass(DependsOnTouchable.class);
110 parent.registerComponent(Touchable.class, SimpleTouchable.class);
111
112 // You might have thought that starting the internals shouldn't be necessary
113 // just to get the types, but it is. The map holding the types->component instances
114 // doesn't receive anything until the components are instantiated.
115 parent.instantiateComponents();
116 child.instantiateComponents();
117
118 Collection types = child.getComponentKeys();
119 assertEquals("There should be 2 types", 2, types.size());
120 assertTrue("There should be a DependsOnTouchable type", types.contains(DependsOnTouchable.class));
121 assertTrue("There should be a Touchable type", types.contains(Touchable.class));
122 assertTrue("There should not be a SimpleTouchable type", !types.contains(SimpleTouchable.class));
123
124 assertNotNull("Should have a thing implementing Touchable", hcr.findImplementingComponent(Touchable.class));
125 assertEquals("Should have a thing implementing Touchable", hcr.findImplementingComponent(Touchable.class).getClass(), SimpleTouchable.class);
126 assertNotNull("Should have a thing implementing Touchable", hcr.findImplementingComponentSpecification(Touchable.class));
127
128 }
129
130 public void testParentContainer() throws PicoRegistrationException, PicoInitializationException {
131
132 DefaultComponentRegistry parentReg = new DefaultComponentRegistry();
133 DefaultPicoContainer parent = new DefaultPicoContainer.WithComponentRegistry(parentReg);
134 HierarchicalComponentRegistry hcr = new HierarchicalComponentRegistry.Default(parentReg);
135 DefaultPicoContainer child = new DefaultPicoContainer.WithComponentRegistry(hcr);
136
137 parent.registerComponent(Touchable.class, SimpleTouchable.class);
138 parent.instantiateComponents();
139 child.registerComponentByClass(DependsOnTouchable.class);
140 child.instantiateComponents();
141
142 assertEquals("The parent should return 2 components (one from the parent)", 2, child.getComponents().size());
143 assertTrue("Touchable should have been passed through the parent internals", child.hasComponent(Touchable.class));
144 assertTrue("There should have been a DependsOnTouchable in the internals", child.hasComponent(DependsOnTouchable.class));
145
146 }
147
148 }
This page was automatically generated by Maven