1 /* Copyright (c) 2000-2003, jMock.org. See bundled LICENSE.txt */
2 package org.jmock.dynamic;
3
4 import junit.framework.TestCase;
5 import org.jmock.Mock;
6 import org.jmock.expectation.AssertMo;
7
8 import java.lang.reflect.Method;
9 import java.util.Arrays;
10
11 public class InvocationTest
12 extends TestCase {
13 public String exampleMethod(int number, boolean flag) {
14 return "hello, world";
15 }
16
17 final String METHOD_NAME = "exampleMethod";
18 final Class[] ARG_TYPES = {int.class, boolean.class};
19 final Class RETURN_TYPE = String.class;
20 final Object[] ARG_VALUES = {new Integer(0), Boolean.TRUE};
21
22
23 public InvocationTest(String name) {
24 super(name);
25 }
26
27 public void testCanBeConstructedWithExplicitCallDetails() {
28 Invocation call = new Invocation(METHOD_NAME, ARG_TYPES,
29 RETURN_TYPE, ARG_VALUES);
30
31 assertEquals("name", METHOD_NAME, call.getMethodName());
32 assertEquals("parameter types",
33 Arrays.asList(ARG_TYPES), call.getParameterTypes());
34 assertEquals("return type",
35 RETURN_TYPE, call.getReturnType());
36 assertEquals("argument values",
37 Arrays.asList(ARG_VALUES), call.getParameterValues());
38 }
39
40 public void testCanBeConstructedFromAMethodObject() throws Exception {
41 Method method = getClass().getMethod(METHOD_NAME, ARG_TYPES);
42
43 Invocation call = new Invocation(method, ARG_VALUES);
44
45 assertEquals("name", method.getName(), call.getMethodName());
46 assertEquals("parameter types",
47 Arrays.asList(method.getParameterTypes()),
48 call.getParameterTypes());
49 assertEquals("return type",
50 method.getReturnType(), call.getReturnType());
51 assertEquals("argument values",
52 Arrays.asList(ARG_VALUES), call.getParameterValues());
53 }
54
55 public void testConstructorInterpretsNullParameterValueArrayAsZeroArguments() {
56 Invocation call = new Invocation(METHOD_NAME, new Class[0],
57 RETURN_TYPE, null);
58
59 assertEquals("expected no parameters values",
60 0, call.getParameterValues().size());
61 }
62
63 public void testTestsForEqualityOnMethodSignatureAndArguments() {
64 Invocation call1 = new Invocation(
65 METHOD_NAME, ARG_TYPES, RETURN_TYPE,
66 ARG_VALUES);
67 Invocation call2 = new Invocation(
68 METHOD_NAME, ARG_TYPES, RETURN_TYPE,
69 ARG_VALUES);
70 Invocation differentName = new Invocation(
71 "other" + METHOD_NAME, ARG_TYPES, RETURN_TYPE,
72 ARG_VALUES);
73 Invocation differentReturnType = new Invocation(
74 "other" + METHOD_NAME, ARG_TYPES, int.class,
75 ARG_VALUES);
76 Invocation differentArgTypes = new Invocation(
77 "other" + METHOD_NAME, new Class[]{double.class}, RETURN_TYPE,
78 ARG_VALUES);
79 Invocation differentArgValues = new Invocation(
80 "other" + METHOD_NAME, ARG_TYPES, RETURN_TYPE,
81 new Object[]{new Integer(1), Boolean.FALSE});
82
83 assertTrue("should be equal to itself", call1.equals(call1));
84 assertTrue("identical calls should be equal", call1.equals(call2));
85
86 assertFalse("should not be equal to object that is not an ActiveCall",
87 call1.equals(new Object()));
88 assertFalse("should not be equal to null", call1.equals(null));
89 assertFalse("should not be equal if different name",
90 call1.equals(differentName));
91 assertFalse("should not be equal if different parameter types",
92 call1.equals(differentArgTypes));
93 assertFalse("should not be equal if different return type",
94 call1.equals(differentReturnType));
95 assertFalse("should not be equal if different argumentValues",
96 call1.equals(differentArgValues));
97 }
98
99 public void testFollowsEqualsHashcodeProtocol() {
100 Invocation call1 = new Invocation(
101 METHOD_NAME, ARG_TYPES, RETURN_TYPE,
102 ARG_VALUES);
103 Invocation call2 = new Invocation(
104 METHOD_NAME, ARG_TYPES, RETURN_TYPE,
105 ARG_VALUES);
106
107 assertEquals("should have equal hash codes",
108 call1.hashCode(), call2.hashCode());
109 }
110
111 public void testToStringWithTwoArguments() throws Exception {
112 Invocation invocation =
113 new Invocation("methodName", new Class[]{String.class, String.class}, void.class,
114 new Object[]{"arg1", "arg2"});
115 String result = invocation.toString();
116
117 AssertMo.assertIncludes("Should contain method name", "methodName", result);
118 AssertMo.assertIncludes("Should contain firstArg", "arg1", result);
119 AssertMo.assertIncludes("Should contain second Arg", "arg2", result);
120 }
121
122 public void testToStringWithStringArray() throws Exception {
123 Invocation invocation =
124 new Invocation("methodName", new Class[]{String[].class}, void.class,
125 new Object[]{new String[]{"arg1", "arg2"}});
126 String result = invocation.toString();
127
128 AssertMo.assertIncludes("Should contain method name", "methodName", result);
129 AssertMo.assertIncludes("Should contain args as an array", "[<arg1>, <arg2>]", result);
130 }
131
132 public void testToStringWithPrimitiveArray() throws Exception {
133 Invocation invocation =
134 new Invocation("methodName", new Class[]{long[].class}, void.class,
135 new Object[]{new long[]{1, 2}});
136 String result = invocation.toString();
137
138 AssertMo.assertIncludes("Should contain method name", "methodName", result);
139 AssertMo.assertIncludes("Should contain args as an array", "[<1>, <2>]", result);
140 }
141
142 public void testMethodToStringWithProxyArg() throws Exception {
143 Mock mockDummyInterface = new Mock(DummyInterface.class, "DummyMock");
144
145 Invocation invocation =
146 new Invocation("methodName", new Class[]{String.class, DummyInterface.class}, void.class,
147 new Object[]{"arg1", mockDummyInterface.proxy()});
148 String result = invocation.toString();
149
150 AssertMo.assertIncludes("Should contain method name", "methodName", result);
151 AssertMo.assertIncludes("Should contain firstArg", "arg1", result);
152 AssertMo.assertIncludes("Should contain second Arg", "DummyMock", result);
153 }
154
155 public void testMethodToStringWithNullArg() throws Exception {
156 Invocation invocation =
157 new Invocation("methodName", new Class[]{String.class}, void.class,
158 new Object[]{null});
159 String result = invocation.toString();
160
161 AssertMo.assertIncludes("Should contain method name", "methodName", result);
162 AssertMo.assertIncludes("Should contain firstArg", "<null>", result);
163 }
164
165 }
This page was automatically generated by Maven