KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.picocontainer.defaults;
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.HashSet JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import junit.framework.TestCase;
17
18 import org.picocontainer.ComponentAdapter;
19 import org.picocontainer.MutablePicoContainer;
20 import org.picocontainer.Parameter;
21 import org.picocontainer.PicoContainer;
22 import org.picocontainer.PicoVisitor;
23
24 /**
25  * @author Michael Rimov
26  */

27 public class TraversalCheckingVisitorTestCase extends TestCase {
28
29     private MutablePicoContainer pico;
30
31     private MutablePicoContainer child;
32
33     private ComponentAdapter parentAdapter;
34
35     private ComponentAdapter childAdapter;
36
37     protected void setUp() throws Exception JavaDoc {
38         super.setUp();
39
40         pico = new DefaultPicoContainer();
41         parentAdapter = pico.registerComponent(new SetterInjectionComponentAdapter(StringBuffer JavaDoc.class, StringBuffer JavaDoc.class, null));
42
43         child = pico.makeChildContainer();
44
45         childAdapter = child.registerComponent(new ConstructorInjectionComponentAdapter(ArrayList JavaDoc.class, ArrayList JavaDoc.class,
46             new Parameter[] {
47             new ConstantParameter(new Integer JavaDoc(3))
48         }));
49     }
50
51     protected void tearDown() throws Exception JavaDoc {
52         super.tearDown();
53         child = null;
54         pico = null;
55         parentAdapter = null;
56         childAdapter = null;
57     }
58
59     public void testVisitComponentAdapter() {
60         final int numExpectedComponentAdapters = 2;
61         final List JavaDoc allAdapters = new ArrayList JavaDoc();
62
63         Set JavaDoc knownAdapters = new HashSet JavaDoc();
64         knownAdapters.add(parentAdapter);
65         knownAdapters.add(childAdapter);
66
67         PicoVisitor containerCollector = new TraversalCheckingVisitor() {
68             public void visitComponentAdapter(ComponentAdapter adapter) {
69                 super.visitComponentAdapter(adapter); //Calls checkTraversal for us.
70
allAdapters.add(adapter);
71             }
72         };
73         containerCollector.traverse(pico);
74
75         assertEquals(numExpectedComponentAdapters, allAdapters.size());
76
77         for (Iterator JavaDoc i = allAdapters.iterator(); i.hasNext(); ) {
78             boolean knownAdapter = knownAdapters.remove(i.next());
79             assertTrue("Encountered unknown adapter in collection: " + allAdapters.toString(), knownAdapter);
80         }
81
82         assertTrue("All adapters should match known adapters.", knownAdapters.size() == 0);
83     }
84
85     public void testVisitContainer() {
86         final List JavaDoc allContainers = new ArrayList JavaDoc();
87         final int expectedNumberOfContainers = 2;
88
89         PicoVisitor containerCollector = new TraversalCheckingVisitor() {
90             public void visitContainer(PicoContainer pico) {
91                 super.visitContainer(pico); //Calls checkTraversal for us.
92
allContainers.add(pico);
93             }
94         };
95
96         containerCollector.traverse(pico);
97
98         assertTrue(allContainers.size() == expectedNumberOfContainers);
99
100         Set JavaDoc knownContainers = new HashSet JavaDoc();
101         knownContainers.add(pico);
102         knownContainers.add(child);
103         for (Iterator JavaDoc i = allContainers.iterator(); i.hasNext(); ) {
104             PicoContainer oneContainer = (PicoContainer) i.next();
105             boolean knownContainer = knownContainers.remove(oneContainer);
106             assertTrue("Found a picocontainer that wasn't previously expected.", knownContainer);
107         }
108
109         assertTrue("All containers must match what is returned by traversal.",
110             knownContainers.size() == 0);
111
112     }
113
114
115     public void testVisitParameter() {
116         final List JavaDoc allParameters = new ArrayList JavaDoc();
117
118         PicoVisitor containerCollector = new TraversalCheckingVisitor() {
119             public void visitParameter(Parameter param) {
120                 super.visitParameter(param); //Calls checkTraversal for us.
121
allParameters.add(param);
122             }
123         };
124
125         containerCollector.traverse(pico);
126
127         assertTrue(allParameters.size() == 1);
128         assertTrue(allParameters.get(0) instanceof ConstantParameter);
129         assertTrue( ( (ConstantParameter) allParameters.get(0)).resolveInstance(null, null, null) instanceof Integer JavaDoc);
130         assertEquals(3, ( (Integer JavaDoc) ( (ConstantParameter) allParameters.get(0)).resolveInstance(null, null,
131             null)).intValue());
132     }
133
134 }
Popular Tags