001: /*
002: $Id: BSFTest.java 4254 2006-11-23 20:38:19Z 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: package org.codehaus.groovy.bsf;
047:
048: import java.io.File;
049: import java.util.List;
050: import java.util.Vector;
051:
052: import junit.framework.TestCase;
053:
054: import org.apache.bsf.BSFManager;
055: import org.apache.bsf.BSFException;
056: import org.apache.bsf.BSFEngine;
057: import org.codehaus.groovy.runtime.DefaultGroovyMethods;
058:
059: /**
060: * Tests the BSF integration
061: *
062: * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
063: * @author Paul King
064: * @version $Revision: 4254 $
065: */
066: public class BSFTest extends TestCase {
067:
068: protected BSFManager manager;
069:
070: protected void setUp() throws Exception {
071: manager = new BSFManager();
072: }
073:
074: public void testInvalidName() throws Exception {
075: manager.exec("groovy", null, 0, 0, "assert bsf != null");
076: manager.exec("groovy", "", 0, 0, "assert bsf != null");
077: manager.exec("groovy", "-", 0, 0, "assert bsf != null");
078: }
079:
080: public void testCompileErrorWithExec() throws Exception {
081: try {
082: manager.exec("groovy", "dummy", 0, 0, "assert assert");
083: fail("Should have caught compile exception");
084: } catch (BSFException e) {
085: assertTrue(
086: "e.getMessage() should contain CompilationError: "
087: + e.getMessage(), e.getMessage().indexOf(
088: "CompilationError") != -1);
089: }
090: }
091:
092: public void testCompileErrorWithEval() throws Exception {
093: try {
094: manager.eval("groovy", "dummy", 0, 0, "assert assert");
095: fail("Should have caught compile exception");
096: } catch (BSFException e) {
097: assertTrue(
098: "e.getMessage() should contain CompilationError: "
099: + e.getMessage(), e.getMessage().indexOf(
100: "CompilationError") != -1);
101: }
102: }
103:
104: public void testExec() throws Exception {
105: manager.exec("groovy", "Test1.groovy", 0, 0,
106: "assert bsf != null , 'should have a bsf variable'");
107: }
108:
109: public void testApplyWithClosure() throws Exception {
110: Vector ignoreParamNames = null;
111: Vector ignoreArgs = null;
112: Integer actual = (Integer) manager.apply("groovy", "applyTest",
113: 0, 0, "251", ignoreParamNames, ignoreArgs);
114: assertEquals(251, actual.intValue());
115: }
116:
117: public void testApply() throws Exception {
118: Vector ignoreParamNames = null;
119: Vector args = new Vector();
120: args.add(new Integer(2));
121: args.add(new Integer(5));
122: args.add(new Integer(1));
123: Integer actual = (Integer) manager.apply("groovy", "applyTest",
124: 0, 0,
125: "def summer = { a, b, c -> a * 100 + b * 10 + c }",
126: ignoreParamNames, args);
127: assertEquals(251, actual.intValue());
128: }
129:
130: public void testBracketName() throws Exception {
131: manager.exec("groovy", "Test1<groovy>", 0, 0,
132: "assert bsf != null , 'should have a bsf variable'");
133: }
134:
135: public void testEval() throws Exception {
136: Object answer = manager.eval("groovy", "Test1.groovy", 0, 0,
137: "return [1, 2, 3]");
138: assertTrue("Should return a list: " + answer,
139: answer instanceof List);
140: List list = (List) answer;
141: assertEquals("List should be of right size", 3, list.size());
142: }
143:
144: public void testTwoEvalsWithSameName() throws Exception {
145: Object answer = manager.eval("groovy", "Test1.groovy", 0, 0,
146: "return 'cheese'");
147: assertEquals("cheese", answer);
148: answer = manager.eval("groovy", "Test1.groovy", 0, 0,
149: "return 'gromit'");
150: assertEquals("gromit", answer);
151: }
152:
153: public void testExecBug() throws Exception {
154: for (int i = 0; i < 10; i++) {
155: manager.exec("groovy", "Test1.groovy", 0, 0, "assert true");
156: manager.exec("groovy", "Test1.groovy", 0, 0, "assert true");
157: }
158: }
159:
160: public void testBsfVariables() throws Exception {
161: Object answer = manager.eval("groovy", "Test1.groovy", 0, 0,
162: "assert this.bsf != null\n return this.bsf");
163: assertTrue("Should have an answer", answer != null);
164: }
165:
166: public void testNotFoundVariables() throws Exception {
167: manager.registerBean("x", new Integer(4));
168: Object answer = manager
169: .eval("groovy", "Test1.groovy", 0, 0,
170: "def valueOfX = this.bsf.lookupBean('y'); assert valueOfX == null");
171: assertNull("Undeclared beans should yield null", answer);
172: }
173:
174: public void testRegisteredVariables() throws Exception {
175: manager.registerBean("x", new Integer(4));
176: Object answer = manager
177: .eval("groovy", "Test1.groovy", 0, 0,
178: "def valueOfX = this.bsf.lookupBean('x'); assert valueOfX == 4; valueOfX + 1");
179: assertEquals("Incorrect return", new Integer(5), answer);
180: }
181:
182: public void testUnregisteredVariables() throws Exception {
183: manager.registerBean("x", new Integer(4));
184: Object answer = manager
185: .eval("groovy", "Test1.groovy", 0, 0,
186: "def valueOfX = this.bsf.lookupBean('x'); assert valueOfX == 4; valueOfX + 1");
187: assertEquals("Incorrect return", new Integer(5), answer);
188: manager.unregisterBean("x");
189: // have to lookup registered beans
190: answer = manager
191: .eval("groovy", "Test1.groovy", 0, 0,
192: "def valueOfX = this.bsf.lookupBean('x'); assert valueOfX == null");
193: assertNull("Unregistered beans should yield null", answer);
194: }
195:
196: public void testDeclaredVariables() throws Exception {
197: manager.declareBean("xyz", new Integer(4), Integer.class);
198: Object answer = manager.eval("groovy", "Test1.groovy", 0, 0,
199: "xyz + 1");
200: assertEquals("Incorrect return", new Integer(5), answer);
201: }
202:
203: public void testUndeclaredVariables() throws Exception {
204: manager.declareBean("abc", new Integer(4), Integer.class);
205: // declared beans should just be available
206: Object answer = manager.eval("groovy", "Test1.groovy", 0, 0,
207: "abc + 1");
208: assertEquals("Incorrect return", new Integer(5), answer);
209: manager.undeclareBean("abc");
210: answer = manager.eval("groovy", "Test1.groovy", 0, 0, "abc");
211: assertNull("Undeclared beans should yield null", answer);
212: }
213:
214: public void testCall() throws Exception {
215: BSFEngine bsfEngine = manager.loadScriptingEngine("groovy");
216: manager.declareBean("myvar", "hello", String.class);
217: Object myvar = manager.lookupBean("myvar");
218: String result = (String) bsfEngine.call(myvar, "reverse",
219: new Object[] {});
220: assertEquals("olleh", result);
221: }
222:
223: public void testExecFile() throws Exception {
224: execScript("src/test/groovy/script/MapFromList.groovy");
225: execScript("src/test/groovy/script/AtomTestScript.groovy");
226: }
227:
228: protected void execScript(String fileName) throws Exception {
229: manager.exec("groovy", fileName, 0, 0, DefaultGroovyMethods
230: .getText(new File(fileName)));
231: }
232: }
|