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.script;
017:
018: import scriptella.AbstractTestCase;
019: import scriptella.configuration.ConfigurationException;
020: import scriptella.configuration.StringResource;
021: import scriptella.spi.Connection;
022: import scriptella.spi.IndexedQueryCallback;
023: import scriptella.spi.MockConnectionParameters;
024: import scriptella.spi.MockParametersCallbacks;
025: import scriptella.spi.ParametersCallback;
026: import scriptella.spi.Resource;
027:
028: import javax.script.ScriptException;
029: import java.io.ByteArrayInputStream;
030: import java.io.ByteArrayOutputStream;
031: import java.io.IOException;
032: import java.io.InputStream;
033: import java.io.OutputStream;
034: import java.net.URL;
035: import java.util.Collections;
036:
037: /**
038: * Tests for {@link ScriptConnection}.
039: *
040: * @author Fyodor Kupolov
041: * @version 1.0
042: * @since 11.01.2007
043: */
044: public class ScriptConnectionTest extends AbstractTestCase {
045: private Object v;
046:
047: public void setValue(Object v) {
048: this .v = v;
049: }
050:
051: public void testExecuteScript() {
052: v = null;
053: Resource r = new StringResource(
054: "for (var x=0;x<=10;x+=2) {obj.setValue(x);}");
055:
056: newConnection().executeScript(
057: r,
058: MockParametersCallbacks.fromMap(Collections
059: .singletonMap("obj", this )));
060: assertEquals(10, ((Number) v).intValue());
061: //Now test invalid syntax
062: r = new StringResource("nosuchvar.nosuchmethod()");
063: try {
064: newConnection().executeScript(r,
065: MockParametersCallbacks.UNSUPPORTED);
066: fail("Compilation errors must be reported");
067: } catch (ScriptProviderException e) {
068: //OK
069: }
070: }
071:
072: static ScriptConnection newConnection() {
073: return new ScriptConnection(new MockConnectionParameters());
074: }
075:
076: /**
077: * Tests various configuration options.
078: */
079: public void testConfiguration() {
080: //JavaScript(ECMAScript) should be used by default
081: Connection c = new Driver()
082: .connect(new MockConnectionParameters());
083: assertEquals("ECMAScript", c.getDialectIdentifier().getName());
084: //now tests wrong name
085: try {
086: new Driver().connect(new MockConnectionParameters(
087: Collections.singletonMap(ScriptConnection.LANGUAGE,
088: "nusuchlanguage"), null));
089: fail("ConfigurationException expected for unknown language");
090: } catch (ConfigurationException e) {
091: //OK
092: }
093: }
094:
095: public void testExecuteQuery() {
096: Resource r = new StringResource(
097: "i=0;a=a0;s='test';while (i < 10) {i=i+1;query.next();}");
098: IndexedQueryCallback callback = new IndexedQueryCallback() {
099: protected void processRow(
100: final ParametersCallback parameters,
101: final int rowNumber) {
102: assertEquals(rowNumber + 1, ((Number) parameters
103: .getParameter("i")).intValue());
104: assertEquals(5, ((Number) parameters.getParameter("a"))
105: .intValue());
106: assertEquals("test", parameters.getParameter("s"));
107: }
108: };
109: newConnection().executeQuery(
110: r,
111: MockParametersCallbacks.fromMap(Collections
112: .singletonMap("a0", 5)), callback);
113:
114: }
115:
116: /**
117: * Tests if readers/writers are working
118: */
119: public void testStreamHandling() {
120: final ByteArrayOutputStream os = new ByteArrayOutputStream();
121: testURLHandler = new TestURLHandler() {
122: public InputStream getInputStream(final URL u)
123: throws IOException {
124: return new ByteArrayInputStream("Hello".getBytes());
125: }
126:
127: public OutputStream getOutputStream(final URL u)
128: throws IOException {
129: return os;
130: }
131:
132: public int getContentLength(final URL u) {
133: return 0;
134: }
135: };
136: Connection c = new Driver()
137: .connect(new MockConnectionParameters(Collections
138: .singletonMap(ScriptConnection.ENCODING,
139: "UTF-8"), "tst:/file"));
140:
141: c.executeScript(new StringResource("print('Hello '+name)"),
142: MockParametersCallbacks.fromMap(Collections
143: .singletonMap("name", "world")));
144: assertEquals("Hello world", os.toString());
145: }
146:
147: public void testGetErrorStatement() {
148: String st = ScriptConnection.getErrorStatement(
149: new StringResource("a\nbb\ncc"), new ScriptException(
150: "test", "test", 2));
151: assertEquals("bb", st);
152: }
153:
154: }
|