Skip to content

Commit

Permalink
refs #47
Browse files Browse the repository at this point in the history
- adds exportDataSet to CDI and JUnit5 modules
  • Loading branch information
rmpestano committed Sep 12, 2016
1 parent 80fdad8 commit 0f8f5ef
Show file tree
Hide file tree
Showing 8 changed files with 427 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,55 +54,68 @@ public Object intercept(InvocationContext invocationContext)
if(isTransactionalTest){
em.getTransaction().begin();
}
LeakHunter leakHunter = null;
boolean leakHunterActivated = dbUnitConfig.isLeakHunter();
int openConnectionsBefore = 0;
try {
LeakHunter leakHunter = null;
boolean leakHunterActivated = dbUnitConfig.isLeakHunter();
int openConnectionsBefore = 0;
if (leakHunterActivated) {
leakHunter = LeakHunterFactory.from(dataSetProcessor.getConnection());
openConnectionsBefore = leakHunter.openConnections();
}
proceed = invocationContext.proceed();

int openConnectionsAfter = 0;
if(leakHunterActivated){
openConnectionsAfter = leakHunter.openConnections();
if(openConnectionsAfter > openConnectionsBefore){
throw new LeakHunterException(invocationContext.getMethod().getName(),openConnectionsAfter - openConnectionsBefore);
}
}
if(isTransactionalTest){
em.getTransaction().commit();
}
ExpectedDataSet expectedDataSet = invocationContext.getMethod().getAnnotation(ExpectedDataSet.class);
if(expectedDataSet != null){
dataSetProcessor.compareCurrentDataSetWith(new DataSetConfig(expectedDataSet.value()).disableConstraints(true),expectedDataSet.ignoreCols());
}

}catch (Exception e){
if(isTransactionalTest){
if(isTransactionalTest && em.getTransaction().isActive()){
em.getTransaction().rollback();
}
throw e;
}
ExpectedDataSet expectedDataSet = invocationContext.getMethod().getAnnotation(ExpectedDataSet.class);
if(expectedDataSet != null){
dataSetProcessor.compareCurrentDataSetWith(new DataSetConfig(expectedDataSet.value()).disableConstraints(true),expectedDataSet.ignoreCols());
}
if(usingDataSet.cleanAfter()){
dataSetProcessor.clearDatabase(dataSetConfig);
}
} finally {
int openConnectionsAfter = 0;
if(leakHunter != null){
openConnectionsAfter = leakHunter.openConnections();
if(openConnectionsAfter > openConnectionsBefore){
throw new LeakHunterException(invocationContext.getMethod().getName(),openConnectionsAfter - openConnectionsBefore);
}
}

if (!"".equals(usingDataSet.executeStatementsAfter())) {
dataSetProcessor.executeStatements(dataSetConfig.getExecuteStatementsAfter());
}
dataSetProcessor.exportDataSet(invocationContext.getMethod());

if(usingDataSet.executeScriptsAfter().length > 0 && !"".equals(usingDataSet.executeScriptsAfter()[0])){
for (int i = 0; i < usingDataSet.executeScriptsAfter().length; i++) {
dataSetProcessor.executeScript(usingDataSet.executeScriptsAfter()[i]);
if(usingDataSet.cleanAfter()){
dataSetProcessor.clearDatabase(dataSetConfig);
}
}

if (!"".equals(usingDataSet.executeStatementsAfter())) {
dataSetProcessor.executeStatements(dataSetConfig.getExecuteStatementsAfter());
}

if(usingDataSet.executeScriptsAfter().length > 0 && !"".equals(usingDataSet.executeScriptsAfter()[0])){
for (int i = 0; i < usingDataSet.executeScriptsAfter().length; i++) {
dataSetProcessor.executeScript(usingDataSet.executeScriptsAfter()[i]);
}
}
}//end finally



} else{//no dataset provided, just proceed and check expectedDataSet
proceed = invocationContext.proceed();
ExpectedDataSet expectedDataSet = invocationContext.getMethod().getAnnotation(ExpectedDataSet.class);
if(expectedDataSet != null){
dataSetProcessor.compareCurrentDataSetWith(new DataSetConfig(expectedDataSet.value()).disableConstraints(true),expectedDataSet.ignoreCols());
try {
proceed = invocationContext.proceed();
ExpectedDataSet expectedDataSet = invocationContext.getMethod().getAnnotation(ExpectedDataSet.class);
if(expectedDataSet != null){
dataSetProcessor.compareCurrentDataSetWith(new DataSetConfig(expectedDataSet.value()).disableConstraints(true),expectedDataSet.ignoreCols());
}
}finally {
dataSetProcessor.exportDataSet(invocationContext.getMethod());
}

}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.github.dbunit.rules.cdi;

import com.github.dbunit.rules.api.dataset.DataSetExecutor;
import com.github.dbunit.rules.api.expoter.DataSetExportConfig;
import com.github.dbunit.rules.api.expoter.ExportDataSet;
import com.github.dbunit.rules.configuration.DBUnitConfig;
import com.github.dbunit.rules.configuration.DataSetConfig;
import com.github.dbunit.rules.connection.ConnectionHolderImpl;
import com.github.dbunit.rules.dataset.DataSetExecutorImpl;
import com.github.dbunit.rules.exporter.DataSetExporterImpl;
import org.dbunit.DatabaseUnitException;
import org.hibernate.Session;
import org.hibernate.internal.SessionImpl;
import org.junit.runner.Description;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -16,16 +20,18 @@
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;

/**
* Created by rafael-pestano on 08/10/2015.
*/
@RequestScoped
public class DataSetProcessor {

private static final String CDI_DBUNIT_EXECUTOR = "CDI_DBUNIT_EXECUTOR";
public static final String CDI_DBUNIT_EXECUTOR = "CDI_DBUNIT_EXECUTOR";

private static final Logger log = LoggerFactory.getLogger(DataSetProcessor.class.getName());

Expand Down Expand Up @@ -109,4 +115,28 @@ public void compareCurrentDataSetWith(DataSetConfig expected, String[] excludeCo
public Connection getConnection() {
return connection;
}

public void exportDataSet(Method method) {
ExportDataSet exportDataSet = resolveExportDataSet(method);
if(exportDataSet != null){
DataSetExportConfig exportConfig = DataSetExportConfig.from(exportDataSet);
String outputName = exportConfig.getOutputFileName();
if(outputName == null || "".equals(outputName.trim())){
outputName = method.getName().toLowerCase()+"."+exportConfig.getDataSetFormat().name().toLowerCase();
}
try {
DataSetExporterImpl.getInstance().export(dataSetExecutor.getDBUnitConnection(),exportConfig ,outputName);
} catch (Exception e) {
java.util.logging.Logger.getLogger(getClass().getName()).log(Level.WARNING,"Could not export dataset after method "+method.getName(),e);
}
}
}

private ExportDataSet resolveExportDataSet(Method method) {
ExportDataSet exportDataSet = method.getAnnotation(ExportDataSet.class);
if (exportDataSet == null) {
exportDataSet = method.getDeclaringClass().getAnnotation(ExportDataSet.class);
}
return exportDataSet;
}
}
229 changes: 229 additions & 0 deletions cdi/src/test/java/com/github/dbunit/rules/cdi/ExportDataSetCDIIt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
package com.github.dbunit.rules.cdi;

import com.github.dbunit.rules.api.dataset.DataSet;
import com.github.dbunit.rules.api.dataset.DataSetFormat;
import com.github.dbunit.rules.api.expoter.ExportDataSet;
import com.github.dbunit.rules.cdi.api.DBUnitInterceptor;
import com.github.dbunit.rules.configuration.DataSetConfig;
import com.github.dbunit.rules.dataset.DataSetExecutorImpl;
import com.github.dbunit.rules.model.User;
import org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner;
import org.junit.AfterClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import java.io.File;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.contentOf;

/**
* Created by pestano on 23/07/15.
*/

@RunWith(CdiTestRunner.class)
@DBUnitInterceptor
public class ExportDataSetCDIIt {

private static final String NEW_LINE = System.getProperty("line.separator");


@Inject
EntityManager em;


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

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

@Test
@ExportDataSet(outputName="target/exported/yml/generatedWithoutDataSetAnnotation")
public void shouldExportAllTablesInYMLFormatWithoutDataSetAnnotation() {
//seed database
DataSetExecutorImpl.getExecutorById(DataSetProcessor.CDI_DBUNIT_EXECUTOR)
.createDataSet(new DataSetConfig("datasets/yml/users.yml"));
}

@Test
@DataSet("datasets/yml/users.yml")
@ExportDataSet(format = DataSetFormat.XML, queryList = {"select * from USER u where u.ID = 1"}, outputName="target/exported/xml/filtered.xml")
public void shouldExportXMLDataSetUsingQueryToFilterRows() {

}

@Test
@DataSet("datasets/yml/users.yml")
@ExportDataSet(format = DataSetFormat.YML, queryList = {"select * from USER u where u.ID = 1"}, outputName="target/exported/yml/filtered.yml")
public void shouldExportYMLDataSetUsingQueryToFilterRows() {

}

@Test
@DataSet("datasets/yml/users.yml")
@ExportDataSet(format = DataSetFormat.XML, queryList = {"select * from USER u where u.ID = 1"}, includeTables = {"TWEET"}, outputName="target/exported/xml/filteredIncludes.xml")
public void shouldExportXMLDataSetUsingQueryAndIncludesToFilterRows() {

}

@Test
@DataSet("datasets/yml/users.yml")
@ExportDataSet(format = DataSetFormat.YML, queryList = {"select * from USER u where u.ID = 1"}, includeTables = "TWEET", outputName="target/exported/yml/filteredIncludes.yml")
public void shouldExportYMLDataSetUsingQueryAndIncludesToFilterRows() {

}

@Test
@DataSet("datasets/yml/users.yml")
@ExportDataSet(format = DataSetFormat.XML, includeTables = "USER", outputName="target/exported/xml/includes.xml")
public void shouldExportXMLDataSetWithTablesInIncludes() {

}

@Test
@DataSet("datasets/yml/users.yml")
@ExportDataSet(format = DataSetFormat.YML, includeTables = "USER", outputName="target/exported/yml/includes.yml")
public void shouldExportYMLDataSetWithTablesInIncludes() {

}

@Test
@DataSet("datasets/yml/users.yml")
@ExportDataSet(format = DataSetFormat.XML, includeTables = "USER", dependentTables = true, outputName="target/exported/xml/dependentTables.xml")
public void shouldExportXMLDataSetUsingIncludesWithDependentTables() {

}


@Test
@DataSet("datasets/yml/users.yml")
@ExportDataSet(format = DataSetFormat.YML, includeTables = {"USER","TWEET"}, dependentTables = true, outputName="target/exported/yml/dependentTables.yml")
public void shouldExportYMLDataSetUsingIncludesWithDependentTables() {

}


@AfterClass
public static void assertGeneratedDataSets(){
File xmlDataSetWithAllTables = new File("target/exported/xml/allTables.xml");
assertThat(xmlDataSetWithAllTables).exists();
assertThat(contentOf(xmlDataSetWithAllTables)).contains("<USER ID=\"1\" NAME=\"@realpestano\"/>");
assertThat(contentOf(xmlDataSetWithAllTables)).contains("<USER ID=\"2\" NAME=\"@dbunit\"/>");
assertThat(contentOf(xmlDataSetWithAllTables)).contains("<FOLLOWER ID=\"1\" USER_ID=\"1\" FOLLOWER_ID=\"2\"/>");

//xmlDataSetWithAllTables.delete();

File ymlDataSetWithAllTables = new File("target/exported/yml/allTables.yml");
assertThat(ymlDataSetWithAllTables).exists();
assertThat(contentOf(ymlDataSetWithAllTables)).
contains("FOLLOWER:"+NEW_LINE +
" - ID: 1"+NEW_LINE +
" USER_ID: 1"+NEW_LINE +
" FOLLOWER_ID: 2"+NEW_LINE );

assertThat(contentOf(ymlDataSetWithAllTables)).
contains("USER:"+NEW_LINE +
" - ID: 1"+NEW_LINE +
" NAME: \"@realpestano\""+NEW_LINE +
" - ID: 2"+NEW_LINE +
" NAME: \"@dbunit\"");


File xmlFilteredDataSet = new File("target/exported/xml/filtered.xml");
assertThat(xmlFilteredDataSet).exists();
assertThat(contentOf(xmlFilteredDataSet)).contains("<USER ID=\"1\" NAME=\"@realpestano\"/>");
assertThat(contentOf(xmlFilteredDataSet)).doesNotContain("<USER ID=\"2\" NAME=\"@dbunit\"/>");
assertThat(contentOf(xmlFilteredDataSet)).doesNotContain("<FOLLOWER ID=\"1\" USER_ID=\"1\" FOLLOWER_ID=\"2\"/>");

File ymlFilteredDataSet = new File("target/exported/yml/filtered.yml");
assertThat(ymlFilteredDataSet).exists();
assertThat(contentOf(ymlFilteredDataSet)).contains("USER:"+NEW_LINE +
" - ID: 1"+NEW_LINE +
" NAME: \"@realpestano\"");


File xmlFilteredWithIncludesDataSet = new File("target/exported/xml/filteredIncludes.xml");
assertThat(xmlFilteredWithIncludesDataSet).exists();
assertThat(contentOf(xmlFilteredWithIncludesDataSet)).contains("<USER ID=\"1\" NAME=\"@realpestano\"/>");
assertThat(contentOf(xmlFilteredWithIncludesDataSet)).contains("<TWEET ID=\"abcdef12345\" CONTENT=\"dbunit rules!\"");
assertThat(contentOf(xmlFilteredWithIncludesDataSet)).doesNotContain("<USER ID=\"2\" NAME=\"@dbunit\"/>");
assertThat(contentOf(xmlFilteredWithIncludesDataSet)).doesNotContain("<FOLLOWER ID=\"1\" USER_ID=\"1\" FOLLOWER_ID=\"2\"/>");

File ymlFilteredIncludesDataSet = new File("target/exported/yml/filteredIncludes.yml");
assertThat(ymlFilteredIncludesDataSet).exists();
assertThat(contentOf(ymlFilteredIncludesDataSet)).contains("USER:" + NEW_LINE +
" - ID: 1" + NEW_LINE +
" NAME: \"@realpestano\"");

assertThat(contentOf(ymlFilteredIncludesDataSet)).
contains("TWEET:"+NEW_LINE +
" - ID: \"abcdef12233\""+NEW_LINE +
" CONTENT: \"dbunit rules!\""+NEW_LINE +
" DATE: \"\""+NEW_LINE +
" LIKES: "+NEW_LINE +
" USER_ID: 2"+NEW_LINE +
" - ID: \"abcdef12345\""+NEW_LINE +
" CONTENT: \"dbunit rules!\""+NEW_LINE +
" DATE: \"\""+NEW_LINE +
" LIKES: "+NEW_LINE +
" USER_ID: 1"+NEW_LINE +
" - ID: \"abcdef1343\""+NEW_LINE +
" CONTENT: \"CDI for the win!\""+NEW_LINE +
" DATE: \"\""+NEW_LINE +
" LIKES: "+NEW_LINE +
" USER_ID: 2");


File xmlDependentTablesDataSet = new File("target/exported/xml/dependentTables.xml");
assertThat(xmlDependentTablesDataSet).exists();
assertThat(contentOf(xmlDependentTablesDataSet)).contains("<USER ID=\"1\" NAME=\"@realpestano\"/>");
assertThat(contentOf(xmlDependentTablesDataSet)).contains("<USER ID=\"2\" NAME=\"@dbunit\"/>");
assertThat(contentOf(xmlDependentTablesDataSet)).contains("<FOLLOWER ID=\"1\" USER_ID=\"1\" FOLLOWER_ID=\"2\"/>");
assertThat(contentOf(xmlDependentTablesDataSet)).contains("<TWEET ID=\"abcdef12345\" CONTENT=\"dbunit rules!\"");


File ymlDependentTablesDataSet = new File("target/exported/yml/dependentTables.yml");
assertThat(ymlDependentTablesDataSet).exists();
assertThat(contentOf(ymlDependentTablesDataSet)).contains("USER:"+NEW_LINE +
" - ID: 1"+NEW_LINE +
" NAME: \"@realpestano\""+NEW_LINE +
" - ID: 2"+NEW_LINE +
" NAME: \"@dbunit\"");

assertThat(contentOf(ymlDependentTablesDataSet)).
contains("TWEET:"+NEW_LINE +
" - ID: \"abcdef12233\""+NEW_LINE +
" CONTENT: \"dbunit rules!\""+NEW_LINE +
" DATE: \"\""+NEW_LINE +
" LIKES: "+NEW_LINE +
" USER_ID: 2"+NEW_LINE +
" - ID: \"abcdef12345\""+NEW_LINE +
" CONTENT: \"dbunit rules!\""+NEW_LINE +
" DATE: \"\""+NEW_LINE +
" LIKES: "+NEW_LINE +
" USER_ID: 1"+NEW_LINE +
" - ID: \"abcdef1343\""+NEW_LINE +
" CONTENT: \"CDI for the win!\""+NEW_LINE +
" DATE: \"\""+NEW_LINE +
" LIKES: "+NEW_LINE +
" USER_ID: 2");

assertThat(contentOf(ymlDependentTablesDataSet)).
contains("FOLLOWER:"+NEW_LINE +
" - ID: 1"+NEW_LINE +
" USER_ID: 1"+NEW_LINE +
" FOLLOWER_ID: 2");
}


}
Loading

0 comments on commit 0f8f5ef

Please sign in to comment.