Skip to content

Commit

Permalink
table ordering in CDI module
Browse files Browse the repository at this point in the history
  • Loading branch information
rmpestano committed Oct 8, 2015
1 parent e35f82c commit b154d58
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.github.dbunit.rules.cdi.api.UsingDataSet;
import org.dbunit.DatabaseUnitException;
import org.dbunit.database.AmbiguousTableNameException;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.DatabaseSequenceFilter;
import org.dbunit.database.IDatabaseConnection;
Expand All @@ -29,6 +30,7 @@
import org.dbunit.dataset.csv.CsvDataSet;
import org.dbunit.dataset.excel.XlsDataSet;
import org.dbunit.dataset.filter.ITableFilter;
import org.dbunit.dataset.filter.SequenceTableFilter;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.hibernate.Session;
import org.hibernate.internal.SessionImpl;
Expand Down Expand Up @@ -134,6 +136,8 @@ public void process(UsingDataSet usingDataSet) {
if (target != null) {
performReplacements(target);

performTableOrdering(target,usingDataSet.tableOrdering());

if(usingDataSet.useSequenceFiltering()){
target = performSequenceFiltering(target);
}
Expand All @@ -156,6 +160,12 @@ public void process(UsingDataSet usingDataSet) {

}

private IDataSet performTableOrdering(IDataSet target, String[] tableOrdering) throws AmbiguousTableNameException {
if (tableOrdering.length > 0 && !"".equals(tableOrdering[0])) {
target = new FilteredDataSet(new SequenceTableFilter(tableOrdering), target);
}
return target;
}

private IDataSet performSequenceFiltering(IDataSet target) throws DataSetException, SQLException {
ITableFilter filteredTable = new DatabaseSequenceFilter(databaseConnection,target.getTableNames());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@
@Nonbinding
boolean cleanBefore() default false;

/**
* @return a list of table names used to reorder DELETE operations to prevent failures due to circular dependencies
*
*/
@Nonbinding
String[] tableOrdering() default {};

/**
*
* @return list of sql commands to execute before dataset creation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void shouldQueryAllContactsUsingJsonDataSet() {
}

@Test
@UsingDataSet(value = "datasets/contacts.yml")
@UsingDataSet(value = "datasets/contacts.yml",tableOrdering = {"company","contact"})
public void shouldFindCompanyByName() {
Company expectedCompany = new Company("Google");
assertNotNull(companyRepository);
Expand Down

0 comments on commit b154d58

Please sign in to comment.