    1   /*
    2    $Id: TupleListTest.java,v 1.5 2004/04/01 06:21:56 cpoirier 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
    8    that the following conditions are met:
    9
    10   1. Redistributions of source code must retain copyright
    11      statements and notices.  Redistributions must also contain a
    12      copy of this document.
    13
    14   2. Redistributions in binary form must reproduce the
    15      above copyright notice, this list of conditions and the
    16      following disclaimer in the documentation and/or other
    17      materials provided with the distribution.
    18
    19   3. The name "groovy" must not be used to endorse or promote
    20      products derived from this Software without prior written
    21      permission of The Codehaus.  For written permission,
    22      please contact info@codehaus.org.
    23
    24   4. Products derived from this Software may not be called "groovy"
    25      nor may "groovy" appear in their names without prior written
    26      permission of The Codehaus. "groovy" is a registered
    27      trademark of The Codehaus.
    28
    29   5. Due credit should be given to The Codehaus -
    30      http://groovy.codehaus.org/
    31
    32   THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
    33   ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
    34   NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
    35   FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
    36   THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
    37   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    38   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    39   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    40   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    41   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    42   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    43   OF THE POSSIBILITY OF SUCH DAMAGE.
    44
    45   */
    46
    47  package org.codehaus.groovy.classgen;
    48
    49  import org.codehaus.groovy.ast.ClassNode;
    50  import org.codehaus.groovy.ast.ConstructorNode;
    51  import org.codehaus.groovy.ast.MethodNode;
    52  import org.codehaus.groovy.ast.Parameter;
    53  import org.codehaus.groovy.ast.PropertyNode;
    54  import org.codehaus.groovy.ast.Type;
    55  import org.codehaus.groovy.ast.expr.BinaryExpression;
    56  import org.codehaus.groovy.ast.expr.ConstantExpression;
    57  import org.codehaus.groovy.ast.expr.Expression;
    58  import org.codehaus.groovy.ast.expr.ListExpression;
    59  import org.codehaus.groovy.ast.expr.MapExpression;
    60  import org.codehaus.groovy.ast.expr.TupleExpression;
    61  import org.codehaus.groovy.ast.expr.VariableExpression;
    62  import org.codehaus.groovy.ast.stmt.ExpressionStatement;
    63  import org.codehaus.groovy.ast.stmt.ForStatement;
    64  import org.codehaus.groovy.ast.stmt.Statement;
    65  import org.codehaus.groovy.ast.stmt.BlockStatement;
    66  import org.codehaus.groovy.runtime.InvokerHelper;
    67  import org.codehaus.groovy.runtime.InvokerInvocationException;
    68  import org.codehaus.groovy.syntax.Token;
    69
    70  /***
    71   *
    72   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
    73   * @version $Revision: 1.5 $
    74   */
    75  public class TupleListTest extends TestSupport {
    76
    77      public void testIterateOverTuple() throws Exception {
    78          TupleExpression listExpression = new TupleExpression();
    79          listExpression.addExpression(new ConstantExpression("a"));
    80          listExpression.addExpression(new ConstantExpression("b"));
    81          listExpression.addExpression(new ConstantExpression("c"));
    82          assertIterate("iterateOverTuple", listExpression);
    83      }
    84
    85      public void testIterateOverList() throws Exception {
    86          ListExpression listExpression = new ListExpression();
    87          listExpression.addExpression(new ConstantExpression("a"));
    88          listExpression.addExpression(new ConstantExpression("b"));
    89          listExpression.addExpression(new ConstantExpression("c"));
    90          listExpression.addExpression(new ConstantExpression("a"));
    91          listExpression.addExpression(new ConstantExpression("b"));
    92          listExpression.addExpression(new ConstantExpression("c"));
    93          assertIterate("iterateOverList", listExpression);
    94      }
    95
    96      public void testIterateOverMap() throws Exception {
    97          MapExpression mapExpression = new MapExpression();
    98          mapExpression.addMapEntryExpression(new ConstantExpression("a"), new ConstantExpression("x"));
    99          mapExpression.addMapEntryExpression(new ConstantExpression("b"), new ConstantExpression("y"));
    100         mapExpression.addMapEntryExpression(new ConstantExpression("c"), new ConstantExpression("z"));
    101         assertIterate("iterateOverMap", mapExpression);
    102     }
    103
    104     protected void assertIterate(String methodName, Expression listExpression) throws Exception {
    105         ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, "java.lang.Object");
    106         classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));
    107         classNode.addProperty(new PropertyNode("bar", ACC_PUBLIC, "java.lang.String", "Foo", null, null, null));
    108
    109         Statement loopStatement = createPrintlnStatement(new VariableExpression("i"));
    110
    111         BlockStatement block = new BlockStatement();
    112         block.addStatement(new ExpressionStatement(new BinaryExpression(new VariableExpression("list"), Token.newSymbol("=", 0, 0), listExpression)));
    113         block.addStatement(new ForStatement("i", Type.DYNAMIC_TYPE, new VariableExpression("list"), loopStatement));
    114         classNode.addMethod(new MethodNode(methodName, ACC_PUBLIC, "void", Parameter.EMPTY_ARRAY, block));
    115
    116         Class fooClass = loadClass(classNode);
    117         assertTrue("Loaded a new class", fooClass != null);
    118
    119         Object bean = fooClass.newInstance();
    120         assertTrue("Managed to create bean", bean != null);
    121
    122         System.out.println("################ Now about to invoke method");
    123
    124         try {
    125             InvokerHelper.invokeMethod(bean, methodName, null);
    126         }
    127         catch (InvokerInvocationException e) {
    128             System.out.println("Caught: " + e.getCause());
    129             e.getCause().printStackTrace();
    130             fail("Should not have thrown an exception");
    131         }
    132         System.out.println("################ Done");    }
    133
    134 }