Skip to content
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

Add multiple DataSet support in @ExpectedDatabase #118

Open
wants to merge 2 commits 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 @@ -132,12 +132,13 @@ private void verifyExpected(DbUnitTestContext testContext, DatabaseConnections c
throws Exception, DataSetException, SQLException, DatabaseUnitException {
String query = annotation.query();
String table = annotation.table();
IDataSet expectedDataSet = loadDataset(testContext, annotation.value(), modifier);
List<IDataSet> expectedDataSets = loadDatasets(testContext, annotation.value(), modifier);
IDatabaseConnection connection = connections.get(annotation.connection());
if (expectedDataSet != null) {
if (expectedDataSets.size() > 0) {
if (logger.isDebugEnabled()) {
logger.debug("Veriftying @DatabaseTest expectation using " + annotation.value());
}
IDataSet expectedDataSet = new CompositeDataSet(expectedDataSets.toArray(new IDataSet[expectedDataSets.size()]));
DatabaseAssertion assertion = annotation.assertionMode().getDatabaseAssertion();
List<IColumnFilter> columnFilters = getColumnFilters(annotation);
if (StringUtils.hasLength(query)) {
Expand All @@ -156,6 +157,15 @@ private void verifyExpected(DbUnitTestContext testContext, DatabaseConnections c
}
}

private List<IDataSet> loadDatasets(DbUnitTestContext testContext, String[] dataSetLocations, DataSetModifier modifier) throws Exception {
List<IDataSet> dataSets = new ArrayList<IDataSet>();
for (String dataSetLocation : dataSetLocations) {
IDataSet dataSet = loadDataset(testContext, dataSetLocation, modifier);
dataSets.add(dataSet);
}
return dataSets;
}

private DataSetModifier getModifier(DbUnitTestContext testContext, Annotations<ExpectedDatabase> annotations) {
DataSetModifiers modifiers = new DataSetModifiers();
for (ExpectedDatabase annotation : annotations) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@
String connection() default "";

/**
* Provides the location of the dataset that will be used to test the database.
* Provides the locations of the datasets that will be used to test the database.
* @return The dataset locations
* @see DbUnitConfiguration#dataSetLoader()
*/
String value() default "";
String[] value() default {};

/**
* Database assertion mode to use. Default is {@link DatabaseAssertionMode#DEFAULT}.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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 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;

import com.github.springtestdbunit.DbUnitTestExecutionListener;
import com.github.springtestdbunit.annotation.ExpectedDatabase;
import com.github.springtestdbunit.assertion.DatabaseAssertionMode;
import com.github.springtestdbunit.entity.EntityAssert;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/META-INF/dbunit-context.xml")
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
DbUnitTestExecutionListener.class})
@ExpectedDatabase(value = {"/META-INF/db/expectedfail_composed1.xml",
"/META-INF/db/expectedfail_composed2.xml", "/META-INF/db/expectedfail_composed3.xml"})
@Transactional
public class ExpectedOnClassAndMethodComposedTest {

@Autowired
private EntityAssert entityAssert;

@Test
@ExpectedDatabase(
value = {"/META-INF/db/expected_nonstrict_composed1.xml",
"/META-INF/db/expected_nonstrict_composed2.xml"},
assertionMode = DatabaseAssertionMode.NON_STRICT)
public void shouldUseMethodExpectation() {
this.entityAssert.assertValues("existing1", "existing2");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<SampleEntity value="existing1" />
</dataset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<SampleEntity value="existing2" />
</dataset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<SampleEntity id="1" value="existing1" />
</dataset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<SampleEntity id="2" value="existing2" />
</dataset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<SampleEntity id="3" value="nonexisting" />
</dataset>