KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > jmock > core > stub > DefaultResultStubTest


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

3 package test.jmock.core.stub;
4
5 import junit.framework.AssertionFailedError;
6 import junit.framework.TestCase;
7 import org.jmock.core.Invocation;
8 import org.jmock.core.stub.DefaultResultStub;
9 import org.jmock.expectation.AssertMo;
10 import test.jmock.core.testsupport.MethodFactory;
11
12
13 public class DefaultResultStubTest extends TestCase {
14     static final Object JavaDoc[] NO_ARG_VALUES = new Object JavaDoc[0];
15
16     private static MethodFactory METHOD_FACTORY = new MethodFactory();
17
18     private DefaultResultStub stub;
19
20     public DefaultResultStubTest( String JavaDoc name ) {
21         super(name);
22     }
23
24     public void setUp() {
25         stub = new DefaultResultStub();
26     }
27
28     public void testWritesDescritionToStringBuffer() {
29         AssertMo.assertIncludes("contains expected description",
30                                 "returns a default value",
31                                 stub.describeTo(new StringBuffer JavaDoc()).toString());
32     }
33
34     public void testReturnsUsefulDefaultResultsForBasicTypes()
35             throws Throwable JavaDoc
36     {
37         assertHasRegisteredValue(stub, boolean.class, Boolean.FALSE);
38         assertHasRegisteredValue(stub, void.class, null);
39         assertHasRegisteredValue(stub, byte.class, new Byte JavaDoc((byte)0));
40         assertHasRegisteredValue(stub, short.class, new Short JavaDoc((short)0));
41         assertHasRegisteredValue(stub, int.class, new Integer JavaDoc(0));
42         assertHasRegisteredValue(stub, long.class, new Long JavaDoc(0L));
43         assertHasRegisteredValue(stub, char.class, new Character JavaDoc('\0'));
44         assertHasRegisteredValue(stub, float.class, new Float JavaDoc(0.0F));
45         assertHasRegisteredValue(stub, double.class, new Double JavaDoc(0.0));
46         assertHasRegisteredValue(stub, Boolean JavaDoc.class, Boolean.FALSE);
47         assertHasRegisteredValue(stub, Byte JavaDoc.class, new Byte JavaDoc((byte)0));
48         assertHasRegisteredValue(stub, Short JavaDoc.class, new Short JavaDoc((short)0));
49         assertHasRegisteredValue(stub, Integer JavaDoc.class, new Integer JavaDoc(0));
50         assertHasRegisteredValue(stub, Long JavaDoc.class, new Long JavaDoc(0L));
51         assertHasRegisteredValue(stub, Character JavaDoc.class, new Character JavaDoc('\0'));
52         assertHasRegisteredValue(stub, Float JavaDoc.class, new Float JavaDoc(0.0F));
53         assertHasRegisteredValue(stub, Double JavaDoc.class, new Double JavaDoc(0.0));
54         assertHasRegisteredValue(stub, String JavaDoc.class, "");
55     }
56
57     public void testReturnsEmptyArrayForAllArrayTypes()
58             throws Throwable JavaDoc
59     {
60         int[] defaultArrayForPrimitiveType =
61                 (int[])stub.invoke(invocationReturning(int[].class));
62         assertEquals("should be empty array", 0, defaultArrayForPrimitiveType.length);
63
64         Object JavaDoc[] defaultArrayForReferenceType =
65                 (Object JavaDoc[])stub.invoke(invocationReturning(Object JavaDoc[].class));
66         assertEquals("should be empty array", 0, defaultArrayForReferenceType.length);
67     }
68
69     public interface InterfaceType {
70         int returnInt();
71     }
72
73     // Inspired by http://www.c2.com/cgi/wiki?JavaNullProxy
74
public void testReturnsProxyOfNewMockObjectWithSameDefaultResultStubForInterfaceTypes()
75          throws Throwable JavaDoc
76     {
77         int intResult = -1;
78
79         stub.addResult(int.class, new Integer JavaDoc(intResult));
80
81         InterfaceType result = (InterfaceType)stub.invoke(invocationReturning(InterfaceType.class));
82
83         assertEquals("int result from 'null' interface implementation",
84                      intResult, result.returnInt());
85     }
86
87     public void testDefaultResultsCanBeExplicitlyOverriddenByType()
88         throws Throwable JavaDoc
89     {
90         int newDefaultIntResult = 20;
91         String JavaDoc newDefaultStringResult = "hello";
92
93         stub.addResult(String JavaDoc.class, newDefaultStringResult);
94         stub.addResult(int.class, new Integer JavaDoc(newDefaultIntResult));
95
96         assertEquals("expected registered value for string result type",
97                      newDefaultStringResult, stub.invoke(invocationReturning(String JavaDoc.class)));
98
99         assertEquals("expected registered value for int result type",
100                      new Integer JavaDoc(newDefaultIntResult), stub.invoke(invocationReturning(int.class)));
101     }
102
103     public void testAnExplicitlyRegisteredResultOverridesThePreviousResultForTheSameType()
104             throws Throwable JavaDoc
105     {
106         stub.addResult(String JavaDoc.class, "result1");
107         stub.addResult(String JavaDoc.class, "result2");
108
109         assertEquals("expected second result",
110                      "result2", stub.invoke(invocationReturning(String JavaDoc.class)));
111     }
112
113     class UnsupportedReturnType
114     {
115     }
116
117     public void testInvocationWithAnUnregisteredReturnTypeCausesAnAssertionFailedError()
118             throws Throwable JavaDoc
119     {
120         Class JavaDoc unsupportedReturnType = UnsupportedReturnType.class;
121         Class JavaDoc[] supportedReturnTypes = {
122             boolean.class, byte.class, char.class, short.class, int.class, long.class,
123             float.class, double.class,
124             Boolean JavaDoc.class, Byte JavaDoc.class, Character JavaDoc.class, Short JavaDoc.class, Integer JavaDoc.class, Long JavaDoc.class,
125             Float JavaDoc.class, Double JavaDoc.class,
126             String JavaDoc.class
127         };
128
129         try {
130             stub.invoke(invocationReturning(unsupportedReturnType));
131         }
132         catch (AssertionFailedError ex) {
133             String JavaDoc message = ex.getMessage();
134
135             AssertMo.assertIncludes("message should include name of unsupported type",
136                                     unsupportedReturnType.getName(), message);
137
138             for (int i = 0; i < supportedReturnTypes.length; i++) {
139                 AssertMo.assertIncludes("message should include names of expected types",
140                                         supportedReturnTypes[i].getName(), message);
141             }
142             return;
143         }
144
145         fail("should have failed");
146     }
147
148     public void assertHasRegisteredValue( DefaultResultStub defaultResultStub,
149                                           Class JavaDoc resultType,
150                                           Object JavaDoc resultValue )
151             throws Throwable JavaDoc
152     {
153         assertEquals("expected " + resultValue + " to be returned",
154                      resultValue, defaultResultStub.invoke(invocationReturning(resultType)));
155     }
156
157     public void assertHasNotRegisteredReturnType( DefaultResultStub defaultResultStub,
158                                                   Class JavaDoc resultType )
159             throws Throwable JavaDoc
160     {
161         try {
162             defaultResultStub.invoke(invocationReturning(resultType));
163             fail("stub should not support return type " + resultType);
164         }
165         catch (AssertionFailedError expected) {
166             return;
167         }
168     }
169
170     private Invocation invocationReturning( Class JavaDoc resultType ) {
171         return new Invocation("INVOKED-OBJECT",
172                               METHOD_FACTORY.newMethodReturning(resultType),
173                               NO_ARG_VALUES);
174     }
175 }
176
Popular Tags