View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2004, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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.DatabaseConfig;
32  import org.dbunit.database.DatabaseConnection;
33  import org.dbunit.database.IDatabaseConnection;
34  import org.dbunit.dataset.CachedDataSet;
35  import org.dbunit.dataset.DataSetException;
36  import org.dbunit.dataset.ITable;
37  import org.dbunit.ext.hsqldb.HsqldbDataTypeFactory;
38  import org.dbunit.operation.DatabaseOperation;
39  import org.dbunit.testutil.TestUtils;
40  import org.dbunit.util.FileHelper;
41  
42  import java.io.File;
43  import java.io.FileInputStream;
44  import java.sql.DriverManager;
45  import java.sql.ResultSet;
46  import java.sql.SQLException;
47  import java.sql.Statement;
48  import java.util.Properties;
49  
50  public class CsvProducerTest extends TestCase {
51      private String driverClass;
52      private String url;
53      private String user;
54      private String password;
55      private IDatabaseConnection connection;
56      private static final int ORDERS_ROWS_NUMBER = 5;
57      private static final int ORDERS_ROW_ROWS_NUMBER = 3;
58      private static final String THE_DIRECTORY = TestUtils.getFileName("csv/orders");
59  
60      public void testProduceFromFolder() throws DataSetException {
61          CsvProducer producer = new CsvProducer(THE_DIRECTORY);
62          CachedDataSet consumer = new CachedDataSet();
63          // producer.setConsumer(new CsvDataSetWriter("src/csv/orders-out"));
64  
65          producer.setConsumer(consumer);
66          producer.produce();
67          final ITable[] tables = consumer.getTables();
68          assertEquals("expected 2 tables", 2, tables.length);
69  
70          final ITable orders = consumer.getTable("orders");
71          assertNotNull("orders table not found", orders);
72          assertEquals("wrong number of rows", ORDERS_ROWS_NUMBER, orders.getRowCount());
73          assertEquals("wrong number of columns", 2, orders.getTableMetaData().getColumns().length);
74  
75          final ITable ordersRow = consumer.getTable("orders_row");
76          assertNotNull("orders_row table not found", ordersRow);
77          assertEquals("wrong number of rows", ORDERS_ROW_ROWS_NUMBER, ordersRow.getRowCount());
78          assertEquals("wrong number of columns", ORDERS_ROW_ROWS_NUMBER, ordersRow.getTableMetaData().getColumns().length);
79      }
80  
81      public void testProduceAndInsertFromFolder() throws DatabaseUnitException, ClassNotFoundException, SQLException {
82          produceAndInsertToDatabase();
83          Statement statement = connection.getConnection().createStatement();
84          ResultSet resultSet = statement.executeQuery("select count(*) from orders");
85          resultSet.next();
86          int count = resultSet.getInt(1);
87          assertEquals(ORDERS_ROWS_NUMBER, count);
88          resultSet.close();
89          statement.close();
90      }
91  
92      private void produceAndInsertToDatabase() throws DatabaseUnitException, SQLException {
93          CsvProducer producer = new CsvProducer(THE_DIRECTORY);
94          CachedDataSet consumer = new CachedDataSet();
95          producer.setConsumer(consumer);
96          producer.produce();
97          DatabaseOperation operation = DatabaseOperation.INSERT;
98          operation.execute(connection, consumer);
99      }
100 
101     public void testInsertOperationWithCsvFormat() throws SQLException, DatabaseUnitException {
102         Operation operation = new Operation();
103         operation.setFormat(AbstractStep.FORMAT_CSV);
104         operation.setSrc(new File(THE_DIRECTORY));
105         operation.setType("INSERT");
106         operation.execute(connection);
107         Statement statement = connection.getConnection().createStatement();
108         ResultSet resultSet = statement.executeQuery("select count(*) from orders");
109         resultSet.next();
110         final int count = resultSet.getInt(1);
111         assertEquals("wrong number of row in orders table", ORDERS_ROWS_NUMBER, count);
112         resultSet.close();
113         statement.close();
114     }
115 
116     public void testExportTaskWithCsvFormat() throws DatabaseUnitException, SQLException {
117         produceAndInsertToDatabase();
118 
119         final String fromAnt = "target/csv/from-ant";
120         final File dir = new File(fromAnt);
121         FileHelper.deleteDirectory(dir);
122         
123         try {
124             Export export = new Export();
125             export.setFormat(AbstractStep.FORMAT_CSV);
126             export.setDest(dir);
127     
128             Query query = new Query();
129             query.setName("orders");
130             query.setSql("select * from orders");
131             export.addQuery(query);
132     
133             Query query2 = new Query();
134             query2.setName("orders_row");
135             query2.setSql("select * from orders_row");
136             export.addQuery(query2);
137     
138             export.execute(getConnection());
139     
140             final File ordersFile = new File(fromAnt + "/orders.csv");
141             assertTrue("file '" + ordersFile.getAbsolutePath() + "' does not exists", ordersFile.exists());
142             final File ordersRowFile = new File(fromAnt + "/orders_row.csv");
143             assertTrue("file " + ordersRowFile + " does not exists", ordersRowFile.exists());
144         }
145         finally {
146             FileHelper.deleteDirectory(dir);
147         }
148     }
149 
150     private IDatabaseConnection getConnection() throws SQLException, DatabaseUnitException {
151         DatabaseConnection connection = new DatabaseConnection(DriverManager.getConnection(url, user, password));
152         DatabaseConfig config = connection.getConfig();
153         config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
154                 new HsqldbDataTypeFactory());
155         return connection;
156     }
157 
158     protected void setUp() throws Exception {
159         Properties properties = new Properties();
160         final FileInputStream inStream = TestUtils.getFileInputStream("csv/cvs-tests.properties");
161         properties.load(inStream);
162         inStream.close();
163         driverClass = properties.getProperty("cvs-tests.driver.class");
164         url = properties.getProperty("cvs-tests.url");
165         user = properties.getProperty("cvs-tests.user");
166         password = properties.getProperty("cvs-tests.password");
167         assertFalse("".equals(driverClass));
168         assertFalse("".equals(url));
169         assertFalse("".equals(user));
170         Class.forName(driverClass);
171         connection = getConnection();
172         Statement statement = connection.getConnection().createStatement();
173         try {
174             statement.execute("DROP TABLE ORDERS");
175             statement.execute("DROP TABLE ORDERS_ROW");
176         } catch (Exception ignored) {}
177         statement.execute("CREATE TABLE ORDERS (ID INTEGER, DESCRIPTION VARCHAR)");
178         statement.execute("CREATE TABLE ORDERS_ROW (ID INTEGER, DESCRIPTION VARCHAR, QUANTITY INTEGER)");
179         //statement.execute("delete from orders");
180         //statement.execute("delete from orders_row");
181         statement.close();
182     }
183 
184     protected void tearDown() throws Exception {
185         HypersonicEnvironment.shutdown(connection.getConnection());
186         connection.close();
187     }
188 }