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 picocontainer.aggregated;
12
13 import junit.framework.TestCase;
14 import picocontainer.ClassRegistrationPicoContainer;
15 import picocontainer.PicoContainer;
16 import picocontainer.PicoInitializationException;
17 import picocontainer.PicoRegistrationException;
18 import picocontainer.hierarchical.HierarchicalPicoContainer;
19 import picocontainer.testmodel.WilmaImpl;
20
21 public class AggregatedContainersContainerTestCase extends TestCase {
22 private ClassRegistrationPicoContainer pico;
23 private AggregatedContainersContainer.Filter filter;
24
25 public void setUp() throws PicoRegistrationException {
26 pico = new HierarchicalPicoContainer.Default();
27 pico.registerComponent(WilmaImpl.class);
28 filter = new AggregatedContainersContainer.Filter(pico);
29 }
30
31 public void testGetComponents() {
32 assertEquals("Content of Component arrays should be the same", pico, filter.getSubject());
33 }
34
35 public void testGetComponentTypes() {
36 assertEquals("Content of Component type arrays should be the same", pico, filter.getSubject());
37 }
38
39 public void testGetComponent() {
40 assertSame("Wilma should be the same", pico.getComponent(WilmaImpl.class), filter.getComponent(WilmaImpl.class));
41 }
42
43 public void testHasComponent() {
44 assertEquals("Containers should contain the same", pico.hasComponent(WilmaImpl.class), filter.hasComponent(WilmaImpl.class));
45 }
46
47 public void testNullContainer() {
48 try {
49 AggregatedContainersContainer.Filter badOne = new AggregatedContainersContainer.Filter(null);
50 fail("Should have failed with an NPE");
51 } catch (NullPointerException e) {
52 // fine
53 }
54 }
55
56 public void testNullArrayContainer() {
57 try {
58 AggregatedContainersContainer badOne = new AggregatedContainersContainer(null);
59 fail("Should have failed with an NPE");
60 } catch (NullPointerException e) {
61 // fine
62 }
63 }
64
65 public void testGetToFilterFor() {
66 assertSame("The PicoContainer to filter for should be the one made in setUp", pico, filter.getSubject());
67 }
68
69 public void testBasic() {
70
71 final String acomp = "hello";
72 final Integer bcomp = new Integer(123);
73
74 PicoContainer a = new PicoContainer() {
75 public boolean hasComponent(Class compType) {
76 return compType == String.class;
77 }
78
79 public Object getComponent(Class compType) {
80 return compType == String.class ? acomp : null;
81 }
82
83 public Object[] getComponents() {
84 return new Object[] {acomp};
85 }
86
87 public Class[] getComponentTypes() {
88 return new Class[] {String.class};
89 }
90
91 public void instantiateComponents() throws PicoInitializationException {
92 }
93 };
94
95 PicoContainer b = new PicoContainer() {
96 public boolean hasComponent(Class compType) {
97 return compType == Integer.class;
98 }
99
100 public Object getComponent(Class compType) {
101 return compType == Integer.class ? bcomp : null;
102 }
103
104 public Object[] getComponents() {
105 return new Object[] {bcomp};
106 }
107
108 public Class[] getComponentTypes() {
109 return new Class[] {Integer.class};
110 }
111
112 public void instantiateComponents() throws PicoInitializationException {
113 }
114 };
115
116 AggregatedContainersContainer acc = new AggregatedContainersContainer(new PicoContainer[] {a, b});
117
118 assertTrue(acc.hasComponent(String.class));
119 assertTrue(acc.hasComponent(Integer.class));
120 assertTrue(acc.getComponent(String.class) == acomp);
121 assertTrue(acc.getComponent(Integer.class) == bcomp);
122 assertTrue(acc.getComponents().length == 2);
123
124 }
125
126 public void testEmpty() {
127
128 AggregatedContainersContainer acc = new AggregatedContainersContainer(new PicoContainer[0]);
129 assertTrue(acc.hasComponent(String.class) == false);
130 assertTrue(acc.getComponent(String.class) == null);
131 assertTrue(acc.getComponents().length == 0);
132
133 }
134 }
This page was automatically generated by Maven