forked from springtestdbunit/spring-test-dbunit
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #112 from ppodgorsek/issue-107
Issue #107 - Support CSV dataset files
- Loading branch information
Showing
11 changed files
with
182 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...st-dbunit-core/src/main/java/com/github/springtestdbunit/dataset/CsvUrlDataSetLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
...bunit-core/src/test/java/com/github/springtestdbunit/dataset/CsvUrlDataSetLoaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...-test-dbunit-core/src/test/resources/com/github/springtestdbunit/dataset/csv/Sample_1.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
id |
1 change: 1 addition & 0 deletions
1
...-test-dbunit-core/src/test/resources/com/github/springtestdbunit/dataset/csv/Sample_2.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
id |
2 changes: 2 additions & 0 deletions
2
...dbunit-core/src/test/resources/com/github/springtestdbunit/dataset/csv/table-ordering.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Sample_2 | ||
Sample_1 |
60 changes: 60 additions & 0 deletions
60
...ple/src/test/java/com/github/springtestdbunit/sample/service/PersonServiceCsvUrlTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
...ple/src/test/resources/com/github/springtestdbunit/sample/service/csv/expected/Person.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
1 change: 1 addition & 0 deletions
1
...test/resources/com/github/springtestdbunit/sample/service/csv/expected/table-ordering.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Person |
4 changes: 4 additions & 0 deletions
4
...ample/src/test/resources/com/github/springtestdbunit/sample/service/csv/sample/Person.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
1 change: 1 addition & 0 deletions
1
...c/test/resources/com/github/springtestdbunit/sample/service/csv/sample/table-ordering.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Person |