KickJava   Java API By Example, From Geeks To Geeks.

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


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 scriptella.AbstractTestCase;
19 import scriptella.spi.MockParametersCallbacks;
20 import scriptella.spi.ParametersCallback;
21 import scriptella.spi.QueryCallback;
22
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 /**
27  * Tests for Janino Query/Script base classes.
28  *
29  * @author Fyodor Kupolov
30  * @version 1.0
31  */

32 public class JaninoBaseClassesTest extends AbstractTestCase {
33     /**
34      * Tests public API methods provided by {@link JaninoScript}.
35      */

36     public void testScript() {
37         JaninoScript js = new JaninoScript() {
38             protected void execute() throws Exception JavaDoc {
39             }
40         };
41         js.setParametersCallback(MockParametersCallbacks.SIMPLE);
42         assertEquals("*1*", js.get("1"));
43     }
44     /**
45      * Tests public API methods provided by {@link JaninoQuery}.
46      */

47     public void testQuery() {
48         final int[] rows = new int[1]; //just to allow inner classes to modify a variable
49
JaninoQuery jq = new JaninoQuery() {
50             protected void execute() throws Exception JavaDoc {
51             }
52         };
53         //now check a query exposing a row with one column
54
jq.setParametersCallback(new ParametersCallback() {
55             public Object JavaDoc getParameter(final String JavaDoc name) {
56                 if (!"2".equals(name)) {
57                     fail("Only parameter with name 2 was not specified, but query requested "+name);
58                 }
59                 return null;
60             }
61         });
62         jq.setQueryCallback(new QueryCallback() {
63             public void processRow(final ParametersCallback parameters) {
64                 assertEquals("v1", parameters.getParameter("1"));
65                 assertEquals(null, parameters.getParameter("2"));
66                 rows[0]++;
67             }
68         });
69         jq.set("1", "v1");
70         jq.next();//1st row
71
//now multiple columns in a row
72
//Query provide enough columns - no need to get parameters from parent
73
jq.setParametersCallback(MockParametersCallbacks.UNSUPPORTED);
74
75         jq.setQueryCallback(new QueryCallback() {
76             public void processRow(final ParametersCallback parameters) {
77                 assertEquals("v1", parameters.getParameter("1"));
78                 assertEquals("v2", parameters.getParameter("2"));
79                 rows[0]++;
80             }
81         });
82         jq.set("1", "v1");
83         jq.set("2", "v2");
84         jq.next();//2nd row
85
jq.next(new String JavaDoc[] {"1", "2"},new Object JavaDoc[] {"v1", "v2"}); //3rd row
86
//Now test passing a map
87
Map JavaDoc<String JavaDoc,Object JavaDoc> m = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
88         m.put("1", "v1");
89         m.put("2", "v2");
90         jq.set(m);
91         jq.next(); //4th row
92
jq.next(m); //5th row
93

94         assertEquals(5, rows[0]);
95     }
96
97 }
98
Popular Tags