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 package groovy.lang;
36
37 import groovy.util.GroovyTestCase;
38
39 import java.util.ArrayList;
40 import java.util.List;
41 import java.util.Iterator;
42
43 import org.codehaus.groovy.runtime.InvokerHelper;
44
45 /***
46 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
47 * @version $Revision: 1.6 $
48 */
49 public class MetaClassTest extends GroovyTestCase {
50
51 public void testMetaClass() {
52 Class foo = String[].class;
53 System.out.println(foo + " name: " + foo.getName());
54
55 MetaClass metaClass = InvokerHelper.getMetaClass(this);
56
57 assertTrue("got metaclass", metaClass != null);
58
59 metaClass.invokeMethod(this, "doSomething", new Object[0]);
60 }
61
62 public void testArray() {
63 String[] value = new String[] { "hello" };
64
65 MetaClass metaClass = InvokerHelper.getMetaClass(value);
66
67 assertTrue("got metaclass", metaClass != null);
68
69 metaClass.invokeMethod(value, "toString", new Object[0]);
70 }
71
72 public void testString() {
73 String value = "hello";
74
75 MetaClass metaClass = InvokerHelper.getMetaClass(value);
76
77 assertTrue("got metaclass", metaClass != null);
78
79 Object answer = metaClass.invokeMethod(value, "toString", new Object[0]);
80
81 assertEquals("hello", answer);
82 }
83
84 public void testObject() {
85 Object value = new Object();
86
87 MetaClass metaClass = InvokerHelper.getMetaClass(value);
88
89 assertTrue("got metaclass", metaClass != null);
90
91 metaClass.invokeMethod(value, "toString", new Object[0]);
92 }
93
94 public void testPublicField() {
95 DymmyClass dymmyClass = new DymmyClass();
96
97 MetaClass metaClass = InvokerHelper.getMetaClass(dymmyClass);
98
99 assertEquals(metaClass.getProperty(dymmyClass, "x"), new Integer(0));
100 assertEquals(metaClass.getProperty(dymmyClass, "y"), "none");
101
102 metaClass.setProperty(dymmyClass, "x", new Integer(25));
103 assertEquals(dymmyClass.x, 25);
104
105 metaClass.setProperty(dymmyClass, "y", "newvalue");
106 assertEquals(dymmyClass.y, "newvalue");
107 }
108
109 public void testSetPropertyWithInt() {
110 DymmyClass dymmyClass = new DymmyClass();
111 MetaClass metaClass = InvokerHelper.getMetaClass(dymmyClass);
112 metaClass.setProperty(dymmyClass, "anInt", new Integer(10));
113 }
114
115 public void testSetPropertyWithDoubleArray() {
116 DymmyClass dymmyClass = new DymmyClass();
117 MetaClass metaClass = InvokerHelper.getMetaClass(dymmyClass);
118 Double[][] matrix2 =
119 {
120 {
121 new Double(35), new Double(50), new Double(120)
122 },
123 {
124 new Double(75), new Double(80), new Double(150)
125 }
126 };
127 metaClass.setProperty(dymmyClass, "matrix", matrix2);
128 metaClass.setProperty(dymmyClass, "matrix2", matrix2);
129 }
130
131 public void testSetPropertyWithArray() {
132 DymmyClass dymmyClass = new DymmyClass();
133 MetaClass metaClass = InvokerHelper.getMetaClass(dymmyClass);
134
135
136 int[] ints = new int[]{
137 0, 1, 2, 3
138 };
139 metaClass.setProperty(dymmyClass, "ints", ints);
140 assertEquals(ints, metaClass.getProperty(dymmyClass, "ints"));
141
142
143 Integer[] integers = new Integer[]{
144 new Integer(0), new Integer(1), new Integer(2), new Integer(3)
145 };
146 metaClass.setProperty(dymmyClass, "integers", integers);
147 assertEquals(integers, metaClass.getProperty(dymmyClass, "integers"));
148 }
149
150 public void testSetPropertyWithList() {
151 DymmyClass dymmyClass = new DymmyClass();
152 MetaClass metaClass = InvokerHelper.getMetaClass(dymmyClass);
153
154
155 ArrayList list = new ArrayList();
156 list.add(new Integer(120));
157 list.add(new Integer(150));
158
159
160 metaClass.setProperty(dymmyClass, "ints", list);
161
162
163 metaClass.setProperty(dymmyClass, "integers", list);
164 }
165
166 public void testMetaMethodsOnlyAddedOnce() {
167 MetaClass metaClass = InvokerHelper.getMetaClass("some String");
168
169 List methods = metaClass.getMetaMethods();
170 for (Iterator iter = methods.iterator(); iter.hasNext();) {
171 MetaMethod method = (MetaMethod) iter.next();
172 int count = 0;
173 for (Iterator inner = methods.iterator(); inner.hasNext(); ) {
174 MetaMethod runner = (MetaMethod) inner.next();
175 if (method.equals(runner)) {
176 System.out.println("runner = " + runner);
177 System.out.println("method = " + method);
178 count++;
179 }
180 }
181 assertEquals("count of Method "+method.getName(), 1, count);
182 }
183
184 }
185
186
187
188 public void doSomething() {
189 System.out.println("Called doSomething()");
190 }
191 }
192
193
194 class DymmyClass {
195 public int x = 0;
196 public String y = "none";
197
198 private int anInt;
199 private int[] ints;
200 private Integer[] integers;
201 double[][] matrix2;
202 Double[][] matrix;
203
204 public Integer[] getIntegers() {
205 return integers;
206 }
207
208 public void setIntegers(Integer[] integers) {
209 this.integers = integers;
210 }
211
212 public int[] getInts() {
213 return ints;
214 }
215
216 public void setInts(int[] ints) {
217 this.ints = ints;
218 }
219
220 public int getAnInt() {
221 return anInt;
222 }
223
224 public void setAnInt(int anInt) {
225 this.anInt = anInt;
226 }
227
228 public void setMatrix(Double[][] matrix) {
229 this.matrix = matrix;
230 }
231
232 public void setMatrix2(double[][] matrixReloaded) {
233 this.matrix2 = matrixReloaded;
234 }
235
236 }
237