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