1
2
3 package org.virtualmock.call;
4
5 import junit.framework.Test;
6 import junit.framework.TestCase;
7 import org.hansel.CoverageDecorator;
8 import org.virtualmock.call.Call;
9 import org.virtualmock.call.Signature;
10
11
12 /***
13 * Tests the Call class
14 *
15 * @author Chad Woolley
16 * @version $Revision: 1.1 $
17 */
18 public class CallTest extends TestCase {
19 Call call = null;
20 Class classType = null;
21 Object returnValue = null;
22 Signature signature = null;
23 String className = null;
24 String methodName = null;
25 Throwable exception = null;
26 Class[] argTypes = null;
27 Object[] argValues = null;
28
29 /***
30 * Test constructor
31 *
32 * @param testTitle title of the test
33 */
34 public CallTest(String testTitle) {
35 super(testTitle);
36 }
37
38 /***
39 * Hansel support
40 *
41 * @return the decorated Test
42 */
43 public static Test suite() {
44 CoverageDecorator cd =
45 new CoverageDecorator(CallTest.class, new Class[] {Call.class});
46 cd.setDisplayStatistics(true);
47
48 return cd;
49 }
50
51 /***
52 * Set up the test
53 *
54 * @throws Exception any exception thrown during setup.
55 */
56 public void setUp() throws Exception {
57 super.setUp();
58 classType = Call.class;
59 className = "org.virtualmock.call.Call";
60 methodName = "MyMethod";
61 argTypes = new Class[] {String.class, Integer.class};
62 argValues = new Object[] {"ARG0", new Integer(1)};
63 returnValue = "RETURN_VALUE";
64 signature =
65 new Signature(returnValue.getClass(), classType, methodName,
66 argTypes);
67 exception = new RuntimeException("EXCEPTION");
68 }
69
70 /***
71 * Tear down the test
72 *
73 * @throws Exception any exception thrown during teardown.
74 */
75 public void tearDown() throws Exception {
76 super.tearDown();
77 }
78
79 /***
80 * Test ArgValues accessors with nulls
81 */
82 public void testArgValuesGetterReturnsEmptyObjectArrayForNoArguments() {
83 signature = new Signature(classType, methodName);
84 call = new Call(signature);
85
86 Object[] args = call.getArgValues();
87 assertEquals(0, args.length);
88 }
89
90 /***
91 * Test primitive return value
92 */
93 public void testBooleanReturnValueIsValid() {
94 Boolean returnValue = new Boolean(true);
95 signature = new Signature(boolean.class, classType, methodName);
96 call = new Call(signature, returnValue);
97 assertTrue(call.isReturnTypePrimitive());
98 }
99
100 /***
101 * Test primitive return value
102 */
103 public void testByteReturnValueIsValid() {
104 byte b = 1;
105 Byte returnValue = new Byte(b);
106 signature = new Signature(byte.class, classType, methodName);
107 call = new Call(signature, returnValue);
108 assertTrue(call.isReturnTypePrimitive());
109 }
110
111 /***
112 * Test the callId
113 */
114 public void testCallId() {
115 call = new Call(signature, argValues, returnValue);
116
117 try {
118 call.getCallId();
119 fail("expected exception");
120 } catch (IllegalStateException e) {
121 }
122
123 call.setSignatureSequence(1);
124 assertEquals(signature + ":1", call.getCallId());
125 }
126
127 /***
128 * Test call creation with arg values.
129 *
130 * @throws Throwable thrown by AspectUtils execute method
131 */
132 public void testCanPlaybackWithNullArgValues() throws Throwable {
133 signature = new Signature(classType, methodName, argTypes);
134 argValues = new Object[] {null, null};
135 call = new Call(signature, argValues);
136 assertNotNull(call.getArgValues());
137 }
138
139 /***
140 * Test primitive return value
141 */
142 public void testCharacterReturnValueIsValid() {
143 Character returnValue = new Character('\u0000');
144 signature = new Signature(char.class, classType, methodName);
145 call = new Call(signature, returnValue);
146 assertTrue(call.isReturnTypePrimitive());
147 }
148
149 /***
150 * Test the constructor and getters when using an exception
151 */
152 public void testCreationWithException() {
153 call = new Call(signature, argValues, exception);
154 assertEquals(className, signature.getClassName());
155 assertEquals(methodName, signature.getMethodName());
156 assertEquals(argTypes, call.getArgTypes());
157 assertEquals(argValues, call.getArgValues());
158 assertNull(call.getReturnValue());
159 assertEquals(exception, call.getThrowable());
160 assertEquals(signature, call.getSignature());
161 assertTrue(call.hasArguments());
162 assertFalse(call.hasReturnValue());
163 }
164
165 /***
166 * Test the constructor and getters when using a return value
167 */
168 public void testCreationWithReturnValue() {
169 call = new Call(signature, argValues, returnValue);
170 assertEquals(className, signature.getClassName());
171 assertEquals(methodName, signature.getMethodName());
172 assertEquals(argTypes, call.getArgTypes());
173 assertEquals(argValues, call.getArgValues());
174 assertEquals(returnValue, call.getReturnValue());
175 assertNull(call.getThrowable());
176 assertEquals(signature, call.getSignature());
177 assertTrue(call.hasArguments());
178 assertTrue(call.hasReturnValue());
179 }
180
181 /***
182 * Test primitive return value
183 */
184 public void testDoubleReturnValueIsValid() {
185 Double returnValue = new Double(1);
186 signature = new Signature(double.class, classType, methodName);
187 call = new Call(signature, returnValue);
188 assertTrue(call.isReturnTypePrimitive());
189 }
190
191 /***
192 * Test that exception and return value are mutually exclusive
193 */
194 public void testExceptionAndReturnValueAreMutuallyExclusive() {
195 call = new Call(signature, argValues, new Object());
196
197
198 call.setThrowable(null);
199
200 try {
201 call.setThrowable(new Throwable());
202 fail("Expected exception");
203 } catch (IllegalArgumentException e) {
204 }
205
206
207 call.setReturnValue(null);
208
209
210 call.setThrowable(new Throwable());
211
212 try {
213 call.setReturnValue(new Object());
214 fail("Expected exception");
215 } catch (IllegalArgumentException e) {
216 }
217 }
218
219 /***
220 * Test primitive return value
221 */
222 public void testFloatReturnValueIsValid() {
223 Float returnValue = new Float(1);
224 signature = new Signature(float.class, classType, methodName);
225 call = new Call(signature, returnValue);
226 assertTrue(call.isReturnTypePrimitive());
227 }
228
229 /***
230 * Test the various getters
231 */
232 public void testGettersWork() {
233 call = new Call(signature, argValues, returnValue);
234 assertEquals(signature, call.getSignature());
235 assertEquals(signature.getClassName(), call.getClassName());
236 assertEquals(signature.getArgTypes(), call.getArgTypes());
237 assertEquals(argValues, call.getArgValues());
238 assertEquals(signature.getMethodName(), call.getMethodName());
239 assertEquals(signature.getReturnType(), call.getReturnType());
240 }
241
242 /***
243 * Test hasMatchingSignature method
244 */
245 public void testHasMatchingSignatureWorks() {
246 call = new Call(signature, argValues);
247
248 Signature signature2 =
249 new Signature(returnValue.getClass(), classType, methodName,
250 argTypes);
251 Call call2 = new Call(signature2, argValues);
252
253 assertTrue(call.hasMatchingSignature(call2));
254 }
255
256 /***
257 * Test primitive return value
258 */
259 public void testIntegerReturnValueIsValid() {
260 Integer returnValue = new Integer(1);
261 signature = new Signature(int.class, classType, methodName);
262 call = new Call(signature, returnValue);
263 assertTrue(call.isReturnTypePrimitive());
264 }
265
266 /***
267 * Test primitive return value
268 */
269 public void testLongReturnValueIsValid() {
270 Long returnValue = new Long(0L);
271 signature = new Signature(long.class, classType, methodName);
272 call = new Call(signature, returnValue);
273 assertTrue(call.isReturnTypePrimitive());
274 }
275
276 /***
277 * Test call creation with mismatched classes of argument types and values
278 */
279 public void testMismatchClassesArgTypesAndValuesThrowsException() {
280 try {
281 argValues[1] = "NOT AN INTEGER!";
282 call = new Call(signature, argValues, returnValue);
283 fail("expected exception");
284 } catch (IllegalArgumentException e) {
285
286 }
287 }
288
289 /***
290 * Test call creation with mismatched number of argument types and values
291 */
292 public void testMismatchNumberArgTypesAndValues() {
293 try {
294 call = new Call(signature, null, returnValue);
295 fail("expected exception");
296 } catch (IllegalArgumentException e) {
297
298 }
299 }
300
301 /***
302 * Test that no exception is thrown if both arg values and arg types are
303 * not null
304 *
305 * @throws Throwable thrown by AspectUtils execute method
306 */
307 public void testNotNullArgValuesAndNotNullArgTypesDoesNotThrowException()
308 throws Throwable {
309 signature = new Signature(classType, methodName, argTypes);
310
311 call = new Call(signature, argValues);
312 }
313
314 /***
315 * Test that exception is thrown if arg values are not null but arg types
316 * are
317 *
318 * @throws Throwable thrown by AspectUtils execute method
319 */
320 public void testNotNullArgValuesAndNullArgTypesThrowsException()
321 throws Throwable {
322 argTypes = null;
323 signature = new Signature(classType, methodName, argTypes);
324
325 try {
326 call = new Call(signature, argValues);
327 fail("expected exception");
328 } catch (IllegalArgumentException e) {
329 }
330 }
331
332 /***
333 * Test that exception is thrown if arg values are null but arg types are
334 * not
335 *
336 * @throws Throwable thrown by AspectUtils execute method
337 */
338 public void testNullArgValuesAndNotNullArgTypesThrowsException()
339 throws Throwable {
340 signature = new Signature(classType, methodName, argTypes);
341 argValues = null;
342
343 try {
344 call = new Call(signature, argValues);
345 fail("expected exception");
346 } catch (IllegalArgumentException e) {
347 }
348 }
349
350 /***
351 * Test that no exception is thrown if both arg values and arg types are
352 * null
353 *
354 * @throws Throwable thrown by AspectUtils execute method
355 */
356 public void testNullArgValuesAndNullArgTypesDoesNotThrowsException()
357 throws Throwable {
358 argTypes = null;
359 argValues = null;
360 signature = new Signature(classType, methodName, argTypes);
361
362 assertEquals(0, signature.getArgTypes().length);
363
364 call = new Call(signature, argValues);
365 }
366
367 /***
368 * Test call creation with null arguments
369 */
370 public void testNullArgs() {
371 signature =
372 new Signature(returnValue.getClass(), classType, methodName,
373 new Class[0]);
374 argValues = new Object[0];
375 call = new Call(signature, argValues, returnValue);
376 assertFalse(call.hasArguments());
377 assertEquals(0, call.getArgTypes().length);
378 assertEquals(0, call.getArgValues().length);
379 assertEquals(0, call.getArgValues().length);
380 }
381
382 /***
383 * Test call creation with null return value
384 */
385 public void testNullReturnValue() {
386 call = new Call(signature, argValues, null);
387 assertFalse(call.hasReturnValue());
388 }
389
390 /***
391 * Test the signatureSequence
392 */
393 public void testPrimitiveArgumentsAreValid() {
394 argTypes = new Class[] {int.class, boolean.class, float.class};
395 argValues =
396 new Object[] {new Integer(1), new Boolean(true), new Float(1.1f)};
397 signature =
398 new Signature(returnValue.getClass(), classType, methodName,
399 argTypes);
400 call = new Call(signature, argValues, returnValue);
401 call.setSignatureSequence(1);
402 assertEquals(1, call.getSignatureSequence());
403 }
404
405 /***
406 * Test primitive return values
407 */
408 public void testPrimitiveReturnValueCanBeSetForPrimitiveWrapper() {
409 Integer intReturnValue = new Integer(5);
410 signature = new Signature(int.class, classType, methodName, argTypes);
411 call = new Call(signature, argValues, intReturnValue);
412 assertTrue(call.isReturnTypePrimitive());
413 }
414
415 /***
416 * Test primitive return values
417 */
418 public void testPrimitiveReturnValueCannotBeSetForNonPrimitiveWrapper() {
419 Object object = new Object();
420 signature = new Signature(int.class, classType, methodName, argTypes);
421
422 try {
423 call = new Call(signature, argValues, object);
424 fail("expected exception");
425 } catch (IllegalArgumentException e) {
426 }
427 }
428
429 /***
430 * Test primitive return values
431 */
432 public void testPrimitiveReturnValueCannotBeSetForNullReturnValue() {
433 signature = new Signature(int.class, classType, methodName, argTypes);
434
435 call = new Call(signature, argValues);
436
437 try {
438 call.setReturnValue(null);
439 fail("expected exception");
440 } catch (IllegalArgumentException e) {
441 }
442 }
443
444 /***
445 * Test primitive return value
446 */
447 public void testShortReturnValueIsValid() {
448 short s = 0;
449 Short returnValue = new Short(s);
450 signature = new Signature(short.class, classType, methodName);
451 call = new Call(signature, returnValue);
452 assertTrue(call.isReturnTypePrimitive());
453 }
454
455 /***
456 * Test the signatureSequence
457 */
458 public void testSignatureSequence() {
459 call = new Call(signature, argValues, returnValue);
460 call.setSignatureSequence(1);
461 assertEquals(1, call.getSignatureSequence());
462 }
463
464 /***
465 * Test the toString method
466 */
467 public void testToString() {
468 call = new Call(signature, argValues, returnValue);
469 assertEquals("RETURN_VALUE org.virtualmock.call.Call.MyMethod(java.lang.String ARG0, java.lang.Integer 1)",
470 call.toString());
471 }
472
473 /***
474 * Test the toString method
475 */
476 public void testToStringWithNullReturnValueAndNoArgs() {
477 signature = new Signature(classType, methodName);
478 call = new Call(signature);
479 assertEquals("org.virtualmock.call.Call.MyMethod()", call.toString());
480 }
481
482 /***
483 * Test the toString method
484 */
485 public void testToStringWithThrowableAndNoArgs() {
486 signature = new Signature(Exception.class, classType, methodName);
487 call = new Call(signature, new Exception());
488 assertEquals("org.virtualmock.call.Call.MyMethod() throws java.lang.Exception",
489 call.toString());
490 }
491 }