KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > csv > CsvProducerTest


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.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 JavaDoc;
38 import java.io.FileInputStream JavaDoc;
39 import java.sql.DriverManager JavaDoc;
40 import java.sql.ResultSet JavaDoc;
41 import java.sql.SQLException JavaDoc;
42 import java.sql.Statement JavaDoc;
43 import java.util.Properties JavaDoc;
44
45 public class CsvProducerTest extends TestCase {
46     private String JavaDoc driverClass;
47     private String JavaDoc url;
48     private String JavaDoc user;
49     private String JavaDoc 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 JavaDoc 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         // producer.setConsumer(new CsvDataSetWriter("src/csv/orders-out"));
59

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 JavaDoc, SQLException JavaDoc {
77         produceAndInsertToDatabase();
78         Statement JavaDoc statement = connection.getConnection().createStatement();
79         ResultSet JavaDoc 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 JavaDoc {
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 JavaDoc, DatabaseUnitException {
97         Operation operation = new Operation();
98         operation.setFormat(AbstractStep.FORMAT_CSV);
99         operation.setSrc(new File JavaDoc(THE_DIRECTORY));
100         operation.setType("INSERT");
101         operation.execute(connection);
102         Statement JavaDoc statement = connection.getConnection().createStatement();
103         ResultSet JavaDoc 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 JavaDoc {
112         produceAndInsertToDatabase();
113
114         final String JavaDoc fromAnt = "target/csv/from-ant";
115         final File JavaDoc dir = new File JavaDoc(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 JavaDoc ordersFile = new File JavaDoc(fromAnt + "/orders.csv");
135         assertTrue("file '" + ordersFile.getAbsolutePath() + "' does not exists", ordersFile.exists());
136         final File JavaDoc ordersRowFile = new File JavaDoc(fromAnt + "/orders_row.csv");
137         assertTrue("file " + ordersRowFile + " does not exists", ordersRowFile.exists());
138     }
139
140     private void deleteDirectory(final File JavaDoc dir) {
141         File JavaDoc[] files = dir.listFiles();
142         if (files == null) return;
143         for (int i = 0; i < files.length; i++) {
144             File JavaDoc file = files[i];
145             file.delete();
146         }
147         dir.delete();
148     }
149
150     private IDatabaseConnection getConnection() throws SQLException JavaDoc {
151         return new DatabaseConnection(DriverManager.getConnection(url, user, password));
152     }
153
154     protected void setUp() throws Exception JavaDoc {
155         Properties JavaDoc properties = new Properties JavaDoc();
156         final FileInputStream JavaDoc inStream = new FileInputStream JavaDoc("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 JavaDoc statement = connection.getConnection().createStatement();
169         try {
170             statement.execute("DROP TABLE ORDERS");
171             statement.execute("DROP TABLE ORDERS_ROW");
172         } catch (Exception JavaDoc 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.execute("delete from orders");
176
//statement.execute("delete from orders_row");
177
statement.close();
178     }
179
180     protected void tearDown() throws Exception JavaDoc {
181         connection.close();
182     }
183 }
184
Popular Tags