1 34 35 package groovy.lang; 36 37 import groovy.util.GroovyTestCase; 38 39 import java.util.ArrayList ; 40 41 import org.codehaus.groovy.runtime.InvokerHelper; 42 43 47 public class MetaClassTest extends GroovyTestCase { 48 49 public void testMetaClass() { 50 Class foo = String [].class; 51 System.out.println(foo + " name: " + foo.getName()); 52 53 MetaClass metaClass = InvokerHelper.getMetaClass(this); 54 55 assertTrue("got metaclass", metaClass != null); 56 57 metaClass.invokeMethod(this, "doSomething", new Object [0]); 58 } 59 60 public void testArray() { 61 String [] value = new String [] { "hello" }; 62 63 MetaClass metaClass = InvokerHelper.getMetaClass(value); 64 65 assertTrue("got metaclass", metaClass != null); 66 67 metaClass.invokeMethod(value, "toString", new Object [0]); 68 } 69 70 public void testString() { 71 String value = "hello"; 72 73 MetaClass metaClass = InvokerHelper.getMetaClass(value); 74 75 assertTrue("got metaclass", metaClass != null); 76 77 Object answer = metaClass.invokeMethod(value, "toString", new Object [0]); 78 79 assertEquals("hello", answer); 80 } 81 82 public void testObject() { 83 Object value = new Object (); 84 85 MetaClass metaClass = InvokerHelper.getMetaClass(value); 86 87 assertTrue("got metaclass", metaClass != null); 88 89 metaClass.invokeMethod(value, "toString", new Object [0]); 90 } 91 92 public void testPublicField() { 93 DymmyClass dymmyClass = new DymmyClass(); 94 95 MetaClass metaClass = InvokerHelper.getMetaClass(dymmyClass); 96 97 assertEquals(metaClass.getProperty(dymmyClass, "x"), new Integer (0)); 98 assertEquals(metaClass.getProperty(dymmyClass, "y"), "none"); 99 100 metaClass.setProperty(dymmyClass, "x", new Integer (25)); 101 assertEquals(dymmyClass.x, 25); 102 103 metaClass.setProperty(dymmyClass, "y", "newvalue"); 104 assertEquals(dymmyClass.y, "newvalue"); 105 } 106 107 public void testSetPropertyWithInt() { 108 DymmyClass dymmyClass = new DymmyClass(); 109 MetaClass metaClass = InvokerHelper.getMetaClass(dymmyClass); 110 metaClass.setProperty(dymmyClass, "anInt", new Integer (10)); 111 } 112 113 public void testSetPropertyWithDoubleArray() { 114 DymmyClass dymmyClass = new DymmyClass(); 115 MetaClass metaClass = InvokerHelper.getMetaClass(dymmyClass); 116 Double [][] matrix2 = 117 { 118 { 119 new Double (35), new Double (50), new Double (120) 120 }, 121 { 122 new Double (75), new Double (80), new Double (150) 123 } 124 }; 125 metaClass.setProperty(dymmyClass, "matrix", matrix2); 126 metaClass.setProperty(dymmyClass, "matrix2", matrix2); 127 } 128 129 public void testSetPropertyWithArray() { 130 DymmyClass dymmyClass = new DymmyClass(); 131 MetaClass metaClass = InvokerHelper.getMetaClass(dymmyClass); 132 133 int[] ints = new int[]{ 135 0, 1, 2, 3 136 }; 137 metaClass.setProperty(dymmyClass, "ints", ints); 138 assertEquals(ints, metaClass.getProperty(dymmyClass, "ints")); 139 140 Integer [] integers = new Integer []{ 142 new Integer (0), new Integer (1), new Integer (2), new Integer (3) 143 }; 144 metaClass.setProperty(dymmyClass, "integers", integers); 145 assertEquals(integers, metaClass.getProperty(dymmyClass, "integers")); 146 } 147 148 public void testSetPropertyWithList() { 149 DymmyClass dymmyClass = new DymmyClass(); 150 MetaClass metaClass = InvokerHelper.getMetaClass(dymmyClass); 151 152 ArrayList list = new ArrayList (); 154 list.add(new Integer (120)); 155 list.add(new Integer (150)); 156 157 metaClass.setProperty(dymmyClass, "ints", list); 159 160 metaClass.setProperty(dymmyClass, "integers", list); 162 } 163 164 public void doSomething() { 165 System.out.println("Called doSomething()"); 166 } 167 } 168 169 170 class DymmyClass { 171 public int x = 0; 172 public String y = "none"; 173 174 private int anInt; 175 private int[] ints; 176 private Integer [] integers; 177 double[][] matrix2; 178 Double [][] matrix; 179 180 public Integer [] getIntegers() { 181 return integers; 182 } 183 184 public void setIntegers(Integer [] integers) { 185 this.integers = integers; 186 } 187 188 public int[] getInts() { 189 return ints; 190 } 191 192 public void setInts(int[] ints) { 193 this.ints = ints; 194 } 195 196 public int getAnInt() { 197 return anInt; 198 } 199 200 public void setAnInt(int anInt) { 201 this.anInt = anInt; 202 } 203 204 public void setMatrix(Double [][] matrix) { 205 this.matrix = matrix; 206 } 207 208 public void setMatrix2(double[][] matrixReloaded) { 209 this.matrix2 = matrixReloaded; 210 } 211 212 } 213 214 | Popular Tags |