めも

技術メモとその他

dbunit + postgresql


package homeWorkTest;

import static org.junit.Assert.*;

import org.junit.Test;
import java.io.FileOutputStream;
import org.dbunit.JdbcDatabaseTester;
import org.dbunit.database.DatabaseConfig;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.database.QueryDataSet;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.excel.XlsDataSet;
import org.dbunit.ext.postgresql.PostgresqlDataTypeFactory;
//import org.dbunit.dataset.xml.FlatXmlDataSet;



public class dbDmpSample {

@Test
public void test() throws Exception {
//fail("Not yet implemented");
JdbcDatabaseTester tester = new JdbcDatabaseTester(
"org.postgresql.Driver",
"jdbc:postgresql://localhost:5432/prototype", "tk", "");
IDatabaseConnection connection = tester.getConnection();
connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
new PostgresqlDataTypeFactory());



QueryDataSet ds = new QueryDataSet(connection);
FileOutputStream stream = new FileOutputStream("/Users/tk/foo_selects.xls");

ds.addTable("foo", "select memo from foo where id = 2");
XlsDataSet.write(ds, stream);

//below are table
//String[] tableNamesToDump = new String[] { "foo" };
//IDataSet target = connection.createDataSet(tableNamesToDump);
//FileOutputStream stream = new FileOutputStream("/Users/tk/foo_select.xls");
//XlsDataSet.write(target, stream);

}

}

it needs some customized class

https://sourceforge.net/p/dbunit/feature-requests/188/

and add jsonb type it like

public DataType createDataType(int sqlType, String sqlTypeName) throws DataTypeException {

if ("json".equals(sqlTypeName) ||
"jsonb".equals(sqlTypeName) ) {//jsonb type added
return new JsonType()
}

}

https://qiita.com/crazyNasubi/items/3ae54248c8dc70ab15d9