Skip to content

Commit

Permalink
refs #41
Browse files Browse the repository at this point in the history
fixes #40
  • Loading branch information
rmpestano committed Aug 30, 2016
1 parent 9cd5db4 commit 2d4cea4
Show file tree
Hide file tree
Showing 7 changed files with 251 additions and 55 deletions.
34 changes: 25 additions & 9 deletions core/src/main/java/com/github/dbunit/rules/DBUnitRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
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.connection.ConnectionHolderImpl;
import com.github.dbunit.rules.dataset.DataSetExecutorImpl;
import org.dbunit.DatabaseUnitException;
Expand Down Expand Up @@ -58,10 +60,7 @@ public Statement apply(final Statement statement, final Description description)
@Override
public void evaluate() throws Throwable {
currentMethod = description.getMethodName();
DataSet dataSet = description.getAnnotation(DataSet.class);
if (dataSet == null) {
dataSet = description.getTestClass().getAnnotation(DataSet.class);
}
DataSet dataSet = resolveDataSet(description);
if (dataSet != null) {
final DataSetModel model = new DataSetModel().from(dataSet);
final String datasetExecutorId = model.getExecutorId();
Expand All @@ -73,6 +72,10 @@ 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.createDataSet(model);
} catch (final Exception e) {
throw new RuntimeException("Could not create dataset due to following error " + e.getMessage(), e);
Expand Down Expand Up @@ -126,8 +129,26 @@ 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);
if (dbUnitConfig == null) {
dbUnitConfig = description.getTestClass().getAnnotation(DBUnitConfig.class);
}
return dbUnitConfig;
}

private void performDataSetComparison(Description description) throws DatabaseUnitException {
ExpectedDataSet expectedDataSet = description.getAnnotation(ExpectedDataSet.class);
Expand Down Expand Up @@ -156,10 +177,5 @@ public DataSetExecutor getDataSetExecutor() {
return executor;
}

public DBUnitRule cacheConnection(boolean cacheConnection) {
executor.cacheConnection(cacheConnection);
return this;
}


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

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

import org.dbunit.DatabaseUnitException;
import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.IDataSet;
Expand Down Expand Up @@ -39,8 +41,7 @@ public interface DataSetExecutor{
void compareCurrentDataSetWith(DataSetModel expected, String[] ignoreCols) throws DatabaseUnitException;


/**
* @param cacheConnection if true database connection will be reused among multiple executions of same executor.
*/
DataSetExecutor cacheConnection(boolean cacheConnection);
void setDbUnitConfig(DBUnitConfigModel dbUnitConfig);


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

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

import java.lang.annotation.*;

import org.dbunit.database.DatabaseConfig;

/**
* Created by rafael-pestano on 30/08/2016.
*
* This annotation configures DBUnit properties
* (http://dbunit.sourceforge.net/properties.html) for a given dataset executor.
*
* It can be used at class or method level.
*/
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface DBUnitConfig {

/**
*
* Executor id for which the properties will be setup.
*/
String executor() default DataSetExecutorImpl.DEFAULT_EXECUTOR_ID;


boolean cacheConnection() default false;


boolean cacheTableNames() default false;


/**
* configures DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES. Defaults to false.
*/
boolean qualifiedTableNames() default false;

/**
*
* DatabaseConfig.FEATURE_BATCHED_STATEMENTS
*/
boolean batchedStatements() default false;

/**
* configures DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS. Defaults to false.
*/
boolean allowEmptyFields() default false;

/**
*
* DatabaseConfig.PROPERTY_FETCH_SIZE. Defaults to 100
*/
int fetchSize() default 100;

/**
* DatabaseConfig.PROPERTY_BATCH_SIZE. Defaults to 100
*/
int batchSize() default 100;

/**
* DatabaseConfig.PROPERTY_ESCAPE_PATTERN. Defaults to none
*/
String escapePattern() default "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.github.dbunit.rules.api.dbunit;

import java.util.HashMap;
import java.util.Map;

import org.dbunit.database.DatabaseConfig;

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

public class DBUnitConfigModel {

private String executorId;

private boolean cacheConnection = false;

private boolean cacheTables = false;

private Map<String,Object> dbunitProperties;

public DBUnitConfigModel(String executor) {
dbunitProperties = new HashMap<>();
this.executorId = executor;
if("".equals(this.executorId)){
this.executorId = DataSetExecutorImpl.DEFAULT_EXECUTOR_ID;
}
}


public static DBUnitConfigModel from(DBUnitConfig dbUnitConfig){
DBUnitConfigModel dbUnitConfigModel = new DBUnitConfigModel(dbUnitConfig.executor());

dbUnitConfigModel.cacheConnection(dbUnitConfig.cacheConnection()).
cacheTables(dbUnitConfig.cacheTables()).
addDBUnitProperty(DatabaseConfig.FEATURE_BATCHED_STATEMENTS, dbUnitConfig.batchedStatements()).
addDBUnitProperty(DatabaseConfig.PROPERTY_BATCH_SIZE, dbUnitConfig.batchSize()).
addDBUnitProperty(DatabaseConfig.FEATURE_ALLOW_EMPTY_FIELDS, dbUnitConfig.allowEmptyFields()).
addDBUnitProperty(DatabaseConfig.PROPERTY_FETCH_SIZE, dbUnitConfig.fetchSize()).
addDBUnitProperty(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, dbUnitConfig.qualifiedTableNames());

if(!"".equals(dbUnitConfig.escapePattern())){
dbUnitConfigModel.addDBUnitProperty(DatabaseConfig.PROPERTY_ESCAPE_PATTERN, dbUnitConfig.escapePattern());
}

return dbUnitConfigModel;
}


public DBUnitConfigModel cacheConnection(boolean cacheConnection){
this.cacheConnection = cacheConnection;
return this;
}


public DBUnitConfigModel cacheTables(boolean cacheTables){
this.cacheTables = cacheTables;
return this;
}

public DBUnitConfigModel addDBUnitProperty(String name, Object value){
dbunitProperties.put(name,value);
return this;
}

public boolean isCacheConnection() {
return cacheConnection;
}


public boolean isCacheTables() {
return cacheTables;
}


public Map<String, Object> getDbunitProperties() {
return dbunitProperties;
}

public String getExecutorId() {
return executorId;
}


}
Loading

0 comments on commit 2d4cea4

Please sign in to comment.