Skip to content

Commit

Permalink
fixes #26
Browse files Browse the repository at this point in the history
  • Loading branch information
rmpestano committed Jun 19, 2016
1 parent 4034fbf commit 030d2ad
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ExpectedDataSetCDIIt {

// tag::expectedCDI[]
@Test
@UsingDataSet //needed to activate interceptor (can be at class level)
@UsingDataSet(cleanBefore = true) //needed to activate interceptor (can be at class level)
@ExpectedDataSet(value = "yml/expectedUsers.yml",ignoreCols = "id")
public void shouldMatchExpectedDataSet() {
User u = new User();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
package com.github.dbunit.rules.dataset;

import java.io.*;
import java.net.URL;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.github.dbunit.rules.api.dataset.*;
import com.github.dbunit.rules.api.connection.ConnectionHolder;
import com.github.dbunit.rules.api.dataset.DataSetExecutor;
import com.github.dbunit.rules.api.dataset.DataSetModel;
import com.github.dbunit.rules.api.dataset.JSONDataSet;
import com.github.dbunit.rules.api.dataset.YamlDataSet;
import com.github.dbunit.rules.assertion.DataSetAssertion;
import com.github.dbunit.rules.exception.DataBaseSeedingException;
import com.github.dbunit.rules.replacer.DateTimeReplacer;
import com.github.dbunit.rules.replacer.ScriptReplacer;
import org.dbunit.DatabaseUnitException;
import org.dbunit.database.AmbiguousTableNameException;
Expand All @@ -31,8 +24,20 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.dbunit.rules.api.connection.ConnectionHolder;
import com.github.dbunit.rules.replacer.DateTimeReplacer;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.URL;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* Created by pestano on 26/07/15.
Expand Down Expand Up @@ -108,7 +113,7 @@ public void createDataSet(DataSetModel dataSetModel) {
}
}

if(dataSetModel.getName() != null && !"".equals(dataSetModel.getName())){
if (dataSetModel.getName() != null && !"".equals(dataSetModel.getName())) {
IDataSet resultingDataSet = loadDataSet(dataSetModel.getName());

resultingDataSet = performSequenceFiltering(dataSetModel, resultingDataSet);
Expand All @@ -123,7 +128,7 @@ public void createDataSet(DataSetModel dataSetModel) {
}

} catch (Exception e) {
throw new DataBaseSeedingException("Could not initialize dataset: "+dataSetModel, e);
throw new DataBaseSeedingException("Could not initialize dataset: " + dataSetModel, e);
}


Expand Down Expand Up @@ -313,23 +318,26 @@ private InputStream getDataSetStream(String dataSet) {
*/
public void clearDatabase(DataSetModel dataset) throws SQLException {
Connection connection = connectionHolder.getConnection();
if (isHSqlDB()) {
connection.createStatement().execute("TRUNCATE SCHEMA public AND COMMIT;");
} else {

if (dataset != null && dataset.getTableOrdering() != null && dataset.getTableOrdering().length > 0) {
for (String table : dataset.getTableOrdering()) {
connection.createStatement().executeUpdate("DELETE FROM " + table + " where 1=1");
connection.commit();
if (dataset != null && dataset.getTableOrdering() != null && dataset.getTableOrdering().length > 0) {
for (String table : dataset.getTableOrdering()) {
if (table.toUpperCase().contains("SEQ")) {
//tables containing 'SEQ'will NOT be cleared see https://github.com/rmpestano/dbunit-rules/issues/26
continue;
}
}
//clear remaining tables in any order(if there are any, also no problem clearing again)
List<String> tables = getTableNames(connection);
for (String tableName : tables) {
connection.createStatement().executeUpdate("DELETE FROM " + tableName + " where 1=1");
connection.createStatement().executeUpdate("DELETE FROM " + table + " where 1=1");
connection.commit();
}
}
//clear remaining tables in any order(if there are any, also no problem clearing again)
List<String> tables = getTableNames(connection);
for (String tableName : tables) {
if (tableName.toUpperCase().contains("SEQ")) {
//tables containing 'SEQ' will NOT be cleared see https://github.com/rmpestano/dbunit-rules/issues/26
continue;
}
connection.createStatement().executeUpdate("DELETE FROM " + tableName + " where 1=1");
connection.commit();
}

}

Expand Down

0 comments on commit 030d2ad

Please sign in to comment.