Skip to content

Commit

Permalink
fixes #36
Browse files Browse the repository at this point in the history
fixes #37
  • Loading branch information
rmpestano committed Aug 25, 2016
1 parent b82c6d7 commit f94ee83
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 25 deletions.
6 changes: 5 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ public class ContactSteps {
<1> Step definitions must be in the same package of the runner. To use different package you can use *glues* as commented above.
<2> DBUnit cdi interceptor can be used in any cucumber step to prepare the database.

== Programmatically creating datasets
== Programmatic creating datasets

You can create datasets without JUnit Rule or CDI as we saw above, here is a pure cucumber example (for the same <<Examples,feature above>>):

Expand Down Expand Up @@ -835,4 +835,8 @@ Snapshots are available in maven central, to use it just add the following snipp
</repositories>
----

== Other examples

. DBUnit Rules https://github.com/rmpestano/dbunit-rules/tree/master/examples[examples module^]
. https://github.com/rmpestano/dbunit-tomee-appcomposer-sample[DBUnit Rules Application Composer^]
. https://github.com/rmpestano/jOOQ-DBUnit-flyway-example/[jOOQ Flyway DBUnit^]
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;

/**
* Created by pestano on 25/07/15.
*/
public interface ConnectionHolder extends Serializable{

Connection getConnection();
Connection getConnection() throws SQLException;


}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
public @interface DataSet {

/**
* @return dataset file name using resources folder as root directory
* @return dataset file name using resources folder as root directory.
* Multiple, comma separated, dataset file names can be provided.
*/
String value() default "";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import org.dbunit.dataset.filter.ITableFilter;
import org.dbunit.dataset.filter.SequenceTableFilter;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.dbunit.ext.h2.H2DataTypeFactory;
import org.dbunit.ext.hsqldb.HsqldbDataTypeFactory;
import org.dbunit.ext.mysql.MySqlDataTypeFactory;
import org.dbunit.ext.oracle.Oracle10DataTypeFactory;
import org.dbunit.ext.oracle.OracleDataTypeFactory;
import org.dbunit.ext.postgresql.PostgresqlDataTypeFactory;
import org.dbunit.operation.DatabaseOperation;
import org.slf4j.Logger;
Expand Down Expand Up @@ -81,7 +81,7 @@ public static DataSetExecutorImpl instance(String executorId, ConnectionHolder c
instance = new DataSetExecutorImpl(executorId, connectionHolder);
log.debug("creating executor instance " + executorId);
executors.put(executorId, instance);
} else{
} else {
instance.setConnectionHolder(connectionHolder);
}
return instance;
Expand Down Expand Up @@ -211,16 +211,27 @@ private IDataSet performSequenceFiltering(DataSetModel dataSet, IDataSet target)
return target;
}

private void configureDataTypeFactory(){
String driverName = getDriverName(connectionHolder);

private void configDatabaseProperties() throws SQLException {
DatabaseConfig config = databaseConnection.getConfig();
if(isInMemoryDb(driverName)){

//FEATURE_QUALIFIED_TABLE_NAMES
String qualifiedTableNames = System.getProperty(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES);
if (qualifiedTableNames != null) {
config.setProperty(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, Boolean.valueOf(qualifiedTableNames));
}

//PROPERTY_DATATYPE_FACTORY
String driverName = getDriverName(connectionHolder);
if (isHsql(driverName)) {
config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory());
} else if(isMysqlDb(driverName)){
} else if (isH2(driverName)) {
config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new H2DataTypeFactory());
} else if (isMysql(driverName)) {
config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory());
} else if(isPostgre(driverName)){
} else if (isPostgre(driverName)) {
config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new PostgresqlDataTypeFactory());
} else if(isOracle(driverName)){
} else if (isOracle(driverName)) {
config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new Oracle10DataTypeFactory());
}

Expand All @@ -229,11 +240,15 @@ private void configureDataTypeFactory(){
private void disableConstraints() throws SQLException {

String driverName = getDriverName(connectionHolder);
if (isInMemoryDb(driverName)) {
if (isHsql(driverName)) {
connectionHolder.getConnection().createStatement().execute("SET DATABASE REFERENTIAL INTEGRITY FALSE;");
}

if (isMysqlDb(driverName)) {
if (isH2(driverName)) {
connectionHolder.getConnection().createStatement().execute("SET foreign_key_checks = 0;");
}

if (isMysql(driverName)) {
connectionHolder.getConnection().createStatement().execute(" SET FOREIGN_KEY_CHECKS=0;");
}

Expand All @@ -243,8 +258,8 @@ private void disableConstraints() throws SQLException {

}

private String getDriverName(ConnectionHolder connectionHolder) {
if(connectionHolder != null && connectionHolder.getConnection() != null){
private String getDriverName(ConnectionHolder connectionHolder) throws SQLException {
if (connectionHolder != null && connectionHolder.getConnection() != null) {
try {
return connectionHolder.getConnection().getMetaData().getDriverName().toLowerCase();
} catch (SQLException e) {
Expand All @@ -254,11 +269,15 @@ private String getDriverName(ConnectionHolder connectionHolder) {
return null;
}

private boolean isInMemoryDb(String driverName) {
return driverName != null && (driverName.contains("hsql") || driverName.contains("h2"));
private boolean isHsql(String driverName) {
return driverName != null && driverName.contains("hsql");
}

private boolean isH2(String driverName) {
return driverName != null && driverName.contains("h2");
}

private boolean isMysqlDb(String driverName) {
private boolean isMysql(String driverName) {
return driverName != null && driverName.contains("mysql");
}

Expand Down Expand Up @@ -297,9 +316,9 @@ private IDataSet performReplacements(IDataSet dataSet) {
}


private void initDatabaseConnection() throws DatabaseUnitException {
private void initDatabaseConnection() throws DatabaseUnitException, SQLException {
databaseConnection = new DatabaseConnection(connectionHolder.getConnection());
configureDataTypeFactory();
configDatabaseProperties();
}


Expand All @@ -308,7 +327,11 @@ public void setConnectionHolder(ConnectionHolder connectionHolder) {
}

public Connection getConnection() {
return connectionHolder.getConnection();
try {
return connectionHolder.getConnection();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

public static Map<String, DataSetExecutorImpl> getExecutors() {
Expand Down Expand Up @@ -382,8 +405,12 @@ public void clearDatabase(DataSetModel dataset) throws SQLException {
//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();
try {
connection.createStatement().executeUpdate("DELETE FROM " + tableName + " where 1=1");
connection.commit();
} catch (Exception e) {
log.warn("Could not clear table " + tableName, e);
}
}

}
Expand All @@ -398,7 +425,9 @@ private List<String> getTableNames(Connection con) {
result = metaData.getTables(null, null, "%", new String[]{"TABLE"});

while (result.next()) {
tables.add(result.getString("TABLE_NAME"));
String schema = resolveSchema(result);
String name = result.getString("TABLE_NAME");
tables.add(schema != null ? schema + "." + name : name);
}

return tables;
Expand All @@ -409,6 +438,15 @@ private List<String> getTableNames(Connection con) {
}
}

private String resolveSchema(ResultSet result) {
try {
return result.getString("TABLE_SCHEMA");
} catch (Exception e) {

}
return null;
}

public void executeScript(String scriptPath) {
if (scriptPath != null && !"".equals(scriptPath)) {
if (!scriptPath.startsWith("/")) {
Expand Down Expand Up @@ -470,7 +508,6 @@ private String[] readScriptStatements(File scriptFile) {
}



public void compareCurrentDataSetWith(DataSetModel expectedDataSetModel, String[] excludeCols) throws DatabaseUnitException {
IDataSet current = null;
IDataSet expected = null;
Expand Down

0 comments on commit f94ee83

Please sign in to comment.