KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmock > core > stub > ReturnStub


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

3 package org.jmock.core.stub;
4
5 import java.util.HashMap JavaDoc;
6 import java.util.Map JavaDoc;
7 import junit.framework.Assert;
8 import org.jmock.core.Invocation;
9 import org.jmock.core.Stub;
10
11
12 public class ReturnStub
13         extends Assert
14         implements Stub
15 {
16     private Object JavaDoc result;
17
18     private static Map JavaDoc BOX_TYPES = new HashMap JavaDoc();
19
20     static {
21         BOX_TYPES.put(boolean.class, Boolean JavaDoc.class);
22         BOX_TYPES.put(byte.class, Byte JavaDoc.class);
23         BOX_TYPES.put(char.class, Character JavaDoc.class);
24         BOX_TYPES.put(short.class, Short JavaDoc.class);
25         BOX_TYPES.put(int.class, Integer JavaDoc.class);
26         BOX_TYPES.put(long.class, Long JavaDoc.class);
27         BOX_TYPES.put(float.class, Float JavaDoc.class);
28         BOX_TYPES.put(double.class, Double JavaDoc.class);
29     }
30
31
32     public ReturnStub( Object JavaDoc result ) {
33         this.result = result;
34     }
35
36     public Object JavaDoc invoke( Invocation invocation ) throws Throwable JavaDoc {
37         checkTypeCompatiblity(invocation.invokedMethod.getReturnType());
38         return result;
39     }
40
41     public StringBuffer JavaDoc describeTo( StringBuffer JavaDoc buffer ) {
42         return buffer.append("returns <").append(result).append(">");
43     }
44
45     private void checkTypeCompatiblity( Class JavaDoc returnType ) {
46         if (result == null) {
47             if (returnType.isPrimitive()) reportInvalidNullValue(returnType);
48         } else if (returnType == void.class) {
49             reportReturnFromVoidMethod();
50         } else {
51             Class JavaDoc valueType = result.getClass();
52             if (!isCompatible(returnType, valueType)) {
53                 reportTypeError(returnType, valueType);
54             }
55         }
56     }
57
58     private boolean isCompatible( Class JavaDoc returnType, Class JavaDoc valueType ) {
59         if (returnType.isPrimitive()) {
60             return isBoxedType(returnType, valueType);
61         }
62         return returnType.isAssignableFrom(valueType);
63     }
64
65     private boolean isBoxedType( Class JavaDoc primitiveType, Class JavaDoc referenceType ) {
66         return BOX_TYPES.get(primitiveType) == referenceType;
67     }
68
69     private void reportTypeError( Class JavaDoc returnType, Class JavaDoc valueType ) {
70         fail("tried to return an incompatible value: " +
71              "expected a " + returnType + " but returned a " + valueType);
72     }
73
74     private void reportInvalidNullValue( Class JavaDoc returnType ) {
75         fail("tried to return null value from invokedMethod returning " + returnType);
76     }
77
78     private void reportReturnFromVoidMethod() {
79         fail("tried to return a value from a void method");
80     }
81 }
82
Popular Tags