KickJava   Java API By Example, From Geeks To Geeks.

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


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.DBTestCase;
19 import scriptella.execution.EtlExecutor;
20 import scriptella.execution.EtlExecutorException;
21 import scriptella.jdbc.QueryHelper;
22 import scriptella.spi.ParametersCallback;
23 import scriptella.spi.QueryCallback;
24
25 import java.io.ByteArrayInputStream JavaDoc;
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.sql.Connection JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Set JavaDoc;
33
34 /**
35  * Tests CSV script processing.
36  *
37  * @author Fyodor Kupolov
38  * @version 1.0
39  */

40 public class CsvScriptTest extends DBTestCase {
41     public void test() throws EtlExecutorException {
42         final ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
43         testURLHandler = new TestURLHandler() {
44             public InputStream JavaDoc getInputStream(final URL JavaDoc u) {
45                 return new ByteArrayInputStream JavaDoc("f1,f2,f3\n1,2,3\nCol4,5,6\n7,8,9".getBytes());
46             }
47
48             public OutputStream JavaDoc getOutputStream(final URL JavaDoc u) {
49                 return out;
50             }
51
52             public int getContentLength(final URL JavaDoc u) {
53                 throw new UnsupportedOperationException JavaDoc();
54             }
55         };
56         final EtlExecutor se = newEtlExecutor();
57         se.execute();
58         final Connection JavaDoc connection = getConnection("csv");
59         QueryHelper q = new QueryHelper("SELECT * from Result");
60         final Set JavaDoc<String JavaDoc> expected = new HashSet JavaDoc<String JavaDoc>();
61         expected.add("1 2 3");expected.add("Col4 5 6");expected.add("7 8 9");
62         expected.add("q1"); expected.add("qCol4");//second query filter only first and second rows
63
q.execute(connection, new QueryCallback() {
64             public void processRow(final ParametersCallback parameters) {
65                 final Object JavaDoc parameter = parameters.getParameter("text");
66                 assertTrue("Row "+parameter+" is not expected", expected.remove(parameter));
67             }
68         });
69         assertTrue("Expected row(s) were not selected: "+expected, expected.isEmpty());
70         final String JavaDoc s = out.toString();
71         assertEquals("\"1\",\"One\"\n" +
72                 "\"2\",\" ;,-Two\"\" \"\n" +
73                 "\"3\",\" Three!!, \"\n",s);
74
75     }
76 }
77
Popular Tags