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.text;
017:
018: import scriptella.AbstractTestCase;
019: import scriptella.configuration.MockConnectionEl;
020: import scriptella.configuration.StringResource;
021: import scriptella.spi.ConnectionParameters;
022: import scriptella.spi.MockDriverContext;
023: import scriptella.spi.MockParametersCallbacks;
024: import scriptella.spi.ParametersCallback;
025: import scriptella.spi.QueryCallback;
026: import scriptella.util.RepeatingInputStream;
027:
028: import java.io.ByteArrayOutputStream;
029: import java.io.InputStream;
030: import java.io.OutputStream;
031: import java.io.UnsupportedEncodingException;
032: import java.net.URL;
033: import java.util.HashMap;
034: import java.util.Map;
035:
036: /**
037: * Tests for {@link scriptella.driver.text.TextConnection}.
038: *
039: * @author Fyodor Kupolov
040: * @version 1.0
041: */
042: public class TextConnectionPerfTest extends AbstractTestCase {
043: private int rows;
044:
045: private ByteArrayOutputStream out;
046:
047: protected void setUp() throws Exception {
048: super .setUp();
049: testURLHandler = new TestURLHandler() {
050: byte[] rows = "c1,c2,c3\nc4,c5,c6\n".getBytes();
051:
052: public InputStream getInputStream(final URL u) {
053: return new RepeatingInputStream(rows, 10000);
054: }
055:
056: public OutputStream getOutputStream(final URL u) {
057: if (out == null) {
058: out = new ByteArrayOutputStream();
059: }
060: return out;
061: }
062:
063: public int getContentLength(final URL u) {
064: return -1;
065: }
066: };
067:
068: }
069:
070: /**
071: * History:
072: * 03.12.2006 - Duron 1.7Mhz - 938 ms
073: */
074: public void testQuery() {
075: //Create a configuration with non default values
076: Map<String, String> props = new HashMap<String, String>();
077: ConnectionParameters cp = new ConnectionParameters(
078: new MockConnectionEl(props, "tst://file"),
079: MockDriverContext.INSTANCE);
080:
081: TextConnection con = new TextConnection(cp);
082: //Quering 20000 lines file 10 times.
083: for (int i = 0; i < 10; i++) {
084: rows = 0;
085: con.executeQuery(new StringResource(".*,(.*2),.*"),
086: MockParametersCallbacks.NULL, new QueryCallback() {
087: public void processRow(
088: final ParametersCallback parameters) {
089: rows++;
090: parameters.getParameter("0");
091: parameters.getParameter("1");
092: parameters.getParameter("nosuchcolumn");
093:
094: }
095: });
096: assertEquals(10000, rows);
097: assertNull("No output should be produced by a query", out);
098: }
099:
100: }
101:
102: /**
103: * History:
104: * 03.12.2006 - Duron 1.7Mhz - 703 ms
105: * @throws java.io.UnsupportedEncodingException
106: */
107: public void testScript() throws UnsupportedEncodingException {
108: //Create a configuration with non default values
109: Map<String, String> props = new HashMap<String, String>();
110: ConnectionParameters cp = new ConnectionParameters(
111: new MockConnectionEl(props, "tst://file"),
112: MockDriverContext.INSTANCE);
113:
114: TextConnection con = new TextConnection(cp);
115: String expected = "\"*col1*\",\"col2\",\"col3\"\n\"*col21*\",\"col22\",\"col23\"\n";
116: for (int i = 0; i < 10000; i++) {
117: if (out != null) {
118: out.reset();
119: }
120: con
121: .executeScript(
122: new StringResource(
123: "\"$col1\",\"col2\",\"col3\"\n\"${col21}\",\"col22\",\"col23\""),
124: MockParametersCallbacks.SIMPLE);
125: }
126:
127: con.close();
128: String actual = out.toString(); //checking if script worked correctly
129: //out is filled partially because we use out.reset() to minimize memory usage
130: //So use indexOf to check
131: assertTrue(actual.indexOf(expected) >= 0);
132:
133: }
134:
135: }
|