    1   /*
    2    * $Id: MetaClassTest.java,v 1.4 2004/03/26 16:55:52 ckl 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
    41  import org.codehaus.groovy.runtime.InvokerHelper;
    42
    43  /***
    44   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
    45   * @version $Revision: 1.4 $
    46   */
    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         // test int[]
    134         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         // test Integer[]
    141         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         // test list
    153         ArrayList list = new ArrayList();
    154         list.add(new Integer(120));
    155         list.add(new Integer(150));
    156
    157         // test int[]
    158         metaClass.setProperty(dymmyClass, "ints", list);
    159
    160         // test Integer[]
    161         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