Skip to content

Commit

Permalink
Merge pull request #112 from ppodgorsek/issue-107
Browse files Browse the repository at this point in the history
Issue #107 - Support CSV dataset files
  • Loading branch information
ppodgorsek authored Mar 27, 2019
2 parents 837e166 + 79df683 commit e469242
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 7 deletions.
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<table name>.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

Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
id
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Sample_2
Sample_1
Original file line number Diff line number Diff line change
@@ -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<Person> 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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,title,firstName,lastName
0,Mr,Phillip,Webb
2,Mr,Paul,Podgorsek
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Person
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
id,title,firstName,lastName
0,Mr,Phillip,Webb
1,Mr,Mario,Zagar
2,Mr,Paul,Podgorsek
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Person

0 comments on commit e469242

Please sign in to comment.