1   /*
2    * $Id: MetaClassTest.java,v 1.6 2005/06/23 18:36:28 dierk Exp $
3    * 
4    * Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
5    * 
6    * Redistribution and use of this software and associated documentation
7    * ("Software"), with or without modification, are permitted provided that the
8    * following conditions are met: 1. Redistributions of source code must retain
9    * copyright statements and notices. Redistributions must also contain a copy
10   * of this document. 2. Redistributions in binary form must reproduce the above
11   * copyright notice, this list of conditions and the following disclaimer in
12   * the documentation and/or other materials provided with the distribution. 3.
13   * The name "groovy" must not be used to endorse or promote products derived
14   * from this Software without prior written permission of The Codehaus. For
15   * written permission, please contact info@codehaus.org. 4. Products derived
16   * from this Software may not be called "groovy" nor may "groovy" appear in
17   * their names without prior written permission of The Codehaus. "groovy" is a
18   * registered trademark of The Codehaus. 5. Due credit should be given to The
19   * Codehaus - http://groovy.codehaus.org/
20   * 
21   * THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS ``AS IS'' AND ANY
22   * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24   * DISCLAIMED. IN NO EVENT SHALL THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR
25   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29   * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31   * DAMAGE.
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         // test int[]
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         // test Integer[]
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         // test list
155         ArrayList list = new ArrayList();
156         list.add(new Integer(120));
157         list.add(new Integer(150));
158 
159         // test int[]
160         metaClass.setProperty(dymmyClass, "ints", list);
161 
162         // test Integer[]
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