001: /*
002: $Id: ForTest.java 3419 2006-01-19 00:07:02Z blackdrag $
003:
004: Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
005:
006: Redistribution and use of this software and associated documentation
007: ("Software"), with or without modification, are permitted provided
008: that the following conditions are met:
009:
010: 1. Redistributions of source code must retain copyright
011: statements and notices. Redistributions must also contain a
012: copy of this document.
013:
014: 2. Redistributions in binary form must reproduce the
015: above copyright notice, this list of conditions and the
016: following disclaimer in the documentation and/or other
017: materials provided with the distribution.
018:
019: 3. The name "groovy" must not be used to endorse or promote
020: products derived from this Software without prior written
021: permission of The Codehaus. For written permission,
022: please contact info@codehaus.org.
023:
024: 4. Products derived from this Software may not be called "groovy"
025: nor may "groovy" appear in their names without prior written
026: permission of The Codehaus. "groovy" is a registered
027: trademark of The Codehaus.
028:
029: 5. Due credit should be given to The Codehaus -
030: http://groovy.codehaus.org/
031:
032: THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
033: ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
034: NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
035: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
036: THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
037: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
038: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
039: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
040: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
041: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
042: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
043: OF THE POSSIBILITY OF SUCH DAMAGE.
044:
045: */
046:
047: package org.codehaus.groovy.classgen;
048:
049: import org.codehaus.groovy.ast.*;
050: import org.codehaus.groovy.ast.expr.VariableExpression;
051: import org.codehaus.groovy.ast.stmt.ForStatement;
052: import org.codehaus.groovy.ast.stmt.BlockStatement;
053: import org.codehaus.groovy.ast.stmt.Statement;
054: import org.codehaus.groovy.runtime.InvokerHelper;
055: import org.codehaus.groovy.runtime.InvokerInvocationException;
056:
057: /**
058: * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
059: * @author Pilho Kim
060: * @version $Revision: 3419 $
061: */
062: public class ForTest extends TestSupport {
063:
064: public void testNonLoop() throws Exception {
065: ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC,
066: ClassHelper.OBJECT_TYPE);
067: classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));
068:
069: Parameter[] parameters = { new Parameter(
070: ClassHelper.OBJECT_TYPE, "coll") };
071:
072: Statement statement = createPrintlnStatement(new VariableExpression(
073: "coll"));
074: classNode.addMethod(new MethodNode("oneParamDemo", ACC_PUBLIC,
075: ClassHelper.VOID_TYPE, parameters,
076: ClassNode.EMPTY_ARRAY, statement));
077:
078: Class fooClass = loadClass(classNode);
079: assertTrue("Loaded a new class", fooClass != null);
080:
081: Object bean = fooClass.newInstance();
082: assertTrue("Managed to create bean", bean != null);
083:
084: System.out
085: .println("################ Now about to invoke a method without looping");
086: Object value = new Integer(10000);
087:
088: try {
089: InvokerHelper.invokeMethod(bean, "oneParamDemo",
090: new Object[] { value });
091: } catch (InvokerInvocationException e) {
092: System.out.println("Caught: " + e.getCause());
093: e.getCause().printStackTrace();
094: fail("Should not have thrown an exception");
095: }
096: System.out.println("################ Done");
097: }
098:
099: public void testLoop() throws Exception {
100: ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC,
101: ClassHelper.OBJECT_TYPE);
102: classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));
103:
104: Parameter[] parameters = { new Parameter(
105: ClassHelper.OBJECT_TYPE.makeArray(), "coll") };
106:
107: Statement loopStatement = createPrintlnStatement(new VariableExpression(
108: "i"));
109:
110: ForStatement statement = new ForStatement(new Parameter(
111: ClassHelper.OBJECT_TYPE, "i"), new VariableExpression(
112: "coll"), loopStatement);
113: classNode.addMethod(new MethodNode("iterateDemo", ACC_PUBLIC,
114: ClassHelper.VOID_TYPE, parameters,
115: ClassNode.EMPTY_ARRAY, statement));
116:
117: Class fooClass = loadClass(classNode);
118: assertTrue("Loaded a new class", fooClass != null);
119:
120: Object bean = fooClass.newInstance();
121: assertTrue("Managed to create bean", bean != null);
122:
123: System.out
124: .println("################ Now about to invoke a method with looping");
125: Object[] array = { new Integer(1234), "abc", "def" };
126:
127: try {
128: InvokerHelper.invokeMethod(bean, "iterateDemo",
129: new Object[] { array });
130: } catch (InvokerInvocationException e) {
131: System.out.println("Caught: " + e.getCause());
132: e.getCause().printStackTrace();
133: fail("Should not have thrown an exception");
134: }
135: System.out.println("################ Done");
136: }
137:
138: public void testManyParam() throws Exception {
139: ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC,
140: ClassHelper.OBJECT_TYPE);
141: classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));
142:
143: Parameter[] parameters = {
144: new Parameter(ClassHelper.OBJECT_TYPE, "coll1"),
145: new Parameter(ClassHelper.OBJECT_TYPE, "coll2"),
146: new Parameter(ClassHelper.OBJECT_TYPE, "coll3") };
147:
148: BlockStatement statement = new BlockStatement();
149: statement
150: .addStatement(createPrintlnStatement(new VariableExpression(
151: "coll1")));
152: statement
153: .addStatement(createPrintlnStatement(new VariableExpression(
154: "coll2")));
155: statement
156: .addStatement(createPrintlnStatement(new VariableExpression(
157: "coll3")));
158:
159: classNode.addMethod(new MethodNode("manyParamDemo", ACC_PUBLIC,
160: ClassHelper.VOID_TYPE, parameters,
161: ClassNode.EMPTY_ARRAY, statement));
162:
163: Class fooClass = loadClass(classNode);
164: assertTrue("Loaded a new class", fooClass != null);
165:
166: Object bean = fooClass.newInstance();
167: assertTrue("Managed to create bean", bean != null);
168:
169: System.out
170: .println("################ Now about to invoke a method with many parameters");
171: Object[] array = { new Integer(1000 * 1000), "foo-", "bar~" };
172:
173: try {
174: InvokerHelper.invokeMethod(bean, "manyParamDemo", array);
175: } catch (InvokerInvocationException e) {
176: System.out.println("Caught: " + e.getCause());
177: e.getCause().printStackTrace();
178: fail("Should not have thrown an exception");
179: }
180: System.out.println("################ Done");
181: }
182: }
|