KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 JavaDoc;
19 import java.util.LinkedList JavaDoc;
20 import java.util.List JavaDoc;
21
22
23 /**
24  * @author Jörg Schaible
25  */

26 public class MethodCallingVisitorTest extends MockObjectTestCase {
27
28     private Method JavaDoc add;
29     private Method JavaDoc touch;
30
31     protected void setUp() throws Exception JavaDoc {
32         super.setUp();
33         add = List JavaDoc.class.getMethod("add", new Class JavaDoc[]{Object JavaDoc.class});
34         touch = Touchable.class.getMethod("touch", (Class JavaDoc[])null);
35     }
36
37     public void testVisitorWillTraverseAndCall() throws Exception JavaDoc {
38         MutablePicoContainer parent = new DefaultPicoContainer();
39         MutablePicoContainer child = new DefaultPicoContainer();
40         parent.addChildContainer(child);
41         parent.registerComponentImplementation(List JavaDoc.class, LinkedList JavaDoc.class, new Parameter[0]);
42         child.registerComponentImplementation(List JavaDoc.class, LinkedList JavaDoc.class, new Parameter[0]);
43         List JavaDoc parentList = (List JavaDoc)parent.getComponentInstanceOfType(List JavaDoc.class);
44         List JavaDoc childList = (List JavaDoc)child.getComponentInstanceOfType(List JavaDoc.class);
45
46         assertEquals(0, parentList.size());
47         assertEquals(0, childList.size());
48
49         PicoVisitor visitor = new MethodCallingVisitor(add, List JavaDoc.class, new Object JavaDoc[]{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 JavaDoc {
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 JavaDoc {
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