KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > driver > text > TextConnectionPerfTest


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.text;
17
18 import scriptella.AbstractTestCase;
19 import scriptella.configuration.MockConnectionEl;
20 import scriptella.configuration.StringResource;
21 import scriptella.spi.ConnectionParameters;
22 import scriptella.spi.MockDriverContext;
23 import scriptella.spi.MockParametersCallbacks;
24 import scriptella.spi.ParametersCallback;
25 import scriptella.spi.QueryCallback;
26 import scriptella.util.RepeatingInputStream;
27
28 import java.io.ByteArrayOutputStream JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.io.UnsupportedEncodingException JavaDoc;
32 import java.net.URL JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Map JavaDoc;
35
36 /**
37  * Tests for {@link scriptella.driver.text.TextConnection}.
38  *
39  * @author Fyodor Kupolov
40  * @version 1.0
41  */

42 public class TextConnectionPerfTest extends AbstractTestCase {
43     private int rows;
44
45     private ByteArrayOutputStream JavaDoc out;
46
47     protected void setUp() throws Exception JavaDoc {
48         super.setUp();
49         testURLHandler = new TestURLHandler() {
50             byte[] rows = "c1,c2,c3\nc4,c5,c6\n".getBytes();
51
52             public InputStream getInputStream(final URL JavaDoc u) {
53                 return new RepeatingInputStream(rows, 10000);
54             }
55
56             public OutputStream JavaDoc getOutputStream(final URL JavaDoc u) {
57                 if (out == null) {
58                     out = new ByteArrayOutputStream JavaDoc();
59                 }
60                 return out;
61             }
62
63             public int getContentLength(final URL JavaDoc u) {
64                 return -1;
65             }
66         };
67
68     }
69     /**
70      * History:
71      * 03.12.2006 - Duron 1.7Mhz - 938 ms
72      */

73     public void testQuery() {
74         //Create a configuration with non default values
75
Map JavaDoc<String JavaDoc, String JavaDoc> props = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
76         ConnectionParameters cp = new ConnectionParameters(new MockConnectionEl(props, "tst://file"), MockDriverContext.INSTANCE);
77
78         TextConnection con = new TextConnection(cp);
79         //Quering 20000 lines file 10 times.
80
for (int i=0;i<10;i++) {
81             rows = 0;
82             con.executeQuery(new StringResource(".*,(.*2),.*"), MockParametersCallbacks.NULL, new QueryCallback() {
83                 public void processRow(final ParametersCallback parameters) {
84                     rows++;
85                     parameters.getParameter("0");
86                     parameters.getParameter("1");
87                     parameters.getParameter("nosuchcolumn");
88
89                 }
90             });
91             assertEquals(10000, rows);
92             assertNull("No output should be produced by a query", out);
93         }
94
95
96     }
97
98     /**
99      * History:
100      * 03.12.2006 - Duron 1.7Mhz - 703 ms
101      * @throws java.io.UnsupportedEncodingException
102      */

103     public void testScript() throws UnsupportedEncodingException JavaDoc {
104         //Create a configuration with non default values
105
Map JavaDoc<String JavaDoc, String JavaDoc> props = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
106         ConnectionParameters cp = new ConnectionParameters(new MockConnectionEl(props, "tst://file"), MockDriverContext.INSTANCE);
107
108         TextConnection con = new TextConnection(cp);
109         String JavaDoc expected = "\"*col1*\",\"col2\",\"col3\"\n\"*col21*\",\"col22\",\"col23\"\n";
110         for (int i = 0; i < 10000; i++) {
111             con.executeScript(new StringResource("\"$col1\",\"col2\",\"col3\"\n\"${col21}\",\"col22\",\"col23\""),
112                     MockParametersCallbacks.SIMPLE);
113             out.reset();
114         }
115
116         con.close();
117         String JavaDoc actual = out.toString(); //checking if script worked correctly
118
//out is filled partially because we use out.reset() to minimize memory usage
119
//So use indexOf to check
120
assertTrue(actual.indexOf(expected) >= 0);
121
122
123     }
124
125
126 }
127
Popular Tags