Skip to content

Commit

Permalink
refs #47
Browse files Browse the repository at this point in the history
- adjusted windows tests
- adds csv and xls support
  • Loading branch information
rmpestano committed Sep 12, 2016
1 parent f22b6db commit 63384ad
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,7 @@ In order to export database state *after test* execution into datasets files one

After execution of above test, all tables will be exported to a xml dataset.

NOTE: For now only *xml* and *yml* formats are supported.
NOTE: *xml*, *yml*, *XLS* and *CSV* formats are supported.

TIP: Full example above (and other related tests) can be https://github.com/rmpestano/dbunit-rules/blob/master/core/src/test/java/com/github/dbunit/rules/ExportDataSetIt.java#L35[found here^].

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
*/
public enum DataSetFormat {

YML, JSON, XML, XLS
YML, JSON, XML, XLS, CSV
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.dbunit.database.*;
import org.dbunit.database.search.TablesDependencyHelper;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.csv.CsvDataSetWriter;
import org.dbunit.dataset.excel.XlsDataSetWriter;
import org.dbunit.dataset.xml.FlatXmlDataSet;

import java.io.File;
Expand Down Expand Up @@ -71,6 +73,10 @@ public OutputStream export(DatabaseConnection databaseConnection, DataSetExportC
if(!outputFile.contains(".")){
outputFile = outputFile +"."+dataSetExportConfig.getDataSetFormat().name().toLowerCase();
}

if(outputFile.contains("/") && System.getProperty("os.name").toLowerCase().contains("win")){
outputFile = outputFile.replace("/", "\\");
}

boolean hasIncludes = dataSetExportConfig.getIncludeTables() != null && dataSetExportConfig.getIncludeTables().length > 0;

Expand Down Expand Up @@ -104,25 +110,36 @@ public OutputStream export(DatabaseConnection databaseConnection, DataSetExportC
if(outputFile.contains(System.getProperty("file.separator"))){
String pathWithoutFileName = outputFile.substring(0,outputFile.lastIndexOf(System.getProperty("file.separator"))+1);
new File(pathWithoutFileName).mkdirs();

}
fos = new FileOutputStream(outputFile);
switch (dataSetExportConfig.getDataSetFormat()) {
case XML: {
FlatXmlDataSet.write(dataSet, fos);
log.info("DataSet exported successfully at "+ Paths.get(outputFile).toAbsolutePath().toString());
break;
}
case YML: {
new YMLWriter(fos).write(dataSet);
log.info("DataSet exported successfully at "+ Paths.get(outputFile).toAbsolutePath().toString());
break;
}
case XLS: {
config.setProperty(DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY, new CachedResultSetTableFactory());
new XlsDataSetWriter().write(dataSet, fos);
break;
}
case CSV: {
//csv needs a directory instead of file
outputFile = outputFile.substring(0,outputFile.lastIndexOf("."));
CsvDataSetWriter.write(dataSet, new File(outputFile));
break;
}
default: {
throw new RuntimeException("Format not supported.");
}

}

log.info("DataSet exported successfully at "+ Paths.get(outputFile).toAbsolutePath().toString());

} catch (Exception e) {
log.log(Level.SEVERE, "Could not export dataset.", e);
throw new RuntimeException("Could not export dataset.", e);
Expand Down
29 changes: 29 additions & 0 deletions core/src/test/java/com/github/dbunit/rules/ExportDataSetIt.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ public void shouldExportAllTablesInYMLFormatWithoutDataSetAnnotation() {
DataSetExecutorImpl.getExecutorById(DataSetExecutorImpl.DEFAULT_EXECUTOR_ID)
.createDataSet(new DataSetConfig("datasets/yml/users.yml"));
}

@Test
@DataSet("datasets/yml/users.yml")
@ExportDataSet(format = DataSetFormat.XLS,outputName="target/exported/xls/allTables.xls")
public void shouldExportAllTablesInXLSFormat() {
}

@Test
@DataSet("datasets/yml/users.yml")
@ExportDataSet(format = DataSetFormat.CSV,outputName="target/exported/csv/allTables.csv")
public void shouldExportAllTablesInCSVFormat() {
}

@Test
@DataSet("datasets/yml/users.yml")
Expand Down Expand Up @@ -135,6 +147,21 @@ public static void assertGeneratedDataSets(){
" - ID: 2"+NEW_LINE +
" NAME: \"@dbunit\"");

File xlsDataSetWithAllTables = new File("target/exported/xls/allTables.xls");
assertThat(xlsDataSetWithAllTables).exists();


File csvDataSetWithAllTables = new File("target/exported/csv/allTables");
assertThat(csvDataSetWithAllTables).exists();

File userCsvDataSet = new File("target/exported/csv/allTables/USER.csv");
assertThat(userCsvDataSet).exists();

File tweetCsvDataSet = new File("target/exported/csv/allTables/TWEET.csv");
assertThat(tweetCsvDataSet).exists();

File followerCsvDataSet = new File("target/exported/csv/allTables/FOLLOWER.csv");
assertThat(followerCsvDataSet).exists();

File xmlFilteredDataSet = new File("target/exported/xml/filtered.xml");
assertThat(xmlFilteredDataSet).exists();
Expand All @@ -149,6 +176,8 @@ public static void assertGeneratedDataSets(){
" NAME: \"@realpestano\"");




File xmlFilteredWithIncludesDataSet = new File("target/exported/xml/filteredIncludes.xml");
assertThat(xmlFilteredWithIncludesDataSet).exists();
assertThat(contentOf(xmlFilteredWithIncludesDataSet)).contains("<USER ID=\"1\" NAME=\"@realpestano\"/>");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.github.dbunit.rules.api.configuration.DBUnit;
import com.github.dbunit.rules.api.dataset.DataSet;
import com.github.dbunit.rules.api.dataset.SeedStrategy;

import org.apache.xmlbeans.impl.common.IOUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -41,7 +43,13 @@ public void shouldLoadDBUnitConfigViaGlobalFile(){

@Test
public void shouldLoadDBUnitConfigViaCustomGlobalFile() throws IOException {
File customConfig = new File("target/test-classes/dbunit.yml");
File backupConfig = new File("target/test-classes/dbunit-backup.yml");
File customConfig = new File("target/test-classes/dbunit.yml");
FileOutputStream backupStream = new FileOutputStream(backupConfig);
backupStream.write(Files.readAllBytes(Paths.get(getClass().getResource("/default/dbunit.yml").getPath().replaceFirst("^/(.:/)", "$1"))));

backupStream.flush();
backupStream.close();
FileOutputStream fos = new FileOutputStream(customConfig);
fos.write(Files.readAllBytes(Paths.get(getClass().getResource("/config/sample-dbunit.yml").getPath().replaceFirst("^/(.:/)", "$1"))));
fos.flush();
Expand All @@ -60,7 +68,11 @@ public void shouldLoadDBUnitConfigViaCustomGlobalFile() throws IOException {
containsEntry("fetchSize",200).
containsEntry("escapePattern","[?]");

customConfig.delete();
FileOutputStream originalStream = new FileOutputStream(customConfig);
originalStream.write(Files.readAllBytes(Paths.get(backupConfig.toURI())));
originalStream.flush();
originalStream.close();

}


Expand Down

0 comments on commit 63384ad

Please sign in to comment.