diff --git a/cdi/src/main/java/com/github/dbunit/rules/cdi/DBUnitInterceptorImpl.java b/cdi/src/main/java/com/github/dbunit/rules/cdi/DBUnitInterceptorImpl.java index 5cc20eb..86d776e 100644 --- a/cdi/src/main/java/com/github/dbunit/rules/cdi/DBUnitInterceptorImpl.java +++ b/cdi/src/main/java/com/github/dbunit/rules/cdi/DBUnitInterceptorImpl.java @@ -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()); } + } diff --git a/cdi/src/main/java/com/github/dbunit/rules/cdi/DataSetProcessor.java b/cdi/src/main/java/com/github/dbunit/rules/cdi/DataSetProcessor.java index 32766ed..181bc87 100644 --- a/cdi/src/main/java/com/github/dbunit/rules/cdi/DataSetProcessor.java +++ b/cdi/src/main/java/com/github/dbunit/rules/cdi/DataSetProcessor.java @@ -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; @@ -16,8 +20,10 @@ 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. @@ -25,7 +31,7 @@ @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()); @@ -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; + } } \ No newline at end of file diff --git a/cdi/src/test/java/com/github/dbunit/rules/cdi/ExportDataSetCDIIt.java b/cdi/src/test/java/com/github/dbunit/rules/cdi/ExportDataSetCDIIt.java new file mode 100644 index 0000000..80e9c42 --- /dev/null +++ b/cdi/src/test/java/com/github/dbunit/rules/cdi/ExportDataSetCDIIt.java @@ -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(""); + assertThat(contentOf(xmlDataSetWithAllTables)).contains(""); + assertThat(contentOf(xmlDataSetWithAllTables)).contains(""); + + //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(""); + assertThat(contentOf(xmlFilteredDataSet)).doesNotContain(""); + assertThat(contentOf(xmlFilteredDataSet)).doesNotContain(""); + + 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(""); + assertThat(contentOf(xmlFilteredWithIncludesDataSet)).contains(""); + assertThat(contentOf(xmlFilteredWithIncludesDataSet)).doesNotContain(""); + + 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(""); + assertThat(contentOf(xmlDependentTablesDataSet)).contains(""); + assertThat(contentOf(xmlDependentTablesDataSet)).contains(""); + assertThat(contentOf(xmlDependentTablesDataSet)).contains(" + * * based on: http://archive.oreilly.com/pub/post/dbunit_made_easy.html */ public class DataSetExporterImpl { @@ -138,6 +135,9 @@ public OutputStream export(DatabaseConnection databaseConnection, DataSetExportC log.log(Level.SEVERE, "Could not close file output stream.", e); } } + //set back default ResultSetTableFactory + config.setProperty(DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY, new CachedResultSetTableFactory()); + } return null; diff --git a/core/src/test/java/com/github/dbunit/rules/ExportDataSetIt.java b/core/src/test/java/com/github/dbunit/rules/ExportDataSetIt.java index ed29795..e5b574b 100644 --- a/core/src/test/java/com/github/dbunit/rules/ExportDataSetIt.java +++ b/core/src/test/java/com/github/dbunit/rules/ExportDataSetIt.java @@ -194,6 +194,5 @@ public static void assertGeneratedDataSets(){ " - ID: 1"+NEW_LINE + " USER_ID: 1"+NEW_LINE + " FOLLOWER_ID: 2"); - } } diff --git a/junit5/src/main/java/com/github/dbunit/rules/junit5/DBUnitExtension.java b/junit5/src/main/java/com/github/dbunit/rules/junit5/DBUnitExtension.java index 2359184..db41694 100644 --- a/junit5/src/main/java/com/github/dbunit/rules/junit5/DBUnitExtension.java +++ b/junit5/src/main/java/com/github/dbunit/rules/junit5/DBUnitExtension.java @@ -4,10 +4,13 @@ import com.github.dbunit.rules.api.dataset.DataSet; import com.github.dbunit.rules.api.dataset.DataSetExecutor; import com.github.dbunit.rules.api.dataset.ExpectedDataSet; +import com.github.dbunit.rules.api.expoter.DataSetExportConfig; +import com.github.dbunit.rules.api.expoter.ExportDataSet; import com.github.dbunit.rules.api.leak.LeakHunter; import com.github.dbunit.rules.configuration.DBUnitConfig; 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.leak.LeakHunterException; import com.github.dbunit.rules.leak.LeakHunterFactory; import org.dbunit.DatabaseUnitException; @@ -21,6 +24,7 @@ import java.lang.reflect.Method; import java.util.Arrays; import java.util.Optional; +import java.util.logging.Level; import static com.github.dbunit.rules.util.EntityManagerProvider.em; import static com.github.dbunit.rules.util.EntityManagerProvider.isEntityManagerActive; @@ -95,21 +99,40 @@ private boolean shouldCompareDataSet(TestExtensionContext testExtensionContext) return testExtensionContext.getTestMethod().get().isAnnotationPresent(ExpectedDataSet.class) || testExtensionContext.getTestClass().get().isAnnotationPresent(ExpectedDataSet.class); } + private boolean shouldExportDataSet(TestExtensionContext testExtensionContext) { + return testExtensionContext.getTestMethod().get().isAnnotationPresent(ExportDataSet.class) || testExtensionContext.getTestClass().get().isAnnotationPresent(ExportDataSet.class); + } + + public void exportDataSet(DataSetExecutor dataSetExecutor, 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; + } + @Override public void afterTestExecution(TestExtensionContext testExtensionContext) throws Exception { DBUnitConfig dbUnitConfig = DBUnitConfig.from(testExtensionContext.getTestMethod().get()); ExtensionContext.Namespace executorNamespace = getExecutorNamespace(testExtensionContext); try { - if (dbUnitConfig != null && dbUnitConfig.isLeakHunter()) { - LeakHunter leakHunter = testExtensionContext.getStore(executorNamespace).get(LEAK_STORE, LeakHunter.class); - int openConnectionsBefore = testExtensionContext.getStore(executorNamespace).get(CONNECTION_BEFORE_STORE, Integer.class); - int openConnectionsAfter = leakHunter.openConnections(); - if (openConnectionsAfter > openConnectionsBefore) { - throw new LeakHunterException(testExtensionContext.getTestMethod().get().getName(), openConnectionsAfter - openConnectionsBefore); - } - - } if (shouldCompareDataSet(testExtensionContext)) { ExpectedDataSet expectedDataSet = testExtensionContext.getTestMethod().get().getAnnotation(ExpectedDataSet.class); if (expectedDataSet == null) { @@ -127,12 +150,29 @@ public void afterTestExecution(TestExtensionContext testExtensionContext) throws executor.compareCurrentDataSetWith(new DataSetConfig(expectedDataSet.value()).disableConstraints(true), expectedDataSet.ignoreCols()); } } + + if (dbUnitConfig != null && dbUnitConfig.isLeakHunter()) { + LeakHunter leakHunter = testExtensionContext.getStore(executorNamespace).get(LEAK_STORE, LeakHunter.class); + int openConnectionsBefore = testExtensionContext.getStore(executorNamespace).get(CONNECTION_BEFORE_STORE, Integer.class); + int openConnectionsAfter = leakHunter.openConnections(); + if (openConnectionsAfter > openConnectionsBefore) { + throw new LeakHunterException(testExtensionContext.getTestMethod().get().getName(), openConnectionsAfter - openConnectionsBefore); + } + + } + } finally { + DataSetConfig dataSetConfig = testExtensionContext.getStore(executorNamespace).get(DATASET_CONFIG_STORE, DataSetConfig.class); if (dataSetConfig == null) { return; } DataSetExecutor executor = testExtensionContext.getStore(executorNamespace).get(EXECUTOR_STORE, DataSetExecutor.class); + + if(shouldExportDataSet(testExtensionContext)){ + exportDataSet(executor,testExtensionContext.getTestMethod().get()); + } + if (dataSetConfig.getExecuteStatementsAfter() != null && dataSetConfig.getExecuteStatementsAfter().length > 0) { try { for (int i = 0; i < dataSetConfig.getExecuteScriptsAfter().length; i++) { diff --git a/junit5/src/test/java/com/github/dbunit/rules/junit5/ExportDataSetJUnit5It.java b/junit5/src/test/java/com/github/dbunit/rules/junit5/ExportDataSetJUnit5It.java new file mode 100644 index 0000000..d8043c8 --- /dev/null +++ b/junit5/src/test/java/com/github/dbunit/rules/junit5/ExportDataSetJUnit5It.java @@ -0,0 +1,69 @@ +package com.github.dbunit.rules.junit5; + +import com.github.dbunit.rules.api.connection.ConnectionHolder; +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 org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.platform.runner.JUnitPlatform; +import org.junit.runner.RunWith; + +import java.io.File; + +import static com.github.dbunit.rules.util.EntityManagerProvider.instance; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.contentOf; + +/** + * Created by pestano on 23/07/15. + */ + +@ExtendWith(DBUnitExtension.class) +@RunWith(JUnitPlatform.class) +public class ExportDataSetJUnit5It { + + private static final String NEW_LINE = System.getProperty("line.separator"); + + private ConnectionHolder connectionHolder = () -> //<3> + instance("junit5-pu").connection();//<4> + + @Test + @DataSet("users.yml") + @ExportDataSet(format = DataSetFormat.XML,outputName="target/exported/xml/allTables.xml") + public void shouldExportAllTablesInXMLFormat() { + } + + @Test + @DataSet("users.yml") + @ExportDataSet(format = DataSetFormat.YML,outputName="target/exported/yml/allTables") + public void shouldExportAllTablesInYMLFormatOmmitingExtension() { + } + + + + @AfterAll + public static void assertGeneratedDataSets(){ + File xmlDataSetWithAllTables = new File("target/exported/xml/allTables.xml"); + assertThat(xmlDataSetWithAllTables).exists(); + assertThat(contentOf(xmlDataSetWithAllTables)).contains(""); + assertThat(contentOf(xmlDataSetWithAllTables)).contains(""); + + //xmlDataSetWithAllTables.delete(); + + File ymlDataSetWithAllTables = new File("target/exported/yml/allTables.yml"); + assertThat(ymlDataSetWithAllTables).exists(); + assertThat(contentOf(ymlDataSetWithAllTables)). + contains("USER:"+NEW_LINE + + " - ID: 1"+NEW_LINE + + " NAME: \"@realpestano\""+NEW_LINE + + " - ID: 2"+NEW_LINE + + " NAME: \"@dbunit\""); + + + + } + + +} \ No newline at end of file