KickJava   Java API By Example, From Geeks To Geeks.

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


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