KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > jmock > core > FIFOInvocationDispatcherTest


1 /* Copyright (c) 2000-2004 jMock.org
2  */

3 package test.jmock.core;
4
5 import java.lang.reflect.Method JavaDoc;
6 import junit.framework.AssertionFailedError;
7 import junit.framework.TestCase;
8 import org.jmock.core.Invocation;
9 import org.jmock.core.FIFOInvocationDispatcher;
10 import org.jmock.util.Dummy;
11 import test.jmock.core.testsupport.MockInvokable;
12 import test.jmock.core.testsupport.MockStub;
13
14
15 public class FIFOInvocationDispatcherTest extends TestCase
16 {
17     private Invocation invocation;
18     private FIFOInvocationDispatcher dispatcher;
19     private MockInvokable invokable1 = new MockInvokable();
20     private MockInvokable invokable2 = new MockInvokable();
21
22     public void setUp() throws NoSuchMethodException JavaDoc {
23         invocation = new Invocation("INVOKED-OBJECT", getDummyMethod(), null);
24         dispatcher = new FIFOInvocationDispatcher();
25     }
26
27     public void dummyMethod() { /* just used to create Invocation objects */
28     }
29
30     public void testInvokeFailsWhenEmpty() throws Throwable JavaDoc {
31         try {
32             dispatcher.dispatch(invocation);
33         }
34         catch (AssertionFailedError expected) {
35             return;
36         }
37         fail("expected AssertionFailedError");
38     }
39
40     public void testInvokesInvokableThatMatches() throws Throwable JavaDoc {
41         Object JavaDoc result = "invoke result";
42
43         invokable1.matchesInvocation.setExpected(invocation);
44         invokable1.matchesResult = true;
45         invokable1.invokeInvocation.setExpected(invocation);
46         invokable1.invokeResult = result;
47
48         dispatcher.add(invokable1);
49         dispatcher.dispatch(invocation);
50
51         invokable1.verifyExpectations();
52     }
53
54     public void testReturnsValueFromInvokable() throws Throwable JavaDoc {
55         Object JavaDoc result = "invoke result";
56
57         invokable1.matchesResult = true;
58         invokable1.invokeResult = result;
59
60         dispatcher.add(invokable1);
61
62         assertSame("should be same result", result, dispatcher.dispatch(invocation));
63     }
64
65     public void testPropagatesExceptionFromInvokable() throws Throwable JavaDoc {
66         Throwable JavaDoc exception = new Throwable JavaDoc("test throwable");
67
68         invokable1.matchesResult = true;
69         invokable1.invokeThrow = exception;
70
71         dispatcher.add(invokable1);
72
73         try {
74             dispatcher.dispatch(invocation);
75             fail("expected exception");
76         }
77         catch (Throwable JavaDoc t) {
78             assertSame("should be same exception", exception, t);
79         }
80     }
81
82     public void testByDefaultInvokeFailsWhenNoInvokablesMatch() throws Throwable JavaDoc {
83         invokable1.matchesResult = false;
84         invokable2.matchesResult = false;
85
86         dispatcher.add(invokable1);
87         dispatcher.add(invokable2);
88
89         try {
90             dispatcher.dispatch(invocation);
91         }
92         catch (AssertionFailedError ex) {
93             return;
94         }
95         fail("expected AssertionFailedError");
96     }
97
98     public void testEarlierInvokablesOverrideLaterInvokables() throws Throwable JavaDoc {
99         invokable1.matchesInvocation.setExpected(invocation);
100         invokable1.matchesResult = true;
101         invokable1.invokeInvocation.setExpected(invocation);
102
103         invokable2.matchesInvocation.setExpectNothing();
104         invokable2.matchesResult = true;
105         invokable2.invokeInvocation.setExpectNothing();
106
107         dispatcher.add(invokable1);
108         dispatcher.add(invokable2);
109
110         dispatcher.dispatch(invocation);
111
112         verifyInvokables();
113     }
114
115     public void testSearchesForMatchInFIFOOrder() throws Throwable JavaDoc {
116         invokable1.matchesInvocation.setExpected(invocation);
117         invokable1.matchesResult = false;
118         invokable1.invokeInvocation.setExpectNothing();
119
120         invokable2.matchesInvocation.setExpected(invocation);
121         invokable2.matchesResult = true;
122         invokable2.invokeInvocation.setExpected(invocation);
123         invokable2.invokeResult = "two";
124
125         dispatcher.add(invokable1);
126         dispatcher.add(invokable2);
127
128         assertEquals("Should be invokable2", "two", dispatcher.dispatch(invocation));
129
130         verifyInvokables();
131     }
132
133     public void testVerifiesAllInvokables() {
134         invokable1.verifyCalls.setExpected(1);
135         invokable2.verifyCalls.setExpected(1);
136
137         dispatcher.add(invokable1);
138         dispatcher.add(invokable2);
139
140         dispatcher.verify();
141
142         verifyInvokables();
143     }
144
145     public void testClearRemovesAllInvokables() throws Throwable JavaDoc {
146         invokable1.matchesResult = true;
147
148         dispatcher.add(invokable1);
149
150         dispatcher.clear();
151         testInvokeFailsWhenEmpty();
152     }
153
154     public void testInvokesDefaultStubWhenNoInvokablesMatch() throws Throwable JavaDoc {
155         MockStub mockDefaultStub = new MockStub("mockDefaultStub");
156         Object JavaDoc defaultStubResult = Dummy.newDummy("DEFAULT STUB RESULT");
157
158         dispatcher.add(invokable1);
159         dispatcher.add(invokable2);
160
161         invokable1.matchesResult = false;
162         invokable2.matchesResult = false;
163
164         mockDefaultStub.invokeInvocation.setExpected(invocation);
165         mockDefaultStub.invokeResult = defaultStubResult;
166
167         dispatcher.setDefaultStub(mockDefaultStub);
168         assertSame("should be result of calling default stub",
169                    defaultStubResult, dispatcher.dispatch(invocation));
170
171         verifyInvokables();
172         mockDefaultStub.verify();
173     }
174
175     private Method JavaDoc getDummyMethod() throws NoSuchMethodException JavaDoc {
176         return getClass().getDeclaredMethod("dummyMethod", new Class JavaDoc[0]);
177     }
178
179     private void verifyInvokables() {
180         invokable1.verifyExpectations();
181         invokable2.verifyExpectations();
182     }
183 }
184
Popular Tags