Skip to content

Commit

Permalink
refs #47
Browse files Browse the repository at this point in the history
- removed sql logs
- updated readme
  • Loading branch information
rmpestano committed Sep 12, 2016
1 parent a9f2803 commit 6f7fb45
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 23 deletions.
31 changes: 31 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,37 @@ 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.


[TIP]
====
You don't need `@ExportDataSet`, you can use DataSetExporter component programmatically:
[source,java,linenums]
----
@Test
@DataSet(cleanBefore=true)
public void shouldExportYMLDataSetWithoutAnnotations() throws SQLException, DatabaseUnitException{
tx().begin();
User u1 = new User();
u1.setName("u1");
em().persist(u1);
tx().commit();
DataSetExporterImpl.getInstance().export(new DatabaseConnection(emProvider.connection()), <1>
new DataSetExportConfig().outputFileName("target/user.yml"));
File ymlDataSet = new File("target/user.yml");
assertThat(ymlDataSet).exists();
assertThat(contentOf(ymlDataSet)).
contains("USER:"+NEW_LINE +
" - ID: 1"+NEW_LINE +
" NAME: \"u1\""+NEW_LINE
);
}
----
<1> DatabaseConnection is from DBUnit api and it needs a JDBC connection.
====

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
Expand Up @@ -124,8 +124,9 @@ public void exportDataSet(Method method) {
if(outputName == null || "".equals(outputName.trim())){
outputName = method.getName().toLowerCase()+"."+exportConfig.getDataSetFormat().name().toLowerCase();
}
exportConfig.outputFileName(outputName);
try {
DataSetExporterImpl.getInstance().export(dataSetExecutor.getDBUnitConnection(),exportConfig ,outputName);
DataSetExporterImpl.getInstance().export(dataSetExecutor.getDBUnitConnection(),exportConfig);
} catch (Exception e) {
java.util.logging.Logger.getLogger(getClass().getName()).log(Level.WARNING,"Could not export dataset after method "+method.getName(),e);
}
Expand Down
4 changes: 2 additions & 2 deletions cdi/src/test/resources/META-INF/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.logging.level" value="INFO"/>
<property name="eclipselink.logging.level.sql" value="FINE"/>
<!-- <property name="eclipselink.logging.level" value="INFO"/>
<property name="eclipselink.logging.level.sql" value="FINE"/> -->
<property name="eclipselink.logging.parameters" value="false"/>
</properties>

Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/com/github/dbunit/rules/DBUnitRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,9 @@ private void exportDataSet(DataSetExecutor executor, Description description) {
if(outputName == null || "".equals(outputName.trim())){
outputName = description.getMethodName().toLowerCase()+"."+exportConfig.getDataSetFormat().name().toLowerCase();
}
exportConfig.outputFileName(outputName);
try {
DataSetExporterImpl.getInstance().export(executor.getDBUnitConnection(),exportConfig ,outputName);
DataSetExporterImpl.getInstance().export(executor.getDBUnitConnection(),exportConfig);
} catch (Exception e) {
Logger.getLogger(getClass().getName()).log(Level.WARNING,"Could not export dataset after method "+description.getMethodName(),e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static DataSetExporterImpl getInstance(){
return instance;
}

public OutputStream export(DatabaseConnection databaseConnection, DataSetExportConfig dataSetExportConfig, String outputFile) throws SQLException, DatabaseUnitException {
public OutputStream export(DatabaseConnection databaseConnection, DataSetExportConfig dataSetExportConfig) throws SQLException, DatabaseUnitException {

if (databaseConnection == null || databaseConnection.getConnection() == null || databaseConnection.getConnection().isClosed()) {
throw new RuntimeException("Provide a valid connection to export datasets");
Expand All @@ -66,11 +66,13 @@ public OutputStream export(DatabaseConnection databaseConnection, DataSetExportC
if (dataSetExportConfig == null) {
dataSetExportConfig = new DataSetExportConfig();
}

String outputFile = dataSetExportConfig.getOutputFileName();

if(outputFile == null || "".equals(outputFile)){
throw new RuntimeException("Provide output file name to export dataset.");
}

if(!outputFile.contains(".")){
outputFile = outputFile +"."+dataSetExportConfig.getDataSetFormat().name().toLowerCase();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
package com.github.dbunit.rules;
package com.github.dbunit.rules.exporter;

import com.github.dbunit.rules.DBUnitRule;
import com.github.dbunit.rules.api.dataset.DataSet;
import com.github.dbunit.rules.api.dataset.DataSetFormat;
import com.github.dbunit.rules.api.expoter.DataSetExportConfig;
import com.github.dbunit.rules.api.expoter.ExportDataSet;
import com.github.dbunit.rules.configuration.DataSetConfig;
import com.github.dbunit.rules.dataset.DataSetExecutorImpl;
import com.github.dbunit.rules.exporter.DataSetExporterImpl;
import com.github.dbunit.rules.model.User;
import com.github.dbunit.rules.util.EntityManagerProvider;

import org.dbunit.DatabaseUnitException;
import org.dbunit.database.DatabaseConnection;
import org.junit.AfterClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.io.File;
import java.sql.SQLException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.contentOf;
import static com.github.dbunit.rules.util.EntityManagerProvider.*;

/**
* Created by pestano on 11/09/16.
Expand Down Expand Up @@ -58,6 +67,25 @@ public void shouldExportAllTablesInYMLFormatWithoutDataSetAnnotation() {
public void shouldExportAllTablesInXLSFormat() {
}

@Test
@DataSet(cleanBefore=true)
public void shouldExportYMLDataSetWithoutAnnotations() throws SQLException, DatabaseUnitException{
tx().begin();
User u1 = new User();
u1.setName("u1");
em().persist(u1);
tx().commit();
DataSetExporterImpl.getInstance().export(new DatabaseConnection(emProvider.connection()), new DataSetExportConfig().outputFileName("target/user.yml"));
File ymlDataSet = new File("target/user.yml");
assertThat(ymlDataSet).exists();
assertThat(contentOf(ymlDataSet)).
contains("USER:"+NEW_LINE +
" - ID: 1"+NEW_LINE +
" NAME: \"u1\""+NEW_LINE
);

}

@Test
@DataSet("datasets/yml/users.yml")
@ExportDataSet(format = DataSetFormat.CSV,outputName="target/exported/csv/allTables.csv")
Expand Down
24 changes: 12 additions & 12 deletions core/src/test/resources/META-INF/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.logging.level" value="INFO"/>
<property name="eclipselink.logging.level.sql" value="FINE"/>
<!-- <property name="eclipselink.logging.level" value="WARN"/>
<property name="eclipselink.logging.level.sql" value="FINE"/> -->
<property name="eclipselink.logging.parameters" value="false"/>
</properties>

Expand All @@ -32,8 +32,8 @@
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.logging.level" value="INFO"/>
<property name="eclipselink.logging.level.sql" value="FINE"/>
<!-- <property name="eclipselink.logging.level" value="INFO"/>
<property name="eclipselink.logging.level.sql" value="FINE"/> -->
<property name="eclipselink.logging.parameters" value="false"/>
</properties>

Expand All @@ -51,8 +51,8 @@
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.logging.level" value="INFO"/>
<property name="eclipselink.logging.level.sql" value="FINE"/>
<!-- <property name="eclipselink.logging.level" value="INFO"/>
<property name="eclipselink.logging.level.sql" value="FINE"/> -->
<property name="eclipselink.logging.parameters" value="false"/>
</properties>

Expand All @@ -70,8 +70,8 @@
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.logging.level" value="INFO"/>
<property name="eclipselink.logging.level.sql" value="FINE"/>
<!-- <property name="eclipselink.logging.level" value="INFO"/>
<property name="eclipselink.logging.level.sql" value="FINE"/> -->
<property name="eclipselink.logging.parameters" value="false"/>
</properties>

Expand All @@ -89,8 +89,8 @@
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.logging.level" value="INFO"/>
<property name="eclipselink.logging.level.sql" value="FINE"/>
<!-- <property name="eclipselink.logging.level" value="INFO"/>
<property name="eclipselink.logging.level.sql" value="FINE"/> -->
<property name="eclipselink.logging.parameters" value="false"/>
</properties>

Expand All @@ -108,8 +108,8 @@
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="eclipselink.logging.level" value="INFO"/>
<property name="eclipselink.logging.level.sql" value="FINE"/>
<!-- <property name="eclipselink.logging.level" value="INFO"/>
<property name="eclipselink.logging.level.sql" value="FINE"/> -->
<property name="eclipselink.logging.parameters" value="false"/>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</appender>

<root>
<priority value="debug"/>
<priority value="error"/>
<appender-ref ref="stdout"/>
</root>
</log4j:configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.show_sql" value="false"/>

</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ public void exportDataSet(DataSetExecutor dataSetExecutor, Method method) {
if(outputName == null || "".equals(outputName.trim())){
outputName = method.getName().toLowerCase()+"."+exportConfig.getDataSetFormat().name().toLowerCase();
}
exportConfig.outputFileName(outputName);
try {
DataSetExporterImpl.getInstance().export(dataSetExecutor.getDBUnitConnection(),exportConfig ,outputName);
DataSetExporterImpl.getInstance().export(dataSetExecutor.getDBUnitConnection(),exportConfig);
} catch (Exception e) {
java.util.logging.Logger.getLogger(getClass().getName()).log(Level.WARNING,"Could not export dataset after method "+method.getName(),e);
}
Expand Down
2 changes: 1 addition & 1 deletion junit5/src/test/resources/META-INF/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.show_sql" value="false"/>
</properties>

</persistence-unit>
Expand Down

0 comments on commit 6f7fb45

Please sign in to comment.