KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > jmock > core > InvocationTest


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

3 package test.jmock.core;
4
5 import java.lang.reflect.Method JavaDoc;
6 import java.util.Arrays JavaDoc;
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 JavaDoc INVOKED = new Object JavaDoc()
17     {
18         public String JavaDoc toString() {
19             return "INVOKED";
20         }
21     };
22     final String JavaDoc METHOD_NAME = "methodName";
23     final Class JavaDoc[] ARG_TYPES = new Class JavaDoc[]{int.class, boolean.class};
24     final Class JavaDoc RETURN_TYPE = String JavaDoc.class;
25     final Object JavaDoc[] ARG_VALUES = {new Integer JavaDoc(0), Boolean.TRUE};
26     final Class JavaDoc[] EXCEPTION_TYPES = new Class JavaDoc[]{InterruptedException JavaDoc.class, SecurityException JavaDoc.class};
27
28     MethodFactory methodFactory;
29     Method JavaDoc method;
30
31     public InvocationTest( String JavaDoc name ) {
32         super(name);
33     }
34
35     public void setUp() throws Exception JavaDoc {
36         methodFactory = new MethodFactory();
37         method = methodFactory.newMethod(METHOD_NAME, ARG_TYPES, RETURN_TYPE, EXCEPTION_TYPES);
38     }
39
40     public void testCanBeConstructedFromAMethodObject() throws Exception JavaDoc {
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 JavaDoc[]{new Integer JavaDoc(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 JavaDoc()));
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 JavaDoc {
97         Invocation invocation = new Invocation(INVOKED,
98                                                methodFactory.newMethod(METHOD_NAME, new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class}, void.class,
99                                                                        EXCEPTION_TYPES),
100                                                new Object JavaDoc[]{"arg1", "arg2"});
101         String JavaDoc 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 JavaDoc {
109         Invocation invocation = new Invocation(INVOKED,
110                                                methodFactory.newMethod(METHOD_NAME, new Class JavaDoc[]{String JavaDoc[].class}, void.class, EXCEPTION_TYPES),
111                                                new Object JavaDoc[]{new String JavaDoc[]{"arg1", "arg2"}});
112         String JavaDoc 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 JavaDoc {
119         Invocation invocation = new Invocation(INVOKED,
120                                                methodFactory.newMethod(METHOD_NAME, new Class JavaDoc[]{long[].class}, void.class, EXCEPTION_TYPES),
121                                                new Object JavaDoc[]{new long[]{1, 2}});
122         String JavaDoc 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 JavaDoc {
129         Invocation invocation = new Invocation(INVOKED, methodFactory.newMethod(METHOD_NAME, new Class JavaDoc[]{String JavaDoc.class}, void.class, EXCEPTION_TYPES),
130                                                new Object JavaDoc[]{null});
131         String JavaDoc 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 JavaDoc {
138         final String JavaDoc argAsString = "TO_STRING_RESULT";
139         Object JavaDoc arg = new Object JavaDoc()
140         {
141             public String JavaDoc toString() {
142                 return argAsString;
143             }
144         };
145
146         Invocation invocation = new Invocation(INVOKED, methodFactory.newMethod(METHOD_NAME, new Class JavaDoc[]{String JavaDoc.class}, void.class, EXCEPTION_TYPES),
147                                                new Object JavaDoc[]{arg});
148         String JavaDoc 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