KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > jmock > core > testsupport > MethodFactoryTest


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

3 package test.jmock.core.testsupport;
4
5 import java.lang.reflect.Method JavaDoc;
6 import java.lang.reflect.Modifier JavaDoc;
7 import junit.framework.TestCase;
8
9
10 public class MethodFactoryTest extends TestCase
11 {
12     private class ReturnType
13     {
14     }
15
16     static final String JavaDoc METHOD_NAME = "METHOD_NAME";
17     static final Class JavaDoc[] ARG_TYPES = new Class JavaDoc[]{int.class, Object JavaDoc.class, double[].class, String JavaDoc[].class};
18     static final Class JavaDoc RETURN_TYPE = ReturnType.class;
19     static final Class JavaDoc[] EXCEPTION_TYPES = new Class JavaDoc[]{InterruptedException JavaDoc.class};
20
21     MethodFactory factory;
22
23
24     public void setUp() {
25         factory = new MethodFactory();
26     }
27
28     public void testCreatesMethodInNewNamedClass() {
29         Method JavaDoc method = factory.newMethod(METHOD_NAME, ARG_TYPES, RETURN_TYPE, EXCEPTION_TYPES);
30
31         assertTrue("is public", Modifier.isPublic(method.getModifiers()));
32         assertEquals("invokedMethod name", METHOD_NAME, method.getName());
33         assertAllSame("arg types", ARG_TYPES, method.getParameterTypes());
34         assertSame("return type", RETURN_TYPE, method.getReturnType());
35         assertAllSame("exception types", EXCEPTION_TYPES, method.getExceptionTypes());
36     }
37
38     public void testCreatesMethodThatReturnsAType() {
39         Method JavaDoc method = factory.newMethodReturning(RETURN_TYPE);
40
41         assertSame("return type", RETURN_TYPE, method.getReturnType());
42     }
43
44     private void assertAllSame( String JavaDoc message, Class JavaDoc[] expected, Class JavaDoc[] actual ) {
45         assertEquals(message + ": number of elements ", expected.length, actual.length);
46
47         for (int i = 0; i < expected.length; i++) {
48             assertSame(message + ": element " + i, expected[i], actual[i]);
49         }
50     }
51 }
52
Popular Tags