1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.oness.common.model.testing;
17
18 import java.io.FileOutputStream;
19 import java.sql.Connection;
20
21 import javax.sql.DataSource;
22
23 import org.dbunit.database.DatabaseConnection;
24 import org.dbunit.database.DatabaseSequenceFilter;
25 import org.dbunit.database.IDatabaseConnection;
26 import org.dbunit.dataset.FilteredDataSet;
27 import org.dbunit.dataset.IDataSet;
28 import org.dbunit.dataset.filter.ITableFilter;
29 import org.dbunit.dataset.xml.FlatDtdDataSet;
30 import org.dbunit.dataset.xml.FlatXmlWriter;
31
32 /***
33 * <p>
34 * Utility class to export database to file dbunit.xml and dbunit.dtd for its
35 * use with dbunit
36 * </p>
37 *
38 * <p>
39 * Uses "dataSource" object defined in Spring application context
40 * </p>
41 *
42 * @author Carlos Sanchez
43 * @version $Revision: 1.5 $
44 */
45 public class SpringDatabaseExport {
46
47 private static final String DATASET_FILE_NAME = "dbunit.xml";
48
49 private static final String DTD_FILE_NAME = "dbunit.dtd";
50
51 public static void main(String[] args) throws Exception {
52
53 DataSource dataSource = (DataSource) SpringApplicationContext
54 .getBean("dataSource");
55
56
57 Connection jdbcConnection = dataSource.getConnection();
58 IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
59
60
61 ITableFilter filter = new DatabaseSequenceFilter(connection);
62 IDataSet fullDataSet = new FilteredDataSet(filter, connection
63 .createDataSet());
64
65 FlatXmlWriter datasetWriter = new FlatXmlWriter(new FileOutputStream(
66 DATASET_FILE_NAME));
67 datasetWriter.setDocType(DTD_FILE_NAME);
68 datasetWriter.write(fullDataSet);
69
70
71 FlatDtdDataSet.write(connection.createDataSet(), new FileOutputStream(
72 DTD_FILE_NAME));
73
74
75 jdbcConnection.close();
76
77 System.out.println("DONE");
78 }
79
80 }