1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.dbunit.dataset.csv;
23
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.net.MalformedURLException;
28 import java.net.URL;
29 import java.sql.DriverManager;
30 import java.sql.ResultSet;
31 import java.sql.SQLException;
32 import java.sql.Statement;
33 import java.util.Properties;
34
35 import junit.framework.TestCase;
36
37 import org.dbunit.DatabaseUnitException;
38 import org.dbunit.HypersonicEnvironment;
39 import org.dbunit.ant.AbstractStep;
40 import org.dbunit.ant.Export;
41 import org.dbunit.ant.Operation;
42 import org.dbunit.ant.Query;
43 import org.dbunit.database.DatabaseConnection;
44 import org.dbunit.database.IDatabaseConnection;
45 import org.dbunit.dataset.CachedDataSet;
46 import org.dbunit.dataset.DataSetException;
47 import org.dbunit.dataset.ITable;
48 import org.dbunit.operation.DatabaseOperation;
49 import org.dbunit.testutil.TestUtils;
50 import org.dbunit.util.FileHelper;
51
52 public class CsvURLProducerTest extends TestCase {
53 private String driverClass;
54 private String url;
55 private String user;
56 private String password;
57 private IDatabaseConnection connection;
58 private static final int ORDERS_ROWS_NUMBER = 5;
59 private static final int ORDERS_ROW_ROWS_NUMBER = 3;
60 private static final String THE_DIRECTORY = "csv/orders";
61
62 public void testProduceFromFolder() throws DataSetException, MalformedURLException {
63 CsvURLProducer producer = new CsvURLProducer(TestUtils.getFile(THE_DIRECTORY).toURL(), CsvDataSet.TABLE_ORDERING_FILE);
64 doTestWithProducer(producer);
65 }
66
67 public void testProduceFromJar() throws DataSetException, IOException {
68 File file = TestUtils.getFile(THE_DIRECTORY + "/orders.jar");
69 URL jarFile = new URL("jar:" + file.toURL() + "!/");
70 CsvURLProducer producer = new CsvURLProducer(jarFile, CsvDataSet.TABLE_ORDERING_FILE);
71 doTestWithProducer(producer);
72 }
73
74 private void doTestWithProducer(CsvURLProducer producer) throws DataSetException
75 {
76 CachedDataSet consumer = new CachedDataSet();
77
78
79 producer.setConsumer(consumer);
80 producer.produce();
81 final ITable[] tables = consumer.getTables();
82 assertEquals("expected 2 tables", 2, tables.length);
83
84 final ITable orders = consumer.getTable("orders");
85 assertNotNull("orders table not found", orders);
86 assertEquals("wrong number of rows", ORDERS_ROWS_NUMBER, orders.getRowCount());
87 assertEquals("wrong number of columns", 2, orders.getTableMetaData().getColumns().length);
88
89 final ITable ordersRow = consumer.getTable("orders_row");
90 assertNotNull("orders_row table not found", ordersRow);
91 assertEquals("wrong number of rows", ORDERS_ROW_ROWS_NUMBER, ordersRow.getRowCount());
92 assertEquals("wrong number of columns", ORDERS_ROW_ROWS_NUMBER, ordersRow.getTableMetaData().getColumns().length);
93
94 }
95
96 public void testProduceAndInsertFromFolder() throws ClassNotFoundException, MalformedURLException, DatabaseUnitException, SQLException {
97 produceAndInsertToDatabase();
98 Statement statement = connection.getConnection().createStatement();
99 ResultSet resultSet = statement.executeQuery("select count(*) from orders");
100 resultSet.next();
101 int count = resultSet.getInt(1);
102 assertEquals(ORDERS_ROWS_NUMBER, count);
103 resultSet.close();
104 statement.close();
105 }
106
107 private void produceAndInsertToDatabase() throws DatabaseUnitException, SQLException, MalformedURLException {
108 CsvURLProducer producer = new CsvURLProducer(TestUtils.getFile(THE_DIRECTORY).toURL(), CsvDataSet.TABLE_ORDERING_FILE);
109 CachedDataSet consumer = new CachedDataSet();
110 producer.setConsumer(consumer);
111 producer.produce();
112 DatabaseOperation operation = DatabaseOperation.INSERT;
113 operation.execute(connection, consumer);
114 }
115
116 public void testInsertOperationWithCsvFormat() throws SQLException, DatabaseUnitException {
117 Operation operation = new Operation();
118 operation.setFormat(AbstractStep.FORMAT_CSV);
119 operation.setSrc(TestUtils.getFile(THE_DIRECTORY));
120 operation.setType("INSERT");
121 operation.execute(connection);
122 Statement statement = connection.getConnection().createStatement();
123 ResultSet resultSet = statement.executeQuery("select count(*) from orders");
124 resultSet.next();
125 final int count = resultSet.getInt(1);
126 assertEquals("wrong number of row in orders table", ORDERS_ROWS_NUMBER, count);
127 resultSet.close();
128 statement.close();
129 }
130
131 public void testExportTaskWithCsvFormat() throws MalformedURLException, DatabaseUnitException, SQLException {
132 produceAndInsertToDatabase();
133
134 final String fromAnt = "target/csv/from-ant";
135 final File dir = new File(fromAnt);
136 try {
137 FileHelper.deleteDirectory(dir);
138
139 Export export = new Export();
140 export.setFormat(AbstractStep.FORMAT_CSV);
141 export.setDest(dir);
142
143 Query query = new Query();
144 query.setName("orders");
145 query.setSql("select * from orders");
146 export.addQuery(query);
147
148 Query query2 = new Query();
149 query2.setName("orders_row");
150 query2.setSql("select * from orders_row");
151 export.addQuery(query2);
152
153 export.execute(getConnection());
154
155 final File ordersFile = new File(fromAnt + "/orders.csv");
156 assertTrue("file '" + ordersFile.getAbsolutePath() + "' does not exists", ordersFile.exists());
157 final File ordersRowFile = new File(fromAnt + "/orders_row.csv");
158 assertTrue("file " + ordersRowFile + " does not exists", ordersRowFile.exists());
159 }
160 finally {
161 FileHelper.deleteDirectory(dir);
162 }
163 }
164
165 private IDatabaseConnection getConnection() throws SQLException, DatabaseUnitException {
166 return new DatabaseConnection(DriverManager.getConnection(url, user, password));
167 }
168
169 protected void setUp() throws Exception {
170 Properties properties = new Properties();
171 final FileInputStream inStream = TestUtils.getFileInputStream("csv/cvs-tests.properties");
172 properties.load(inStream);
173 inStream.close();
174 driverClass = properties.getProperty("cvs-tests.driver.class");
175 url = properties.getProperty("cvs-tests.url");
176 user = properties.getProperty("cvs-tests.user");
177 password = properties.getProperty("cvs-tests.password");
178 assertFalse("".equals(driverClass));
179 assertFalse("".equals(url));
180 assertFalse("".equals(user));
181 Class.forName(driverClass);
182 connection = getConnection();
183 Statement statement = connection.getConnection().createStatement();
184 try {
185 statement.execute("DROP TABLE ORDERS");
186 statement.execute("DROP TABLE ORDERS_ROW");
187 } catch (Exception ignored) {}
188 statement.execute("CREATE TABLE ORDERS (ID INTEGER, DESCRIPTION VARCHAR)");
189 statement.execute("CREATE TABLE ORDERS_ROW (ID INTEGER, DESCRIPTION VARCHAR, QUANTITY INTEGER)");
190
191
192 statement.close();
193 }
194
195 protected void tearDown() throws Exception {
196 HypersonicEnvironment.shutdown(connection.getConnection());
197 connection.close();
198 }
199 }