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
package org.gbif.checklistbank.dbunit;

import org.gbif.checklistbank.service.mybatis.DatabaseDrivenChecklistBankTestRule;

import java.io.File;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Map;

import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.DatabaseSequenceFilter;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.database.QueryDataSet;
import org.dbunit.dataset.FilteredDataSet;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.filter.ITableFilter;
import org.dbunit.dataset.xml.FlatXmlDataSet;

/**
* A simple dbunit export script to generate dbunit xml files for IT tests.
* This script requires an existing checklistbank database with an allready imported checklist.
* You can generate one by using the CLB commandline tool and the squirrel test dwc archive found in the test
* resources.
* <p/>
* The generator creates two files. The main full db dump plus another one for the name_usage table alone
* which needs to be sorted specifically to not break the relational integrity when reading in.
* Please manually replace the name_usage entries in the main file with this sorted name_usage dump.
*/
public class TestFileGenerator {

public static void main(String[] args) throws Exception {
// database connection
Class<?> driverClass = Class.forName("org.postgresql.Driver");
// replace with your local db to be exported
Connection jdbcConnection = DriverManager.getConnection("jdbc:postgresql://localhost/clb-test", "postgres", "");
IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
for (Map.Entry<String, Object> prop : DatabaseDrivenChecklistBankTestRule.DB_UNIT_CLB_PROPERTIES.entrySet()) {
connection.getConfig().setProperty(prop.getKey(), prop.getValue());
}

// full database export, automatically ordering tables by foreign key constraints
ITableFilter filter = new DatabaseSequenceFilter(connection);
IDataSet fullDataSet = new FilteredDataSet(filter, connection.createDataSet());
File outf = new File("checklistbank-mybatis-service/src/test/resources/dbunit/squirrels-full.xml");
FlatXmlDataSet.write(fullDataSet, new FileOutputStream(outf));

// partial database export
QueryDataSet partialDataSet = new QueryDataSet(connection);
partialDataSet.addTable("name_usage", "SELECT * FROM name_usage order by lft nulls last");
FlatXmlDataSet.write(partialDataSet,
new FileOutputStream("checklistbank-mybatis-service/src/test/resources/dbunit/name_usage.xml"));

}
}

Change log

r4510 by wixner on Nov 2, 2011   Diff
Renaming DatabaseDrivenChecklistBankTest
to DatabaseDrivenChecklistBankTestRule to
not confuse jenkins and updating the
checklist usage mapper & service test to
order by rank and then only by scientific
name
Go to: 
Project members, sign in to write a code review

Older revisions

r4424 by lars.francke on Oct 25, 2011   Diff
Code formatting.
r4307 by wixner on Oct 12, 2011   Diff
fix generics
r4258 by wixner on Oct 10, 2011   Diff
remove passwords but keep simple jdbc
connection in code as its a rarely
executed manual script
All revisions of this file

File info

Size: 2611 bytes, 55 lines
Powered by Google Project Hosting