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 package org.codehaus.groovy.bsf;
35
36 import java.util.List;
37 import java.util.Vector;
38
39 import junit.framework.TestCase;
40
41 import org.apache.bsf.BSFManager;
42 import org.apache.bsf.BSFEngine;
43 import org.apache.bsf.BSFException;
44 import groovy.lang.GroovyShell;
45
46 /***
47 * Tests the Caching BSF integration
48 *
49 * @author James Birchfield
50 * @version $Revision: 4166 $
51 */
52 public class CacheBSFTest extends TestCase {
53
54 protected BSFManager manager;
55 private static final Class CACHING_ENGINE = CachingGroovyEngine.class;
56
57 protected void setUp() throws Exception {
58
59 BSFManager.registerScriptingEngine("groovy", CACHING_ENGINE.getName(), new String[] { "groovy", "gy" });
60 manager = new BSFManager();
61 }
62
63 public void testVersion() throws Exception {
64
65 BSFEngine bsfEngine = manager.loadScriptingEngine("groovy");
66 assertEquals(CACHING_ENGINE, bsfEngine.getClass());
67 }
68
69 public void testExec() throws Exception {
70 manager.exec("groovy", "Test1.groovy", 0, 0, "println('testing Exec')");
71
72
73 manager.exec("groovy", "Test1.groovy", 0, 0, "println('testing Exec')");
74 }
75
76 public void testCompileErrorWithExec() throws Exception {
77 try {
78 manager.exec("groovy", "dummy", 0, 0, "assert assert");
79 fail("Should have caught compile exception");
80 } catch (BSFException e) {
81 assertTrue("e.getMessage() should contain CompilationError: " + e.getMessage(),
82 e.getMessage().indexOf("CompilationError") != -1);
83 }
84 }
85
86 public void testEval() throws Exception {
87 Object dontcare = manager.eval("groovy", "Test1.groovy", 0, 0, "return [1, 2, 3]");
88
89
90 Object answer = manager.eval("groovy", "Test.groovy", 0, 0, "return [1, 2, 3]");
91 assertTrue("Should return a list: " + answer, answer instanceof List);
92 List list = (List) answer;
93 assertEquals("List should be of right size", 3, list.size());
94 }
95
96 public void testCompileErrorWithEval() throws Exception {
97 try {
98 manager.eval("groovy", "dummy", 0, 0, "assert assert");
99 fail("Should have caught compile exception");
100 } catch (BSFException e) {
101 assertTrue("e.getMessage() should contain CompilationError: " + e.getMessage(),
102 e.getMessage().indexOf("CompilationError") != -1);
103 }
104 }
105
106 public void testBuiltInVariable() throws Exception {
107 Object answer = manager.eval("groovy", "Test1.groovy", 0, 0,
108 "assert this.bsf != null\n return this.bsf");
109 assertTrue("Should have an answer", answer != null);
110 }
111
112 public void testVariables() throws Exception {
113 manager.registerBean("x", new Integer(4));
114 Object dontcare = manager.eval("groovy", "Test1.groovy", 0, 0,
115 "valueOfX = this.bsf.lookupBean('x'); assert valueOfX == 4; valueOfX + 1");
116
117
118 Object answer = manager.eval("groovy", "Test2.groovy", 0, 0,
119 "valueOfX = this.bsf.lookupBean('x'); assert valueOfX == 4; valueOfX + 1");
120 assertEquals("Incorrect return", new Integer(5), answer);
121 }
122
123 public void testClassLoaderSet() throws BSFException {
124 CachingGroovyEngine cachingGroovyEngine = new CachingGroovyEngine();
125 manager.setClassLoader(null);
126 cachingGroovyEngine.initialize(manager, "dummy", new Vector());
127
128 assertEquals("hi", manager.eval("groovy", "dummy", 0, 0, "'hi'"));
129 }
130
131 public void testDeclaredVariables() throws Exception {
132 manager.declareBean("foo", new Integer(5), Integer.class);
133 Object answer = manager.eval("groovy", "Test1.groovy", 0, 0, "valueOfFoo = foo; return valueOfFoo");
134 assertEquals(new Integer(5), answer);
135 manager.declareBean("foo", new Integer(6), Integer.class);
136 answer = manager.eval("groovy", "Test2.groovy", 0, 0, "valueOfFoo = foo; return valueOfFoo");
137 assertEquals(new Integer(6), answer);
138 }
139 }