001: /*
002: $Id: MethodTest.java 3279 2005-12-15 18:39:38Z 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.ClassHelper;
050: import org.codehaus.groovy.ast.ClassNode;
051: import org.codehaus.groovy.ast.ConstructorNode;
052: import org.codehaus.groovy.ast.MethodNode;
053: import org.codehaus.groovy.ast.Parameter;
054: import org.codehaus.groovy.ast.expr.ConstantExpression;
055: import org.codehaus.groovy.ast.stmt.BlockStatement;
056: import org.codehaus.groovy.ast.stmt.ReturnStatement;
057: import org.codehaus.groovy.ast.stmt.Statement;
058: import org.codehaus.groovy.runtime.InvokerHelper;
059:
060: /**
061: *
062: * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
063: * @version $Revision: 3279 $
064: */
065: public class MethodTest extends TestSupport {
066:
067: public void testMethods() throws Exception {
068: ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC,
069: ClassHelper.OBJECT_TYPE);
070: classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));
071:
072: Statement statementA = new ReturnStatement(
073: new ConstantExpression("calledA"));
074: Statement statementB = new ReturnStatement(
075: new ConstantExpression("calledB"));
076: Statement emptyStatement = new BlockStatement();
077:
078: classNode.addMethod(new MethodNode("a", ACC_PUBLIC,
079: ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY,
080: ClassNode.EMPTY_ARRAY, statementA));
081: classNode.addMethod(new MethodNode("b", ACC_PUBLIC, null,
082: Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY,
083: statementB));
084:
085: classNode.addMethod(new MethodNode("noReturnMethodA",
086: ACC_PUBLIC, null, Parameter.EMPTY_ARRAY,
087: ClassNode.EMPTY_ARRAY, emptyStatement));
088: classNode.addMethod(new MethodNode("noReturnMethodB",
089: ACC_PUBLIC, ClassHelper.OBJECT_TYPE,
090: Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY,
091: emptyStatement));
092:
093: classNode.addMethod(new MethodNode("c", ACC_PUBLIC,
094: ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY,
095: ClassNode.EMPTY_ARRAY, emptyStatement));
096:
097: Class fooClass = loadClass(classNode);
098: assertTrue("Loaded a new class", fooClass != null);
099:
100: Object bean = fooClass.newInstance();
101: assertTrue("Created instance of class: " + bean, bean != null);
102:
103: assertCallMethod(bean, "a", "calledA");
104: assertCallMethod(bean, "b", "calledB");
105: assertCallMethod(bean, "noReturnMethodA", null);
106: assertCallMethod(bean, "noReturnMethodB", null);
107: assertCallMethod(bean, "c", null);
108: }
109:
110: protected void assertCallMethod(Object object, String method,
111: Object expected) {
112: Object value = InvokerHelper.invokeMethod(object, method,
113: new Object[0]);
114: assertEquals("Result of calling method: " + method + " on: "
115: + object + " with empty list", expected, value);
116:
117: value = InvokerHelper.invokeMethod(object, method, null);
118: assertEquals("Result of calling method: " + method + " on: "
119: + object + " with null", expected, value);
120: }
121: }
|