My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.ctp.test.db;

import java.io.IOException;
import java.net.URL;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import org.dbunit.DatabaseUnitException;
import org.dbunit.database.DatabaseConfig;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.datatype.DefaultDataTypeFactory;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.dbunit.operation.DatabaseOperation;

import com.ctp.test.configuration.DatabaseConfiguration;

/**
*
* @author Bartosz Majsak
*
*/
public class XmlDatasetSeeder implements DataSeeder {

private DatabaseConnection databaseConnection;

private final DatabaseConfiguration dbConfig;

private final String xmlFile;

public XmlDatasetSeeder(String xmlFile, DatabaseConfiguration dbConfig) {
this.dbConfig = dbConfig;
this.xmlFile = xmlFile;
}

@Override
public void prepare() {
try {
setupDatabase();
applyInitStatement();
fillDatabase();
} catch (Exception e) {
throw new RuntimeException(e);
}

}

@Override
public void cleanup() {
try {
setupDatabase();
cleanDatabase();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private void applyInitStatement() {
try {
Statement initStatement = databaseConnection.getConnection().createStatement();
initStatement.execute(dbConfig.getInitStatement());
} catch (SQLException e) {
throw new RuntimeException(e);
}

}

private void setupDatabase()
throws IOException, SQLException, DatabaseUnitException {
if (null != databaseConnection) {
return;
}
DatabaseConnection con = new DatabaseConnection(DriverManager.getConnection(dbConfig.getUrl(), dbConfig.getUsername(), dbConfig.getPassword()));
// used to avoid problems with boolean
con.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new DefaultDataTypeFactory());
databaseConnection = con;
}

private void fillDatabase() throws IOException, SQLException, DatabaseUnitException {
FlatXmlDataSetBuilder flatXmlDataSetBuilder = new FlatXmlDataSetBuilder();
URL xmlUrl = getClass().getClassLoader().getResource(xmlFile);
FlatXmlDataSet fx = flatXmlDataSetBuilder.build(xmlUrl);
DatabaseOperation.CLEAN_INSERT.execute(databaseConnection, fx);
}

private void cleanDatabase() throws DatabaseUnitException, SQLException {
IDataSet dataSet = databaseConnection.createDataSet();
DatabaseOperation.DELETE_ALL.execute(databaseConnection, dataSet);
}

}

Change log

r83 by bartosz.majsak on Jul 8, 2010   Diff
Example project for arquillian blog post -
initial version.
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 2992 bytes, 93 lines
Powered by Google Project Hosting