1 8 package org.picocontainer.defaults; 9 10 import org.picocontainer.MutablePicoContainer; 11 import org.picocontainer.Parameter; 12 import org.picocontainer.PicoVisitor; 13 import org.picocontainer.testmodel.Touchable; 14 15 import org.jmock.Mock; 16 import org.jmock.MockObjectTestCase; 17 18 import java.lang.reflect.Method ; 19 import java.util.LinkedList ; 20 import java.util.List ; 21 22 23 26 public class MethodCallingVisitorTest extends MockObjectTestCase { 27 28 private Method add; 29 private Method touch; 30 31 protected void setUp() throws Exception { 32 super.setUp(); 33 add = List .class.getMethod("add", new Class []{Object .class}); 34 touch = Touchable.class.getMethod("touch", (Class [])null); 35 } 36 37 public void testVisitorWillTraverseAndCall() throws Exception { 38 MutablePicoContainer parent = new DefaultPicoContainer(); 39 MutablePicoContainer child = new DefaultPicoContainer(); 40 parent.addChildContainer(child); 41 parent.registerComponentImplementation(List .class, LinkedList .class, new Parameter[0]); 42 child.registerComponentImplementation(List .class, LinkedList .class, new Parameter[0]); 43 List parentList = (List )parent.getComponentInstanceOfType(List .class); 44 List childList = (List )child.getComponentInstanceOfType(List .class); 45 46 assertEquals(0, parentList.size()); 47 assertEquals(0, childList.size()); 48 49 PicoVisitor visitor = new MethodCallingVisitor(add, List .class, new Object []{Boolean.TRUE}); 50 visitor.traverse(parent); 51 52 assertEquals(1, parentList.size()); 53 assertEquals(1, childList.size()); 54 } 55 56 public void testVisitsInInstantiationOrder() throws Exception { 57 Mock mockTouchable1 = mock(Touchable.class); 58 Mock mockTouchable2 = mock(Touchable.class); 59 60 MutablePicoContainer parent = new DefaultPicoContainer(); 61 MutablePicoContainer child = new DefaultPicoContainer(); 62 parent.addChildContainer(child); 63 parent.registerComponentInstance(mockTouchable1.proxy()); 64 child.registerComponentInstance(mockTouchable2.proxy()); 65 66 mockTouchable1.expects(once()).method("touch").id("1"); 67 mockTouchable2.expects(once()).method("touch").after(mockTouchable1, "1"); 68 69 PicoVisitor visitor = new MethodCallingVisitor(touch, Touchable.class, null); 70 visitor.traverse(parent); 71 } 72 73 public void testVisitsInReverseInstantiationOrder() throws Exception { 74 Mock mockTouchable1 = mock(Touchable.class); 75 Mock mockTouchable2 = mock(Touchable.class); 76 77 MutablePicoContainer parent = new DefaultPicoContainer(); 78 MutablePicoContainer child = new DefaultPicoContainer(); 79 parent.addChildContainer(child); 80 parent.registerComponentInstance(mockTouchable1.proxy()); 81 child.registerComponentInstance(mockTouchable2.proxy()); 82 83 mockTouchable2.expects(once()).method("touch").id("1"); 84 mockTouchable1.expects(once()).method("touch").after(mockTouchable2, "1"); 85 86 PicoVisitor visitor = new MethodCallingVisitor(touch, Touchable.class, null, false); 87 visitor.traverse(parent); 88 } 89 } 90 | Popular Tags |