1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package org.codehaus.groovy.bsf;
47
48 import java.io.File;
49 import java.util.List;
50 import java.util.Vector;
51
52 import junit.framework.TestCase;
53
54 import org.apache.bsf.BSFManager;
55 import org.apache.bsf.BSFException;
56 import org.apache.bsf.BSFEngine;
57 import org.codehaus.groovy.runtime.DefaultGroovyMethods;
58
59 /***
60 * Tests the BSF integration
61 *
62 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
63 * @author Paul King
64 * @version $Revision: 4254 $
65 */
66 public class BSFTest extends TestCase {
67
68 protected BSFManager manager;
69
70 protected void setUp() throws Exception {
71 manager = new BSFManager();
72 }
73
74 public void testInvalidName() throws Exception {
75 manager.exec("groovy", null, 0, 0, "assert bsf != null");
76 manager.exec("groovy", "", 0, 0, "assert bsf != null");
77 manager.exec("groovy", "-", 0, 0, "assert bsf != null");
78 }
79
80 public void testCompileErrorWithExec() throws Exception {
81 try {
82 manager.exec("groovy", "dummy", 0, 0, "assert assert");
83 fail("Should have caught compile exception");
84 } catch (BSFException e) {
85 assertTrue("e.getMessage() should contain CompilationError: " + e.getMessage(),
86 e.getMessage().indexOf("CompilationError") != -1);
87 }
88 }
89
90 public void testCompileErrorWithEval() throws Exception {
91 try {
92 manager.eval("groovy", "dummy", 0, 0, "assert assert");
93 fail("Should have caught compile exception");
94 } catch (BSFException e) {
95 assertTrue("e.getMessage() should contain CompilationError: " + e.getMessage(),
96 e.getMessage().indexOf("CompilationError") != -1);
97 }
98 }
99
100 public void testExec() throws Exception {
101 manager.exec("groovy", "Test1.groovy", 0, 0, "assert bsf != null , 'should have a bsf variable'");
102 }
103
104 public void testApplyWithClosure() throws Exception {
105 Vector ignoreParamNames = null;
106 Vector ignoreArgs = null;
107 Integer actual = (Integer) manager.apply("groovy", "applyTest", 0, 0,
108 "251", ignoreParamNames, ignoreArgs);
109 assertEquals(251, actual.intValue());
110 }
111
112 public void testApply() throws Exception {
113 Vector ignoreParamNames = null;
114 Vector args = new Vector();
115 args.add(new Integer(2));
116 args.add(new Integer(5));
117 args.add(new Integer(1));
118 Integer actual = (Integer) manager.apply("groovy", "applyTest", 0, 0,
119 "def summer = { a, b, c -> a * 100 + b * 10 + c }", ignoreParamNames, args);
120 assertEquals(251, actual.intValue());
121 }
122
123 public void testBracketName() throws Exception {
124 manager.exec("groovy", "Test1<groovy>", 0, 0, "assert bsf != null , 'should have a bsf variable'");
125 }
126
127 public void testEval() throws Exception {
128 Object answer = manager.eval("groovy", "Test1.groovy", 0, 0, "return [1, 2, 3]");
129 assertTrue("Should return a list: " + answer, answer instanceof List);
130 List list = (List) answer;
131 assertEquals("List should be of right size", 3, list.size());
132 }
133
134 public void testTwoEvalsWithSameName() throws Exception {
135 Object answer = manager.eval("groovy", "Test1.groovy", 0, 0, "return 'cheese'");
136 assertEquals("cheese", answer);
137 answer = manager.eval("groovy", "Test1.groovy", 0, 0, "return 'gromit'");
138 assertEquals("gromit", answer);
139 }
140
141 public void testExecBug() throws Exception {
142 for (int i = 0; i < 10; i++) {
143 manager.exec("groovy", "Test1.groovy", 0, 0, "assert true");
144 manager.exec("groovy", "Test1.groovy", 0, 0, "assert true");
145 }
146 }
147
148 public void testBsfVariables() throws Exception {
149 Object answer = manager.eval("groovy", "Test1.groovy", 0, 0,
150 "assert this.bsf != null\n return this.bsf");
151 assertTrue("Should have an answer", answer != null);
152 }
153
154 public void testNotFoundVariables() throws Exception {
155 manager.registerBean("x", new Integer(4));
156 Object answer = manager.eval("groovy", "Test1.groovy", 0, 0,
157 "def valueOfX = this.bsf.lookupBean('y'); assert valueOfX == null");
158 assertNull("Undeclared beans should yield null", answer);
159 }
160
161 public void testRegisteredVariables() throws Exception {
162 manager.registerBean("x", new Integer(4));
163 Object answer = manager.eval("groovy", "Test1.groovy", 0, 0,
164 "def valueOfX = this.bsf.lookupBean('x'); assert valueOfX == 4; valueOfX + 1");
165 assertEquals("Incorrect return", new Integer(5), answer);
166 }
167
168 public void testUnregisteredVariables() throws Exception {
169 manager.registerBean("x", new Integer(4));
170 Object answer = manager.eval("groovy", "Test1.groovy", 0, 0,
171 "def valueOfX = this.bsf.lookupBean('x'); assert valueOfX == 4; valueOfX + 1");
172 assertEquals("Incorrect return", new Integer(5), answer);
173 manager.unregisterBean("x");
174
175 answer = manager.eval("groovy", "Test1.groovy", 0, 0,
176 "def valueOfX = this.bsf.lookupBean('x'); assert valueOfX == null");
177 assertNull("Unregistered beans should yield null", answer);
178 }
179
180 public void testDeclaredVariables() throws Exception {
181 manager.declareBean("xyz", new Integer(4), Integer.class);
182 Object answer = manager.eval("groovy", "Test1.groovy", 0, 0, "xyz + 1");
183 assertEquals("Incorrect return", new Integer(5), answer);
184 }
185
186 public void testUndeclaredVariables() throws Exception {
187 manager.declareBean("abc", new Integer(4), Integer.class);
188
189 Object answer = manager.eval("groovy", "Test1.groovy", 0, 0, "abc + 1");
190 assertEquals("Incorrect return", new Integer(5), answer);
191 manager.undeclareBean("abc");
192 answer = manager.eval("groovy", "Test1.groovy", 0, 0, "abc");
193 assertNull("Undeclared beans should yield null", answer);
194 }
195
196 public void testCall() throws Exception {
197 BSFEngine bsfEngine = manager.loadScriptingEngine("groovy");
198 manager.declareBean("myvar", "hello", String.class);
199 Object myvar = manager.lookupBean("myvar");
200 String result = (String) bsfEngine.call(myvar, "reverse", new Object[]{});
201 assertEquals("olleh", result);
202 }
203
204 public void testExecFile() throws Exception {
205 execScript("src/test/groovy/script/MapFromList.groovy");
206 execScript("src/test/groovy/script/AtomTestScript.groovy");
207 }
208
209 protected void execScript(String fileName) throws Exception {
210 manager.exec("groovy", fileName, 0, 0, DefaultGroovyMethods.getText(new File(fileName)));
211 }
212 }