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.Signature;
9
10
11 /***
12 * Tests the Signature class
13 *
14 * @author Chad Woolley
15 * @version $Revision: 1.1 $
16 */
17 public class SignatureTest extends TestCase {
18 Class classType = null;
19 Class returnType = null;
20 Signature signature = null;
21 Signature signatureVoid = null;
22 Signature signatureVoidNoArgs = null;
23 String className = null;
24 String methodName = null;
25 Class[] argTypes = null;
26
27 /***
28 * Test constructor
29 *
30 * @param testTitle title of the test
31 */
32 public SignatureTest(String testTitle) {
33 super(testTitle);
34 }
35
36 /***
37 * Hansel support
38 *
39 * @return the decorated Test
40 */
41 public static Test suite() {
42 CoverageDecorator cd =
43 new CoverageDecorator(SignatureTest.class,
44 new Class[] {Signature.class});
45 cd.setDisplayStatistics(true);
46
47 return cd;
48 }
49
50 /***
51 * Set up the test
52 *
53 * @throws Exception any exception thrown during setup.
54 */
55 public void setUp() throws Exception {
56 super.setUp();
57 returnType = String.class;
58 classType = Signature.class;
59 className = "org.virtualmock.call.Signature";
60 methodName = "myMethod";
61 argTypes = new Class[] {String.class, Integer.class};
62 signature = new Signature(returnType, classType, methodName, argTypes);
63 signatureVoid = new Signature(classType, methodName, argTypes);
64 signatureVoidNoArgs = new Signature(classType, methodName);
65 }
66
67 /***
68 * Tear down the test
69 *
70 * @throws Exception any exception thrown during teardown.
71 */
72 public void tearDown() throws Exception {
73 super.tearDown();
74 }
75
76 /***
77 * Test the isReturnTypePrimitive method returning false
78 */
79 public void testCanTellIfReturnTypeIsNotPrimitive() {
80 assertFalse(signature.isReturnTypePrimitive());
81 }
82
83 /***
84 * Test the isReturnTypePrimitive method returning true
85 */
86 public void testCanTellIfReturnTypeIsPrimitive() {
87 returnType = int.class;
88 signature = new Signature(returnType, classType, methodName, argTypes);
89 assertTrue(signature.isReturnTypePrimitive());
90 }
91
92 /***
93 * Test the equals method when argument types don't match
94 */
95 public void testEqualsReturnsFalseIfArgumentTypesDoNotMatch() {
96 Class[] newArgTypes = new Class[] {Object.class, Object.class};
97 Signature newSignature =
98 new Signature(Signature.class, "myMethod", newArgTypes);
99 assertFalse(signature.equals(newSignature));
100 }
101
102 /***
103 * Test the equals method when classTypes don't match
104 */
105 public void testEqualsReturnsFalseIfClassTypesDoNotMatch() {
106 Signature newSignature = new Signature(Object.class, "myMethod");
107 assertFalse(signature.equals(newSignature));
108 }
109
110 /***
111 * Test the equals method returns false if different argument counts
112 */
113 public void testEqualsReturnsFalseIfDifferentArgumentCounts() {
114 Class[] newArgTypes = new Class[] {String.class};
115 Signature newSignature =
116 new Signature(Signature.class, "myMethod", newArgTypes);
117 assertFalse(signature.equals(newSignature));
118 }
119
120 /***
121 * Test the equals method returns false when method names don't match
122 */
123 public void testEqualsReturnsFalseIfMethodNamesDoNotMatch() {
124 Signature newSignature = new Signature(Signature.class, "XXX");
125 assertFalse(signature.equals(newSignature));
126 }
127
128 /***
129 * Test the equals method returns false when not an instance of Signature
130 */
131 public void testEqualsReturnsFalseIfNotInstanceOfSignature() {
132 assertFalse(signature.equals(new Object()));
133 }
134
135 /***
136 * Test the equals method returns true when signatures match
137 */
138 public void testEqualsReturnsTrueWhenSignaturesMatch() {
139 Class[] newArgTypes = new Class[] {String.class, Integer.class};
140 Signature newSignature =
141 new Signature(Signature.class, "myMethod", newArgTypes);
142 assertEquals(signature, newSignature);
143 }
144
145 /***
146 * Test the getters
147 */
148 public void testGetters() {
149 assertEquals(Signature.class, signature.getClassType());
150 assertEquals(className, signature.getClassName());
151 assertEquals(methodName, signature.getMethodName());
152
153 Class[] testArgTypes = signature.getArgTypes();
154
155 for (int i = 0; i < argTypes.length; i++) {
156 assertEquals(argTypes[i], testArgTypes[i]);
157 }
158 }
159
160 /***
161 * Test the mashCode method returns the same hashCode when signatures match
162 */
163 public void testHashCodeReturnsSameValueWhenSignaturesMatch() {
164 Class[] newArgTypes = new Class[] {String.class, Integer.class};
165 Signature newSignature =
166 new Signature(Signature.class, "myMethod", newArgTypes);
167 assertEquals(signature.hashCode(), newSignature.hashCode());
168 }
169
170 /***
171 * Test the isAssignableFrom method when it returns false due to different
172 * number of arguments
173 */
174 public void testIsAssignableFromDifferentArgCount() {
175 Class[] newArgTypes = new Class[] {String.class};
176 Signature newSignature =
177 new Signature(Signature.class, "myMethod", newArgTypes);
178 assertFalse(newSignature.isAssignableFrom(signature));
179 }
180
181 /***
182 * Test the isAssignableFrom method when it returns false due to comparing
183 * a different class type
184 */
185 public void testIsAssignableFromDifferentClass() {
186 Class[] newArgTypes = new Class[] {String.class, Integer.class};
187 Signature newSignature =
188 new Signature(String.class, "myMethod", newArgTypes);
189 assertFalse(newSignature.isAssignableFrom(signature));
190 }
191
192 /***
193 * Test the isAssignableFrom method when it returns false due to comparing
194 * a different method name
195 */
196 public void testIsAssignableFromDifferentMethod() {
197 Class[] newArgTypes = new Class[] {String.class, Integer.class};
198 Signature newSignature =
199 new Signature(Signature.class, "myMethodXX", newArgTypes);
200 assertFalse(newSignature.isAssignableFrom(signature));
201 }
202
203 /***
204 * Test the isAssignableFrom method when it returns true due to comparing
205 * two identical calls
206 */
207 public void testIsAssignableFromIdenticalArgs() {
208 Class[] newArgTypes = new Class[] {String.class, Integer.class};
209 Signature newSignature =
210 new Signature(Signature.class, "myMethod", newArgTypes);
211 assertTrue(signature.isAssignableFrom(newSignature));
212 }
213
214 /***
215 * Test the isAssignableFrom method when it returns false due to comparing
216 * an argument that is not a subclass
217 */
218 public void testIsAssignableFromNoSubclassArg() {
219 Class[] newArgTypes = new Class[] {Long.class, Integer.class};
220 Signature newSignature =
221 new Signature(Signature.class, "myMethod", newArgTypes);
222 assertFalse(newSignature.isAssignableFrom(signature));
223 }
224
225 /***
226 * Test the isAssignableFrom method when it returns true due to comparing
227 * an argument that is a subclass
228 */
229 public void testIsAssignableFromSubclassArg() {
230 Class[] newArgTypes = new Class[] {Object.class, Integer.class};
231 Signature newSignature =
232 new Signature(Signature.class, "myMethod", newArgTypes);
233 assertTrue(newSignature.isAssignableFrom(signature));
234 }
235
236 /***
237 * Test the isReturnTypePrimitive method with void return type
238 */
239 public void testIsReturnTypePrimitive() {
240 assertFalse(signatureVoid.isReturnTypePrimitive());
241 }
242
243 /***
244 * Test the toString method
245 */
246 public void testToString() {
247 assertEquals("java.lang.String org.virtualmock.call.Signature.myMethod(java.lang.String, java.lang.Integer)",
248 signature.toString());
249 }
250
251 /***
252 * Test the toString method with a null return type
253 */
254 public void testToStringWithNullReturnType() {
255 returnType = null;
256 signature = new Signature(returnType, classType, methodName, argTypes);
257 assertEquals("void org.virtualmock.call.Signature.myMethod(java.lang.String, java.lang.Integer)",
258 signature.toString());
259 }
260
261 /***
262 * Test the toString method with a primitive method
263 */
264 public void testToStringWithPrimitiveMethod() {
265 returnType = int.class;
266 signature = new Signature(returnType, classType, methodName, argTypes);
267 assertEquals("int org.virtualmock.call.Signature.myMethod(java.lang.String, java.lang.Integer)",
268 signature.toString());
269 }
270
271 /***
272 * Test the toString method with a void return type
273 */
274 public void testToStringWithVoidMethod() {
275 returnType = void.class;
276 signature = new Signature(returnType, classType, methodName, argTypes);
277 assertEquals("void org.virtualmock.call.Signature.myMethod(java.lang.String, java.lang.Integer)",
278 signature.toString());
279 }
280 }