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