Skip to content

Commit

Permalink
fixes #41
Browse files Browse the repository at this point in the history
- adds configuration via @dbunit annotation and global config file dbunit.yml
  • Loading branch information
rmpestano committed Sep 4, 2016
1 parent b5ade80 commit faa4f47
Show file tree
Hide file tree
Showing 28 changed files with 727 additions and 466 deletions.
12 changes: 6 additions & 6 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,15 @@ public class DataSetExecutorIt {
@Test
public void shouldSeedUserDataSetUsingExecutor() {
DataSetModel dataSetModel = new DataSetModel("datasets/yml/users.yml");<1>
executor.createDataSet(dataSetModel);<2>
DataSetConfig dataSetConfig = new DataSetConfig("datasets/yml/users.yml");<1>
executor.createDataSet(dataSetConfig);<2>
User user = (User) em().createQuery("select u from User u where u.id = 1").getSingleResult();
assertThat(user).isNotNull();
assertThat(user.getId()).isEqualTo(1);
}
}
----
<1> As we are not using @Rule, which is responsible for reading @DataSet annotation, we have to provide *DataSetModel* so executor can create the dataset.
<1> As we are not using @Rule, which is responsible for reading @DataSet annotation, we have to provide *DataSetConfig* so executor can create the dataset.
<2> this is done implicitly by *@Rule DBUnitRule*.

DataSet executor setup and logic is `hidden` by DBUnit @Rule and @DataSet annotation:
Expand Down Expand Up @@ -286,8 +286,8 @@ public class MultipleExecutorsIt {
@Test
public void shouldSeedUserDataSet() {
for (DataSetExecutorImpl executor : executors) {
DataSetModel dataSetModel = new DataSetModel("datasets/yml/users.yml");
executor.createDataSet(dataSetModel);
DataSetConfig dataSetConfig = new DataSetConfig("datasets/yml/users.yml");
executor.createDataSet(dataSetConfig);
User user = (User) EntityManagerProvider.instance(executor.getId() + "-pu").em().createQuery("select u from User u where u.id = 1").getSingleResult();
assertThat(user).isNotNull();
assertThat(user.getId()).isEqualTo(1);
Expand Down Expand Up @@ -789,7 +789,7 @@ public class ContactStepsWithoutCDI {
@Given("^we have a list of contacts2$")
public void given() {
dbunitExecutor.createDataSet(new DataSetModel("contacts.yml"));
dbunitExecutor.createDataSet(new DataSetConfig("contacts.yml"));
assertEquals(em().createQuery("select count(c.id) from Contact c").getSingleResult(), new Long(3));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.github.dbunit.rules.cdi;

import com.github.dbunit.rules.api.dataset.DataSetModel;
import com.github.dbunit.rules.api.configuration.DBUnit;
import com.github.dbunit.rules.api.dataset.ExpectedDataSet;
import com.github.dbunit.rules.cdi.api.UsingDataSet;
import com.github.dbunit.rules.configuration.DBUnitConfig;
import com.github.dbunit.rules.configuration.DataSetConfig;
import com.github.dbunit.rules.configuration.GlobaConfig;

import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
Expand Down Expand Up @@ -31,7 +34,7 @@ public Object intercept(InvocationContext invocationContext)
Object proceed = null;
UsingDataSet usingDataSet = invocationContext.getMethod().getAnnotation(UsingDataSet.class);
if (usingDataSet != null) {
DataSetModel dataSetModel = new DataSetModel(usingDataSet.value()).
DataSetConfig DataSetConfig = new DataSetConfig(usingDataSet.value()).
cleanAfter(usingDataSet.cleanAfter()).
cleanBefore(usingDataSet.cleanBefore()).
disableConstraints(usingDataSet.disableConstraints()).
Expand All @@ -43,8 +46,8 @@ public Object intercept(InvocationContext invocationContext)
transactional(usingDataSet.transactional()).
tableOrdering(usingDataSet.tableOrdering()).
useSequenceFiltering(usingDataSet.useSequenceFiltering());
dataSetProcessor.process(dataSetModel);
boolean isTransactionalTest = dataSetModel.isTransactional();
dataSetProcessor.process(DataSetConfig,resolveDBUnitConfig(invocationContext));
boolean isTransactionalTest = DataSetConfig.isTransactional();
if(isTransactionalTest){
em.getTransaction().begin();
}
Expand All @@ -61,14 +64,14 @@ public Object intercept(InvocationContext invocationContext)
}
ExpectedDataSet expectedDataSet = invocationContext.getMethod().getAnnotation(ExpectedDataSet.class);
if(expectedDataSet != null){
dataSetProcessor.compareCurrentDataSetWith(new DataSetModel(expectedDataSet.value()).disableConstraints(true),expectedDataSet.ignoreCols());
dataSetProcessor.compareCurrentDataSetWith(new DataSetConfig(expectedDataSet.value()).disableConstraints(true),expectedDataSet.ignoreCols());
}
if(usingDataSet.cleanAfter()){
dataSetProcessor.clearDatabase(dataSetModel);
dataSetProcessor.clearDatabase(DataSetConfig);
}

if (!"".equals(usingDataSet.executeCommandsAfter())) {
dataSetProcessor.executeStatements(dataSetModel.getExecuteStatementsAfter());
dataSetProcessor.executeStatements(DataSetConfig.getExecuteStatementsAfter());
}

if(usingDataSet.executeScriptsAfter().length > 0 && !"".equals(usingDataSet.executeScriptsAfter()[0])){
Expand All @@ -80,13 +83,26 @@ public Object intercept(InvocationContext invocationContext)
proceed = invocationContext.proceed();
ExpectedDataSet expectedDataSet = invocationContext.getMethod().getAnnotation(ExpectedDataSet.class);
if(expectedDataSet != null){
dataSetProcessor.compareCurrentDataSetWith(new DataSetModel(expectedDataSet.value()).disableConstraints(true),expectedDataSet.ignoreCols());
dataSetProcessor.compareCurrentDataSetWith(new DataSetConfig(expectedDataSet.value()).disableConstraints(true),expectedDataSet.ignoreCols());
}
}


return proceed;
}

private DBUnitConfig resolveDBUnitConfig(InvocationContext invocationContext) {
DBUnit dbUnitConfig = invocationContext.getMethod().getAnnotation(DBUnit.class);
if (dbUnitConfig == null) {
dbUnitConfig = invocationContext.getMethod().getDeclaringClass().getAnnotation(DBUnit.class);
}

if (dbUnitConfig != null) {
return DBUnitConfig.from(dbUnitConfig);
} else {
return GlobaConfig.instance().getDbUnitConfig();
}
}


}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.github.dbunit.rules.cdi;

import com.github.dbunit.rules.api.dataset.DataSetExecutor;
import com.github.dbunit.rules.api.dataset.DataSetModel;
import com.github.dbunit.rules.configuration.DBUnitConfig;
import com.github.dbunit.rules.configuration.DataSetConfig;
import com.github.dbunit.rules.cdi.api.UsingDataSet;
import com.github.dbunit.rules.connection.ConnectionHolderImpl;
import com.github.dbunit.rules.dataset.DataSetExecutorImpl;
Expand Down Expand Up @@ -73,8 +74,9 @@ private Connection createConnection() {
return connection;
}

public void process(DataSetModel dataSetModel) {
dataSetExecutor.createDataSet(dataSetModel);
public void process(DataSetConfig dataSetConfig, DBUnitConfig config) {
dataSetExecutor.setDbUnitConfig(config);
dataSetExecutor.createDataSet(dataSetConfig);
}


Expand All @@ -87,9 +89,9 @@ private boolean isHibernatePresentOnClasspath() {
}
}

public void clearDatabase(DataSetModel dataSetModel) {
public void clearDatabase(DataSetConfig DataSetConfig) {
try {
dataSetExecutor.clearDatabase(dataSetModel);
dataSetExecutor.clearDatabase(DataSetConfig);
} catch (SQLException e) {
log.error("Could not clear database.", e);
}
Expand All @@ -103,7 +105,7 @@ public void executeScript(String script) {
dataSetExecutor.executeScript(script);
}

public void compareCurrentDataSetWith(DataSetModel expected, String[] excludeCols) throws DatabaseUnitException {
public void compareCurrentDataSetWith(DataSetConfig expected, String[] excludeCols) throws DatabaseUnitException {
dataSetExecutor.compareCurrentDataSetWith(expected, excludeCols);
}

Expand Down
80 changes: 41 additions & 39 deletions core/src/main/java/com/github/dbunit/rules/DBUnitRule.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.github.dbunit.rules;

import com.github.dbunit.rules.api.configuration.DBUnit;
import com.github.dbunit.rules.api.connection.ConnectionHolder;
import com.github.dbunit.rules.api.dataset.DataSet;
import com.github.dbunit.rules.api.dataset.DataSetExecutor;
import com.github.dbunit.rules.api.dataset.DataSetModel;
import com.github.dbunit.rules.api.dataset.ExpectedDataSet;
import com.github.dbunit.rules.api.dbunit.DBUnitConfig;
import com.github.dbunit.rules.api.dbunit.DBUnitConfigModel;
import com.github.dbunit.rules.configuration.DBUnitConfig;
import com.github.dbunit.rules.configuration.DataSetConfig;
import com.github.dbunit.rules.configuration.GlobaConfig;
import com.github.dbunit.rules.connection.ConnectionHolderImpl;
import com.github.dbunit.rules.dataset.DataSetExecutorImpl;
import org.dbunit.DatabaseUnitException;
import org.junit.internal.runners.statements.Fail;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
Expand Down Expand Up @@ -62,7 +62,7 @@ public void evaluate() throws Throwable {
currentMethod = description.getMethodName();
DataSet dataSet = resolveDataSet(description);
if (dataSet != null) {
final DataSetModel model = new DataSetModel().from(dataSet);
final DataSetConfig model = new DataSetConfig().from(dataSet);
final String datasetExecutorId = model.getExecutorId();
boolean executorNameIsProvided = datasetExecutorId != null && !"".equals(datasetExecutorId.trim());
if (executorNameIsProvided && !executor.getId().equals(datasetExecutorId)) {
Expand All @@ -72,10 +72,7 @@ public void evaluate() throws Throwable {
executor = DataSetExecutorImpl.getExecutorById(datasetExecutorId);
}
try {
DBUnitConfig dbUnitConfig = resolveDBUnitConfig(description);
if(dbUnitConfig != null && dbUnitConfig.executor().equals(executor.getId())){
executor.setDbUnitConfig(DBUnitConfigModel.from(dbUnitConfig));
}
executor.setDbUnitConfig(resolveDBUnitConfig(description));
executor.createDataSet(model);
} catch (final Exception e) {
throw new RuntimeException("Could not create dataset due to following error " + e.getMessage(), e);
Expand Down Expand Up @@ -130,52 +127,57 @@ public void evaluate() throws Throwable {

}


};
}

private DataSet resolveDataSet(Description description) {
DataSet dataSet = description.getAnnotation(DataSet.class);
if (dataSet == null) {
dataSet = description.getTestClass().getAnnotation(DataSet.class);
}
return dataSet;
}

private DBUnitConfig resolveDBUnitConfig(Description description) {
DBUnitConfig dbUnitConfig = description.getAnnotation(DBUnitConfig.class);
DBUnit dbUnitConfig = description.getAnnotation(DBUnit.class);
if (dbUnitConfig == null) {
dbUnitConfig = description.getTestClass().getAnnotation(DBUnitConfig.class);
dbUnitConfig = description.getTestClass().getAnnotation(DBUnit.class);
}

if (dbUnitConfig != null) {
return DBUnitConfig.from(dbUnitConfig);
} else {
return GlobaConfig.instance().getDbUnitConfig();
}
return dbUnitConfig;
}

private void performDataSetComparison(Description description) throws DatabaseUnitException {
ExpectedDataSet expectedDataSet = description.getAnnotation(ExpectedDataSet.class);
if (expectedDataSet == null) {
//try to infer from class level annotation
expectedDataSet = description.getTestClass().getAnnotation(ExpectedDataSet.class);
}
if (expectedDataSet != null) {
executor.compareCurrentDataSetWith(new DataSetModel(expectedDataSet.value()).disableConstraints(true), expectedDataSet.ignoreCols());
}
}
private void performDataSetComparison(Description description) throws DatabaseUnitException {
ExpectedDataSet expectedDataSet = description.getAnnotation(ExpectedDataSet.class);
if (expectedDataSet == null) {
//try to infer from class level annotation
expectedDataSet = description.getTestClass().getAnnotation(ExpectedDataSet.class);
}
if (expectedDataSet != null) {
executor.compareCurrentDataSetWith(new DataSetConfig(expectedDataSet.value()).disableConstraints(true), expectedDataSet.ignoreCols());
}
}

private void init(String name, ConnectionHolder connectionHolder) {
DataSetExecutorImpl instance = DataSetExecutorImpl.getExecutorById(name);
if (instance == null) {
instance = DataSetExecutorImpl.instance(name, connectionHolder);
DataSetExecutorImpl.getExecutors().put(name, instance);
} else{
instance.setConnectionHolder(connectionHolder);
}
executor = instance;
private void init(String name, ConnectionHolder connectionHolder) {
DataSetExecutorImpl instance = DataSetExecutorImpl.getExecutorById(name);
if (instance == null) {
instance = DataSetExecutorImpl.instance(name, connectionHolder);
DataSetExecutorImpl.getExecutors().put(name, instance);
} else {
instance.setConnectionHolder(connectionHolder);
}
executor = instance;

}
}

public DataSetExecutor getDataSetExecutor() {
return executor;
}
public DataSetExecutor getDataSetExecutor() {
return executor;
}


}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.dbunit.rules.api.dbunit;
package com.github.dbunit.rules.api.configuration;

import com.github.dbunit.rules.dataset.DataSetExecutorImpl;

Expand All @@ -18,7 +18,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface DBUnitConfig {
public @interface DBUnit {

/**
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.github.dbunit.rules.api.dataset;

import com.github.dbunit.rules.api.connection.ConnectionHolder;
import com.github.dbunit.rules.api.dbunit.DBUnitConfigModel;

import com.github.dbunit.rules.configuration.DBUnitConfig;
import com.github.dbunit.rules.configuration.DataSetConfig;
import org.dbunit.DatabaseUnitException;
import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.IDataSet;
Expand All @@ -16,16 +17,16 @@
public interface DataSetExecutor{

/**
* creates a dataset into executor's database connection using given dataSetModel
* @param dataSetModel
* creates a dataset into executor's database connection using given dataSetConfig
* @param dataSetConfig
*/
void createDataSet(DataSetModel dataSetModel);
void createDataSet(DataSetConfig dataSetConfig);

IDataSet loadDataSet(String name) throws DataSetException, IOException;

ConnectionHolder getConnectionHolder();

void clearDatabase(DataSetModel dataset) throws SQLException;
void clearDatabase(DataSetConfig dataset) throws SQLException;

void executeStatements(String[] statements);

Expand All @@ -38,10 +39,10 @@ public interface DataSetExecutor{
* @param expected
* @throws DatabaseUnitException if current dataset is not equal current dataset
*/
void compareCurrentDataSetWith(DataSetModel expected, String[] ignoreCols) throws DatabaseUnitException;
void compareCurrentDataSetWith(DataSetConfig expected, String[] ignoreCols) throws DatabaseUnitException;


void setDbUnitConfig(DBUnitConfigModel dbUnitConfig);
void setDbUnitConfig(DBUnitConfig dbUnitConfig);


}
Loading

0 comments on commit faa4f47

Please sign in to comment.