001: package com.mockrunner.test.util;
002:
003: import java.lang.reflect.Method;
004: import java.util.ArrayList;
005: import java.util.Arrays;
006: import java.util.List;
007: import java.util.Set;
008:
009: import junit.framework.TestCase;
010:
011: import com.mockrunner.base.NestedApplicationException;
012: import com.mockrunner.util.common.MethodUtil;
013:
014: public class MethodUtilTest extends TestCase {
015: public void testInvoke() {
016: TestObject testObject = new TestObject();
017: assertNull(MethodUtil.invoke(testObject, "testMethod1"));
018: assertEquals("testMethod2", MethodUtil.invoke(testObject,
019: "testMethod2"));
020: try {
021: MethodUtil.invoke(testObject, "testMethod3");
022: fail();
023: } catch (NestedApplicationException exc) {
024: //should throw exception
025: }
026: try {
027: MethodUtil.invoke(testObject, "testMethod4");
028: fail();
029: } catch (NestedApplicationException exc) {
030: //should throw exception
031: }
032: assertTrue(testObject.wasMethod1Called());
033: assertTrue(testObject.wasMethod2Called());
034: assertFalse(testObject.wasMethod3Called());
035: assertFalse(testObject.wasMethod4Called());
036: }
037:
038: public void testInvokeWithParameter() {
039: TestObject testObject = new TestObject();
040: try {
041: MethodUtil.invoke(testObject, "testMethod1", null);
042: fail();
043: } catch (NestedApplicationException exc) {
044: //should throw exception
045: }
046: try {
047: MethodUtil.invoke(testObject, "testMethod1", "test");
048: fail();
049: } catch (NestedApplicationException exc) {
050: //should throw exception
051: }
052: assertNull(MethodUtil.invoke(testObject, "testMethod4", "test"));
053: assertEquals(new Integer(3), MethodUtil.invoke(testObject,
054: "testMethod5", new Integer[] { new Integer(3) }));
055: assertFalse(testObject.wasMethod1Called());
056: assertTrue(testObject.wasMethod4Called());
057: assertTrue(testObject.wasMethod5Called());
058: }
059:
060: public void testAreMethodEqual() throws Exception {
061: Method method1 = TestObject.class
062: .getMethod("testMethod1", null);
063: Method method2 = TestObject.class
064: .getMethod("testMethod2", null);
065: Method method4 = TestObject.class.getMethod("testMethod4",
066: new Class[] { String.class });
067: Method method5 = TestObject.class.getMethod("testMethod5",
068: new Class[] { Integer[].class });
069: Method anotherMethod2 = AnotherTestObject.class.getMethod(
070: "testMethod2", null);
071: Method anotherMethod4 = AnotherTestObject.class.getMethod(
072: "testMethod4", new Class[] { Integer.class });
073: Method anotherMethod5 = AnotherTestObject.class.getMethod(
074: "testMethod5", new Class[] { Integer[].class });
075: try {
076: MethodUtil.areMethodsEqual(null, method1);
077: fail();
078: } catch (NullPointerException exc) {
079: //should throw exception
080: }
081: try {
082: MethodUtil.areMethodsEqual(method2, null);
083: fail();
084: } catch (NullPointerException exc) {
085: //should throw exception
086: }
087: assertTrue(MethodUtil.areMethodsEqual(method1, method1));
088: assertFalse(MethodUtil.areMethodsEqual(method1, method2));
089: assertFalse(MethodUtil.areMethodsEqual(method2, anotherMethod2));
090: assertFalse(MethodUtil.areMethodsEqual(method2, anotherMethod4));
091: assertFalse(MethodUtil.areMethodsEqual(method4, anotherMethod4));
092: assertTrue(MethodUtil.areMethodsEqual(method5, anotherMethod5));
093: }
094:
095: public void testGetMatchingDeclaredMethods() throws Exception {
096: Method[] methods = MethodUtil.getMatchingDeclaredMethods(
097: TestObject.class, "test.*");
098: assertEquals(5, methods.length);
099: List methodNames = new ArrayList();
100: for (int ii = 0; ii < methods.length; ii++) {
101: methodNames.add(methods[ii].getName());
102: }
103: assertTrue(methodNames.contains("testMethod1"));
104: assertTrue(methodNames.contains("testMethod2"));
105: assertTrue(methodNames.contains("testMethod3"));
106: assertTrue(methodNames.contains("testMethod4"));
107: assertTrue(methodNames.contains("testMethod5"));
108: methods = MethodUtil.getMatchingDeclaredMethods(
109: TestObject.class, "wasMethod.Called");
110: assertEquals(5, methods.length);
111: methodNames = new ArrayList();
112: for (int ii = 0; ii < methods.length; ii++) {
113: methodNames.add(methods[ii].getName());
114: }
115: assertTrue(methodNames.contains("wasMethod1Called"));
116: assertTrue(methodNames.contains("wasMethod2Called"));
117: assertTrue(methodNames.contains("wasMethod3Called"));
118: assertTrue(methodNames.contains("wasMethod4Called"));
119: assertTrue(methodNames.contains("wasMethod5Called"));
120: methods = MethodUtil.getMatchingDeclaredMethods(
121: TestObject.class, "wasMethod5Called");
122: assertEquals(1, methods.length);
123: }
124:
125: public void testOverrides() throws Exception {
126: Method method1Super = TestSuper.class.getDeclaredMethod(
127: "testMethod1", null);
128: Method method2Super = TestSuper.class.getDeclaredMethod(
129: "testMethod2",
130: new Class[] { int[].class, String.class });
131: Method method1SubOverride = TestSub.class.getDeclaredMethod(
132: "testMethod1", null);
133: Method method2SubOverride = TestSub.class.getDeclaredMethod(
134: "testMethod2",
135: new Class[] { int[].class, String.class });
136: Method method1SubNotOverride = TestSub.class.getDeclaredMethod(
137: "testMethod1", new Class[] { String.class });
138: Method method2SubNotOverride = TestSub.class.getDeclaredMethod(
139: "testMethod2", new Class[] { int[].class });
140: Method methodInterface = TestInterface.class.getDeclaredMethod(
141: "testInterface", null);
142: Method methodSubInterface = TestSub.class.getDeclaredMethod(
143: "testInterface", null);
144: assertFalse(MethodUtil.overrides(method1Super, method1Super));
145: assertFalse(MethodUtil.overrides(method1Super, method2Super));
146: assertFalse(MethodUtil.overrides(method1Super,
147: method1SubNotOverride));
148: assertFalse(MethodUtil.overrides(method2Super,
149: method2SubNotOverride));
150: assertFalse(MethodUtil.overrides(method1SubOverride,
151: method1Super));
152: assertFalse(MethodUtil.overrides(methodInterface,
153: methodSubInterface));
154: assertTrue(MethodUtil.overrides(method1Super,
155: method1SubOverride));
156: assertTrue(MethodUtil.overrides(method2Super,
157: method2SubOverride));
158: }
159:
160: public void testGetMethodsSortedByInheritanceHierarchy()
161: throws Exception {
162: Method[][] methods = MethodUtil
163: .getMethodsSortedByInheritanceHierarchy(TestSub3.class);
164: assertEquals(5, methods.length);
165: assertEquals(2, methods[1].length);
166: assertEquals(6, methods[2].length);
167: assertEquals(3, methods[3].length);
168: Method method1Super = TestSuper.class.getDeclaredMethod(
169: "testMethod1", null);
170: Method method2Super = TestSuper.class.getDeclaredMethod(
171: "testMethod2",
172: new Class[] { int[].class, String.class });
173: Method method1SubOverride = TestSub.class.getDeclaredMethod(
174: "testMethod1", null);
175: Method method2SubOverride = TestSub.class.getDeclaredMethod(
176: "testMethod2",
177: new Class[] { int[].class, String.class });
178: Method method1SubNotOverride = TestSub.class.getDeclaredMethod(
179: "testMethod1", new Class[] { String.class });
180: Method method2SubNotOverride = TestSub.class.getDeclaredMethod(
181: "testMethod2", new Class[] { int[].class });
182: Method methodSubInterface = TestSub.class.getDeclaredMethod(
183: "testInterface", null);
184: Method methodSubtoString = TestSub.class.getDeclaredMethod(
185: "toString", null);
186: Method methodSub2another = TestSub2.class.getDeclaredMethod(
187: "anotherMethod", null);
188: Method methodSub2anotherProtected = TestSub2.class
189: .getDeclaredMethod("anotherProtectedMethod", null);
190: Method methodSub2toString = TestSub2.class.getDeclaredMethod(
191: "toString", null);
192: assertTrue(Arrays.asList(methods[1]).contains(method1Super));
193: assertTrue(Arrays.asList(methods[1]).contains(method2Super));
194: assertTrue(Arrays.asList(methods[2]).contains(
195: method1SubOverride));
196: assertTrue(Arrays.asList(methods[2]).contains(
197: method2SubOverride));
198: assertTrue(Arrays.asList(methods[2]).contains(
199: method1SubNotOverride));
200: assertTrue(Arrays.asList(methods[2]).contains(
201: method2SubNotOverride));
202: assertTrue(Arrays.asList(methods[2]).contains(
203: methodSubInterface));
204: assertTrue(Arrays.asList(methods[2])
205: .contains(methodSubtoString));
206: assertTrue(Arrays.asList(methods[3])
207: .contains(methodSub2another));
208: assertTrue(Arrays.asList(methods[3]).contains(
209: methodSub2anotherProtected));
210: assertTrue(Arrays.asList(methods[3]).contains(
211: methodSub2toString));
212: assertEquals(0, methods[4].length);
213: }
214:
215: public void testGetOverriddenMethods() throws Exception {
216: Method method2Super = TestSuper.class.getDeclaredMethod(
217: "testMethod2",
218: new Class[] { int[].class, String.class });
219: Method method1SubOverride = TestSub.class.getDeclaredMethod(
220: "testMethod1", null);
221: Method method2SubOverride = TestSub.class.getDeclaredMethod(
222: "testMethod2",
223: new Class[] { int[].class, String.class });
224: Method method2SubNotOverride = TestSub.class.getDeclaredMethod(
225: "testMethod2", new Class[] { int[].class });
226: Method methodSubtoString = TestSub.class.getDeclaredMethod(
227: "toString", null);
228: Method methodSub2another = TestSub2.class.getDeclaredMethod(
229: "anotherMethod", null);
230: Method methodSub2toString = TestSub2.class.getDeclaredMethod(
231: "toString", null);
232: Method toString = Object.class.getDeclaredMethod("toString",
233: null);
234: Set methods = MethodUtil.getOverriddenMethods(TestSub2.class,
235: new Method[] { method1SubOverride, method2Super,
236: methodSub2another, method2SubNotOverride,
237: toString });
238: assertEquals(5, methods.size());
239: assertTrue(methods.contains(method2Super));
240: assertTrue(methods.contains(method2SubOverride));
241: assertTrue(methods.contains(toString));
242: assertTrue(methods.contains(methodSubtoString));
243: assertTrue(methods.contains(methodSub2toString));
244: }
245:
246: public class Super {
247: public void testSuperMethod() {
248:
249: }
250: }
251:
252: public class TestObject extends Super {
253: private boolean method1Called = false;
254: private boolean method2Called = false;
255: private boolean method3Called = false;
256: private boolean method4Called = false;
257: private boolean method5Called = false;
258:
259: public void testMethod1() {
260: method1Called = true;
261: }
262:
263: public String testMethod2() {
264: method2Called = true;
265: return "testMethod2";
266: }
267:
268: protected void testMethod3() {
269: method3Called = true;
270: }
271:
272: public void testMethod4(String arg) {
273: method4Called = true;
274: }
275:
276: public int testMethod5(Integer[] arg) {
277: method5Called = true;
278: return arg[0].intValue();
279: }
280:
281: public boolean wasMethod1Called() {
282: return method1Called;
283: }
284:
285: public boolean wasMethod2Called() {
286: return method2Called;
287: }
288:
289: public boolean wasMethod3Called() {
290: return method3Called;
291: }
292:
293: public boolean wasMethod4Called() {
294: return method4Called;
295: }
296:
297: public boolean wasMethod5Called() {
298: return method5Called;
299: }
300: }
301:
302: public class AnotherTestObject {
303: public int testMethod2() {
304: return 0;
305: }
306:
307: public void testMethod4(Integer arg) {
308:
309: }
310:
311: public int testMethod5(Integer[] arg) {
312: return 0;
313: }
314: }
315:
316: public static interface TestInterface {
317: public Integer testInterface();
318: }
319:
320: public static class TestSuper {
321: public String testMethod1() {
322: return null;
323: }
324:
325: public void testMethod2(int[] param, String param2) {
326:
327: }
328: }
329:
330: public static class TestSub extends TestSuper implements
331: TestInterface {
332: public Integer testInterface() {
333: return null;
334: }
335:
336: public String testMethod1(String param) {
337: return super .testMethod1();
338: }
339:
340: public String testMethod1() {
341: return super .testMethod1();
342: }
343:
344: public void testMethod2(int[] param) {
345: super .testMethod2(param, null);
346: }
347:
348: public void testMethod2(int[] param, String param2) {
349: super .testMethod2(param, param2);
350: }
351:
352: public static void testMethod3(String param) {
353:
354: }
355:
356: public String toString() {
357: return super .toString();
358: }
359: }
360:
361: public static class TestSub2 extends TestSub {
362: public String toString() {
363: return super .toString();
364: }
365:
366: public static void anotherStaticMethod() {
367:
368: }
369:
370: protected void anotherProtectedMethod() {
371:
372: }
373:
374: public void anotherMethod() {
375:
376: }
377: }
378:
379: public class TestSub3 extends TestSub2 {
380:
381: }
382: }
|