Skip to content

Add DatabaseAssertionLookup #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.List;
import java.util.Map;

import com.github.springtestdbunit.assertion.DatabaseAssertionLookup;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dbunit.DatabaseUnitException;
Expand Down Expand Up @@ -138,7 +139,13 @@ private void verifyExpected(DbUnitTestContext testContext, DatabaseConnections c
if (logger.isDebugEnabled()) {
logger.debug("Veriftying @DatabaseTest expectation using " + annotation.value());
}
DatabaseAssertion assertion = annotation.assertionMode().getDatabaseAssertion();
DatabaseAssertionLookup databaseAssertionLookup;
if (StringUtils.hasLength(annotation.assertionLookup())) {
databaseAssertionLookup = testContext.getDatabaseAssertionLookup(annotation.assertionLookup());
} else {
databaseAssertionLookup = testContext.getDatabaseAssertionLookup();
}
DatabaseAssertion assertion = databaseAssertionLookup.getDatabaseAssertion(annotation.assertionMode());
List<IColumnFilter> columnFilters = getColumnFilters(annotation);
if (StringUtils.hasLength(query)) {
Assert.hasLength(table, "The table name must be specified when using a SQL query");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.lang.reflect.Method;

import com.github.springtestdbunit.assertion.DatabaseAssertionLookup;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;

Expand All @@ -43,6 +44,19 @@ public interface DbUnitTestContext {
*/
DataSetLoader getDataSetLoader();

/**
* Returns the {@link DatabaseAssertionLookup} that should be used.
* @return The database assertion factory
*/
DatabaseAssertionLookup getDatabaseAssertionLookup();

/**
* Returns the {@link DatabaseAssertionLookup} registered with given bean name.
* @param beanName bean name of the {@link DatabaseAssertionLookup}
* @return The database assertion factory
*/
DatabaseAssertionLookup getDatabaseAssertionLookup(String beanName);

/**
* Returns the {@link DatabaseOperationLookup} that should be used to lookup database operations.
* @return the database operation lookup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import javax.sql.DataSource;

import com.github.springtestdbunit.assertion.DatabaseAssertionLookup;
import com.github.springtestdbunit.assertion.DefaultDatabaseAssertionLookup;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dbunit.database.IDatabaseConnection;
Expand Down Expand Up @@ -71,12 +73,17 @@ public class DbUnitTestExecutionListener extends AbstractTestExecutionListener {

private static final String DATA_SET_LOADER_BEAN_NAME = "dbUnitDataSetLoader";

private static final String DATABASE_ASSERTION_LOOKUP_BEAN_NAME = "dbUnitDatabaseAssertionLookup";

protected static final String CONNECTION_ATTRIBUTE = Conventions
.getQualifiedAttributeName(DbUnitTestExecutionListener.class, "connection");

protected static final String DATA_SET_LOADER_ATTRIBUTE = Conventions
.getQualifiedAttributeName(DbUnitTestExecutionListener.class, "dataSetLoader");

protected static final String DATABASE_ASSERTION_LOOKUP_ATTRIBUTE = Conventions
.getQualifiedAttributeName(DbUnitTestExecutionListener.class, "databaseAssertionLookup");

protected static final String DATABASE_OPERATION_LOOKUP_ATTRIBUTE = Conventions
.getQualifiedAttributeName(DbUnitTestExecutionListener.class, "databseOperationLookup");

Expand All @@ -94,6 +101,8 @@ public void prepareTestInstance(DbUnitTestContextAdapter testContext) throws Exc
String[] databaseConnectionBeanNames = null;
String dataSetLoaderBeanName = null;
Class<? extends DataSetLoader> dataSetLoaderClass = FlatXmlDataSetLoader.class;
String databaseAssertionLookupBeanName = null;
Class<? extends DatabaseAssertionLookup> databaseAssertionLookupClass = DefaultDatabaseAssertionLookup.class;
Class<? extends DatabaseOperationLookup> databaseOperationLookupClass = DefaultDatabaseOperationLookup.class;

DbUnitConfiguration configuration = testContext.getTestClass().getAnnotation(DbUnitConfiguration.class);
Expand All @@ -104,6 +113,8 @@ public void prepareTestInstance(DbUnitTestContextAdapter testContext) throws Exc
databaseConnectionBeanNames = configuration.databaseConnection();
dataSetLoaderClass = configuration.dataSetLoader();
dataSetLoaderBeanName = configuration.dataSetLoaderBean();
databaseAssertionLookupClass = configuration.databaseAssertionLookup();
databaseAssertionLookupBeanName = configuration.databaseAssertionLookupBean();
databaseOperationLookupClass = configuration.databaseOperationLookup();
}

Expand All @@ -118,6 +129,12 @@ public void prepareTestInstance(DbUnitTestContextAdapter testContext) throws Exc
}
}

if (!StringUtils.hasLength(databaseAssertionLookupBeanName)) {
if (testContext.getApplicationContext().containsBean(DATABASE_ASSERTION_LOOKUP_BEAN_NAME)) {
dataSetLoaderBeanName = DATABASE_ASSERTION_LOOKUP_BEAN_NAME;
}
}

if (logger.isDebugEnabled()) {
logger.debug("DBUnit tests will run using databaseConnection \""
+ StringUtils.arrayToCommaDelimitedString(databaseConnectionBeanNames)
Expand All @@ -126,6 +143,7 @@ public void prepareTestInstance(DbUnitTestContextAdapter testContext) throws Exc
}
prepareDatabaseConnection(testContext, databaseConnectionBeanNames);
prepareDataSetLoader(testContext, dataSetLoaderBeanName, dataSetLoaderClass);
prepareDatabaseAssertionsFactory(testContext, databaseAssertionLookupBeanName, databaseAssertionLookupClass);
prepareDatabaseOperationLookup(testContext, databaseOperationLookupClass);
}

Expand Down Expand Up @@ -170,6 +188,21 @@ private void prepareDataSetLoader(DbUnitTestContextAdapter testContext, String b
}
}

private void prepareDatabaseAssertionsFactory(DbUnitTestContextAdapter testContext, String beanName,
Class<? extends DatabaseAssertionLookup> databaseAssertionFactoryClass) {
if (StringUtils.hasLength(beanName)) {
testContext.setAttribute(DATABASE_ASSERTION_LOOKUP_ATTRIBUTE,
testContext.getApplicationContext().getBean(beanName, DatabaseAssertionLookup.class));
} else {
try {
testContext.setAttribute(DATABASE_ASSERTION_LOOKUP_ATTRIBUTE, databaseAssertionFactoryClass.newInstance());
} catch (Exception ex) {
throw new IllegalArgumentException(
"Unable to create database assertion factory instance for " + databaseAssertionFactoryClass, ex);
}
}
}

private void prepareDatabaseOperationLookup(DbUnitTestContextAdapter testContext,
Class<? extends DatabaseOperationLookup> databaseOperationLookupClass) {
try {
Expand Down Expand Up @@ -232,6 +265,14 @@ public DataSetLoader getDataSetLoader() {
return (DataSetLoader) getAttribute(DATA_SET_LOADER_ATTRIBUTE);
}

public DatabaseAssertionLookup getDatabaseAssertionLookup() {
return (DatabaseAssertionLookup) getAttribute(DATABASE_ASSERTION_LOOKUP_ATTRIBUTE);
}

public DatabaseAssertionLookup getDatabaseAssertionLookup(String beanName) {
return getApplicationContext().getBean(beanName, DatabaseAssertionLookup.class);
}

public DatabaseOperationLookup getDatbaseOperationLookup() {
return (DatabaseOperationLookup) getAttribute(DATABASE_OPERATION_LOOKUP_ATTRIBUTE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@

import javax.sql.DataSource;

import com.github.springtestdbunit.assertion.DefaultDatabaseAssertionLookup;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;

import com.github.springtestdbunit.DbUnitTestExecutionListener;
import com.github.springtestdbunit.assertion.DatabaseAssertionLookup;
import com.github.springtestdbunit.dataset.DataSetLoader;
import com.github.springtestdbunit.dataset.FlatXmlDataSetLoader;
import com.github.springtestdbunit.operation.DatabaseOperationLookup;
Expand Down Expand Up @@ -68,6 +70,20 @@
*/
String dataSetLoaderBean() default "";

/**
* Returns the class will be used for database assertion factory. The specified class must implement
* {@link DatabaseAssertionLookup} and must have a default constructor.
* @return the database assertion factory class
*/
Class<? extends DatabaseAssertionLookup> databaseAssertionLookup() default DefaultDatabaseAssertionLookup.class;

/**
* Returns the name of the bean that will be used for database assertion factory. The specified bean must
* implement {@link DatabaseAssertionLookup}.
* @return the data set loader bean name
*/
String databaseAssertionLookupBean() default "";

/**
* Returns the class that will be used to lookup DBUnit databse operations. The specific class must implement
* {@link DatabaseOperationLookup} and must have a default constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import com.github.springtestdbunit.assertion.DatabaseAssertionLookup;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.filter.IColumnFilter;

Expand Down Expand Up @@ -66,6 +67,12 @@
*/
DatabaseAssertionMode assertionMode() default DatabaseAssertionMode.DEFAULT;

/**
* {@link DatabaseAssertionLookup} bean name to use.
* @return Bean name of the database assertion factory.
*/
String assertionLookup() default "";

/**
* Optional table name that can be used to limit the comparison to a specific table.
* @return the table name
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.springtestdbunit.assertion;

/**
* Strategy used to lookup {@link DatabaseAssertion} from a value {@link DatabaseAssertionMode enum value}.
*/
public interface DatabaseAssertionLookup {
/**
* Get the {@link DatabaseAssertion} implementation for this mode.
*
* @param mode Database assertion mode
* @return Database assertion
*/
DatabaseAssertion getDatabaseAssertion(DatabaseAssertionMode mode);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.github.springtestdbunit.assertion;

/**
* Default implementation for {@link DatabaseAssertionLookup}, delegating to {@link DatabaseAssertionMode#databaseAssertion}
*/
public class DefaultDatabaseAssertionLookup implements DatabaseAssertionLookup {

public DatabaseAssertion getDatabaseAssertion(DatabaseAssertionMode mode) {
return mode.getDatabaseAssertion();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

import javax.sql.DataSource;

import com.github.springtestdbunit.assertion.DatabaseAssertion;
import com.github.springtestdbunit.assertion.DatabaseAssertionLookup;
import com.github.springtestdbunit.assertion.DatabaseAssertionMode;
import org.dbunit.database.DatabaseDataSourceConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
Expand Down Expand Up @@ -100,6 +103,7 @@ private void testCommonBeanNames(Class<?> testClass) throws Exception {
ExtendedTestContextManager testContextManager = new ExtendedTestContextManager(testClass);
testContextManager.prepareTestInstance();
verify(this.applicationContext).containsBean("dbUnitDataSetLoader");
verify(this.applicationContext).containsBean("dbUnitDatabaseAssertionLookup");
verify(this.applicationContext).containsBean("dbUnitDatabaseConnection");
verify(this.applicationContext).containsBean("dataSource");
verify(this.applicationContext).getBean("dataSource");
Expand Down Expand Up @@ -139,6 +143,19 @@ public void shouldFailIfDatabaseConnectionOfWrongTypeIsFound() throws Exception
}
}

@Test
public void shouldFailIfDatabaseAssertionFactoryWrongTypeIsFound() throws Exception {
addBean("dataSource", this.dataSource);
addBean("dbUnitDatabaseAssertionFactory", new Integer(0));
ExtendedTestContextManager testContextManager = new ExtendedTestContextManager(NoDbUnitConfiguration.class);
try {
testContextManager.prepareTestInstance();
} catch (IllegalArgumentException ex) {
assertEquals("Object of class [java.lang.Integer] must be an instance of interface "
+ "com.github.springtestdbunit.assertion.DatabaseAssertionFactory", ex.getMessage());
}
}

@Test
public void shouldSupportAllDbUnitConfigurationAttributes() throws Exception {
addBean("customBean", this.databaseConnection);
Expand Down Expand Up @@ -196,6 +213,12 @@ public IDataSet loadDataSet(Class<?> testClass, String location) throws Exceptio
public static class CustomDataSetLoader extends AbstractCustomDataSetLoader {
}

public static class CustomDatabaseAssertionLookup implements DatabaseAssertionLookup {
public DatabaseAssertion getDatabaseAssertion(DatabaseAssertionMode mode) {
return null;
}
}

public static class CustomDatabaseOperationLookup implements DatabaseOperationLookup {
public org.dbunit.operation.DatabaseOperation get(DatabaseOperation operation) {
return null;
Expand All @@ -217,7 +240,10 @@ private static class EmptyDbUnitConfiguration {

@ContextConfiguration(loader = LocalApplicationContextLoader.class)
@TestExecutionListeners(DbUnitTestExecutionListener.class)
@DbUnitConfiguration(databaseConnection = "customBean", dataSetLoader = CustomDataSetLoader.class, databaseOperationLookup = CustomDatabaseOperationLookup.class)
@DbUnitConfiguration(databaseConnection = "customBean",
dataSetLoader = CustomDataSetLoader.class,
databaseAssertionLookup = CustomDatabaseAssertionLookup.class,
databaseOperationLookup = CustomDatabaseOperationLookup.class)
private static class CustomConfiguration {

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.github.springtestdbunit.expected;

import com.github.springtestdbunit.assertion.DatabaseAssertion;
import com.github.springtestdbunit.assertion.DatabaseAssertionLookup;
import com.github.springtestdbunit.assertion.DatabaseAssertionMode;
import org.dbunit.DatabaseUnitException;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ITable;
import org.dbunit.dataset.filter.IColumnFilter;

import java.util.List;

public class AssertAllDatabaseAssertionLookup implements DatabaseAssertionLookup {
private static class AssertAllDatabaseAssertion implements DatabaseAssertion {
@Override
public void assertEquals(IDataSet expectedDataSet, IDataSet actualDataSet, List<IColumnFilter> columnFilters) throws DatabaseUnitException {
}

@Override
public void assertEquals(ITable expectedTable, ITable actualTable, List<IColumnFilter> columnFilters) throws DatabaseUnitException {
}
}

@Override
public DatabaseAssertion getDatabaseAssertion(DatabaseAssertionMode mode) {
return new AssertAllDatabaseAssertion();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2002-2016 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.github.springtestdbunit.expected;

import com.github.springtestdbunit.DbUnitTestExecutionListener;
import com.github.springtestdbunit.annotation.ExpectedDatabase;
import com.github.springtestdbunit.assertion.DatabaseAssertion;
import com.github.springtestdbunit.assertion.DatabaseAssertionLookup;
import com.github.springtestdbunit.assertion.DatabaseAssertionMode;
import com.github.springtestdbunit.entity.EntityAssert;
import com.github.springtestdbunit.testutils.MustFailDbUnitTestExecutionListener;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/META-INF/dbunit-context.xml")
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class })
@ExpectedDatabase(value = "/META-INF/db/expectedfail.xml", assertionLookup = "assertAllDatabaseAssertionLookup")
@Transactional
public class ExpectedAssertionLookupOnClassTest {
static class CustomLookup implements DatabaseAssertionLookup {

@Override
public DatabaseAssertion getDatabaseAssertion(DatabaseAssertionMode mode) {
return null;
}
}


@Autowired
private EntityAssert entityAssert;

@Test
public void test() throws Exception {
this.entityAssert.assertValues("existing1", "existing2");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

<bean id="otherEntityAssert" class="com.github.springtestdbunit.entity.OtherEntityAssert"/>

<bean id="assertAllDatabaseAssertionLookup" class="com.github.springtestdbunit.expected.AssertAllDatabaseAssertionLookup"/>

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
Expand Down