1 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 ; 26 import java.io.ByteArrayOutputStream ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.net.URL ; 30 import java.sql.Connection ; 31 import java.util.HashSet ; 32 import java.util.Set ; 33 34 40 public class CsvScriptTest extends DBTestCase { 41 public void test() throws EtlExecutorException { 42 final ByteArrayOutputStream out = new ByteArrayOutputStream (); 43 testURLHandler = new TestURLHandler() { 44 public InputStream getInputStream(final URL u) { 45 return new ByteArrayInputStream ("f1,f2,f3\n1,2,3\nCol4,5,6\n7,8,9".getBytes()); 46 } 47 48 public OutputStream getOutputStream(final URL u) { 49 return out; 50 } 51 52 public int getContentLength(final URL u) { 53 throw new UnsupportedOperationException (); 54 } 55 }; 56 final EtlExecutor se = newEtlExecutor(); 57 se.execute(); 58 final Connection connection = getConnection("csv"); 59 QueryHelper q = new QueryHelper("SELECT * from Result"); 60 final Set <String > expected = new HashSet <String >(); 61 expected.add("1 2 3");expected.add("Col4 5 6");expected.add("7 8 9"); 62 expected.add("q1"); expected.add("qCol4"); q.execute(connection, new QueryCallback() { 64 public void processRow(final ParametersCallback parameters) { 65 final Object 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 s = out.toString(); 71 assertEquals("\"1\",\"One\"\n" + 72 "\"2\",\" ;,-Two\"\" \"\n" + 73 "\"3\",\" Three!!, \"\n",s); 74 75 } 76 } 77 | Popular Tags |