1 3 package test.jmock.core; 4 5 import java.lang.reflect.Method ; 6 import java.util.Arrays ; 7 import junit.framework.TestCase; 8 import org.jmock.core.Invocation; 9 import org.jmock.expectation.AssertMo; 10 import test.jmock.core.testsupport.MethodFactory; 11 12 13 public class InvocationTest extends TestCase 14 { 15 16 final Object INVOKED = new Object () 17 { 18 public String toString() { 19 return "INVOKED"; 20 } 21 }; 22 final String METHOD_NAME = "methodName"; 23 final Class [] ARG_TYPES = new Class []{int.class, boolean.class}; 24 final Class RETURN_TYPE = String .class; 25 final Object [] ARG_VALUES = {new Integer (0), Boolean.TRUE}; 26 final Class [] EXCEPTION_TYPES = new Class []{InterruptedException .class, SecurityException .class}; 27 28 MethodFactory methodFactory; 29 Method method; 30 31 public InvocationTest( String name ) { 32 super(name); 33 } 34 35 public void setUp() throws Exception { 36 methodFactory = new MethodFactory(); 37 method = methodFactory.newMethod(METHOD_NAME, ARG_TYPES, RETURN_TYPE, EXCEPTION_TYPES); 38 } 39 40 public void testCanBeConstructedFromAMethodObject() throws Exception { 41 Invocation invocation = new Invocation(INVOKED, method, ARG_VALUES); 42 43 assertSame("invoked object", INVOKED, invocation.invokedObject); 44 assertEquals("invoked method", method, invocation.invokedMethod); 45 assertEquals("name", method.getName(), invocation.invokedMethod.getName()); 46 assertEquals("parameter types", 47 Arrays.asList(method.getParameterTypes()), 48 Arrays.asList(invocation.invokedMethod.getParameterTypes())); 49 assertEquals("return type", 50 method.getReturnType(), invocation.invokedMethod.getReturnType()); 51 assertEquals("argument values", 52 Arrays.asList(ARG_VALUES), invocation.parameterValues); 53 } 54 55 public void testConstructorInterpretsNullParameterValueArrayAsZeroArguments() { 56 Invocation invocation = new Invocation(INVOKED, method, null); 57 58 assertEquals("expected no parameters values", 59 0, invocation.parameterValues.size()); 60 } 61 62 public void testTestsForEqualityOnTargetAndMethodSignatureAndArguments() { 63 Invocation invocation1 = new Invocation(INVOKED, method, ARG_VALUES); 64 Invocation invocation2 = new Invocation(INVOKED, method, ARG_VALUES); 65 Invocation differentTarget = new Invocation("OTHER TARGET", method, ARG_VALUES); 66 67 Invocation differentMethod = new Invocation(INVOKED, 68 methodFactory.newMethod("OTHER_" + METHOD_NAME, ARG_TYPES, RETURN_TYPE, EXCEPTION_TYPES), 69 ARG_VALUES); 70 Invocation differentArgValues = new Invocation(INVOKED, method, 71 new Object []{new Integer (1), Boolean.FALSE}); 72 73 assertTrue("should be equal to itself", invocation1.equals(invocation1)); 74 assertTrue("identical calls should be equal", invocation1.equals(invocation2)); 75 76 assertFalse("should not be equal to object that is not an Invocation", 77 invocation1.equals(new Object ())); 78 assertFalse("should not be equal to null", 79 invocation1.equals(null)); 80 assertFalse("should not be equal if different invoked object", 81 invocation1.equals(differentTarget)); 82 assertFalse("should not be equal if different method", 83 invocation1.equals(differentMethod)); 84 assertFalse("should not be equal if different argumentValues", 85 invocation1.equals(differentArgValues)); 86 } 87 88 public void testFollowsEqualsHashcodeProtocol() { 89 Invocation invocation1 = new Invocation(INVOKED, method, ARG_VALUES); 90 Invocation invocation2 = new Invocation(INVOKED, method, ARG_VALUES); 91 92 assertEquals("should have equal hash codes", 93 invocation1.hashCode(), invocation2.hashCode()); 94 } 95 96 public void testToStringWithTwoArguments() throws Exception { 97 Invocation invocation = new Invocation(INVOKED, 98 methodFactory.newMethod(METHOD_NAME, new Class []{String .class, String .class}, void.class, 99 EXCEPTION_TYPES), 100 new Object []{"arg1", "arg2"}); 101 String result = invocation.toString(); 102 103 AssertMo.assertIncludes("Should contain invokedMethod name", METHOD_NAME, result); 104 AssertMo.assertIncludes("Should contain firstArg", "arg1", result); 105 AssertMo.assertIncludes("Should contain second Arg", "arg2", result); 106 } 107 108 public void testToStringWithStringArray() throws Exception { 109 Invocation invocation = new Invocation(INVOKED, 110 methodFactory.newMethod(METHOD_NAME, new Class []{String [].class}, void.class, EXCEPTION_TYPES), 111 new Object []{new String []{"arg1", "arg2"}}); 112 String result = invocation.toString(); 113 114 AssertMo.assertIncludes("Should contain invokedMethod name", METHOD_NAME, result); 115 AssertMo.assertIncludes("Should contain args as an array", "[<arg1>, <arg2>]", result); 116 } 117 118 public void testToStringWithPrimitiveArray() throws Exception { 119 Invocation invocation = new Invocation(INVOKED, 120 methodFactory.newMethod(METHOD_NAME, new Class []{long[].class}, void.class, EXCEPTION_TYPES), 121 new Object []{new long[]{1, 2}}); 122 String result = invocation.toString(); 123 124 AssertMo.assertIncludes("Should contain invokedMethod name", METHOD_NAME, result); 125 AssertMo.assertIncludes("Should contain args as an array", "[<1>, <2>]", result); 126 } 127 128 public void testMethodToStringWithNullArg() throws Exception { 129 Invocation invocation = new Invocation(INVOKED, methodFactory.newMethod(METHOD_NAME, new Class []{String .class}, void.class, EXCEPTION_TYPES), 130 new Object []{null}); 131 String result = invocation.toString(); 132 133 AssertMo.assertIncludes("Should contain invokedMethod name", METHOD_NAME, result); 134 AssertMo.assertIncludes("Should contain firstArg", "<null>", result); 135 } 136 137 public void testMethodToStringWithObjectArg() throws Exception { 138 final String argAsString = "TO_STRING_RESULT"; 139 Object arg = new Object () 140 { 141 public String toString() { 142 return argAsString; 143 } 144 }; 145 146 Invocation invocation = new Invocation(INVOKED, methodFactory.newMethod(METHOD_NAME, new Class []{String .class}, void.class, EXCEPTION_TYPES), 147 new Object []{arg}); 148 String result = invocation.toString(); 149 150 AssertMo.assertIncludes("Should contain invokedMethod name", METHOD_NAME, result); 151 AssertMo.assertIncludes("Should contain firstArg", argAsString, result); 152 } 153 } 154 | Popular Tags |