KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.picocontainer.defaults;
2
3 import org.jmock.Mock;
4 import org.jmock.MockObjectTestCase;
5 import org.jmock.core.Constraint;
6 import org.picocontainer.ComponentMonitor;
7 import org.picocontainer.monitors.ConsoleComponentMonitor;
8
9 import java.lang.reflect.Constructor JavaDoc;
10 import java.util.*;
11
12 /**
13  * @author Mauro Talevi
14  * @version $Revision: 2200 $
15  */

16 public class DelegatingComponentMonitorTestCase extends MockObjectTestCase {
17
18     public void testDelegatingMonitorThrowsExpectionWhenConstructionWithNullDelegate(){
19         try {
20             new DelegatingComponentMonitor(null);
21             fail("NPE expected");
22         } catch (NullPointerException JavaDoc e) {
23             assertEquals("NPE", "monitor", e.getMessage());
24         }
25     }
26
27     public void testDelegatingMonitorThrowsExpectionWhenChangingToNullMonitor(){
28         DelegatingComponentMonitor dcm = new DelegatingComponentMonitor();
29         try {
30             dcm.changeMonitor(null);
31             fail("NPE expected");
32         } catch (NullPointerException JavaDoc e) {
33             assertEquals("NPE", "monitor", e.getMessage());
34         }
35     }
36
37     public void testDelegatingMonitorCanChangeMonitorInDelegateThatDoesSupportMonitorStrategy() {
38         ComponentMonitor monitor = mockMonitorWithNoExpectedMethods();
39         DelegatingComponentMonitor dcm = new DelegatingComponentMonitor(mockMonitorThatSupportsStrategy(monitor));
40         dcm.changeMonitor(monitor);
41         assertEquals(monitor, dcm.currentMonitor());
42         dcm.instantiating(null);
43     }
44
45     public void testDelegatingMonitorChangesDelegateThatDoesNotSupportMonitorStrategy() {
46         ComponentMonitor delegate = mockMonitorWithNoExpectedMethods();
47         DelegatingComponentMonitor dcm = new DelegatingComponentMonitor(delegate);
48         ComponentMonitor monitor = mockMonitorWithNoExpectedMethods();
49         assertEquals(delegate, dcm.currentMonitor());
50         dcm.changeMonitor(monitor);
51         assertEquals(monitor, dcm.currentMonitor());
52     }
53
54     public void testDelegatingMonitorReturnsDelegateThatDoesNotSupportMonitorStrategy() {
55         ComponentMonitor delegate = mockMonitorWithNoExpectedMethods();
56         DelegatingComponentMonitor dcm = new DelegatingComponentMonitor(delegate);
57         assertEquals(delegate, dcm.currentMonitor());
58     }
59
60     private ComponentMonitor mockMonitorWithNoExpectedMethods() {
61         Mock mock = mock(ComponentMonitor.class);
62         return (ComponentMonitor)mock.proxy();
63     }
64
65     private ComponentMonitor mockMonitorThatSupportsStrategy(ComponentMonitor currentMonitor) {
66         Mock mock = mock(MonitorThatSupportsStrategy.class);
67         mock.expects(once()).method("changeMonitor").with(eq(currentMonitor));
68         mock.expects(once()).method("currentMonitor").withAnyArguments().will(returnValue(currentMonitor));
69         mock.expects(once()).method("instantiating").withAnyArguments();
70         return (ComponentMonitor)mock.proxy();
71     }
72
73     public void testMonitoringHappensBeforeAndAfterInstantiation() throws NoSuchMethodException JavaDoc {
74         final Vector ourIntendedInjectee0 = new Vector();
75         final String JavaDoc ourIntendedInjectee1 = "hullo";
76         Mock monitor = mock(ComponentMonitor.class);
77         Constructor JavaDoc nacotCtor = NeedsACoupleOfThings.class.getConstructors()[0];
78         monitor.expects(once()).method("instantiating").with(eq(nacotCtor));
79         Constraint durationIsGreaterThanOrEqualToZero = new Constraint() {
80             public boolean eval(Object JavaDoc o) {
81                 Long JavaDoc duration = (Long JavaDoc)o;
82                 return 0 <= duration.longValue();
83             }
84
85             public StringBuffer JavaDoc describeTo(StringBuffer JavaDoc stringBuffer) {
86                 return stringBuffer.append("The endTime wasn't after the startTime");
87             }
88         };
89         Constraint isANACOTThatWozCreated = new Constraint() {
90             public boolean eval(Object JavaDoc o) {
91                 return o instanceof NeedsACoupleOfThings;
92             }
93
94             public StringBuffer JavaDoc describeTo(StringBuffer JavaDoc stringBuffer) {
95                 return stringBuffer.append("Should have been a hashmap");
96             }
97         };
98         Constraint collectionAndStringWereInjected = new Constraint() {
99             public boolean eval(Object JavaDoc o) {
100                 Object JavaDoc[] ctorArgs = (Object JavaDoc[]) o;
101                 return ctorArgs.length == 2 && ctorArgs[0] == ourIntendedInjectee0 && ctorArgs[1] == ourIntendedInjectee1;
102             }
103             public StringBuffer JavaDoc describeTo(StringBuffer JavaDoc stringBuffer) {
104                 return stringBuffer.append("Should have injected our intended vector and string");
105             }
106         };
107         monitor.expects(once()).method("instantiated").with(eq(nacotCtor), isANACOTThatWozCreated, collectionAndStringWereInjected, durationIsGreaterThanOrEqualToZero);
108         DefaultPicoContainer parent = new DefaultPicoContainer();
109         parent.registerComponentInstance(ourIntendedInjectee0);
110         parent.registerComponentInstance(ourIntendedInjectee1);
111         DefaultPicoContainer child = new DefaultPicoContainer(new DelegatingComponentMonitor((ComponentMonitor) monitor.proxy()), parent);
112         child.registerComponentImplementation(NeedsACoupleOfThings.class);
113         child.getComponentInstance(NeedsACoupleOfThings.class);
114     }
115
116     static class NeedsACoupleOfThings {
117         public NeedsACoupleOfThings(Collection collection, String JavaDoc string) {
118         }
119     }
120
121     static interface MonitorThatSupportsStrategy extends ComponentMonitor, ComponentMonitorStrategy {
122     }
123 }
124
Popular Tags