1   // Copyright (c) 2003, Chad Woolley, All rights reserved.
2   
3   package org.virtualmock.call;
4   
5   import java.security.InvalidKeyException;
6   import java.util.Hashtable;
7   import java.util.List;
8   import junit.framework.Test;
9   import junit.framework.TestCase;
10  import org.hansel.CoverageDecorator;
11  import org.virtualmock.Constants;
12  import org.virtualmock.VM;
13  import org.virtualmock.call.Call;
14  import org.virtualmock.call.CallManager;
15  import org.virtualmock.call.InvokedCall;
16  import org.virtualmock.call.RecordedCall;
17  import org.virtualmock.call.RecordedCallQueue;
18  import org.virtualmock.call.Signature;
19  import org.virtualmock.matcher.ArgMatcher;
20  
21  
22  /***
23   * Tests the CallManager class
24   *
25   * @author Chad Woolley
26   * @version $Revision: 1.5 $
27   */
28  public class CallManagerTest extends TestCase {
29      Call call = null;
30      Call tempCall = null;
31      CallManager callManager = null;
32      Class classType = null;
33      Hashtable args = null;
34      Object returnValue = null;
35      Signature signature = null;
36      String callId = null;
37      String className = null;
38      String methodName = null;
39      Throwable exception = null;
40      Class[] argTypes = null;
41      Object[] argValues = null;
42  
43      /***
44       * Set up the test
45       *
46       * @throws Exception any exception thrown during setup.
47       */
48      public void setUp() throws Exception {
49          super.setUp();
50          callManager =
51              new CallManager(new InvokedCallQueue(), new RecordedCallQueue());
52          classType = CallManager.class;
53          className = "CallManager";
54          methodName = "MyMethod";
55          argTypes = new Class[] {String.class, Integer.class};
56          argValues = new Object[] {"ARG0", new Integer(1)};
57          returnValue = "RETURN_VALUE";
58          signature =
59              new Signature(returnValue.getClass(), classType, methodName,
60                  argTypes);
61          exception = new RuntimeException("EXCEPTION");
62      }
63  
64      /***
65       * Hansel support
66       *
67       * @return the decorated Test
68       */
69      public static Test suite() {
70          CoverageDecorator cd =
71              new CoverageDecorator(CallManagerTest.class,
72                  new Class[] {CallManager.class});
73          cd.setDisplayStatistics(true);
74  
75          return cd;
76      }
77  
78      /***
79       * Tear down the test
80       *
81       * @throws Exception any exception thrown during teardown.
82       */
83      public void tearDown() throws Exception {
84          super.tearDown();
85      }
86  
87      /***
88       * Test getting recorded calls
89       */
90      public void testCanGetRecordedCalls() {
91          tempCall = callManager.recordCall(signature, argValues);
92  
93          List recordedCalls = callManager.getRecordedCalls();
94  
95          assertNotNull(recordedCalls);
96      }
97  
98      /***
99       * Test getting uninvoked calls
100      */
101     public void testCanGetUninvokedCalls() {
102         tempCall = callManager.recordCall(signature, argValues);
103 
104         RecordedCall[] uninvokedCalls = callManager.getUninvokedCalls();
105 
106         assertNotNull(uninvokedCalls);
107     }
108 
109     /***
110      * Test can record arg matchers
111      */
112     public void testCanRecordArgMatchers() {
113         callManager.recordCall(signature, argValues);
114 
115         ArgMatcher[] argMatchers =
116             new ArgMatcher[] {VM.ALWAYS_MATCHER, VM.ALWAYS_MATCHER};
117         callManager.recordArgMatchers(argMatchers);
118     }
119 
120     /***
121      * Test can record return value for last call
122      */
123     public void testCanRecordReturnValueForLastCall() {
124         tempCall = callManager.recordCall(signature, argValues);
125         callManager.recordReturnValueForLastCall(returnValue);
126 
127         assertEquals(returnValue, tempCall.getReturnValue());
128     }
129 
130     /***
131      * Test can record throwable for last call
132      */
133     public void testCanRecordThrowableForLastCall() {
134         tempCall = callManager.recordCall(signature, argValues);
135         callManager.recordThrowableForLastCall(exception);
136 
137         assertEquals(exception, tempCall.getThrowable());
138     }
139 
140     /***
141      * Test setTestCase
142      */
143     public void testCanSetAndGetTestCase() {
144         callManager.setTestCase(this);
145         assertEquals(this, callManager.getTestCase());
146     }
147 
148     /***
149      * Test getNextMatchingUninvokedCall if ther are no matching uninvoked
150      * calls.
151      */
152     public void testIndicateCalledWorksIfNoMatchingUninvokedCallsExist() {
153         InvokedCall invokedCall = new InvokedCall(signature, argValues);
154         callManager.indicateCalled(invokedCall);
155     }
156 
157     /***
158      * Test phase functionality
159      */
160     public void testIndicatesIsPlaybackPhaseFalse() {
161         callManager.setPhase(-1);
162         assertFalse(callManager.isPlaybackPhase());
163     }
164 
165     /***
166      * Test phase functionality
167      */
168     public void testIndicatesIsPlaybackPhaseTrue() {
169         callManager.setPhase(Constants.PLAYBACK_PHASE);
170         assertTrue(callManager.isPlaybackPhase());
171     }
172 
173     /***
174      * Test phase functionality
175      */
176     public void testIndicatesIsRecordPhaseFalse() {
177         callManager.setPhase(-1);
178         assertFalse(callManager.isRecordPhase());
179     }
180 
181     /***
182      * Test phase functionality
183      */
184     public void testIndicatesIsRecordPhaseTrue() {
185         callManager.setPhase(Constants.RECORD_PHASE);
186         assertTrue(callManager.isRecordPhase());
187     }
188 
189     /***
190      * Test phase functionality
191      */
192     public void testIndicatesIsVerifyPhaseFalse() {
193         callManager.setPhase(-1);
194         assertFalse(callManager.isVerifyPhase());
195     }
196 
197     /***
198      * Test phase functionality
199      */
200     public void testIndicatesIsVerifyPhaseTrue() {
201         callManager.setPhase(Constants.VERIFY_PHASE);
202         assertTrue(callManager.isVerifyPhase());
203     }
204 
205     /***
206      * Test correct tracking of the last recorded call
207      */
208     public void testLastCall() {
209         tempCall = callManager.recordCall(signature, argValues);
210         assertEquals(tempCall, callManager.getLastCall());
211     }
212 
213     /***
214      * Tests the addMockedClass and isClassMocked methods
215      */
216     public void testMockedClasses() {
217         callManager.addMockClass("String");
218         assertTrue(callManager.isClassMocked("String"));
219         assertFalse(callManager.isClassMocked("Integer"));
220     }
221 
222     /***
223      * Test subsequent calls with identical signatures, but with the second
224      * throwing an exception instead of returning a value
225      */
226     public void testQueuedException() {
227         String returnValue1 = new String("XXXYYY");
228         Class[] argTypes1 = new Class[] {String.class, Integer.class};
229         Object[] argValues1 = new Object[] {"XXX", new Integer(111)};
230         String methodName1 = "concatWithException";
231         Signature signature1 =
232             new Signature(returnValue.getClass(), CallManager.class,
233                 methodName1, argTypes1);
234         InvokedCall invokedCall1 = new InvokedCall(signature1, argValues1);
235         String callId1 = signature1 + ":1";
236         tempCall = callManager.recordCall(signature1, argValues1);
237         callManager.recordReturnValueForLastCall(returnValue1);
238 
239         InvalidKeyException mockException =
240             new InvalidKeyException("MOCK EXCEPTION");
241         Class[] argTypes2 = new Class[] {String.class, Integer.class};
242         Object[] argValues2 = new Object[] {"YYY", new Integer(111)};
243         String methodName2 = "concatWithException";
244         Signature signature2 =
245             new Signature(returnValue.getClass(), CallManager.class,
246                 methodName2, argTypes2);
247         InvokedCall invokedCall2 = new InvokedCall(signature2, argValues2);
248         String callId2 = signature2 + ":2";
249         tempCall = callManager.recordCall(signature2, argValues2);
250         callManager.recordThrowableForLastCall(mockException);
251 
252         RecordedCallQueue callsRecorded = callManager.getRecordedCallQueue();
253         callManager.indicateCalled(invokedCall1);
254         assertEquals(invokedCall1.getCallId(), callId1);
255 
256         // verify the invocation count was incremented
257         RecordedCall retrievedCall1 =
258             (RecordedCall) callsRecorded.getCallByCallId(callId1);
259         int call1InvocationCount = retrievedCall1.getInvocationCount();
260         assertEquals(1, call1InvocationCount);
261 
262         callManager.indicateCalled(invokedCall2);
263         assertEquals(invokedCall2.getCallId(), callId2);
264 
265         // verify the invocation count was incremented
266         RecordedCall retrievedCall2 =
267             (RecordedCall) callsRecorded.getCallByCallId(callId2);
268         int call2InvocationCount = retrievedCall2.getInvocationCount();
269         assertEquals(1, call2InvocationCount);
270 
271         assertEquals(retrievedCall1.getReturnValue(), returnValue1);
272 
273         // verify that we can successfully get a handle to the second call even
274         // though it has a matching signature
275         assertEquals(retrievedCall2.getThrowable(), mockException);
276     }
277 
278     /***
279      * Test recording, calling, and verifying a call with no args.
280      */
281     public void testRecordCallVerifyNoArgs() {
282         tempCall = callManager.recordCall(signature, argValues);
283         callManager.recordReturnValueForLastCall(returnValue);
284 
285         InvokedCall invokedCall = new InvokedCall(signature, argValues);
286         assertNotNull(callManager.getMatchingCall(invokedCall));
287         assertEquals(returnValue, tempCall.getReturnValue());
288         callManager.indicateCalled(invokedCall);
289     }
290 
291     /***
292      * Test recording, calling, and verifying a call with args or return value.
293      */
294     public void testRecordCallVerifyNoArgsNoReturn() {
295         tempCall = callManager.recordCall(signature, argValues);
296 
297         InvokedCall invokedCall = new InvokedCall(signature, argValues);
298         assertNotNull(callManager.getMatchingCall(invokedCall));
299         assertNull(tempCall.getReturnValue());
300         callManager.indicateCalled(invokedCall);
301     }
302 
303     /***
304      * Test recording, calling, and verifying a call with an exception
305      */
306     public void testRecordCallVerifyNoArgsWithException() {
307         tempCall = callManager.recordCall(signature, argValues);
308         callManager.recordThrowableForLastCall(exception);
309 
310         InvokedCall invokedCall = new InvokedCall(signature, argValues);
311         assertNull(tempCall.getReturnValue());
312         assertEquals(exception, tempCall.getThrowable());
313         callManager.indicateCalled(invokedCall);
314     }
315 
316     /***
317      * Test recording, calling, and verifying a call with args.
318      */
319     public void testRecordCallVerifyWithArgs() {
320         tempCall = callManager.recordCall(signature, argValues);
321         callManager.recordReturnValueForLastCall(returnValue);
322 
323         InvokedCall invokedCall = new InvokedCall(signature, argValues);
324         assertNotNull(callManager.getMatchingCall(invokedCall));
325         assertEquals(returnValue, tempCall.getReturnValue());
326         callManager.indicateCalled(invokedCall);
327     }
328 
329     /***
330      * Test recording, calling, and verifying a call with args and no return
331      * value.
332      */
333     public void testRecordCallVerifyWithArgsNoReturn() {
334         tempCall = callManager.recordCall(signature, argValues);
335 
336         InvokedCall invokedCall = new InvokedCall(signature, argValues);
337         assertNotNull(callManager.getMatchingCall(invokedCall));
338         assertNull(tempCall.getReturnValue());
339         callManager.indicateCalled(invokedCall);
340     }
341 
342     /***
343      * Test recording, calling, and verifying a call with args and with an
344      * exception.
345      */
346     public void testRecordCallVerifyWithException() {
347         tempCall = callManager.recordCall(signature, argValues);
348         callManager.recordThrowableForLastCall(exception);
349 
350         InvokedCall invokedCall = new InvokedCall(signature, argValues);
351         assertNotNull(callManager.getMatchingCall(invokedCall));
352         assertEquals(exception, tempCall.getThrowable());
353         callManager.indicateCalled(invokedCall);
354     }
355 }