Skip to content

Commit

Permalink
refs #47
Browse files Browse the repository at this point in the history
- adds json support
  • Loading branch information
rmpestano committed Sep 12, 2016
1 parent 63384ad commit a9f2803
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 2 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: *xml*, *yml*, *XLS* and *CSV* formats are supported.
NOTE: *xml*, *yml*, *JSON*, *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
@@ -0,0 +1,129 @@
package com.github.dbunit.rules.dataset.writer;

import org.dbunit.dataset.Column;
import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ITableMetaData;
import org.dbunit.dataset.stream.DataSetProducerAdapter;
import org.dbunit.dataset.stream.IDataSetConsumer;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Created by pestano on 11/09/16.
*/
public class JSONWriter implements IDataSetConsumer {

private static final String NEW_LINE = System.getProperty("line.separator");
private static final String DOUBLE_SPACES = " ";
private static final String FOUR_SPACES = DOUBLE_SPACES+DOUBLE_SPACES;
private static final Logger LOG = Logger.getLogger(JSONWriter.class.getName());

private IDataSet dataSet;

private OutputStreamWriter out;
private ITableMetaData metaData;
private int tableCount;
private int rowCount;

public JSONWriter(OutputStream outputStream, IDataSet dataSet) throws IOException {
out = new OutputStreamWriter(outputStream, "UTF-8");
this.dataSet = dataSet;
}

@Override
public void startDataSet() throws DataSetException {
try {
tableCount = 0;
out.write("{"+NEW_LINE);
} catch (IOException e) {
LOG.log(Level.WARNING, "Could not start dataset.", e);
}
}

@Override
public void endDataSet() throws DataSetException {
try {
out.write("}");
out.flush();
} catch (IOException e) {
LOG.log(Level.WARNING, "Could not end dataset.", e);
}
}

@Override
public void startTable(ITableMetaData metaData) throws DataSetException {
this.metaData = metaData;
rowCount = 0;
try {
out.write(DOUBLE_SPACES+"\""+metaData.getTableName() + "\": [" + NEW_LINE);
} catch (IOException e) {
LOG.log(Level.WARNING, "Could not start table.", e);
}
}

@Override
public void endTable() throws DataSetException {
try {
tableCount++;
if(dataSet.getTableNames().length == tableCount){
out.write(DOUBLE_SPACES+"]"+NEW_LINE);
}else{
out.write(DOUBLE_SPACES+"],"+NEW_LINE);
}
} catch (IOException e) {
LOG.log(Level.WARNING, "Could end table.", e);
}
}

@Override
public void row(Object[] values) throws DataSetException {
rowCount++;
try {
out.write(FOUR_SPACES+"{"+NEW_LINE);
for (int i = 0; i < values.length; i++) {
if(values[i] == null){
continue;
}

Column currentColumn = metaData.getColumns()[i];
out.write(FOUR_SPACES+DOUBLE_SPACES+"\""+metaData.getColumns()[i].getColumnName() + "\": ");
boolean isNumber = currentColumn.getDataType().isNumber();
if (!isNumber) {
out.write("\"");
}

out.write(values[i].toString());

if (!isNumber) {
out.write("\"");
}
if(i != values.length-1){
out.write(",");
}
out.write(NEW_LINE);

}
if(dataSet.getTable(metaData.getTableName()).getRowCount() != rowCount ){
out.write(FOUR_SPACES+"},"+NEW_LINE);
}else {
out.write(FOUR_SPACES+"}"+NEW_LINE);

}


} catch (Exception e) {
LOG.log(Level.WARNING, "Could not write row.", e);
}
}

public synchronized void write() throws DataSetException {
DataSetProducerAdapter provider = new DataSetProducerAdapter(dataSet);
provider.setConsumer(this);
provider.produce();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.dbunit.rules.exporter;

import com.github.dbunit.rules.api.expoter.DataSetExportConfig;
import com.github.dbunit.rules.dataset.writer.JSONWriter;
import com.github.dbunit.rules.dataset.writer.YMLWriter;
import org.dbunit.DatabaseUnitException;
import org.dbunit.database.*;
Expand Down Expand Up @@ -132,6 +133,11 @@ public OutputStream export(DatabaseConnection databaseConnection, DataSetExportC
CsvDataSetWriter.write(dataSet, new File(outputFile));
break;
}
case JSON: {
config.setProperty(DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY, new CachedResultSetTableFactory());
new JSONWriter(fos,dataSet).write();
break;
}
default: {
throw new RuntimeException("Format not supported.");
}
Expand Down
13 changes: 12 additions & 1 deletion core/src/test/java/com/github/dbunit/rules/ExportDataSetIt.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public void shouldExportAllTablesInXLSFormat() {
@ExportDataSet(format = DataSetFormat.CSV,outputName="target/exported/csv/allTables.csv")
public void shouldExportAllTablesInCSVFormat() {
}

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

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

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

File jsonDataSetWithAllTables = new File("target/exported/json/allTables.json");
//TODO validate generated content
assertThat(jsonDataSetWithAllTables).exists();


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

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

Expand Down

0 comments on commit a9f2803

Please sign in to comment.