1 3 package test.jmock.core.testsupport; 4 5 import java.lang.reflect.Method ; 6 import java.lang.reflect.Modifier ; 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 METHOD_NAME = "METHOD_NAME"; 17 static final Class [] ARG_TYPES = new Class []{int.class, Object .class, double[].class, String [].class}; 18 static final Class RETURN_TYPE = ReturnType.class; 19 static final Class [] EXCEPTION_TYPES = new Class []{InterruptedException .class}; 20 21 MethodFactory factory; 22 23 24 public void setUp() { 25 factory = new MethodFactory(); 26 } 27 28 public void testCreatesMethodInNewNamedClass() { 29 Method 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 method = factory.newMethodReturning(RETURN_TYPE); 40 41 assertSame("return type", RETURN_TYPE, method.getReturnType()); 42 } 43 44 private void assertAllSame( String message, Class [] expected, Class [] 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 |