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;
23
24 import org.dbunit.database.DatabaseConfig;
25 import org.dbunit.database.DatabaseConnection;
26 import org.dbunit.database.ForwardOnlyResultSetTableFactory;
27 import org.dbunit.database.IDatabaseConnection;
28 import org.dbunit.dataset.DataSetException;
29 import org.dbunit.dataset.IDataSet;
30 import org.dbunit.dataset.excel.XlsDataSet;
31 import org.dbunit.dataset.stream.IDataSetProducer;
32 import org.dbunit.dataset.stream.MockDataSetProducer;
33 import org.dbunit.dataset.stream.StreamingDataSet;
34 import org.dbunit.dataset.xml.*;
35 import org.xml.sax.InputSource;
36
37 import java.io.*;
38 import java.sql.Connection;
39
40 /**
41 * This class is a scratchpad used to try new features.
42 *
43 * @author Manuel Laflamme
44 * @version $Revision: 1162 $
45 * @since Mar 14, 2002
46 */
47 public class Main
48 {
49 public static void main(String[] args) throws Exception
50 {
51 // System.setProperty("dbunit.qualified.table.names", "true");
52
53 // testFlatXmlWriter();
54 testXmlWriter();
55
56 /*
57 IDatabaseConnection connection =
58 DatabaseEnvironment.getInstance().getConnection();
59
60 // IDataSet dataSet = new XmlDataSet(TestUtils.getFileReader("xml/dataSetTest.xml"));
61 InputSource source = new InputSource(TestUtils.getFile("xml/xmlTableTest.xml").toURL().toString());
62 // InputSource source = new InputSource(new File("writerTest.xml").toURL().toString());
63 FlatXmlProducer flatXmlProducer = new FlatXmlProducer(source);
64 XmlProducer xmlProducer = new XmlProducer(source);
65 MockDataSetProducer producer = new MockDataSetProducer();
66 producer.setupColumnCount(4);
67 producer.setupRowCount(2);
68 producer.setupTableCount(3);
69 IDataSet dataSet = new StreamingDataSet(xmlProducer);
70 // IDataSet dataSet = new StreamingDataSet(xmlProducer);
71
72 // System.out.println(connection.createDataSet());
73
74 // DatabaseOperation.INSERT.execute(connection, dataSet);
75
76 // IDataSet dataSet = connection.createDataSet();
77 // OutputStream out = new FileOutputStream("c://writerTest.xml");
78 OutputStream out = System.out;
79 // FlatXmlWriter writer = new FlatXmlWriter(new OutputStreamWriter(out, "UTF8"));
80 XmlDataSetWriter writer = new XmlDataSetWriter(new OutputStreamWriter(out, "UTF8"));
81 writer.write(dataSet);
82 */
83
84 // FileWriter writer = new FileWriter("writerTest.xml");
85 // FlatXmlDataSet.write(connection.createDataSet(), writer);
86 // new FlatXmlWriter().write(connection.createDataSet(), writer);
87 // writer.close();
88 // ITableIterator iterator = connection.createDataSet().iterator();
89 // while(iterator.next())
90 // {
91 // System.out.println(iterator.getTableMetaData().getTableName());
92 // }
93 // oldMain();
94 // testWrite();
95 // writeXls();
96 // newSheet();
97 // createCells();
98 // createDateCells();
99 // readWriteWorkbook();
100 // cellTypes();
101 }
102
103 private static void testFlatXmlWriter() throws Exception
104 {
105 MockDataSetProducer mockProducer = new MockDataSetProducer();
106 mockProducer.setupColumnCount(5);
107 mockProducer.setupRowCount(100000);
108 mockProducer.setupTableCount(10);
109 IDataSet dataSet = new StreamingDataSet(mockProducer);
110
111 OutputStream out = new FileOutputStream("flatXmlWriterTest.xml");
112 FlatXmlWriter writer = new FlatXmlWriter(new OutputStreamWriter(out, "UTF8"));
113 writer.write(dataSet);
114 }
115
116 private static void testXmlWriter() throws Exception
117 {
118 MockDataSetProducer mockProducer = new MockDataSetProducer();
119 mockProducer.setupColumnCount(5);
120 mockProducer.setupRowCount(100000);
121 mockProducer.setupTableCount(10);
122 IDataSet dataSet = new StreamingDataSet(mockProducer);
123
124 OutputStream out = new FileOutputStream("xmlWriterTest.xml");
125 XmlDataSetWriter writer = new XmlDataSetWriter(new OutputStreamWriter(out, "UTF8"));
126 writer.write(dataSet);
127 }
128
129 // private static void testWrite() throws Exception
130 // {
131 // Writer out = new databaseFileWriter("test.xml");
132 //
133 // Document document = new Document();
134 // document.write(out);
135 // out.flush();
136 // }
137
138 public void test() throws Exception
139 {
140 Connection jdbcConnection = null;
141 IDatabaseConnection connection = new DatabaseConnection(jdbcConnection, "");
142 DatabaseConfig config = connection.getConfig();
143
144 // Use the ForwardOnlyResultSetTableFactory to export very large dataset.
145 config.setProperty(DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY,
146 new ForwardOnlyResultSetTableFactory());
147
148 // Use the StreamingDataSet to import very large dataset.
149 IDataSetProducer producer = new FlatXmlProducer(
150 new InputSource("dataset.xml"));
151 IDataSet dataSet = new StreamingDataSet(producer);
152 }
153
154
155 private static void oldMain() throws Exception
156 {
157
158 // System.setProperty("dbunit.name.escapePattern", "\"?\"");
159 IDatabaseConnection connection =
160 DatabaseEnvironment.getInstance().getConnection();
161 // IDataSet dataSet = new XmlDataSet(new FileReader("dataSetTest.xml"));
162 // DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
163
164 // String[] tableNames = connection.createDataSet().getTableNames();
165 // Arrays.sort(tableNames);
166 // FlatXmlDataSet.writeDtd(new FilteredDataSet(tableNames,
167 // connection.createDataSet()),
168 // new FileOutputStream("test.dtd"));
169 //
170 //
171 Writer out = new FileWriter("test.xml");
172 // FlatXmlDataSet.write(connection.createDataSet(), out, "ISO-8859-1");
173 FlatXmlDataSet.write(connection.createDataSet(), out);
174 // out.flush();
175 // out.close();
176
177
178 // ////////////////////////////////
179 // Document document = new Document(TestUtils.getFile("xml/flatXmlDataSetTest.xml"));
180 // DocType docType = document.getDocType();
181 // System.out.println(docType);
182 //
183 // // display children of DocType
184 // for (Children decls = docType.getChildren(); decls.hasMoreElements();)
185 // {
186 // Child decl = decls.next();
187 // String type = decl.getClass().getName();
188 // System.out.println("decl = " + decl + ", class: " + type);
189 // }
190
191 // IDataSet dataSet = new FlatXmlDataSet(
192 // new FileInputStream("flatXmlDataSetTest.xml"));
193 // FlatDtdDataSet.write(new FlatXmlDataSet(
194 // TestUtils.getFileInputStream("xml/flatXmlDataSetTest.xml")),
195 // new FileOutputStream("src/dtd/flatXmlDataSetTest.dtd"));
196 }
197
198 private static void writeXls() throws IOException, DataSetException
199 {
200 Reader in = new FileReader(
201 "P:/dbunit-cvs/dbunit/src/xml/dataSetTest.xml");
202 FileOutputStream out = new FileOutputStream(
203 "P:/dbunit-cvs/dbunit/dataSetTest.xls");
204 XlsDataSet.write(new XmlDataSet(in), out);
205 out.close();
206 }
207
208 /*
209 public static void newSheet() throws Exception
210 {
211 HSSFWorkbook wb = new HSSFWorkbook();
212 HSSFSheet sheet1 = wb.createSheet("new sheet");
213 HSSFSheet sheet2 = wb.createSheet("second sheet");
214 FileOutputStream fileOut = new FileOutputStream("workbook.xls");
215 wb.write(fileOut);
216 fileOut.close();
217 }
218
219 public static void readWriteWorkbook() throws Exception
220 {
221 POIFSFileSystem fs =
222 new POIFSFileSystem(new FileInputStream("workbook.xls"));
223 HSSFWorkbook wb = new HSSFWorkbook(fs);
224 HSSFSheet sheet = wb.getSheetAt(0);
225 HSSFRow row = sheet.getRow(2);
226 HSSFCell cell = row.getCell((short)3);
227 if (cell == null)
228 cell = row.createCell((short)3);
229 cell.setCellType(HSSFCell.CELL_TYPE_STRING);
230 cell.setCellValue("a test");
231
232 // Write the output to a file
233 FileOutputStream fileOut = new FileOutputStream("workbook.xls");
234 wb.write(fileOut);
235 fileOut.close();
236 }
237
238 public static void cellTypes() throws Exception
239 {
240 HSSFWorkbook wb = new HSSFWorkbook();
241 HSSFSheet sheet = wb.createSheet("new sheet");
242 HSSFRow row = sheet.createRow((short)2);
243 row.createCell((short) 0).setCellValue(1.1);
244 row.createCell((short) 1).setCellValue(new Date());
245 row.createCell((short) 2).setCellValue("a string");
246 row.createCell((short) 3).setCellValue(true);
247 row.createCell((short) 4).setCellType(HSSFCell.CELL_TYPE_ERROR);
248
249 // Write the output to a file
250 FileOutputStream fileOut = new FileOutputStream("workbook.xls");
251 wb.write(fileOut);
252 fileOut.close();
253 }
254
255 public static void createCells() throws Exception
256 {
257 HSSFWorkbook wb = new HSSFWorkbook();
258 HSSFSheet sheet = wb.createSheet("new sheet");
259
260 // Create a row and put some cells in it. Rows are 0 based.
261 HSSFRow row = sheet.createRow((short)0);
262 // Create a cell and put a value in it.
263 HSSFCell cell = row.createCell((short)0);
264 cell.setCellValue(1);
265
266 // Or do it on one line.
267 row.createCell((short)1).setCellValue(1.2);
268 row.createCell((short)2).setCellValue("This is a string");
269 row.createCell((short)3).setCellValue(true);
270
271 // Write the output to a file
272 FileOutputStream fileOut = new FileOutputStream("workbook.xls");
273 wb.write(fileOut);
274 fileOut.close();
275 }
276
277 public static void createDateCells() throws Exception
278 {
279 HSSFWorkbook wb = new HSSFWorkbook();
280 HSSFSheet sheet = wb.createSheet("new sheet");
281
282 // Create a row and put some cells in it. Rows are 0 based.
283 HSSFRow row = sheet.createRow((short)0);
284
285 // Create a cell and put a date value in it. The first cell is not styled as a date.
286 HSSFCell cell = row.createCell((short)0);
287 cell.setCellValue(new Date());
288
289 // we style the second cell as a date (and time). It is important to create a new cell style from the workbook
290 // otherwise you can end up modifying the built in style and effecting not only this cell but other cells.
291 HSSFCellStyle cellStyle = wb.createCellStyle();
292 cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
293 cell = row.createCell((short)1);
294 cell.setCellValue(new Date());
295 cell.setCellStyle(cellStyle);
296
297 // Write the output to a file
298 FileOutputStream fileOut = new FileOutputStream("workbook.xls");
299 wb.write(fileOut);
300 fileOut.close();
301 }
302 */
303
304 }
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321