diff --git a/README.md b/README.md index 8592d111..c84dd682 100644 --- a/README.md +++ b/README.md @@ -208,22 +208,28 @@ In some situations you may need to create an `IDatabaseConnection` with a specif NOTE: In most circumstances the username and password properties should not be set on the `DatabaseDataSourceConnectionFactoryBean`. These properties will cause DBUnit to start a new transaction and may cause unexpected behaviour. -## Writing a DataSet Loader +## DataSet loaders Several dataset loaders are already available to read from: * flat XML files, (default) +* CSV files, * XLS files. If you need to load data from another source you will need to write your own DataSet loader and configure your tests to use it. Custom loaders must implement the `DataSetLoader` interface and provide an implementation of the `loadDataSet` method. The `AbstractDataSetLoader` is also available and provides a convenient base class for most loaders. -Here is an example loader that reads data from a CSV formatted file. +### CSV datasets configuration - public class CsvDataSetLoader extends AbstractDataSetLoader { - protected IDataSet createDataSet(Resource resource) throws Exception { - return new CsvURLDataSet(resource.getURL()); - } - } +Unlike XML and XLS files, CSV files have a very basic structure which doesn't allow multiple tables to be defined in a single file. + +The CSV dataset loader therefore relies on a folder containing: + +* a `table-ordering.txt` file listing the tables in the order they should be imported, +* a dedicated `.csv` file for each table which has to be imported. + +The dataset parameter should therefore point to that folder, not to individual files. + +Another point to keep in mind is that the current implementation of DBUnit uses commas as a value separator, not semicolons. ## Custom DBUnit Database Operations diff --git a/spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/dataset/CsvUrlDataSetLoader.java b/spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/dataset/CsvUrlDataSetLoader.java new file mode 100644 index 00000000..7e142ca6 --- /dev/null +++ b/spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/dataset/CsvUrlDataSetLoader.java @@ -0,0 +1,35 @@ +/* + * Copyright 2019 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.dataset; + +import org.dbunit.dataset.IDataSet; +import org.dbunit.dataset.csv.CsvURLDataSet; +import org.springframework.core.io.Resource; + +/** + * A {@link DataSetLoader data set loader} that can be used to load {@link CsvURLDataSet}s. + * + * @author Paul Podgorsek + */ +public class CsvUrlDataSetLoader extends AbstractDataSetLoader { + + @Override + protected IDataSet createDataSet(Resource resource) throws Exception { + return new CsvURLDataSet(resource.getURL()); + } + +} diff --git a/spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/dataset/CsvUrlDataSetLoaderTest.java b/spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/dataset/CsvUrlDataSetLoaderTest.java new file mode 100644 index 00000000..bb3ed7ee --- /dev/null +++ b/spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/dataset/CsvUrlDataSetLoaderTest.java @@ -0,0 +1,61 @@ +/* + * Copyright 2019 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.dataset; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import org.dbunit.dataset.IDataSet; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.test.context.TestContext; + +import com.github.springtestdbunit.testutils.ExtendedTestContextManager; + +/** + * Tests for {@link CsvUrlDataSetLoader}. + * + * @author Paul Podgorsek + */ +public class CsvUrlDataSetLoaderTest { + + private TestContext testContext; + + private CsvUrlDataSetLoader loader; + + @BeforeEach + public void setup() throws Exception { + loader = new CsvUrlDataSetLoader(); + ExtendedTestContextManager manager = new ExtendedTestContextManager(getClass()); + testContext = manager.accessTestContext(); + } + + @Test + public void shouldLoadFromRelativeFile() throws Exception { + IDataSet dataset = loader.loadDataSet(testContext.getTestClass(), "csv/"); + assertEquals(2, dataset.getTableNames().length, "Wrong number of tables"); + assertEquals("Sample_2", dataset.getTableNames()[0]); + assertEquals("Sample_1", dataset.getTableNames()[1]); + } + + @Test + public void shouldReturnNullOnMissingFile() throws Exception { + IDataSet dataset = loader.loadDataSet(testContext.getTestClass(), "doesnotexist/"); + assertNull(dataset); + } + +} diff --git a/spring-test-dbunit-core/src/test/resources/com/github/springtestdbunit/dataset/csv/Sample_1.csv b/spring-test-dbunit-core/src/test/resources/com/github/springtestdbunit/dataset/csv/Sample_1.csv new file mode 100644 index 00000000..074d1eeb --- /dev/null +++ b/spring-test-dbunit-core/src/test/resources/com/github/springtestdbunit/dataset/csv/Sample_1.csv @@ -0,0 +1 @@ +id diff --git a/spring-test-dbunit-core/src/test/resources/com/github/springtestdbunit/dataset/csv/Sample_2.csv b/spring-test-dbunit-core/src/test/resources/com/github/springtestdbunit/dataset/csv/Sample_2.csv new file mode 100644 index 00000000..074d1eeb --- /dev/null +++ b/spring-test-dbunit-core/src/test/resources/com/github/springtestdbunit/dataset/csv/Sample_2.csv @@ -0,0 +1 @@ +id diff --git a/spring-test-dbunit-core/src/test/resources/com/github/springtestdbunit/dataset/csv/table-ordering.txt b/spring-test-dbunit-core/src/test/resources/com/github/springtestdbunit/dataset/csv/table-ordering.txt new file mode 100644 index 00000000..4f658f3b --- /dev/null +++ b/spring-test-dbunit-core/src/test/resources/com/github/springtestdbunit/dataset/csv/table-ordering.txt @@ -0,0 +1,2 @@ +Sample_2 +Sample_1 diff --git a/spring-test-dbunit-sample/src/test/java/com/github/springtestdbunit/sample/service/PersonServiceCsvUrlTest.java b/spring-test-dbunit-sample/src/test/java/com/github/springtestdbunit/sample/service/PersonServiceCsvUrlTest.java new file mode 100644 index 00000000..6789ebba --- /dev/null +++ b/spring-test-dbunit-sample/src/test/java/com/github/springtestdbunit/sample/service/PersonServiceCsvUrlTest.java @@ -0,0 +1,60 @@ +/* + * Copyright 2019 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.sample.service; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.List; + +import javax.annotation.Resource; + +import org.junit.jupiter.api.Test; +import org.springframework.test.context.TestExecutionListeners; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; +import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; + +import com.github.springtestdbunit.DbUnitTestExecutionListener; +import com.github.springtestdbunit.annotation.DatabaseSetup; +import com.github.springtestdbunit.annotation.DbUnitConfiguration; +import com.github.springtestdbunit.annotation.ExpectedDatabase; +import com.github.springtestdbunit.dataset.CsvUrlDataSetLoader; +import com.github.springtestdbunit.sample.config.SampleTestConfiguration; +import com.github.springtestdbunit.sample.entity.Person; + +@SpringJUnitConfig(SampleTestConfiguration.class) +@DbUnitConfiguration(dataSetLoader = CsvUrlDataSetLoader.class) +@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class }) +public class PersonServiceCsvUrlTest { + + @Resource + private PersonService personService; + + @Test + @DatabaseSetup("csv/sample/") + public void testFind() throws Exception { + List personList = personService.find("gor"); + assertEquals(1, personList.size(), "Wrong number of results"); + assertEquals("Paul", personList.get(0).getFirstName(), "Wrong user"); + } + + @Test + @DatabaseSetup("csv/sample/") + @ExpectedDatabase("csv/expected/") + public void testRemove() throws Exception { + personService.remove(1); + } + +} diff --git a/spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/csv/expected/Person.csv b/spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/csv/expected/Person.csv new file mode 100644 index 00000000..89969e54 --- /dev/null +++ b/spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/csv/expected/Person.csv @@ -0,0 +1,3 @@ +id,title,firstName,lastName +0,Mr,Phillip,Webb +2,Mr,Paul,Podgorsek diff --git a/spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/csv/expected/table-ordering.txt b/spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/csv/expected/table-ordering.txt new file mode 100644 index 00000000..7bfe8176 --- /dev/null +++ b/spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/csv/expected/table-ordering.txt @@ -0,0 +1 @@ +Person diff --git a/spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/csv/sample/Person.csv b/spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/csv/sample/Person.csv new file mode 100644 index 00000000..441f6ee4 --- /dev/null +++ b/spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/csv/sample/Person.csv @@ -0,0 +1,4 @@ +id,title,firstName,lastName +0,Mr,Phillip,Webb +1,Mr,Mario,Zagar +2,Mr,Paul,Podgorsek diff --git a/spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/csv/sample/table-ordering.txt b/spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/csv/sample/table-ordering.txt new file mode 100644 index 00000000..7bfe8176 --- /dev/null +++ b/spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/csv/sample/table-ordering.txt @@ -0,0 +1 @@ +Person