KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > driver > janino > JaninoConnectionTest


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.driver.janino;
17
18 import org.codehaus.janino.CompileException;
19 import scriptella.AbstractTestCase;
20 import scriptella.configuration.MockConnectionEl;
21 import scriptella.configuration.StringResource;
22 import scriptella.spi.ConnectionParameters;
23 import scriptella.spi.MockDriverContext;
24 import scriptella.spi.MockParametersCallbacks;
25 import scriptella.spi.ParametersCallback;
26 import scriptella.spi.ProviderException;
27 import scriptella.spi.QueryCallback;
28
29 import java.util.ArrayList JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.List JavaDoc;
32
33 /**
34  * Tests Janino connection class.
35  *
36  * @author Fyodor Kupolov
37  * @version 1.0
38  */

39 public class JaninoConnectionTest extends AbstractTestCase {
40     public static int field; //Field to be filled by a script
41

42     /**
43      * This test creates a Janino connection that executes simple valid and invalid scripts.
44      */

45     public void testScript() {
46         JaninoConnection c = new JaninoConnection(new ConnectionParameters(new MockConnectionEl(), MockDriverContext.INSTANCE));
47         field = 0;
48         c.executeScript(new StringResource(JaninoConnectionTest.class.getName() + ".field=1;"), null);
49         try {
50             c.executeScript(new StringResource(JaninoConnectionTest.class.getName() + ".nosuchfield=1;"), null);
51             fail("This script should fail");
52         } catch (ProviderException e) {
53             Throwable JavaDoc ne = e.getNativeException();
54             assertTrue(ne instanceof CompileException);
55             //OK
56
}
57         c.close();
58         assertEquals(1, field);
59     }
60
61     public void testQuery() {
62         JaninoConnection c = new JaninoConnection(new ConnectionParameters(new MockConnectionEl(), MockDriverContext.INSTANCE));
63         field = 0;
64         final List JavaDoc<String JavaDoc> rows = new ArrayList JavaDoc<String JavaDoc>();
65
66         c.executeQuery(new StringResource(
67                 "set(\"p\", \"//\"+get(\"p\") );" +
68                 "next();" +
69                 "next(new String[] {\"p\"}, new Object[] {\"v2\"});"),
70                 MockParametersCallbacks.SIMPLE, new QueryCallback() {
71             public void processRow(final ParametersCallback parameters) {
72                 rows.add(parameters.getParameter("p").toString());
73             }
74         });
75         c.close();
76         List JavaDoc<String JavaDoc> expected = Arrays.asList("//*p*", "v2");
77         assertEquals(expected, rows);
78     }
79
80     public void testErrorSourceCode() {
81         JaninoConnection c = new JaninoConnection(new ConnectionParameters(new MockConnectionEl(), MockDriverContext.INSTANCE));
82         //test compilation errors
83
try {
84             c.executeScript(new StringResource("int b=1;\na='"), MockParametersCallbacks.NULL);
85             fail("Error statements must be recognized");
86         } catch (JaninoProviderException e) {
87             assertEquals("a='", e.getErrorStatement());
88         }
89         //test execution errors
90
try {
91             c.executeScript(new StringResource("int b=1;\nObject a=get(\"1\");"), MockParametersCallbacks.UNSUPPORTED);
92             fail("Error statements must be recognized");
93         } catch (JaninoProviderException e) {
94             assertEquals("Object a=get(\"1\");", e.getErrorStatement());
95         }
96
97
98
99     }
100 }
Popular Tags