1 /* Copyright (c) 2000-2003, jMock.org. See bundled LICENSE.txt */
2 package org.jmock.dynamic;
3
4 import junit.framework.TestCase;
5 import org.jmock.dynamic.support.MockInvokable;
6
7 import java.lang.reflect.Method;
8
9 public class LIFOInvocationDispatcherTest extends TestCase {
10
11 private Invocation invocation;
12 private LIFOInvocationDispatcher dispatcher;
13 private MockInvokable invokable1 = new MockInvokable();
14 private MockInvokable invokable2 = new MockInvokable();
15
16 public void setUp() throws NoSuchMethodException {
17 invocation = new Invocation(getDummyMethod(), null);
18 dispatcher = new LIFOInvocationDispatcher();
19 }
20
21 public void dummyMethod() {
22 };
23
24 public void testInvokeFailsWhenEmpty() throws Throwable {
25 try {
26 dispatcher.dispatch(invocation);
27 } catch (DynamicMockError ex) {
28 assertSame("should be same invocation", invocation, ex.invocation);
29 return;
30 }
31 fail("expected AssertionFailedError");
32 }
33
34 public void testInvokesInvokableThatMatches() throws Throwable {
35 Object result = "invoke result";
36
37 invokable1.matchesInvocation.setExpected(invocation);
38 invokable1.matchesResult = true;
39 invokable1.invokeInvocation.setExpected(invocation);
40 invokable1.invokeResult = result;
41
42 dispatcher.add(invokable1);
43 dispatcher.dispatch(invocation);
44
45 invokable1.verifyExpectations();
46 }
47
48 public void testReturnsValueFromInvokable() throws Throwable {
49 Object result = "invoke result";
50
51 invokable1.matchesResult = true;
52 invokable1.invokeResult = result;
53
54 dispatcher.add(invokable1);
55
56 assertSame("should be same result", result, dispatcher.dispatch(invocation));
57 }
58
59 public void testPropagatesExceptionFromInvokable() throws Throwable {
60 Throwable exception = new Throwable("test throwable");
61
62 invokable1.matchesResult = true;
63 invokable1.invokeThrow = exception;
64
65 dispatcher.add(invokable1);
66
67 try {
68 dispatcher.dispatch(invocation);
69 fail("expected exception");
70 } catch (Throwable t) {
71 assertSame("should be same exception", exception, t);
72 }
73 }
74
75 public void testInvokeFailsWhenNoInvokablesMatch() throws Throwable {
76 invokable1.matchesResult = false;
77 invokable2.matchesResult = false;
78
79 dispatcher.add(invokable1);
80 dispatcher.add(invokable2);
81
82 try {
83 dispatcher.dispatch(invocation);
84 } catch (DynamicMockError ex) {
85 assertSame("should be same invocation", invocation, ex.invocation);
86 return;
87 }
88 fail("expected AssertionFailedError");
89 }
90
91 public void testLaterInvokablesOverrideEarlierInvokables() throws Throwable {
92 invokable1.matchesInvocation.setExpectNothing();
93 invokable1.matchesResult = true;
94 invokable1.invokeInvocation.setExpectNothing();
95
96 invokable2.matchesInvocation.setExpected(invocation);
97 invokable2.matchesResult = true;
98 invokable2.invokeInvocation.setExpected(invocation);
99
100
101 dispatcher.add(invokable1);
102 dispatcher.add(invokable2);
103
104 dispatcher.dispatch(invocation);
105
106 verifyInvokables();
107 }
108
109 public void testSearchesForMatchInLIFOOrder() throws Throwable {
110 invokable1.matchesInvocation.setExpected(invocation);
111 invokable1.matchesResult = true;
112 invokable1.invokeInvocation.setExpected(invocation);
113 invokable1.invokeResult = "one";
114
115 invokable2.matchesInvocation.setExpected(invocation);
116 invokable2.matchesResult = false;
117 invokable2.invokeInvocation.setExpectNothing();
118
119
120 dispatcher.add(invokable1);
121 dispatcher.add(invokable2);
122
123 assertEquals("Should be invokable1", "one", dispatcher.dispatch(invocation));
124
125 verifyInvokables();
126 }
127
128 public void testVerifiesAllInvokables() {
129 invokable1.verifyCalls.setExpected(1);
130 invokable2.verifyCalls.setExpected(1);
131
132 dispatcher.add(invokable1);
133 dispatcher.add(invokable2);
134
135 dispatcher.verify();
136
137 verifyInvokables();
138 }
139
140 public void testClearRemovesAllInvokables() throws Throwable {
141 invokable1.matchesResult = true;
142
143 dispatcher.add(invokable1);
144
145 dispatcher.clear();
146 testInvokeFailsWhenEmpty();
147 }
148
149 private Method getDummyMethod() throws NoSuchMethodException {
150 return getClass().getDeclaredMethod("dummyMethod", new Class[0]);
151 }
152
153 private void verifyInvokables() {
154 invokable1.verifyExpectations();
155 invokable2.verifyExpectations();
156 }
157 }
This page was automatically generated by Maven