KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > driver > csv > CsvConnectionPerfTest


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.csv;
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.csv.CsvConnection}.
38  *
39  * @author Fyodor Kupolov
40  * @version 1.0
41  */

42 public class CsvConnectionPerfTest 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     /**
71      * History:
72      * 11.09.2006 - Duron 1.7Mhz - 1265 ms
73      * 09.09.2006 - Duron 1.7Mhz - 1400 ms
74      */

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

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