1 8 package org.picocontainer.defaults; 9 10 import java.util.ArrayList ; 11 import java.util.HashSet ; 12 import java.util.Iterator ; 13 import java.util.List ; 14 import java.util.Set ; 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 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 { 38 super.setUp(); 39 40 pico = new DefaultPicoContainer(); 41 parentAdapter = pico.registerComponent(new SetterInjectionComponentAdapter(StringBuffer .class, StringBuffer .class, null)); 42 43 child = pico.makeChildContainer(); 44 45 childAdapter = child.registerComponent(new ConstructorInjectionComponentAdapter(ArrayList .class, ArrayList .class, 46 new Parameter[] { 47 new ConstantParameter(new Integer (3)) 48 })); 49 } 50 51 protected void tearDown() throws Exception { 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 allAdapters = new ArrayList (); 62 63 Set knownAdapters = new HashSet (); 64 knownAdapters.add(parentAdapter); 65 knownAdapters.add(childAdapter); 66 67 PicoVisitor containerCollector = new TraversalCheckingVisitor() { 68 public void visitComponentAdapter(ComponentAdapter adapter) { 69 super.visitComponentAdapter(adapter); allAdapters.add(adapter); 71 } 72 }; 73 containerCollector.traverse(pico); 74 75 assertEquals(numExpectedComponentAdapters, allAdapters.size()); 76 77 for (Iterator 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 allContainers = new ArrayList (); 87 final int expectedNumberOfContainers = 2; 88 89 PicoVisitor containerCollector = new TraversalCheckingVisitor() { 90 public void visitContainer(PicoContainer pico) { 91 super.visitContainer(pico); allContainers.add(pico); 93 } 94 }; 95 96 containerCollector.traverse(pico); 97 98 assertTrue(allContainers.size() == expectedNumberOfContainers); 99 100 Set knownContainers = new HashSet (); 101 knownContainers.add(pico); 102 knownContainers.add(child); 103 for (Iterator 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 allParameters = new ArrayList (); 117 118 PicoVisitor containerCollector = new TraversalCheckingVisitor() { 119 public void visitParameter(Parameter param) { 120 super.visitParameter(param); 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 ); 130 assertEquals(3, ( (Integer ) ( (ConstantParameter) allParameters.get(0)).resolveInstance(null, null, 131 null)).intValue()); 132 } 133 134 } | Popular Tags |