001: /*
002: * Copyright 2006-2007 The Scriptella Project Team.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package scriptella.driver.janino;
017:
018: import org.codehaus.janino.CompileException;
019: import scriptella.AbstractTestCase;
020: import scriptella.configuration.MockConnectionEl;
021: import scriptella.configuration.StringResource;
022: import scriptella.spi.ConnectionParameters;
023: import scriptella.spi.MockDriverContext;
024: import scriptella.spi.MockParametersCallbacks;
025: import scriptella.spi.ParametersCallback;
026: import scriptella.spi.ProviderException;
027: import scriptella.spi.QueryCallback;
028:
029: import java.util.ArrayList;
030: import java.util.Arrays;
031: import java.util.List;
032:
033: /**
034: * Tests Janino connection class.
035: *
036: * @author Fyodor Kupolov
037: * @version 1.0
038: */
039: public class JaninoConnectionTest extends AbstractTestCase {
040: public static int field; //Field to be filled by a script
041:
042: /**
043: * This test creates a Janino connection that executes simple valid and invalid scripts.
044: */
045: public void testScript() {
046: JaninoConnection c = new JaninoConnection(
047: new ConnectionParameters(new MockConnectionEl(),
048: MockDriverContext.INSTANCE));
049: field = 0;
050: c.executeScript(new StringResource(JaninoConnectionTest.class
051: .getName()
052: + ".field=1;"), null);
053: try {
054: c.executeScript(new StringResource(
055: JaninoConnectionTest.class.getName()
056: + ".nosuchfield=1;"), null);
057: fail("This script should fail");
058: } catch (ProviderException e) {
059: Throwable ne = e.getNativeException();
060: assertTrue(ne instanceof CompileException);
061: //OK
062: }
063: c.close();
064: assertEquals(1, field);
065: }
066:
067: public void testQuery() {
068: JaninoConnection c = new JaninoConnection(
069: new ConnectionParameters(new MockConnectionEl(),
070: MockDriverContext.INSTANCE));
071: field = 0;
072: final List<String> rows = new ArrayList<String>();
073:
074: c
075: .executeQuery(
076: new StringResource(
077: "set(\"p\", \"//\"+get(\"p\") );"
078: + "next();"
079: + "next(new String[] {\"p\"}, new Object[] {\"v2\"});"),
080: MockParametersCallbacks.SIMPLE,
081: new QueryCallback() {
082: public void processRow(
083: final ParametersCallback parameters) {
084: rows.add(parameters.getParameter("p")
085: .toString());
086: }
087: });
088: c.close();
089: List<String> expected = Arrays.asList("//*p*", "v2");
090: assertEquals(expected, rows);
091: }
092:
093: public void testErrorSourceCode() {
094: JaninoConnection c = new JaninoConnection(
095: new ConnectionParameters(new MockConnectionEl(),
096: MockDriverContext.INSTANCE));
097: //test compilation errors
098: try {
099: c.executeScript(new StringResource("int b=1;\na='"),
100: MockParametersCallbacks.NULL);
101: fail("Error statements must be recognized");
102: } catch (JaninoProviderException e) {
103: assertEquals("a='", e.getErrorStatement());
104: }
105: //test execution errors
106: try {
107: c.executeScript(new StringResource(
108: "int b=1;\nObject a=get(\"1\");"),
109: MockParametersCallbacks.UNSUPPORTED);
110: fail("Error statements must be recognized");
111: } catch (JaninoProviderException e) {
112: assertEquals("Object a=get(\"1\");", e.getErrorStatement());
113: }
114:
115: }
116: }
|