1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package org.codehaus.groovy.runtime;
47
48 import java.lang.reflect.Method;
49
50 import junit.framework.TestCase;
51
52 /***
53 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
54 * @version $Revision: 1.2 $
55 */
56 public class NewStaticMetaMethodTest extends TestCase {
57
58 public void testInvokeMetaMethod() throws Exception {
59 Method method = getClass().getMethod("dummyMethod", new Class[] { String.class, String.class });
60 assertTrue("Should have found a method", method != null);
61
62 NewInstanceMetaMethod metaMethod = createNewMetaMethod(method);
63
64 Object answer = metaMethod.invoke("abc", new Object[] { "xyz" });
65 assertEquals("def", answer);
66
67 assertTrue("Should not appear as static method", metaMethod.isStatic() == false);
68 }
69
70 public void testInvokeDefaultGroovyMethod() throws Exception {
71 Method method = DefaultGroovyMethods.class.getMethod("plus", new Class[] { String.class, Object.class });
72 assertTrue("Should have found a method", method != null);
73
74 NewInstanceMetaMethod metaMethod = createNewMetaMethod(method);
75
76 Object answer = metaMethod.invoke("abc", new Object[] { "123" });
77 assertEquals("abc123", answer);
78
79 System.out.println("Found: " + answer);
80 }
81
82 public void testInvokeDefaultGroovyMethodUsingMetaClass() {
83 Object answer = InvokerHelper.invokeMethod("abc", "plus", new Object[] { "123" });
84 assertEquals("abc123", answer);
85
86 System.out.println("Found: " + answer);
87 }
88
89 public static String dummyMethod(String foo, String bar) throws Exception {
90 assertEquals("abc", foo);
91 assertEquals("xyz", bar);
92 return "def";
93 }
94
95 protected NewInstanceMetaMethod createNewMetaMethod(Method method) {
96 return new NewInstanceMetaMethod(new ReflectionMetaMethod(method));
97 }
98 }