├── spring-test-dbunit-core └── src │ ├── test │ ├── resources │ │ ├── com │ │ │ └── github │ │ │ │ └── springtestdbunit │ │ │ │ └── dataset │ │ │ │ ├── csv │ │ │ │ ├── Sample_1.csv │ │ │ │ ├── Sample_2.csv │ │ │ │ └── table-ordering.txt │ │ │ │ ├── sql │ │ │ │ ├── sample.dat │ │ │ │ ├── tables.lst │ │ │ │ ├── Sample_1.ctl │ │ │ │ └── Sample_2.ctl │ │ │ │ ├── test.xml │ │ │ │ ├── test-replacement.xml │ │ │ │ ├── non-flat-xmldataset.xml │ │ │ │ ├── test-column-sensing.xml │ │ │ │ └── test.xls │ │ ├── init-datasource2.sql │ │ ├── META-INF │ │ │ └── db │ │ │ │ ├── delete.xml │ │ │ │ ├── insert.xml │ │ │ │ ├── update.xml │ │ │ │ ├── insert2.xml │ │ │ │ ├── insert_Other.xml │ │ │ │ ├── multi-insert.xml │ │ │ │ ├── multi-insert2.xml │ │ │ │ ├── expected_nonstrict.xml │ │ │ │ ├── expected_query_nonstrict.xml │ │ │ │ ├── expected_nonstrict_unordered.xml │ │ │ │ ├── expected_query.xml │ │ │ │ ├── multi-expected.xml │ │ │ │ ├── refresh.xml │ │ │ │ ├── expected_query_modified.xml │ │ │ │ ├── multi-refresh.xml │ │ │ │ ├── expectedfail.xml │ │ │ │ ├── expectedsuccess.xml │ │ │ │ └── expected_nonstrict_with_dtd.xml │ │ └── import.sql │ └── java │ │ └── com │ │ └── github │ │ └── springtestdbunit │ │ ├── entity │ │ ├── OtherSampleEntity.java │ │ ├── HibernateImportTest.java │ │ └── SampleEntity.java │ │ ├── setup │ │ ├── ParentDatabaseSetupInheritanceTest.java │ │ ├── AbstractParentDatabaseSetup.java │ │ ├── DeleteAllSetupOnClassTest.java │ │ ├── DeleteSetupOnClassTest.java │ │ ├── DeleteSetupOnMethodTest.java │ │ ├── TruncateSetupOnClassTest.java │ │ ├── UpdateSetupOnClassTest.java │ │ ├── CleanInsertSetupOnClassTest.java │ │ ├── UpdateSetupOnMethodTest.java │ │ ├── InsertSetupOnClassTest.java │ │ ├── InsertSetupOnMethodTest.java │ │ ├── RefreshSetupOnClassTest.java │ │ ├── RefreshSetupOnMethodTest.java │ │ ├── MultipleInsertSetupOnClassTest.java │ │ ├── DeleteAllSetupOnMethodTest.java │ │ ├── TruncateSetupOnMethodTest.java │ │ └── MixedSetupOnClassAndMethodTest.java │ │ ├── dataset │ │ ├── ReplacementDataSetLoaderTest.java │ │ ├── XmlDataSetLoaderTest.java │ │ ├── CsvUrlDataSetLoaderTest.java │ │ ├── XlsDataSetLoaderTest.java │ │ ├── SqlLoaderControlDataSetLoaderTest.java │ │ └── FlatXmlDataSetLoaderTest.java │ │ ├── TransactionDbUnitTestExecutionListenerTest.java │ │ ├── testutils │ │ ├── MustFailDbUnitTestExecutionListener.java │ │ ├── AfterTearDownDbUnitTestExecutionListener.java │ │ ├── CallAfterTestMethodExecutionListener.java │ │ └── ExtendedTestContextManager.java │ │ ├── expected │ │ ├── ExpectedQueryOnMethodTest.java │ │ ├── ExpectedQueryFailureOnMethodTest.java │ │ ├── ExpectedQueryNonStrictOnMethodTest.java │ │ ├── ExpectedQueryNonStrictFailureOnMethodTest.java │ │ ├── ExpectedFailureOnClassTest.java │ │ ├── ExpectedFailureOnMethodTest.java │ │ ├── ExpectedNonStrictOnMethodTest.java │ │ ├── ExpectedNonStrictOnClassTest.java │ │ ├── ExpectedOnClassAndMethodTest.java │ │ ├── ExpectedFailureOnMethodWithRepeatableTest.java │ │ ├── ExpectedFailureOnMethodWithRepeatableReverseTest.java │ │ ├── ExpectedTableNonStrictOnClassTest.java │ │ ├── ExpectedNonStrictUnorderedOnMethodTest.java │ │ ├── ExpectedNonStrictUnorderedOnClassTest.java │ │ ├── ExpectedOnClassAndMethodWithoutOverrideTest.java │ │ └── ExpectedNonStrictWithDtdTest.java │ │ ├── operation │ │ ├── DefaultDatabaseOperationLookupTest.java │ │ └── MicrosoftSqlDatabaseOperationLookupTest.java │ │ └── teardown │ │ ├── DeleteTearDownOnClass.java │ │ ├── DeleteTearDownOnMethod.java │ │ ├── TruncateTearDownOnClass.java │ │ ├── TruncateTearDownOnMethod.java │ │ ├── DeleteAllTearDownOnMethod.java │ │ ├── CleanInsertTearDownOnClass.java │ │ ├── UpdateTearDownOnClass.java │ │ ├── UpdateTearDownOnMethod.java │ │ ├── CleanInsertTearDownOnMethod.java │ │ ├── InsertTearDownOnClass.java │ │ ├── InsertTearDownOnMethod.java │ │ ├── RefreshTearDownOnClass.java │ │ ├── RefreshTearDownOnMethod.java │ │ ├── MultipleInsertTearDownOnClassTest.java │ │ ├── MixedTearDownOnClassAndMethodTest.java │ │ └── DeleteAllTearDownOnClass.java │ ├── site │ ├── resources │ │ └── css │ │ │ └── site.css │ ├── site.xml │ └── markdown │ │ └── faq.md │ └── main │ └── java │ └── com │ └── github │ └── springtestdbunit │ ├── dataset │ ├── ReplacementDataSetModifier.java │ ├── XmlDataSetLoader.java │ ├── CsvUrlDataSetLoader.java │ ├── DataSetModifier.java │ ├── XlsDataSetLoader.java │ ├── DataSetLoader.java │ ├── SqlLoaderControlDataSetLoader.java │ └── FlatXmlDataSetLoader.java │ ├── annotation │ ├── DatabaseSetups.java │ ├── DatabaseTearDowns.java │ ├── ExpectedDatabases.java │ ├── ExpectedDatabaseAnnotationAttributes.java │ ├── DatabaseSetupTearDownAnnotationAttributes.java │ └── DatabaseOperation.java │ ├── operation │ ├── DatabaseOperationLookup.java │ ├── MicrosoftSqlDatabaseOperationLookup.java │ └── DefaultDatabaseOperationLookup.java │ ├── DbUnitTestContextConstants.java │ ├── assertion │ ├── DefaultDatabaseAssertion.java │ ├── DatabaseAssertion.java │ └── NonStrictUnorderedDatabaseAssertion.java │ ├── TransactionDbUnitTestExecutionListener.java │ ├── DatabaseConnections.java │ ├── DataSetModifiers.java │ └── DbUnitTestContext.java ├── spring-test-dbunit-sample └── src │ ├── test │ ├── resources │ │ └── com │ │ │ └── github │ │ │ └── springtestdbunit │ │ │ └── sample │ │ │ └── service │ │ │ ├── sql │ │ │ ├── expected │ │ │ │ ├── tables.lst │ │ │ │ ├── Person.dat │ │ │ │ └── Person.ctl │ │ │ └── sample │ │ │ │ ├── tables.lst │ │ │ │ ├── Person.dat │ │ │ │ └── Person.ctl │ │ │ ├── csv │ │ │ ├── sample │ │ │ │ ├── table-ordering.txt │ │ │ │ └── Person.csv │ │ │ └── expected │ │ │ │ ├── table-ordering.txt │ │ │ │ └── Person.csv │ │ │ ├── expectedData.xml │ │ │ ├── expectedData.xls │ │ │ ├── sampleData.xls │ │ │ └── sampleData.xml │ └── java │ │ └── com │ │ └── github │ │ └── springtestdbunit │ │ └── sample │ │ └── service │ │ ├── PersonServiceXmlTest.java │ │ ├── PersonServiceCsvUrlTest.java │ │ ├── PersonServiceXlsTest.java │ │ └── PersonServiceSqlLoaderControlTest.java │ └── main │ └── java │ └── com │ └── github │ └── springtestdbunit │ └── sample │ ├── config │ └── SampleConfiguration.java │ ├── service │ └── PersonService.java │ └── entity │ └── Person.java ├── eclipse └── org.eclipse.m2e.core.prefs ├── .gitignore ├── .github ├── workflows │ └── maven.yml └── FUNDING.yml └── spring-test-dbunit-test └── src └── main └── resources ├── properties └── database.properties └── log4j.xml /spring-test-dbunit-core/src/test/resources/com/github/springtestdbunit/dataset/csv/Sample_1.csv: -------------------------------------------------------------------------------- 1 | id 2 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/com/github/springtestdbunit/dataset/csv/Sample_2.csv: -------------------------------------------------------------------------------- 1 | id 2 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/com/github/springtestdbunit/dataset/sql/sample.dat: -------------------------------------------------------------------------------- 1 | id 2 | 1 3 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/com/github/springtestdbunit/dataset/sql/tables.lst: -------------------------------------------------------------------------------- 1 | Sample_2 2 | Sample_1 3 | -------------------------------------------------------------------------------- /spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/sql/expected/tables.lst: -------------------------------------------------------------------------------- 1 | Person 2 | -------------------------------------------------------------------------------- /spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/sql/sample/tables.lst: -------------------------------------------------------------------------------- 1 | Person 2 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/com/github/springtestdbunit/dataset/csv/table-ordering.txt: -------------------------------------------------------------------------------- 1 | Sample_2 2 | Sample_1 3 | -------------------------------------------------------------------------------- /spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/csv/sample/table-ordering.txt: -------------------------------------------------------------------------------- 1 | Person 2 | -------------------------------------------------------------------------------- /spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/csv/expected/table-ordering.txt: -------------------------------------------------------------------------------- 1 | Person 2 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/init-datasource2.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE second (id int, value varchar(100), primary key(id)); 2 | -------------------------------------------------------------------------------- /spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/sql/expected/Person.dat: -------------------------------------------------------------------------------- 1 | 0;Mr;Phillip;Webb 2 | 2;Mr;Paul;Podgorsek 3 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/META-INF/db/delete.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /eclipse/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | #Sat Mar 10 15:15:11 PST 2012 2 | activeProfiles= 3 | eclipse.preferences.version=1 4 | resolveWorkspaceProjects=true 5 | version=1 6 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/com/github/springtestdbunit/dataset/test.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/sql/sample/Person.dat: -------------------------------------------------------------------------------- 1 | 0;Mr;Phillip;Webb 2 | 1;Mr;Mario;Zagar 3 | 2;Mr;Paul;Podgorsek 4 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/META-INF/db/insert.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/META-INF/db/update.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.versionsBackup 2 | .project 3 | .classpath 4 | .settings 5 | target 6 | spring-test-dbunit/src/site/markdown/index.md 7 | bin 8 | .DS_Store 9 | .idea 10 | *.iml 11 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/META-INF/db/insert2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/csv/expected/Person.csv: -------------------------------------------------------------------------------- 1 | id,title,firstName,lastName 2 | 0,Mr,Phillip,Webb 3 | 2,Mr,Paul,Podgorsek 4 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/META-INF/db/insert_Other.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/com/github/springtestdbunit/dataset/test-replacement.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/META-INF/db/multi-insert.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/META-INF/db/multi-insert2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/csv/sample/Person.csv: -------------------------------------------------------------------------------- 1 | id,title,firstName,lastName 2 | 0,Mr,Phillip,Webb 3 | 1,Mr,Mario,Zagar 4 | 2,Mr,Paul,Podgorsek 5 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/site/resources/css/site.css: -------------------------------------------------------------------------------- 1 | pre code { 2 | background-color: #F5F5F5; 3 | line-height: 1.2; 4 | padding: 1px 0px; 5 | } 6 | 7 | div#breadcrumbs ul { 8 | padding-right: 60px; 9 | } -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/com/github/springtestdbunit/dataset/non-flat-xmldataset.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 |
6 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/META-INF/db/expected_nonstrict.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/com/github/springtestdbunit/dataset/test-column-sensing.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/import.sql: -------------------------------------------------------------------------------- 1 | insert into SampleEntity (value) values ('existing1') 2 | insert into SampleEntity (value) values ('existing2') 3 | insert into OtherSampleEntity (value) values ('existing1') 4 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/META-INF/db/expected_query_nonstrict.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/META-INF/db/expected_nonstrict_unordered.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/META-INF/db/expected_query.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/META-INF/db/multi-expected.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/com/github/springtestdbunit/dataset/test.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ppodgorsek/spring-test-dbunit/HEAD/spring-test-dbunit-core/src/test/resources/com/github/springtestdbunit/dataset/test.xls -------------------------------------------------------------------------------- /spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/expectedData.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/META-INF/db/refresh.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/META-INF/db/expected_query_modified.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/expectedData.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ppodgorsek/spring-test-dbunit/HEAD/spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/expectedData.xls -------------------------------------------------------------------------------- /spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/sampleData.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ppodgorsek/spring-test-dbunit/HEAD/spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/sampleData.xls -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/META-INF/db/multi-refresh.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/META-INF/db/expectedfail.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/META-INF/db/expectedsuccess.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/sampleData.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/com/github/springtestdbunit/dataset/sql/Sample_1.ctl: -------------------------------------------------------------------------------- 1 | OPTIONS (SKIP=1) 2 | LOAD DATA 3 | INFILE 'sample.dat' 4 | APPEND 5 | INTO TABLE Sample_1 6 | FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' 7 | TRAILING NULLCOLS 8 | ( 9 | ID CHAR 10 | TERMINATED BY EOF 11 | ) 12 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/com/github/springtestdbunit/dataset/sql/Sample_2.ctl: -------------------------------------------------------------------------------- 1 | OPTIONS (SKIP=1) 2 | LOAD DATA 3 | INFILE 'sample.dat' 4 | APPEND 5 | INTO TABLE Sample_2 6 | FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' 7 | TRAILING NULLCOLS 8 | ( 9 | ID CHAR 10 | TERMINATED BY EOF 11 | ) 12 | -------------------------------------------------------------------------------- /spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/sql/sample/Person.ctl: -------------------------------------------------------------------------------- 1 | OPTIONS (SKIP=1) 2 | LOAD DATA 3 | INFILE 'Person.dat' 4 | APPEND 5 | INTO TABLE Person 6 | FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' 7 | TRAILING NULLCOLS 8 | ( 9 | ID INTEGER, 10 | TITLE CHAR, 11 | FIRSTNAME CHAR, 12 | LASTNAME CHAR 13 | TERMINATED BY EOF 14 | ) 15 | -------------------------------------------------------------------------------- /spring-test-dbunit-sample/src/test/resources/com/github/springtestdbunit/sample/service/sql/expected/Person.ctl: -------------------------------------------------------------------------------- 1 | OPTIONS (SKIP=1) 2 | LOAD DATA 3 | INFILE 'Person.dat' 4 | APPEND 5 | INTO TABLE Person 6 | FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' 7 | TRAILING NULLCOLS 8 | ( 9 | ID INTEGER, 10 | TITLE CHAR, 11 | FIRSTNAME CHAR, 12 | LASTNAME CHAR 13 | TERMINATED BY EOF 14 | ) 15 | -------------------------------------------------------------------------------- /.github/workflows/maven.yml: -------------------------------------------------------------------------------- 1 | name: Maven CI 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - uses: actions/checkout@v3 11 | - name: Set up JDK 17 12 | uses: actions/setup-java@v3 13 | with: 14 | java-version: '17' 15 | distribution: 'temurin' 16 | - name: Build with Maven 17 | run: mvn --batch-mode --update-snapshots package 18 | -------------------------------------------------------------------------------- /spring-test-dbunit-test/src/main/resources/properties/database.properties: -------------------------------------------------------------------------------- 1 | database.datasource.poolname=springHikariCP 2 | database.datasource.driver=org.hsqldb.jdbc.JDBCDriver 3 | database.datasource.url=jdbc:hsqldb:mem:springtestdbunit 4 | database.datasource.username=sa 5 | database.datasource.password= 6 | 7 | database.dbunit.dataTypeFactory=org.dbunit.ext.hsqldb.HsqldbDataTypeFactory 8 | 9 | database.hibernate.dialect=org.hibernate.dialect.HSQLDialect 10 | database.hibernate.format_sql=true 11 | database.hibernate.show_sql=true 12 | database.hibernate.hbm2ddl.auto=create-drop 13 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/resources/META-INF/db/expected_nonstrict_with_dtd.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 8 | ]> 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/dataset/ReplacementDataSetModifier.java: -------------------------------------------------------------------------------- 1 | package com.github.springtestdbunit.dataset; 2 | 3 | import org.dbunit.dataset.IDataSet; 4 | import org.dbunit.dataset.ReplacementDataSet; 5 | 6 | /** 7 | * {@link DataSetModifier} to create a {@link ReplacementDataSet}. 8 | * 9 | * @author Phillip Webb 10 | */ 11 | public abstract class ReplacementDataSetModifier implements DataSetModifier { 12 | 13 | public IDataSet modify(IDataSet dataSet) { 14 | if (!(dataSet instanceof ReplacementDataSet)) { 15 | dataSet = new ReplacementDataSet(dataSet); 16 | } 17 | addReplacements((ReplacementDataSet) dataSet); 18 | return dataSet; 19 | } 20 | 21 | protected abstract void addReplacements(ReplacementDataSet dataSet); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: ppodgorsek # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry 13 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 14 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/annotation/DatabaseSetups.java: -------------------------------------------------------------------------------- 1 | package com.github.springtestdbunit.annotation; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Inherited; 6 | import java.lang.annotation.Retention; 7 | import java.lang.annotation.RetentionPolicy; 8 | import java.lang.annotation.Target; 9 | 10 | /** 11 | * Container for repeating {@link DatabaseSetup} annotations. 12 | * 13 | * @author Phillip Webb 14 | * @see DatabaseSetup 15 | */ 16 | @Documented 17 | @Inherited 18 | @Retention(RetentionPolicy.RUNTIME) 19 | @Target({ ElementType.TYPE, ElementType.METHOD }) 20 | public @interface DatabaseSetups { 21 | 22 | /** 23 | * The {@link DatabaseSetup} annotations to apply. 24 | * @return the {@link DatabaseSetup} annotations 25 | */ 26 | DatabaseSetup[] value(); 27 | 28 | } 29 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/annotation/DatabaseTearDowns.java: -------------------------------------------------------------------------------- 1 | package com.github.springtestdbunit.annotation; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Inherited; 6 | import java.lang.annotation.Retention; 7 | import java.lang.annotation.RetentionPolicy; 8 | import java.lang.annotation.Target; 9 | 10 | /** 11 | * Container for repeating {@link DatabaseTearDown} annotations. 12 | * 13 | * @author Phillip Webb 14 | * @see DatabaseTearDown 15 | */ 16 | @Documented 17 | @Inherited 18 | @Retention(RetentionPolicy.RUNTIME) 19 | @Target({ ElementType.TYPE, ElementType.METHOD }) 20 | public @interface DatabaseTearDowns { 21 | 22 | /** 23 | * The {@link DatabaseTearDown} annotations to apply. 24 | * @return the {@link DatabaseTearDown} annotations 25 | */ 26 | DatabaseTearDown[] value(); 27 | 28 | } 29 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/annotation/ExpectedDatabases.java: -------------------------------------------------------------------------------- 1 | package com.github.springtestdbunit.annotation; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Inherited; 6 | import java.lang.annotation.Retention; 7 | import java.lang.annotation.RetentionPolicy; 8 | import java.lang.annotation.Target; 9 | 10 | /** 11 | * Container for repeating {@link ExpectedDatabase} annotations. 12 | * 13 | * @author Phillip Webb 14 | * @see ExpectedDatabase 15 | */ 16 | @Documented 17 | @Inherited 18 | @Retention(RetentionPolicy.RUNTIME) 19 | @Target({ ElementType.TYPE, ElementType.METHOD }) 20 | public @interface ExpectedDatabases { 21 | 22 | /** 23 | * The {@link ExpectedDatabase} annotations to apply. 24 | * @return the {@link ExpectedDatabase} annotations 25 | */ 26 | ExpectedDatabase[] value(); 27 | 28 | } 29 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/dataset/XmlDataSetLoader.java: -------------------------------------------------------------------------------- 1 | package com.github.springtestdbunit.dataset; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | 6 | import org.dbunit.dataset.DataSetException; 7 | import org.dbunit.dataset.IDataSet; 8 | import org.dbunit.dataset.xml.XmlDataSet; 9 | import org.springframework.core.io.Resource; 10 | 11 | /** 12 | * A {@link DataSetLoader data set loader} that can be used to load {@link XmlDataSet XmlDataSets}. 13 | * 14 | * @author Jorge Davison 15 | * @since 1.3.0 16 | */ 17 | public class XmlDataSetLoader extends AbstractDataSetLoader { 18 | 19 | @Override 20 | protected IDataSet createDataSet(Resource resource) throws IOException, DataSetException { 21 | InputStream inputStream = resource.getInputStream(); 22 | try { 23 | return new XmlDataSet(inputStream); 24 | } finally { 25 | inputStream.close(); 26 | } 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /spring-test-dbunit-test/src/main/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/entity/OtherSampleEntity.java: -------------------------------------------------------------------------------- 1 | package com.github.springtestdbunit.entity; 2 | 3 | import org.hibernate.annotations.GenericGenerator; 4 | 5 | import jakarta.persistence.Column; 6 | import jakarta.persistence.Entity; 7 | import jakarta.persistence.GeneratedValue; 8 | import jakarta.persistence.GenerationType; 9 | import jakarta.persistence.Id; 10 | 11 | /** 12 | * @author Oleksii Lomako 13 | */ 14 | @Entity 15 | public class OtherSampleEntity { 16 | 17 | @Id 18 | @GeneratedValue(strategy = GenerationType.AUTO, generator = "native") 19 | @GenericGenerator(name = "native", strategy = "native") 20 | private Integer id; 21 | 22 | @Column 23 | private String value; 24 | 25 | public String getValue() { 26 | return value; 27 | } 28 | 29 | public void setValue(String newValue) { 30 | value = newValue; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "OtherSampleEntity{" + "id=" + id + ", value='" + value + '\'' + '}'; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /spring-test-dbunit-sample/src/main/java/com/github/springtestdbunit/sample/config/SampleConfiguration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.springtestdbunit.sample.config; 17 | 18 | import org.springframework.context.annotation.ComponentScan; 19 | import org.springframework.context.annotation.Configuration; 20 | 21 | /** 22 | * Configuration of the Sample module. 23 | * 24 | * @author Paul Podgorsek 25 | */ 26 | @Configuration 27 | @ComponentScan("com.github.springtestdbunit.sample.service") 28 | public class SampleConfiguration { 29 | 30 | } 31 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/setup/ParentDatabaseSetupInheritanceTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.springtestdbunit.setup; 17 | 18 | import org.junit.jupiter.api.Test; 19 | import org.springframework.beans.factory.annotation.Autowired; 20 | 21 | import com.github.springtestdbunit.entity.EntityAssert; 22 | 23 | /** 24 | * The setup annotations are carried on the parent class in order to test the inheritance of the setup. 25 | * 26 | * @author Paul Podgorsek 27 | */ 28 | public class ParentDatabaseSetupInheritanceTest extends AbstractParentDatabaseSetup { 29 | 30 | @Autowired 31 | private EntityAssert entityAssert; 32 | 33 | @Test 34 | public void test() throws Exception { 35 | entityAssert.assertValues("fromDbUnit"); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/dataset/CsvUrlDataSetLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | package com.github.springtestdbunit.dataset; 18 | 19 | import java.io.IOException; 20 | 21 | import org.dbunit.dataset.DataSetException; 22 | import org.dbunit.dataset.IDataSet; 23 | import org.dbunit.dataset.csv.CsvURLDataSet; 24 | import org.springframework.core.io.Resource; 25 | 26 | /** 27 | * A {@link DataSetLoader data set loader} that can be used to load {@link CsvURLDataSet}s. 28 | * 29 | * @author Paul Podgorsek 30 | */ 31 | public class CsvUrlDataSetLoader extends AbstractDataSetLoader { 32 | 33 | @Override 34 | protected IDataSet createDataSet(Resource resource) throws DataSetException, IOException { 35 | return new CsvURLDataSet(resource.getURL()); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/dataset/ReplacementDataSetLoaderTest.java: -------------------------------------------------------------------------------- 1 | package com.github.springtestdbunit.dataset; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | import static org.junit.jupiter.api.Assertions.assertNull; 5 | 6 | import org.dbunit.dataset.IDataSet; 7 | import org.dbunit.dataset.ITable; 8 | import org.junit.jupiter.api.BeforeEach; 9 | import org.junit.jupiter.api.Test; 10 | import org.springframework.test.context.TestContext; 11 | 12 | import com.github.springtestdbunit.testutils.ExtendedTestContextManager; 13 | 14 | /** 15 | * Tests for {@link ReplacementDataSetLoader}. 16 | * 17 | * @author Stijn Van Bael 18 | */ 19 | public class ReplacementDataSetLoaderTest { 20 | 21 | private TestContext testContext; 22 | 23 | private ReplacementDataSetLoader loader; 24 | 25 | @BeforeEach 26 | public void setup() throws Exception { 27 | loader = new ReplacementDataSetLoader(); 28 | ExtendedTestContextManager manager = new ExtendedTestContextManager(getClass()); 29 | testContext = manager.accessTestContext(); 30 | } 31 | 32 | @Test 33 | public void shouldReplaceNulls() throws Exception { 34 | IDataSet dataset = loader.loadDataSet(testContext.getTestClass(), "test-replacement.xml"); 35 | assertEquals("Sample", dataset.getTableNames()[0]); 36 | ITable table = dataset.getTable("Sample"); 37 | assertEquals(1, table.getRowCount()); 38 | assertNull(table.getValue(0, "value")); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/dataset/XmlDataSetLoaderTest.java: -------------------------------------------------------------------------------- 1 | package com.github.springtestdbunit.dataset; 2 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; 4 | import static org.junit.jupiter.api.Assertions.assertNull; 5 | 6 | import org.dbunit.dataset.IDataSet; 7 | import org.junit.jupiter.api.BeforeEach; 8 | import org.junit.jupiter.api.Test; 9 | import org.springframework.test.context.TestContext; 10 | 11 | import com.github.springtestdbunit.testutils.ExtendedTestContextManager; 12 | 13 | /** 14 | * Tests for {@link XmlDataSetLoader}. 15 | * 16 | * @author Phillip Webb 17 | */ 18 | public class XmlDataSetLoaderTest { 19 | 20 | private TestContext testContext; 21 | 22 | private XmlDataSetLoader loader; 23 | 24 | @BeforeEach 25 | public void setup() throws Exception { 26 | loader = new XmlDataSetLoader(); 27 | ExtendedTestContextManager manager = new ExtendedTestContextManager(getClass()); 28 | testContext = manager.accessTestContext(); 29 | } 30 | 31 | @Test 32 | public void shouldLoadFromRelativeFile() throws Exception { 33 | IDataSet dataset = loader.loadDataSet(testContext.getTestClass(), "non-flat-xmldataset.xml"); 34 | assertEquals("Sample", dataset.getTableNames()[0]); 35 | } 36 | 37 | @Test 38 | public void shouldReturnNullOnMissingFile() throws Exception { 39 | IDataSet dataset = loader.loadDataSet(testContext.getTestClass(), "doesnotexist.xml"); 40 | assertNull(dataset); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/entity/HibernateImportTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.entity; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 21 | 22 | import com.github.springtestdbunit.config.CoreTestConfiguration; 23 | 24 | import jakarta.annotation.Resource; 25 | 26 | /** 27 | * Test the initial state of the database to ensure that import.sql has run. 28 | * 29 | * @author Phillip Webb 30 | */ 31 | @SpringJUnitConfig(CoreTestConfiguration.class) 32 | public class HibernateImportTest { 33 | 34 | @Resource 35 | private EntityAssert entityAssert; 36 | 37 | @Test 38 | public void shouldHaveUpdatedData() throws Exception { 39 | entityAssert.assertValues("existing1", "existing2"); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/TransactionDbUnitTestExecutionListenerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit; 18 | 19 | import static org.junit.jupiter.api.Assertions.assertEquals; 20 | 21 | import org.junit.jupiter.api.Test; 22 | import org.springframework.test.context.transaction.TransactionalTestExecutionListener; 23 | 24 | /** 25 | * Tests for {@link TransactionDbUnitTestExecutionListener}. 26 | * 27 | * @author Phillip Webb 28 | */ 29 | public class TransactionDbUnitTestExecutionListenerTest { 30 | 31 | @Test 32 | public void shouldRunTransactionsBeforeDbUnit() throws Exception { 33 | Class[] chain = new TransactionDbUnitTestExecutionListener().getChain(); 34 | assertEquals(TransactionalTestExecutionListener.class, chain[0]); 35 | assertEquals(DbUnitTestExecutionListener.class, chain[1]); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/dataset/DataSetModifier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.dataset; 18 | 19 | import org.dbunit.dataset.IDataSet; 20 | import org.dbunit.dataset.ReplacementDataSet; 21 | 22 | /** 23 | * Strategy interface that can be used to modify a {@link IDataSet}. 24 | * 25 | * @author Phillip Webb 26 | */ 27 | public interface DataSetModifier { 28 | 29 | /** 30 | * No-op {@link DataSetModifier}. 31 | */ 32 | public static final DataSetModifier NONE = new DataSetModifier() { 33 | 34 | public IDataSet modify(IDataSet dataSet) { 35 | return dataSet; 36 | } 37 | 38 | }; 39 | 40 | /** 41 | * Modify the given {@link IDataSet}, for example by wrapping it with a {@link ReplacementDataSet}. 42 | * @param dataSet the {@link IDataSet} to modify 43 | * @return the modified {@link IDataSet} 44 | */ 45 | IDataSet modify(IDataSet dataSet); 46 | 47 | } 48 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/dataset/XlsDataSetLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | package com.github.springtestdbunit.dataset; 18 | 19 | import java.io.IOException; 20 | import java.io.InputStream; 21 | 22 | import org.dbunit.dataset.DataSetException; 23 | import org.dbunit.dataset.IDataSet; 24 | import org.dbunit.dataset.excel.XlsDataSet; 25 | import org.springframework.core.io.Resource; 26 | 27 | /** 28 | * A {@link DataSetLoader data set loader} that can be used to load {@link XlsDataSet}s. 29 | * 30 | * @author Paul Podgorsek 31 | */ 32 | public class XlsDataSetLoader extends AbstractDataSetLoader { 33 | 34 | @Override 35 | protected IDataSet createDataSet(final Resource resource) throws IOException, DataSetException { 36 | 37 | InputStream inputStream = resource.getInputStream(); 38 | 39 | try { 40 | return new XlsDataSet(inputStream); 41 | } finally { 42 | inputStream.close(); 43 | } 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/testutils/MustFailDbUnitTestExecutionListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.testutils; 18 | 19 | import static org.junit.jupiter.api.Assertions.assertNotNull; 20 | 21 | import org.springframework.test.context.TestContext; 22 | 23 | import com.github.springtestdbunit.TransactionDbUnitTestExecutionListener; 24 | 25 | /** 26 | * An extension of {@link TransactionDbUnitTestExecutionListener} that ensures that a test method has failed. 27 | * 28 | * @author Phillip Webb 29 | */ 30 | public class MustFailDbUnitTestExecutionListener extends TransactionDbUnitTestExecutionListener { 31 | 32 | @Override 33 | public void afterTestMethod(TestContext testContext) throws Exception { 34 | Throwable caught = null; 35 | try { 36 | super.afterTestMethod(testContext); 37 | } catch (Throwable ex) { 38 | caught = ex; 39 | } 40 | assertNotNull(caught, "Test did not fail"); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/operation/DatabaseOperationLookup.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.operation; 18 | 19 | import com.github.springtestdbunit.annotation.DatabaseOperation; 20 | 21 | /** 22 | * Strategy used to lookup a DBUnit {@link org.dbunit.operation.DatabaseOperation Database Operation} from a 23 | * {@link DatabaseOperation enum value}. 24 | * 25 | * @author Phillip Webb 26 | * @see DefaultDatabaseOperationLookup 27 | * @see MicrosoftSqlDatabaseOperationLookup 28 | */ 29 | public interface DatabaseOperationLookup { 30 | 31 | /** 32 | * Returns the DBUnit {@link org.dbunit.operation.DatabaseOperation Database Operation} that should be used for the 33 | * specified enum or {@code null} if the operation is not supported. 34 | * @param operation the enum value 35 | * @return the DBUnit databsae operation or {@code null}. 36 | */ 37 | org.dbunit.operation.DatabaseOperation get(DatabaseOperation operation); 38 | 39 | } 40 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/testutils/AfterTearDownDbUnitTestExecutionListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.testutils; 18 | 19 | import org.springframework.test.context.transaction.TransactionalTestExecutionListener; 20 | 21 | import com.github.springtestdbunit.DbUnitTestExecutionListener; 22 | import com.github.springtestdbunit.TestExecutionListenerChain; 23 | 24 | /** 25 | * A {@link TestExecutionListenerChain} that triggers a {@link CallAfterTestMethodExecutionListener} as well as 26 | * transaction and DBUnit listeners. 27 | * 28 | * @author Phillip Webb 29 | */ 30 | public class AfterTearDownDbUnitTestExecutionListener extends TestExecutionListenerChain { 31 | 32 | private static final Class CHAIN[] = { TransactionalTestExecutionListener.class, 33 | CallAfterTestMethodExecutionListener.class, DbUnitTestExecutionListener.class }; 34 | 35 | @Override 36 | protected Class[] getChain() { 37 | return CHAIN; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/testutils/CallAfterTestMethodExecutionListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.testutils; 18 | 19 | import java.lang.reflect.Method; 20 | 21 | import org.springframework.test.context.TestContext; 22 | import org.springframework.test.context.TestExecutionListener; 23 | import org.springframework.test.context.support.AbstractTestExecutionListener; 24 | import org.springframework.util.ReflectionUtils; 25 | 26 | /** 27 | * A {@link TestExecutionListener} that triggers a afterTest() method once a test has executed. 28 | * 29 | * @author Phillip Webb 30 | */ 31 | public class CallAfterTestMethodExecutionListener extends AbstractTestExecutionListener { 32 | 33 | @Override 34 | public void afterTestMethod(TestContext testContext) throws Exception { 35 | Method method = testContext.getTestClass().getMethod("afterTest"); 36 | ReflectionUtils.invokeMethod(method, testContext.getTestInstance()); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/site/site.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | springtestdbunit/spring-test-dbunit 10 | right 11 | black 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | ${project.name} 21 | 22 | 23 | 24 | org.apache.maven.skins 25 | maven-fluido-skin 26 | 1.5 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/DbUnitTestContextConstants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.springtestdbunit; 17 | 18 | import org.springframework.core.Conventions; 19 | 20 | /** 21 | * Constants related to the DBUnit test context. 22 | * 23 | * @author Paul Podgorsek 24 | */ 25 | public final class DbUnitTestContextConstants { 26 | 27 | public static final String CONNECTION_ATTRIBUTE = Conventions 28 | .getQualifiedAttributeName(DbUnitTestExecutionListener.class, "connection"); 29 | 30 | public static final String DATA_SET_LOADER_ATTRIBUTE = Conventions 31 | .getQualifiedAttributeName(DbUnitTestExecutionListener.class, "dataSetLoader"); 32 | 33 | public static final String DATABASE_OPERATION_LOOKUP_ATTRIBUTE = Conventions 34 | .getQualifiedAttributeName(DbUnitTestExecutionListener.class, "databaseOperationLookup"); 35 | 36 | /** 37 | * Default private constructor to avoid instantiating this class. 38 | */ 39 | private DbUnitTestContextConstants() { 40 | super(); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /spring-test-dbunit-sample/src/main/java/com/github/springtestdbunit/sample/service/PersonService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.sample.service; 18 | 19 | import java.util.List; 20 | 21 | import org.springframework.stereotype.Service; 22 | import org.springframework.transaction.annotation.Transactional; 23 | 24 | import com.github.springtestdbunit.sample.entity.Person; 25 | 26 | import jakarta.persistence.EntityManager; 27 | import jakarta.persistence.PersistenceContext; 28 | import jakarta.persistence.Query; 29 | 30 | @Service 31 | @Transactional 32 | public class PersonService { 33 | 34 | @PersistenceContext 35 | private EntityManager entityManager; 36 | 37 | @SuppressWarnings("unchecked") 38 | public List find(String name) { 39 | Query query = entityManager.createNamedQuery("Person.find"); 40 | query.setParameter("name", "%" + name + "%"); 41 | return query.getResultList(); 42 | } 43 | 44 | public void remove(int personId) { 45 | Person person = entityManager.find(Person.class, personId); 46 | entityManager.remove(person); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/assertion/DefaultDatabaseAssertion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.assertion; 18 | 19 | import java.util.List; 20 | 21 | import org.dbunit.Assertion; 22 | import org.dbunit.DatabaseUnitException; 23 | import org.dbunit.dataset.IDataSet; 24 | import org.dbunit.dataset.ITable; 25 | import org.dbunit.dataset.filter.IColumnFilter; 26 | 27 | /** 28 | * Default database assertion strategy which uses DbUnit {@link Assertion#assertEquals(IDataSet, IDataSet)}. 29 | * 30 | * @author Mario Zagar 31 | * @author Sunitha Rajarathnam 32 | */ 33 | class DefaultDatabaseAssertion implements DatabaseAssertion { 34 | 35 | public void assertEquals(IDataSet expectedDataSet, IDataSet actualDataSet, List columnFilters) 36 | throws DatabaseUnitException { 37 | Assertion.assertEquals(expectedDataSet, actualDataSet); 38 | } 39 | 40 | public void assertEquals(ITable expectedTable, ITable actualTable, List columnFilters) 41 | throws DatabaseUnitException { 42 | Assertion.assertEquals(expectedTable, actualTable); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/entity/SampleEntity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.entity; 18 | 19 | import org.hibernate.annotations.GenericGenerator; 20 | import org.springframework.core.style.ToStringCreator; 21 | 22 | import jakarta.persistence.Column; 23 | import jakarta.persistence.Entity; 24 | import jakarta.persistence.GeneratedValue; 25 | import jakarta.persistence.GenerationType; 26 | import jakarta.persistence.Id; 27 | 28 | /** 29 | * A sample entity for use with tests. 30 | * 31 | * @author Phillip Webb 32 | */ 33 | @Entity 34 | public class SampleEntity { 35 | 36 | @Id 37 | @GeneratedValue(strategy = GenerationType.AUTO, generator = "native") 38 | @GenericGenerator(name = "native", strategy = "native") 39 | private Integer id; 40 | 41 | @Column 42 | private String value; 43 | 44 | public String getValue() { 45 | return value; 46 | } 47 | 48 | public void setValue(String newValue) { 49 | value = newValue; 50 | } 51 | 52 | @Override 53 | public String toString() { 54 | return new ToStringCreator(this).append("id", id).append("value", value).toString(); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/expected/ExpectedQueryOnMethodTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.expected; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.test.context.TestExecutionListeners; 21 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 22 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 23 | import org.springframework.transaction.annotation.Transactional; 24 | 25 | import com.github.springtestdbunit.DbUnitTestExecutionListener; 26 | import com.github.springtestdbunit.annotation.ExpectedDatabase; 27 | import com.github.springtestdbunit.config.CoreTestConfiguration; 28 | 29 | @SpringJUnitConfig(CoreTestConfiguration.class) 30 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class }) 31 | @Transactional 32 | public class ExpectedQueryOnMethodTest { 33 | 34 | @Test 35 | @ExpectedDatabase(value = "/META-INF/db/expected_query.xml", query = "select * from SampleEntity where id in (1,2)", table = "SampleEntity") 36 | public void test() throws Exception { 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/dataset/DataSetLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.dataset; 18 | 19 | import java.io.IOException; 20 | 21 | import org.dbunit.dataset.DataSetException; 22 | import org.dbunit.dataset.IDataSet; 23 | 24 | /** 25 | * Strategy interface for {@link #loadDataSet loading} a {@link IDataSet dataset}. 26 | *

27 | * Concrete implementations must provide a {@code public} no-args constructor. 28 | * 29 | * @author Phillip Webb 30 | * 31 | * @see FlatXmlDataSetLoader 32 | */ 33 | public interface DataSetLoader { 34 | 35 | /** 36 | * Load and return {@link IDataSet dataset} from the specified. If the dataset cannot be found {@code null} may be 37 | * returned. 38 | * @param testClass The class under test 39 | * @param location The location to load 40 | * @return a {@link IDataSet dataset} or {@code null} 41 | * @throws DataSetException An exception thrown if the dataset itself has a problem. 42 | * @throws IOException An exception thrown if the dataset could not be loaded. 43 | */ 44 | IDataSet loadDataSet(Class testClass, String location) throws DataSetException, IOException; 45 | 46 | } 47 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/expected/ExpectedQueryFailureOnMethodTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.expected; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.test.context.TestExecutionListeners; 21 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 22 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 23 | import org.springframework.transaction.annotation.Transactional; 24 | 25 | import com.github.springtestdbunit.annotation.ExpectedDatabase; 26 | import com.github.springtestdbunit.config.CoreTestConfiguration; 27 | import com.github.springtestdbunit.testutils.MustFailDbUnitTestExecutionListener; 28 | 29 | @SpringJUnitConfig(CoreTestConfiguration.class) 30 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, MustFailDbUnitTestExecutionListener.class }) 31 | @Transactional 32 | public class ExpectedQueryFailureOnMethodTest { 33 | 34 | @Test 35 | @ExpectedDatabase(value = "/META-INF/db/expected_query.xml", query = "select * from SampleEntity where id=1", table = "SampleEntity") 36 | public void test() throws Exception { 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /spring-test-dbunit-sample/src/main/java/com/github/springtestdbunit/sample/entity/Person.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.sample.entity; 18 | 19 | import jakarta.persistence.Entity; 20 | import jakarta.persistence.Id; 21 | import jakarta.persistence.NamedQueries; 22 | import jakarta.persistence.NamedQuery; 23 | 24 | @Entity 25 | @NamedQueries({ @NamedQuery(name = "Person.find", query = "SELECT p from Person p where p.firstName like :name " 26 | + "or p.lastName like :name") }) 27 | public class Person { 28 | 29 | @Id 30 | private int id; 31 | 32 | private String title; 33 | 34 | private String firstName; 35 | 36 | private String lastName; 37 | 38 | public int getId() { 39 | return id; 40 | } 41 | 42 | public String getTitle() { 43 | return title; 44 | } 45 | 46 | public void setTitle(String title) { 47 | this.title = title; 48 | } 49 | 50 | public String getFirstName() { 51 | return firstName; 52 | } 53 | 54 | public void setFirstName(String firstName) { 55 | this.firstName = firstName; 56 | } 57 | 58 | public String getLastName() { 59 | return lastName; 60 | } 61 | 62 | public void setLastName(String lastName) { 63 | this.lastName = lastName; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/dataset/SqlLoaderControlDataSetLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | package com.github.springtestdbunit.dataset; 18 | 19 | import java.io.File; 20 | import java.io.IOException; 21 | 22 | import org.dbunit.dataset.DataSetException; 23 | import org.dbunit.dataset.IDataSet; 24 | import org.dbunit.dataset.sqlloader.SqlLoaderControlDataSet; 25 | import org.springframework.core.io.Resource; 26 | 27 | /** 28 | * A {@link DataSetLoader data set loader} that can be used to load {@link SqlLoaderControlDataSet}s. 29 | * 30 | * @author Paul Podgorsek 31 | */ 32 | public class SqlLoaderControlDataSetLoader extends AbstractDataSetLoader { 33 | 34 | private static final String ORDERED_TABLE_FILE = "tables.lst"; 35 | 36 | @Override 37 | protected IDataSet createDataSet(final Resource resource) throws IOException, DataSetException { 38 | 39 | File ctlDir = resource.getFile(); 40 | String ctlDirPath = ctlDir.getAbsolutePath(); 41 | 42 | if (!ctlDirPath.endsWith("/")) { 43 | ctlDirPath = ctlDirPath + "/"; 44 | } 45 | 46 | File orderedTablesFile = new File(ctlDirPath + ORDERED_TABLE_FILE); 47 | 48 | return new SqlLoaderControlDataSet(ctlDir, orderedTablesFile); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/expected/ExpectedQueryNonStrictOnMethodTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.expected; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.test.context.TestExecutionListeners; 21 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 22 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 23 | import org.springframework.transaction.annotation.Transactional; 24 | 25 | import com.github.springtestdbunit.DbUnitTestExecutionListener; 26 | import com.github.springtestdbunit.annotation.ExpectedDatabase; 27 | import com.github.springtestdbunit.assertion.DatabaseAssertionMode; 28 | import com.github.springtestdbunit.config.CoreTestConfiguration; 29 | 30 | @SpringJUnitConfig(CoreTestConfiguration.class) 31 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class }) 32 | @Transactional 33 | public class ExpectedQueryNonStrictOnMethodTest { 34 | 35 | @Test 36 | @ExpectedDatabase(value = "/META-INF/db/expected_query_nonstrict.xml", assertionMode = DatabaseAssertionMode.NON_STRICT, query = "select * from SampleEntity where id in (1,2)", table = "SampleEntity") 37 | public void test() throws Exception { 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/operation/MicrosoftSqlDatabaseOperationLookup.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.operation; 18 | 19 | import java.util.HashMap; 20 | import java.util.Map; 21 | 22 | import org.dbunit.ext.mssql.InsertIdentityOperation; 23 | 24 | import com.github.springtestdbunit.annotation.DatabaseOperation; 25 | 26 | /** 27 | * Microsoft SQL Server implementation of {@link DatabaseOperationLookup}. 28 | * 29 | * @author Phillip Webb 30 | */ 31 | public class MicrosoftSqlDatabaseOperationLookup extends DefaultDatabaseOperationLookup { 32 | 33 | private static Map MSSQL_LOOKUP; 34 | 35 | static { 36 | MSSQL_LOOKUP = new HashMap(); 37 | MSSQL_LOOKUP.put(DatabaseOperation.INSERT, InsertIdentityOperation.INSERT); 38 | MSSQL_LOOKUP.put(DatabaseOperation.REFRESH, InsertIdentityOperation.REFRESH); 39 | MSSQL_LOOKUP.put(DatabaseOperation.CLEAN_INSERT, InsertIdentityOperation.CLEAN_INSERT); 40 | } 41 | 42 | @Override 43 | public org.dbunit.operation.DatabaseOperation get(DatabaseOperation operation) { 44 | if (MSSQL_LOOKUP.containsKey(operation)) { 45 | return MSSQL_LOOKUP.get(operation); 46 | } 47 | return super.get(operation); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/setup/AbstractParentDatabaseSetup.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.springtestdbunit.setup; 17 | 18 | import org.springframework.test.context.TestExecutionListeners; 19 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 20 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 21 | import org.springframework.transaction.annotation.Transactional; 22 | 23 | import com.github.springtestdbunit.TransactionDbUnitTestExecutionListener; 24 | import com.github.springtestdbunit.annotation.DatabaseOperation; 25 | import com.github.springtestdbunit.annotation.DatabaseSetup; 26 | import com.github.springtestdbunit.config.CoreTestConfiguration; 27 | 28 | /** 29 | * Parent class carrying setup annotations. This class does not contain any tests, those are defined in the child 30 | * classes in order to test the inheritance of the setup. 31 | * 32 | * @author Paul Podgorsek 33 | */ 34 | @SpringJUnitConfig(CoreTestConfiguration.class) 35 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 36 | TransactionDbUnitTestExecutionListener.class }) 37 | @DatabaseSetup(type = DatabaseOperation.CLEAN_INSERT, value = "/META-INF/db/insert.xml") 38 | @Transactional 39 | public abstract class AbstractParentDatabaseSetup { 40 | 41 | } 42 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/expected/ExpectedQueryNonStrictFailureOnMethodTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.expected; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.test.context.TestExecutionListeners; 21 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 22 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 23 | import org.springframework.transaction.annotation.Transactional; 24 | 25 | import com.github.springtestdbunit.annotation.ExpectedDatabase; 26 | import com.github.springtestdbunit.assertion.DatabaseAssertionMode; 27 | import com.github.springtestdbunit.config.CoreTestConfiguration; 28 | import com.github.springtestdbunit.testutils.MustFailDbUnitTestExecutionListener; 29 | 30 | @SpringJUnitConfig(CoreTestConfiguration.class) 31 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, MustFailDbUnitTestExecutionListener.class }) 32 | @Transactional 33 | public class ExpectedQueryNonStrictFailureOnMethodTest { 34 | 35 | @Test 36 | @ExpectedDatabase(value = "/META-INF/db/expected_query_nonstrict.xml", assertionMode = DatabaseAssertionMode.NON_STRICT, query = "select * from SampleEntity where id=1", table = "SampleEntity") 37 | public void test() throws Exception { 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/expected/ExpectedFailureOnClassTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.expected; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.annotation.ExpectedDatabase; 27 | import com.github.springtestdbunit.config.CoreTestConfiguration; 28 | import com.github.springtestdbunit.entity.EntityAssert; 29 | import com.github.springtestdbunit.testutils.MustFailDbUnitTestExecutionListener; 30 | 31 | @SpringJUnitConfig(CoreTestConfiguration.class) 32 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, MustFailDbUnitTestExecutionListener.class }) 33 | @ExpectedDatabase("/META-INF/db/expectedfail.xml") 34 | @Transactional 35 | public class ExpectedFailureOnClassTest { 36 | 37 | @Autowired 38 | private EntityAssert entityAssert; 39 | 40 | @Test 41 | public void test() throws Exception { 42 | this.entityAssert.assertValues("existing1", "existing2"); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/expected/ExpectedFailureOnMethodTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.expected; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.annotation.ExpectedDatabase; 27 | import com.github.springtestdbunit.config.CoreTestConfiguration; 28 | import com.github.springtestdbunit.entity.EntityAssert; 29 | import com.github.springtestdbunit.testutils.MustFailDbUnitTestExecutionListener; 30 | 31 | @SpringJUnitConfig(CoreTestConfiguration.class) 32 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, MustFailDbUnitTestExecutionListener.class }) 33 | @Transactional 34 | public class ExpectedFailureOnMethodTest { 35 | 36 | @Autowired 37 | private EntityAssert entityAssert; 38 | 39 | @Test 40 | @ExpectedDatabase("/META-INF/db/expectedfail.xml") 41 | public void test() throws Exception { 42 | entityAssert.assertValues("existing1", "existing2"); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/testutils/ExtendedTestContextManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.testutils; 18 | 19 | import java.lang.reflect.Constructor; 20 | 21 | import org.springframework.test.annotation.DirtiesContext.HierarchyMode; 22 | import org.springframework.test.context.TestContext; 23 | import org.springframework.test.context.TestContextManager; 24 | 25 | /** 26 | * Extended version of the {@link TestContextManager} that can be used when writing tests. 27 | * 28 | * @author Phillip Webb 29 | */ 30 | public class ExtendedTestContextManager extends TestContextManager { 31 | 32 | private Object testInstance; 33 | 34 | public ExtendedTestContextManager(Class testClass) throws Exception { 35 | super(testClass); 36 | getTestContext().markApplicationContextDirty(HierarchyMode.CURRENT_LEVEL); 37 | Constructor constructor = testClass.getDeclaredConstructor(); 38 | constructor.setAccessible(true); 39 | testInstance = constructor.newInstance(); 40 | } 41 | 42 | public void prepareTestInstance() throws Exception { 43 | prepareTestInstance(testInstance); 44 | } 45 | 46 | public Object getTestContextAttribute(String name) { 47 | return getTestContext().getAttribute(name); 48 | } 49 | 50 | public TestContext accessTestContext() { 51 | return getTestContext(); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/setup/DeleteAllSetupOnClassTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.setup; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.TransactionDbUnitTestExecutionListener; 27 | import com.github.springtestdbunit.annotation.DatabaseOperation; 28 | import com.github.springtestdbunit.annotation.DatabaseSetup; 29 | import com.github.springtestdbunit.config.CoreTestConfiguration; 30 | import com.github.springtestdbunit.entity.EntityAssert; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | TransactionDbUnitTestExecutionListener.class }) 35 | @Transactional 36 | @DatabaseSetup(type = DatabaseOperation.DELETE_ALL, value = "/META-INF/db/delete.xml") 37 | public class DeleteAllSetupOnClassTest { 38 | 39 | @Autowired 40 | private EntityAssert entityAssert; 41 | 42 | @Test 43 | public void test() throws Exception { 44 | entityAssert.assertValues(); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/setup/DeleteSetupOnClassTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.setup; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.TransactionDbUnitTestExecutionListener; 27 | import com.github.springtestdbunit.annotation.DatabaseOperation; 28 | import com.github.springtestdbunit.annotation.DatabaseSetup; 29 | import com.github.springtestdbunit.config.CoreTestConfiguration; 30 | import com.github.springtestdbunit.entity.EntityAssert; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | TransactionDbUnitTestExecutionListener.class }) 35 | @DatabaseSetup(type = DatabaseOperation.DELETE, value = "/META-INF/db/delete.xml") 36 | @Transactional 37 | public class DeleteSetupOnClassTest { 38 | 39 | @Autowired 40 | private EntityAssert entityAssert; 41 | 42 | @Test 43 | public void test() throws Exception { 44 | entityAssert.assertValues("existing2"); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/setup/DeleteSetupOnMethodTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.setup; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.TransactionDbUnitTestExecutionListener; 27 | import com.github.springtestdbunit.annotation.DatabaseOperation; 28 | import com.github.springtestdbunit.annotation.DatabaseSetup; 29 | import com.github.springtestdbunit.config.CoreTestConfiguration; 30 | import com.github.springtestdbunit.entity.EntityAssert; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | TransactionDbUnitTestExecutionListener.class }) 35 | @Transactional 36 | public class DeleteSetupOnMethodTest { 37 | 38 | @Autowired 39 | private EntityAssert entityAssert; 40 | 41 | @Test 42 | @DatabaseSetup(type = DatabaseOperation.DELETE, value = "/META-INF/db/delete.xml") 43 | public void test() throws Exception { 44 | entityAssert.assertValues("existing2"); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/setup/TruncateSetupOnClassTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.setup; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.TransactionDbUnitTestExecutionListener; 27 | import com.github.springtestdbunit.annotation.DatabaseOperation; 28 | import com.github.springtestdbunit.annotation.DatabaseSetup; 29 | import com.github.springtestdbunit.config.CoreTestConfiguration; 30 | import com.github.springtestdbunit.entity.EntityAssert; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | TransactionDbUnitTestExecutionListener.class }) 35 | @Transactional 36 | @DatabaseSetup(type = DatabaseOperation.TRUNCATE_TABLE, value = "/META-INF/db/delete.xml") 37 | public class TruncateSetupOnClassTest { 38 | 39 | @Autowired 40 | private EntityAssert entityAssert; 41 | 42 | @Test 43 | public void test() throws Exception { 44 | entityAssert.assertValues(); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/operation/DefaultDatabaseOperationLookupTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.operation; 18 | 19 | import static org.junit.jupiter.api.Assertions.assertSame; 20 | 21 | import org.junit.jupiter.api.Test; 22 | 23 | import com.github.springtestdbunit.annotation.DatabaseOperation; 24 | 25 | /** 26 | * Tests for {@link DefaultDatabaseOperationLookup}. 27 | * 28 | * @author Phillip Webb 29 | */ 30 | public class DefaultDatabaseOperationLookupTest { 31 | 32 | @Test 33 | public void shouldLookup() throws Exception { 34 | DefaultDatabaseOperationLookup lookup = new DefaultDatabaseOperationLookup(); 35 | assertSame(org.dbunit.operation.DatabaseOperation.UPDATE, lookup.get(DatabaseOperation.UPDATE)); 36 | assertSame(org.dbunit.operation.DatabaseOperation.INSERT, lookup.get(DatabaseOperation.INSERT)); 37 | assertSame(org.dbunit.operation.DatabaseOperation.REFRESH, lookup.get(DatabaseOperation.REFRESH)); 38 | assertSame(org.dbunit.operation.DatabaseOperation.DELETE, lookup.get(DatabaseOperation.DELETE)); 39 | assertSame(org.dbunit.operation.DatabaseOperation.DELETE_ALL, lookup.get(DatabaseOperation.DELETE_ALL)); 40 | assertSame(org.dbunit.operation.DatabaseOperation.TRUNCATE_TABLE, lookup.get(DatabaseOperation.TRUNCATE_TABLE)); 41 | assertSame(org.dbunit.operation.DatabaseOperation.CLEAN_INSERT, lookup.get(DatabaseOperation.CLEAN_INSERT)); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/setup/UpdateSetupOnClassTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.setup; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.TransactionDbUnitTestExecutionListener; 27 | import com.github.springtestdbunit.annotation.DatabaseOperation; 28 | import com.github.springtestdbunit.annotation.DatabaseSetup; 29 | import com.github.springtestdbunit.config.CoreTestConfiguration; 30 | import com.github.springtestdbunit.entity.EntityAssert; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | TransactionDbUnitTestExecutionListener.class }) 35 | @DatabaseSetup(type = DatabaseOperation.UPDATE, value = "/META-INF/db/update.xml") 36 | @Transactional 37 | public class UpdateSetupOnClassTest { 38 | 39 | @Autowired 40 | private EntityAssert entityAssert; 41 | 42 | @Test 43 | public void test() throws Exception { 44 | entityAssert.assertValues("existing2", "fromDbUnit"); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/TransactionDbUnitTestExecutionListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit; 18 | 19 | import org.springframework.test.context.transaction.TransactionalTestExecutionListener; 20 | 21 | import com.github.springtestdbunit.annotation.DatabaseSetup; 22 | import com.github.springtestdbunit.annotation.DatabaseTearDown; 23 | import com.github.springtestdbunit.annotation.ExpectedDatabase; 24 | 25 | /** 26 | * TestExecutionListener which provides support for {@link DatabaseSetup @DatabaseSetup}, 27 | * {@link DatabaseTearDown @DatabaseTearDown} and {@link ExpectedDatabase @ExpectedDatabase} annotations and 28 | * executed tests within {@link TransactionalTestExecutionListener transactions}. 29 | *

30 | * Transactions start before {@link DatabaseSetup @DatabaseSetup} and end after {@link DatabaseTearDown 31 | * @DatabaseTearDown} and {@link ExpectedDatabase @ExpectedDatabase}. 32 | * 33 | * @see TransactionalTestExecutionListener 34 | * @see DbUnitTestExecutionListener 35 | * @author Phillip Webb 36 | */ 37 | public class TransactionDbUnitTestExecutionListener extends TestExecutionListenerChain { 38 | 39 | private static final Class[] CHAIN = { TransactionalTestExecutionListener.class, 40 | DbUnitTestExecutionListener.class }; 41 | 42 | @Override 43 | protected Class[] getChain() { 44 | return CHAIN; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/setup/CleanInsertSetupOnClassTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.setup; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.TransactionDbUnitTestExecutionListener; 27 | import com.github.springtestdbunit.annotation.DatabaseOperation; 28 | import com.github.springtestdbunit.annotation.DatabaseSetup; 29 | import com.github.springtestdbunit.config.CoreTestConfiguration; 30 | import com.github.springtestdbunit.entity.EntityAssert; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | TransactionDbUnitTestExecutionListener.class }) 35 | @DatabaseSetup(type = DatabaseOperation.CLEAN_INSERT, value = "/META-INF/db/insert.xml") 36 | @Transactional 37 | public class CleanInsertSetupOnClassTest { 38 | 39 | @Autowired 40 | private EntityAssert entityAssert; 41 | 42 | @Test 43 | public void test() throws Exception { 44 | entityAssert.assertValues("fromDbUnit"); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/setup/UpdateSetupOnMethodTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.setup; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.TransactionDbUnitTestExecutionListener; 27 | import com.github.springtestdbunit.annotation.DatabaseOperation; 28 | import com.github.springtestdbunit.annotation.DatabaseSetup; 29 | import com.github.springtestdbunit.config.CoreTestConfiguration; 30 | import com.github.springtestdbunit.entity.EntityAssert; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | TransactionDbUnitTestExecutionListener.class }) 35 | @Transactional 36 | public class UpdateSetupOnMethodTest { 37 | 38 | @Autowired 39 | private EntityAssert entityAssert; 40 | 41 | @Test 42 | @DatabaseSetup(type = DatabaseOperation.UPDATE, value = "/META-INF/db/update.xml") 43 | public void test() throws Exception { 44 | entityAssert.assertValues("existing2", "fromDbUnit"); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/setup/InsertSetupOnClassTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.setup; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.TransactionDbUnitTestExecutionListener; 27 | import com.github.springtestdbunit.annotation.DatabaseOperation; 28 | import com.github.springtestdbunit.annotation.DatabaseSetup; 29 | import com.github.springtestdbunit.config.CoreTestConfiguration; 30 | import com.github.springtestdbunit.entity.EntityAssert; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | TransactionDbUnitTestExecutionListener.class }) 35 | @DatabaseSetup(type = DatabaseOperation.INSERT, value = "/META-INF/db/insert.xml") 36 | @Transactional 37 | public class InsertSetupOnClassTest { 38 | 39 | @Autowired 40 | private EntityAssert entityAssert; 41 | 42 | @Test 43 | public void test() throws Exception { 44 | entityAssert.assertValues("existing1", "existing2", "fromDbUnit"); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/setup/InsertSetupOnMethodTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.setup; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.TransactionDbUnitTestExecutionListener; 27 | import com.github.springtestdbunit.annotation.DatabaseOperation; 28 | import com.github.springtestdbunit.annotation.DatabaseSetup; 29 | import com.github.springtestdbunit.config.CoreTestConfiguration; 30 | import com.github.springtestdbunit.entity.EntityAssert; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | TransactionDbUnitTestExecutionListener.class }) 35 | @Transactional 36 | public class InsertSetupOnMethodTest { 37 | 38 | @Autowired 39 | private EntityAssert entityAssert; 40 | 41 | @Test 42 | @DatabaseSetup(type = DatabaseOperation.INSERT, value = "/META-INF/db/insert.xml") 43 | public void test() throws Exception { 44 | entityAssert.assertValues("existing1", "existing2", "fromDbUnit"); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/operation/MicrosoftSqlDatabaseOperationLookupTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.operation; 18 | 19 | import static org.junit.jupiter.api.Assertions.assertSame; 20 | 21 | import org.dbunit.ext.mssql.InsertIdentityOperation; 22 | import org.junit.jupiter.api.Test; 23 | 24 | import com.github.springtestdbunit.annotation.DatabaseOperation; 25 | 26 | /** 27 | * Tests for {@link MicrosoftSqlDatabaseOperationLookup}. 28 | * 29 | * @author Phillip Webb 30 | */ 31 | public class MicrosoftSqlDatabaseOperationLookupTest { 32 | 33 | @Test 34 | public void shouldLookup() throws Exception { 35 | DefaultDatabaseOperationLookup lookup = new MicrosoftSqlDatabaseOperationLookup(); 36 | assertSame(org.dbunit.operation.DatabaseOperation.UPDATE, lookup.get(DatabaseOperation.UPDATE)); 37 | assertSame(InsertIdentityOperation.INSERT, lookup.get(DatabaseOperation.INSERT)); 38 | assertSame(InsertIdentityOperation.REFRESH, lookup.get(DatabaseOperation.REFRESH)); 39 | assertSame(org.dbunit.operation.DatabaseOperation.DELETE, lookup.get(DatabaseOperation.DELETE)); 40 | assertSame(org.dbunit.operation.DatabaseOperation.DELETE_ALL, lookup.get(DatabaseOperation.DELETE_ALL)); 41 | assertSame(org.dbunit.operation.DatabaseOperation.TRUNCATE_TABLE, lookup.get(DatabaseOperation.TRUNCATE_TABLE)); 42 | assertSame(InsertIdentityOperation.CLEAN_INSERT, lookup.get(DatabaseOperation.CLEAN_INSERT)); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/setup/RefreshSetupOnClassTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.setup; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.TransactionDbUnitTestExecutionListener; 27 | import com.github.springtestdbunit.annotation.DatabaseOperation; 28 | import com.github.springtestdbunit.annotation.DatabaseSetup; 29 | import com.github.springtestdbunit.config.CoreTestConfiguration; 30 | import com.github.springtestdbunit.entity.EntityAssert; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | TransactionDbUnitTestExecutionListener.class }) 35 | @DatabaseSetup(type = DatabaseOperation.REFRESH, value = "/META-INF/db/refresh.xml") 36 | @Transactional 37 | public class RefreshSetupOnClassTest { 38 | 39 | @Autowired 40 | private EntityAssert entityAssert; 41 | 42 | @Test 43 | public void test() throws Exception { 44 | entityAssert.assertValues("existing2", "addedFromDbUnit", "replacedFromDbUnit"); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/setup/RefreshSetupOnMethodTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.setup; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.TransactionDbUnitTestExecutionListener; 27 | import com.github.springtestdbunit.annotation.DatabaseOperation; 28 | import com.github.springtestdbunit.annotation.DatabaseSetup; 29 | import com.github.springtestdbunit.config.CoreTestConfiguration; 30 | import com.github.springtestdbunit.entity.EntityAssert; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | TransactionDbUnitTestExecutionListener.class }) 35 | @Transactional 36 | public class RefreshSetupOnMethodTest { 37 | 38 | @Autowired 39 | private EntityAssert entityAssert; 40 | 41 | @Test 42 | @DatabaseSetup(type = DatabaseOperation.REFRESH, value = "/META-INF/db/refresh.xml") 43 | public void test() throws Exception { 44 | entityAssert.assertValues("existing2", "addedFromDbUnit", "replacedFromDbUnit"); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/assertion/DatabaseAssertion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.assertion; 18 | 19 | import java.util.List; 20 | 21 | import org.dbunit.DatabaseUnitException; 22 | import org.dbunit.dataset.IDataSet; 23 | import org.dbunit.dataset.ITable; 24 | import org.dbunit.dataset.filter.IColumnFilter; 25 | 26 | /** 27 | * Database assertion strategy interface. 28 | * 29 | * @author Mario Zagar 30 | * @author Sunitha Rajarathnam 31 | */ 32 | public interface DatabaseAssertion { 33 | 34 | /** 35 | * Assert that the specified {@link IDataSet dataSets} are conceptually equal. 36 | * @param expectedDataSet the expected dataset 37 | * @param actualDataSet the actual dataset 38 | * @param columnFilters any column filters to apply 39 | * @throws DatabaseUnitException if the datasets are not equal 40 | */ 41 | void assertEquals(IDataSet expectedDataSet, IDataSet actualDataSet, List columnFilters) 42 | throws DatabaseUnitException; 43 | 44 | /** 45 | * Assert that the specified {@link IDataSet dataSets} are conceptually equal. 46 | * @param expectedTable the expected table 47 | * @param actualTable the actual table 48 | * @param columnFilters any column filters to apply 49 | * @throws DatabaseUnitException if the tables are not equal 50 | */ 51 | void assertEquals(ITable expectedTable, ITable actualTable, List columnFilters) 52 | throws DatabaseUnitException; 53 | 54 | } 55 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/setup/MultipleInsertSetupOnClassTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.setup; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.TransactionDbUnitTestExecutionListener; 27 | import com.github.springtestdbunit.annotation.DatabaseOperation; 28 | import com.github.springtestdbunit.annotation.DatabaseSetup; 29 | import com.github.springtestdbunit.config.CoreTestConfiguration; 30 | import com.github.springtestdbunit.entity.EntityAssert; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | TransactionDbUnitTestExecutionListener.class }) 35 | @DatabaseSetup(type = DatabaseOperation.CLEAN_INSERT, value = { "/META-INF/db/insert.xml", "/META-INF/db/insert2.xml" }) 36 | @Transactional 37 | public class MultipleInsertSetupOnClassTest { 38 | 39 | @Autowired 40 | private EntityAssert entityAssert; 41 | 42 | @Test 43 | public void test() throws Exception { 44 | entityAssert.assertValues("fromDbUnit", "fromDbUnit2"); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/assertion/NonStrictUnorderedDatabaseAssertion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.assertion; 18 | 19 | import java.util.List; 20 | 21 | import org.dbunit.DatabaseUnitException; 22 | import org.dbunit.dataset.Column; 23 | import org.dbunit.dataset.ITable; 24 | import org.dbunit.dataset.SortedTable; 25 | import org.dbunit.dataset.filter.IColumnFilter; 26 | 27 | /** 28 | * Implements non-strict unordered database assertion strategy : compares data sets ignoring all tables and columns 29 | * which are not specified in expected data set but possibly exist in actual data set and sorting rows in expected and 30 | * actual data sets with column order in expected data set to ignore row orders in expected and actual data sets. 31 | * 32 | * @author Mario Zagar 33 | * @author Sunitha Rajarathnam 34 | * @author Mehmet Aslan 35 | */ 36 | class NonStrictUnorderedDatabaseAssertion extends NonStrictDatabaseAssertion { 37 | 38 | @Override 39 | public void assertEquals(ITable expectedSortedTable, ITable actualSortedTable, List columnFilters) 40 | throws DatabaseUnitException { 41 | Column[] expectedColumns = expectedSortedTable.getTableMetaData().getColumns(); 42 | expectedSortedTable = new SortedTable(expectedSortedTable, expectedColumns); 43 | actualSortedTable = new SortedTable(actualSortedTable, expectedColumns); 44 | super.assertEquals(expectedSortedTable, actualSortedTable, columnFilters); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/annotation/ExpectedDatabaseAnnotationAttributes.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.springtestdbunit.annotation; 17 | 18 | import java.lang.annotation.Annotation; 19 | import java.util.ArrayList; 20 | import java.util.Collection; 21 | import java.util.List; 22 | import java.util.Map; 23 | 24 | import org.springframework.core.annotation.AnnotationUtils; 25 | import org.springframework.util.Assert; 26 | 27 | public class ExpectedDatabaseAnnotationAttributes extends AbstractDatabaseAnnotationAttributes { 28 | 29 | private final String value; 30 | 31 | public ExpectedDatabaseAnnotationAttributes(final Annotation annotation) { 32 | 33 | super(annotation); 34 | 35 | Assert.state((annotation instanceof ExpectedDatabase), "Only ExpectedDatabase annotations are supported"); 36 | 37 | Map attributes = AnnotationUtils.getAnnotationAttributes(annotation); 38 | value = (String) attributes.get("value"); 39 | } 40 | 41 | public static Collection get( 42 | final Annotations annotations) { 43 | 44 | List annotationAttributes = new ArrayList(); 45 | 46 | for (T annotation : annotations) { 47 | annotationAttributes.add(new ExpectedDatabaseAnnotationAttributes(annotation)); 48 | } 49 | 50 | return annotationAttributes; 51 | } 52 | 53 | public String getValue() { 54 | return value; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/expected/ExpectedNonStrictOnMethodTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.expected; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.DbUnitTestExecutionListener; 27 | import com.github.springtestdbunit.annotation.ExpectedDatabase; 28 | import com.github.springtestdbunit.assertion.DatabaseAssertionMode; 29 | import com.github.springtestdbunit.config.CoreTestConfiguration; 30 | import com.github.springtestdbunit.entity.EntityAssert; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class }) 34 | @Transactional 35 | public class ExpectedNonStrictOnMethodTest { 36 | 37 | @Autowired 38 | private EntityAssert entityAssert; 39 | 40 | @Test 41 | @ExpectedDatabase(value = "/META-INF/db/expected_nonstrict.xml", assertionMode = DatabaseAssertionMode.NON_STRICT) 42 | public void shouldNotFailEvenThoughExpectedTableDoesNotSpecifyAllColumns() { 43 | entityAssert.assertValues("existing1", "existing2"); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/DatabaseConnections.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit; 18 | 19 | import java.sql.SQLException; 20 | 21 | import org.dbunit.database.IDatabaseConnection; 22 | import org.springframework.util.Assert; 23 | import org.springframework.util.StringUtils; 24 | 25 | /** 26 | * Holds a number of {@link IDatabaseConnection} beans. 27 | * 28 | * @author Phillip Webb 29 | */ 30 | public class DatabaseConnections { 31 | 32 | private final String[] names; 33 | 34 | private final IDatabaseConnection[] connections; 35 | 36 | public DatabaseConnections(String[] names, IDatabaseConnection[] connections) { 37 | Assert.notEmpty(names, "Names must not be empty"); 38 | Assert.notEmpty(connections, "Connections must not be empty"); 39 | Assert.isTrue(names.length == connections.length, "Names and Connections must have the same length"); 40 | this.names = names; 41 | this.connections = connections; 42 | } 43 | 44 | public void closeAll() throws SQLException { 45 | for (IDatabaseConnection connection : this.connections) { 46 | connection.close(); 47 | } 48 | } 49 | 50 | public IDatabaseConnection get(String name) { 51 | if (!StringUtils.hasLength(name)) { 52 | return this.connections[0]; 53 | } 54 | for (int i = 0; i < this.names.length; i++) { 55 | if (this.names[i].equals(name)) { 56 | return this.connections[i]; 57 | } 58 | } 59 | throw new IllegalStateException("Unable to find connection named " + name); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/expected/ExpectedNonStrictOnClassTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.expected; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.DbUnitTestExecutionListener; 27 | import com.github.springtestdbunit.annotation.ExpectedDatabase; 28 | import com.github.springtestdbunit.assertion.DatabaseAssertionMode; 29 | import com.github.springtestdbunit.config.CoreTestConfiguration; 30 | import com.github.springtestdbunit.entity.EntityAssert; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class }) 34 | @ExpectedDatabase(value = "/META-INF/db/expected_nonstrict.xml", assertionMode = DatabaseAssertionMode.NON_STRICT) 35 | @Transactional 36 | public class ExpectedNonStrictOnClassTest { 37 | 38 | @Autowired 39 | private EntityAssert entityAssert; 40 | 41 | @Test 42 | public void shouldNotFailEvenThoughExpectedTableDoesNotSpecifyAllColumns() throws Exception { 43 | entityAssert.assertValues("existing1", "existing2"); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/expected/ExpectedOnClassAndMethodTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.expected; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.DbUnitTestExecutionListener; 27 | import com.github.springtestdbunit.annotation.ExpectedDatabase; 28 | import com.github.springtestdbunit.assertion.DatabaseAssertionMode; 29 | import com.github.springtestdbunit.config.CoreTestConfiguration; 30 | import com.github.springtestdbunit.entity.EntityAssert; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class }) 34 | @ExpectedDatabase(value = "/META-INF/db/expectedfail.xml") 35 | @Transactional 36 | public class ExpectedOnClassAndMethodTest { 37 | 38 | @Autowired 39 | private EntityAssert entityAssert; 40 | 41 | @Test 42 | @ExpectedDatabase(value = "/META-INF/db/expected_nonstrict.xml", assertionMode = DatabaseAssertionMode.NON_STRICT) 43 | public void shouldUseMethodExpectation() { 44 | entityAssert.assertValues("existing1", "existing2"); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/expected/ExpectedFailureOnMethodWithRepeatableTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.expected; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.annotation.ExpectedDatabase; 27 | import com.github.springtestdbunit.annotation.ExpectedDatabases; 28 | import com.github.springtestdbunit.config.CoreTestConfiguration; 29 | import com.github.springtestdbunit.entity.EntityAssert; 30 | import com.github.springtestdbunit.testutils.MustFailDbUnitTestExecutionListener; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, MustFailDbUnitTestExecutionListener.class }) 34 | @Transactional 35 | public class ExpectedFailureOnMethodWithRepeatableTest { 36 | 37 | @Autowired 38 | private EntityAssert entityAssert; 39 | 40 | @Test 41 | @ExpectedDatabases({ @ExpectedDatabase("/META-INF/db/expectedfail.xml"), 42 | @ExpectedDatabase("/META-INF/db/expectedsuccess.xml") }) 43 | public void test() throws Exception { 44 | entityAssert.assertValues("existing1", "existing2"); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/expected/ExpectedFailureOnMethodWithRepeatableReverseTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.expected; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.annotation.ExpectedDatabase; 27 | import com.github.springtestdbunit.annotation.ExpectedDatabases; 28 | import com.github.springtestdbunit.config.CoreTestConfiguration; 29 | import com.github.springtestdbunit.entity.EntityAssert; 30 | import com.github.springtestdbunit.testutils.MustFailDbUnitTestExecutionListener; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, MustFailDbUnitTestExecutionListener.class }) 34 | @Transactional 35 | public class ExpectedFailureOnMethodWithRepeatableReverseTest { 36 | 37 | @Autowired 38 | private EntityAssert entityAssert; 39 | 40 | @Test 41 | @ExpectedDatabases({ @ExpectedDatabase("/META-INF/db/expectedsuccess.xml"), 42 | @ExpectedDatabase("/META-INF/db/expectedfail.xml") }) 43 | public void test() throws Exception { 44 | entityAssert.assertValues("existing1", "existing2"); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/expected/ExpectedTableNonStrictOnClassTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.expected; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.DbUnitTestExecutionListener; 27 | import com.github.springtestdbunit.annotation.ExpectedDatabase; 28 | import com.github.springtestdbunit.assertion.DatabaseAssertionMode; 29 | import com.github.springtestdbunit.config.CoreTestConfiguration; 30 | import com.github.springtestdbunit.entity.EntityAssert; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class }) 34 | @ExpectedDatabase(value = "/META-INF/db/expected_nonstrict.xml", assertionMode = DatabaseAssertionMode.NON_STRICT, table = "SampleEntity") 35 | @Transactional 36 | public class ExpectedTableNonStrictOnClassTest { 37 | 38 | @Autowired 39 | private EntityAssert entityAssert; 40 | 41 | @Test 42 | public void shouldNotFailEvenThoughExpectedTableDoesNotSpecifyAllColumns() throws Exception { 43 | entityAssert.assertValues("existing1", "existing2"); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/teardown/DeleteTearDownOnClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.teardown; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.annotation.DatabaseOperation; 27 | import com.github.springtestdbunit.annotation.DatabaseTearDown; 28 | import com.github.springtestdbunit.config.CoreTestConfiguration; 29 | import com.github.springtestdbunit.entity.EntityAssert; 30 | import com.github.springtestdbunit.testutils.AfterTearDownDbUnitTestExecutionListener; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | AfterTearDownDbUnitTestExecutionListener.class }) 35 | @DatabaseTearDown(type = DatabaseOperation.DELETE_ALL, value = "/META-INF/db/delete.xml") 36 | @Transactional 37 | public class DeleteTearDownOnClass { 38 | 39 | @Autowired 40 | private EntityAssert entityAssert; 41 | 42 | @Test 43 | public void test() throws Exception { 44 | entityAssert.assertValues("existing1", "existing2"); 45 | } 46 | 47 | public void afterTest() throws Exception { 48 | entityAssert.assertValues(); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/teardown/DeleteTearDownOnMethod.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.teardown; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.annotation.DatabaseOperation; 27 | import com.github.springtestdbunit.annotation.DatabaseTearDown; 28 | import com.github.springtestdbunit.config.CoreTestConfiguration; 29 | import com.github.springtestdbunit.entity.EntityAssert; 30 | import com.github.springtestdbunit.testutils.AfterTearDownDbUnitTestExecutionListener; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | AfterTearDownDbUnitTestExecutionListener.class }) 35 | @Transactional 36 | public class DeleteTearDownOnMethod { 37 | 38 | @Autowired 39 | private EntityAssert entityAssert; 40 | 41 | @Test 42 | @DatabaseTearDown(type = DatabaseOperation.DELETE_ALL, value = "/META-INF/db/delete.xml") 43 | public void test() throws Exception { 44 | entityAssert.assertValues("existing1", "existing2"); 45 | } 46 | 47 | public void afterTest() throws Exception { 48 | entityAssert.assertValues(); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/expected/ExpectedNonStrictUnorderedOnMethodTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.expected; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.DbUnitTestExecutionListener; 27 | import com.github.springtestdbunit.annotation.ExpectedDatabase; 28 | import com.github.springtestdbunit.assertion.DatabaseAssertionMode; 29 | import com.github.springtestdbunit.config.CoreTestConfiguration; 30 | import com.github.springtestdbunit.entity.EntityAssert; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class }) 34 | @Transactional 35 | public class ExpectedNonStrictUnorderedOnMethodTest { 36 | 37 | @Autowired 38 | private EntityAssert entityAssert; 39 | 40 | @Test 41 | @ExpectedDatabase(value = "/META-INF/db/expected_nonstrict_unordered.xml", assertionMode = DatabaseAssertionMode.NON_STRICT_UNORDERED) 42 | public void shouldNotFailEvenThoughExpectedTableDoesNotSpecifyAllColumnsAndDoesNotMatchColumnOrders() { 43 | entityAssert.assertValues("existing1", "existing2"); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/teardown/TruncateTearDownOnClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.teardown; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.annotation.DatabaseOperation; 27 | import com.github.springtestdbunit.annotation.DatabaseTearDown; 28 | import com.github.springtestdbunit.config.CoreTestConfiguration; 29 | import com.github.springtestdbunit.entity.EntityAssert; 30 | import com.github.springtestdbunit.testutils.AfterTearDownDbUnitTestExecutionListener; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | AfterTearDownDbUnitTestExecutionListener.class }) 35 | @DatabaseTearDown(type = DatabaseOperation.TRUNCATE_TABLE, value = "/META-INF/db/delete.xml") 36 | @Transactional 37 | public class TruncateTearDownOnClass { 38 | 39 | @Autowired 40 | private EntityAssert entityAssert; 41 | 42 | @Test 43 | public void test() throws Exception { 44 | entityAssert.assertValues("existing1", "existing2"); 45 | } 46 | 47 | public void afterTest() throws Exception { 48 | entityAssert.assertValues(); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/teardown/TruncateTearDownOnMethod.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.teardown; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.annotation.DatabaseOperation; 27 | import com.github.springtestdbunit.annotation.DatabaseTearDown; 28 | import com.github.springtestdbunit.config.CoreTestConfiguration; 29 | import com.github.springtestdbunit.entity.EntityAssert; 30 | import com.github.springtestdbunit.testutils.AfterTearDownDbUnitTestExecutionListener; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | AfterTearDownDbUnitTestExecutionListener.class }) 35 | @Transactional 36 | public class TruncateTearDownOnMethod { 37 | 38 | @Autowired 39 | private EntityAssert entityAssert; 40 | 41 | @Test 42 | @DatabaseTearDown(type = DatabaseOperation.TRUNCATE_TABLE, value = "/META-INF/db/delete.xml") 43 | public void test() throws Exception { 44 | entityAssert.assertValues("existing1", "existing2"); 45 | } 46 | 47 | public void afterTest() throws Exception { 48 | entityAssert.assertValues(); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/teardown/DeleteAllTearDownOnMethod.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.teardown; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.annotation.DatabaseOperation; 27 | import com.github.springtestdbunit.annotation.DatabaseTearDown; 28 | import com.github.springtestdbunit.config.CoreTestConfiguration; 29 | import com.github.springtestdbunit.entity.EntityAssert; 30 | import com.github.springtestdbunit.testutils.AfterTearDownDbUnitTestExecutionListener; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | AfterTearDownDbUnitTestExecutionListener.class }) 35 | @Transactional 36 | public class DeleteAllTearDownOnMethod { 37 | 38 | @Autowired 39 | private EntityAssert entityAssert; 40 | 41 | @Test 42 | @DatabaseTearDown(type = DatabaseOperation.DELETE, value = "/META-INF/db/delete.xml") 43 | public void test() throws Exception { 44 | entityAssert.assertValues("existing1", "existing2"); 45 | } 46 | 47 | public void afterTest() throws Exception { 48 | entityAssert.assertValues("existing2"); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/teardown/CleanInsertTearDownOnClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.teardown; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.annotation.DatabaseOperation; 27 | import com.github.springtestdbunit.annotation.DatabaseTearDown; 28 | import com.github.springtestdbunit.config.CoreTestConfiguration; 29 | import com.github.springtestdbunit.entity.EntityAssert; 30 | import com.github.springtestdbunit.testutils.AfterTearDownDbUnitTestExecutionListener; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | AfterTearDownDbUnitTestExecutionListener.class }) 35 | @DatabaseTearDown(type = DatabaseOperation.CLEAN_INSERT, value = "/META-INF/db/insert.xml") 36 | @Transactional 37 | public class CleanInsertTearDownOnClass { 38 | 39 | @Autowired 40 | private EntityAssert entityAssert; 41 | 42 | @Test 43 | public void test() throws Exception { 44 | entityAssert.assertValues("existing1", "existing2"); 45 | } 46 | 47 | public void afterTest() throws Exception { 48 | entityAssert.assertValues("fromDbUnit"); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/teardown/UpdateTearDownOnClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.teardown; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.annotation.DatabaseOperation; 27 | import com.github.springtestdbunit.annotation.DatabaseTearDown; 28 | import com.github.springtestdbunit.config.CoreTestConfiguration; 29 | import com.github.springtestdbunit.entity.EntityAssert; 30 | import com.github.springtestdbunit.testutils.AfterTearDownDbUnitTestExecutionListener; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | AfterTearDownDbUnitTestExecutionListener.class }) 35 | @DatabaseTearDown(type = DatabaseOperation.UPDATE, value = "/META-INF/db/update.xml") 36 | @Transactional 37 | public class UpdateTearDownOnClass { 38 | 39 | @Autowired 40 | private EntityAssert entityAssert; 41 | 42 | @Test 43 | public void test() throws Exception { 44 | entityAssert.assertValues("existing1", "existing2"); 45 | } 46 | 47 | public void afterTest() throws Exception { 48 | entityAssert.assertValues("existing2", "fromDbUnit"); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/teardown/UpdateTearDownOnMethod.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.teardown; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.annotation.DatabaseOperation; 27 | import com.github.springtestdbunit.annotation.DatabaseTearDown; 28 | import com.github.springtestdbunit.config.CoreTestConfiguration; 29 | import com.github.springtestdbunit.entity.EntityAssert; 30 | import com.github.springtestdbunit.testutils.AfterTearDownDbUnitTestExecutionListener; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | AfterTearDownDbUnitTestExecutionListener.class }) 35 | @Transactional 36 | public class UpdateTearDownOnMethod { 37 | 38 | @Autowired 39 | private EntityAssert entityAssert; 40 | 41 | @Test 42 | @DatabaseTearDown(type = DatabaseOperation.UPDATE, value = "/META-INF/db/update.xml") 43 | public void test() throws Exception { 44 | entityAssert.assertValues("existing1", "existing2"); 45 | } 46 | 47 | public void afterTest() throws Exception { 48 | entityAssert.assertValues("existing2", "fromDbUnit"); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/expected/ExpectedNonStrictUnorderedOnClassTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.expected; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.DbUnitTestExecutionListener; 27 | import com.github.springtestdbunit.annotation.ExpectedDatabase; 28 | import com.github.springtestdbunit.assertion.DatabaseAssertionMode; 29 | import com.github.springtestdbunit.config.CoreTestConfiguration; 30 | import com.github.springtestdbunit.entity.EntityAssert; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class }) 34 | @ExpectedDatabase(value = "/META-INF/db/expected_nonstrict_unordered.xml", assertionMode = DatabaseAssertionMode.NON_STRICT_UNORDERED) 35 | @Transactional 36 | public class ExpectedNonStrictUnorderedOnClassTest { 37 | 38 | @Autowired 39 | private EntityAssert entityAssert; 40 | 41 | @Test 42 | public void shouldNotFailEvenThoughExpectedTableDoesNotSpecifyAllColumnsAndDoesNotMatchColumnOrders() 43 | throws Exception { 44 | entityAssert.assertValues("existing1", "existing2"); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/teardown/CleanInsertTearDownOnMethod.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.teardown; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.annotation.DatabaseOperation; 27 | import com.github.springtestdbunit.annotation.DatabaseTearDown; 28 | import com.github.springtestdbunit.config.CoreTestConfiguration; 29 | import com.github.springtestdbunit.entity.EntityAssert; 30 | import com.github.springtestdbunit.testutils.AfterTearDownDbUnitTestExecutionListener; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | AfterTearDownDbUnitTestExecutionListener.class }) 35 | @Transactional 36 | public class CleanInsertTearDownOnMethod { 37 | 38 | @Autowired 39 | private EntityAssert entityAssert; 40 | 41 | @Test 42 | @DatabaseTearDown(type = DatabaseOperation.CLEAN_INSERT, value = "/META-INF/db/insert.xml") 43 | public void test() throws Exception { 44 | entityAssert.assertValues("existing1", "existing2"); 45 | } 46 | 47 | public void afterTest() throws Exception { 48 | entityAssert.assertValues("fromDbUnit"); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/setup/DeleteAllSetupOnMethodTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.setup; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.TransactionDbUnitTestExecutionListener; 27 | import com.github.springtestdbunit.annotation.DatabaseOperation; 28 | import com.github.springtestdbunit.annotation.DatabaseSetup; 29 | import com.github.springtestdbunit.config.CoreTestConfiguration; 30 | import com.github.springtestdbunit.entity.EntityAssert; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | TransactionDbUnitTestExecutionListener.class }) 35 | @Transactional 36 | public class DeleteAllSetupOnMethodTest { 37 | 38 | @Autowired 39 | private EntityAssert entityAssert; 40 | 41 | @Test 42 | @DatabaseSetup(type = DatabaseOperation.DELETE_ALL, value = "/META-INF/db/delete.xml") 43 | public void test() throws Exception { 44 | entityAssert.assertValues(); 45 | } 46 | 47 | @Test 48 | @DatabaseSetup(type = DatabaseOperation.DELETE_ALL) 49 | public void testAllTables() throws Exception { 50 | entityAssert.assertValues(); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/teardown/InsertTearDownOnClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.teardown; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.annotation.DatabaseOperation; 27 | import com.github.springtestdbunit.annotation.DatabaseTearDown; 28 | import com.github.springtestdbunit.config.CoreTestConfiguration; 29 | import com.github.springtestdbunit.entity.EntityAssert; 30 | import com.github.springtestdbunit.testutils.AfterTearDownDbUnitTestExecutionListener; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | AfterTearDownDbUnitTestExecutionListener.class }) 35 | @DatabaseTearDown(type = DatabaseOperation.INSERT, value = "/META-INF/db/insert.xml") 36 | @Transactional 37 | public class InsertTearDownOnClass { 38 | 39 | @Autowired 40 | private EntityAssert entityAssert; 41 | 42 | @Test 43 | public void test() throws Exception { 44 | entityAssert.assertValues("existing1", "existing2"); 45 | } 46 | 47 | public void afterTest() throws Exception { 48 | entityAssert.assertValues("existing1", "existing2", "fromDbUnit"); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/teardown/InsertTearDownOnMethod.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.teardown; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.annotation.DatabaseOperation; 27 | import com.github.springtestdbunit.annotation.DatabaseTearDown; 28 | import com.github.springtestdbunit.config.CoreTestConfiguration; 29 | import com.github.springtestdbunit.entity.EntityAssert; 30 | import com.github.springtestdbunit.testutils.AfterTearDownDbUnitTestExecutionListener; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | AfterTearDownDbUnitTestExecutionListener.class }) 35 | @Transactional 36 | public class InsertTearDownOnMethod { 37 | 38 | @Autowired 39 | private EntityAssert entityAssert; 40 | 41 | @Test 42 | @DatabaseTearDown(type = DatabaseOperation.INSERT, value = "/META-INF/db/insert.xml") 43 | public void test() throws Exception { 44 | entityAssert.assertValues("existing1", "existing2"); 45 | } 46 | 47 | public void afterTest() throws Exception { 48 | entityAssert.assertValues("existing1", "existing2", "fromDbUnit"); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/DataSetModifiers.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit; 18 | 19 | import java.lang.reflect.Constructor; 20 | import java.lang.reflect.Modifier; 21 | import java.util.ArrayList; 22 | import java.util.List; 23 | 24 | import org.dbunit.dataset.IDataSet; 25 | 26 | import com.github.springtestdbunit.dataset.DataSetModifier; 27 | 28 | /** 29 | * A collection of {@link DataSetModifier} items loaded during testing. 30 | * 31 | * @author Phillip Webb 32 | */ 33 | class DataSetModifiers implements DataSetModifier { 34 | 35 | private final List modifiers = new ArrayList(); 36 | 37 | public IDataSet modify(IDataSet dataSet) { 38 | for (DataSetModifier modifier : this.modifiers) { 39 | dataSet = modifier.modify(dataSet); 40 | } 41 | return dataSet; 42 | } 43 | 44 | public void add(Object testInstance, Class modifierClass) { 45 | try { 46 | Class enclosingClass = modifierClass.getEnclosingClass(); 47 | if ((enclosingClass == null) || Modifier.isStatic(modifierClass.getModifiers())) { 48 | add(modifierClass.getDeclaredConstructor()); 49 | } else { 50 | add(modifierClass.getDeclaredConstructor(enclosingClass), testInstance); 51 | } 52 | } catch (Exception ex) { 53 | throw new IllegalStateException(ex); 54 | } 55 | } 56 | 57 | private void add(Constructor constructor, Object... args) throws Exception { 58 | constructor.setAccessible(true); 59 | this.modifiers.add(constructor.newInstance(args)); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/expected/ExpectedOnClassAndMethodWithoutOverrideTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.expected; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.annotation.ExpectedDatabase; 27 | import com.github.springtestdbunit.assertion.DatabaseAssertionMode; 28 | import com.github.springtestdbunit.config.CoreTestConfiguration; 29 | import com.github.springtestdbunit.entity.EntityAssert; 30 | import com.github.springtestdbunit.testutils.MustFailDbUnitTestExecutionListener; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, MustFailDbUnitTestExecutionListener.class }) 34 | @ExpectedDatabase(value = "/META-INF/db/expectedfail.xml") 35 | @Transactional 36 | public class ExpectedOnClassAndMethodWithoutOverrideTest { 37 | 38 | @Autowired 39 | private EntityAssert entityAssert; 40 | 41 | @Test 42 | @ExpectedDatabase(value = "/META-INF/db/expected_nonstrict.xml", assertionMode = DatabaseAssertionMode.NON_STRICT, override = false) 43 | public void shouldUseMethodExpectation() { 44 | entityAssert.assertValues("existing1", "existing2"); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/setup/TruncateSetupOnMethodTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.setup; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.TransactionDbUnitTestExecutionListener; 27 | import com.github.springtestdbunit.annotation.DatabaseOperation; 28 | import com.github.springtestdbunit.annotation.DatabaseSetup; 29 | import com.github.springtestdbunit.config.CoreTestConfiguration; 30 | import com.github.springtestdbunit.entity.EntityAssert; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | TransactionDbUnitTestExecutionListener.class }) 35 | @Transactional 36 | public class TruncateSetupOnMethodTest { 37 | 38 | @Autowired 39 | private EntityAssert entityAssert; 40 | 41 | @Test 42 | @DatabaseSetup(type = DatabaseOperation.TRUNCATE_TABLE, value = "/META-INF/db/delete.xml") 43 | public void test() throws Exception { 44 | entityAssert.assertValues(); 45 | } 46 | 47 | @Test 48 | @DatabaseSetup(type = DatabaseOperation.TRUNCATE_TABLE) 49 | public void testAllTables() throws Exception { 50 | entityAssert.assertValues(); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/teardown/RefreshTearDownOnClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.teardown; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.annotation.DatabaseOperation; 27 | import com.github.springtestdbunit.annotation.DatabaseTearDown; 28 | import com.github.springtestdbunit.config.CoreTestConfiguration; 29 | import com.github.springtestdbunit.entity.EntityAssert; 30 | import com.github.springtestdbunit.testutils.AfterTearDownDbUnitTestExecutionListener; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | AfterTearDownDbUnitTestExecutionListener.class }) 35 | @DatabaseTearDown(type = DatabaseOperation.REFRESH, value = "/META-INF/db/refresh.xml") 36 | @Transactional 37 | public class RefreshTearDownOnClass { 38 | 39 | @Autowired 40 | private EntityAssert entityAssert; 41 | 42 | @Test 43 | public void test() throws Exception { 44 | entityAssert.assertValues("existing1", "existing2"); 45 | } 46 | 47 | public void afterTest() throws Exception { 48 | entityAssert.assertValues("addedFromDbUnit", "existing2", "replacedFromDbUnit"); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/teardown/RefreshTearDownOnMethod.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.teardown; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.annotation.DatabaseOperation; 27 | import com.github.springtestdbunit.annotation.DatabaseTearDown; 28 | import com.github.springtestdbunit.config.CoreTestConfiguration; 29 | import com.github.springtestdbunit.entity.EntityAssert; 30 | import com.github.springtestdbunit.testutils.AfterTearDownDbUnitTestExecutionListener; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | AfterTearDownDbUnitTestExecutionListener.class }) 35 | @Transactional 36 | public class RefreshTearDownOnMethod { 37 | 38 | @Autowired 39 | private EntityAssert entityAssert; 40 | 41 | @Test 42 | @DatabaseTearDown(type = DatabaseOperation.REFRESH, value = "/META-INF/db/refresh.xml") 43 | public void test() throws Exception { 44 | entityAssert.assertValues("existing1", "existing2"); 45 | } 46 | 47 | public void afterTest() throws Exception { 48 | entityAssert.assertValues("addedFromDbUnit", "existing2", "replacedFromDbUnit"); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/operation/DefaultDatabaseOperationLookup.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.operation; 18 | 19 | import java.util.HashMap; 20 | import java.util.Map; 21 | 22 | import com.github.springtestdbunit.annotation.DatabaseOperation; 23 | 24 | /** 25 | * Default implementation of {@link DatabaseOperationLookup}. 26 | * 27 | * @author Phillip Webb 28 | */ 29 | public class DefaultDatabaseOperationLookup implements DatabaseOperationLookup { 30 | 31 | private static Map OPERATION_LOOKUP; 32 | 33 | static { 34 | OPERATION_LOOKUP = new HashMap(); 35 | OPERATION_LOOKUP.put(DatabaseOperation.UPDATE, org.dbunit.operation.DatabaseOperation.UPDATE); 36 | OPERATION_LOOKUP.put(DatabaseOperation.INSERT, org.dbunit.operation.DatabaseOperation.INSERT); 37 | OPERATION_LOOKUP.put(DatabaseOperation.REFRESH, org.dbunit.operation.DatabaseOperation.REFRESH); 38 | OPERATION_LOOKUP.put(DatabaseOperation.DELETE, org.dbunit.operation.DatabaseOperation.DELETE); 39 | OPERATION_LOOKUP.put(DatabaseOperation.DELETE_ALL, org.dbunit.operation.DatabaseOperation.DELETE_ALL); 40 | OPERATION_LOOKUP.put(DatabaseOperation.TRUNCATE_TABLE, org.dbunit.operation.DatabaseOperation.TRUNCATE_TABLE); 41 | OPERATION_LOOKUP.put(DatabaseOperation.CLEAN_INSERT, org.dbunit.operation.DatabaseOperation.CLEAN_INSERT); 42 | } 43 | 44 | public org.dbunit.operation.DatabaseOperation get(DatabaseOperation operation) { 45 | return OPERATION_LOOKUP.get(operation); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/dataset/CsvUrlDataSetLoaderTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | package com.github.springtestdbunit.dataset; 18 | 19 | import static org.junit.jupiter.api.Assertions.assertEquals; 20 | import static org.junit.jupiter.api.Assertions.assertNull; 21 | 22 | import org.dbunit.dataset.IDataSet; 23 | import org.junit.jupiter.api.BeforeEach; 24 | import org.junit.jupiter.api.Test; 25 | import org.springframework.test.context.TestContext; 26 | 27 | import com.github.springtestdbunit.testutils.ExtendedTestContextManager; 28 | 29 | /** 30 | * Tests for {@link CsvUrlDataSetLoader}. 31 | * 32 | * @author Paul Podgorsek 33 | */ 34 | public class CsvUrlDataSetLoaderTest { 35 | 36 | private TestContext testContext; 37 | 38 | private CsvUrlDataSetLoader loader; 39 | 40 | @BeforeEach 41 | public void setup() throws Exception { 42 | loader = new CsvUrlDataSetLoader(); 43 | ExtendedTestContextManager manager = new ExtendedTestContextManager(getClass()); 44 | testContext = manager.accessTestContext(); 45 | } 46 | 47 | @Test 48 | public void shouldLoadFromRelativeFile() throws Exception { 49 | IDataSet dataset = loader.loadDataSet(testContext.getTestClass(), "csv/"); 50 | assertEquals(2, dataset.getTableNames().length, "Wrong number of tables"); 51 | assertEquals("Sample_2", dataset.getTableNames()[0]); 52 | assertEquals("Sample_1", dataset.getTableNames()[1]); 53 | } 54 | 55 | @Test 56 | public void shouldReturnNullOnMissingFile() throws Exception { 57 | IDataSet dataset = loader.loadDataSet(testContext.getTestClass(), "doesnotexist/"); 58 | assertNull(dataset); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/dataset/XlsDataSetLoaderTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | package com.github.springtestdbunit.dataset; 18 | 19 | import static org.junit.jupiter.api.Assertions.assertEquals; 20 | import static org.junit.jupiter.api.Assertions.assertNull; 21 | 22 | import org.dbunit.dataset.IDataSet; 23 | import org.junit.jupiter.api.BeforeEach; 24 | import org.junit.jupiter.api.Test; 25 | import org.springframework.test.context.TestContext; 26 | 27 | import com.github.springtestdbunit.testutils.ExtendedTestContextManager; 28 | 29 | /** 30 | * Tests for the {@link XlsDataSetLoader} class. 31 | * 32 | * @author Paul Podgorsek 33 | */ 34 | public class XlsDataSetLoaderTest { 35 | 36 | private TestContext testContext; 37 | 38 | private XlsDataSetLoader loader; 39 | 40 | @BeforeEach 41 | public void setup() throws Exception { 42 | loader = new XlsDataSetLoader(); 43 | ExtendedTestContextManager manager = new ExtendedTestContextManager(getClass()); 44 | testContext = manager.accessTestContext(); 45 | } 46 | 47 | @Test 48 | public void shouldLoadFromRelativeFile() throws Exception { 49 | IDataSet dataset = loader.loadDataSet(testContext.getTestClass(), "test.xls"); 50 | assertEquals(2, dataset.getTableNames().length, "Wrong number of tables in the file"); 51 | assertEquals("Sample_1", dataset.getTableNames()[0]); 52 | assertEquals("Sample_2", dataset.getTableNames()[1]); 53 | } 54 | 55 | @Test 56 | public void shouldReturnNullOnMissingFile() throws Exception { 57 | IDataSet dataset = loader.loadDataSet(testContext.getTestClass(), "doesnotexist.xls"); 58 | assertNull(dataset); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/teardown/MultipleInsertTearDownOnClassTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.teardown; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.annotation.DatabaseOperation; 27 | import com.github.springtestdbunit.annotation.DatabaseTearDown; 28 | import com.github.springtestdbunit.config.CoreTestConfiguration; 29 | import com.github.springtestdbunit.entity.EntityAssert; 30 | import com.github.springtestdbunit.testutils.AfterTearDownDbUnitTestExecutionListener; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | AfterTearDownDbUnitTestExecutionListener.class }) 35 | @DatabaseTearDown(type = DatabaseOperation.CLEAN_INSERT, value = { "/META-INF/db/insert.xml", 36 | "/META-INF/db/insert2.xml" }) 37 | @Transactional 38 | public class MultipleInsertTearDownOnClassTest { 39 | 40 | @Autowired 41 | private EntityAssert entityAssert; 42 | 43 | @Test 44 | public void test() throws Exception { 45 | entityAssert.assertValues("existing1", "existing2"); 46 | } 47 | 48 | public void afterTest() throws Exception { 49 | entityAssert.assertValues("fromDbUnit", "fromDbUnit2"); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/teardown/MixedTearDownOnClassAndMethodTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.teardown; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.annotation.DatabaseOperation; 27 | import com.github.springtestdbunit.annotation.DatabaseTearDown; 28 | import com.github.springtestdbunit.config.CoreTestConfiguration; 29 | import com.github.springtestdbunit.entity.EntityAssert; 30 | import com.github.springtestdbunit.testutils.AfterTearDownDbUnitTestExecutionListener; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | AfterTearDownDbUnitTestExecutionListener.class }) 35 | @DatabaseTearDown("/META-INF/db/insert.xml") 36 | @Transactional 37 | public class MixedTearDownOnClassAndMethodTest { 38 | 39 | @Autowired 40 | private EntityAssert entityAssert; 41 | 42 | @Test 43 | @DatabaseTearDown(value = "/META-INF/db/insert2.xml", type = DatabaseOperation.INSERT) 44 | public void testInsert() throws Exception { 45 | entityAssert.assertValues("existing1", "existing2"); 46 | } 47 | 48 | public void afterTest() throws Exception { 49 | entityAssert.assertValues("fromDbUnit", "fromDbUnit2"); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/site/markdown/faq.md: -------------------------------------------------------------------------------- 1 | # Frequently Asked Questions 2 | 3 | ## Is there an easy way to specify the Oracle database schema name to use? 4 | You will need to use a custom dbUnitDatabaseConnection bean if you want to specify a schema. Something like: 5 | 6 | ``` 7 | 8 | 9 | 10 | ``` 11 | 12 | This specified schema will be passed to the org.dbunit.database.DatabaseConnection [constructor](http://www.dbunit.org/apidocs/org/dbunit/database/DatabaseConnection.html#DatabaseConnection%28java.sql.Connection,%20java.lang.String%29). 13 | 14 | ## Is this project in a Maven repository? 15 | Yes it is. see http://mvnrepository.com/artifact/com.github.springtestdbunit/spring-test-dbunit for details. 16 | 17 | ## Is there a way to clear out the Database when tests are complete? 18 | The recommendation from DBUnit is that you have a good database setup and don't cleanup (see http://www.dbunit.org/bestpractices.html#nocleanup). That being said there are occasions where you might want to cleanup your database after every test. 19 | 20 | There are a couple of strategies that you can use: 21 | 22 | 1) Use the Spring TransactionalTestExecutionListener and rollback after each test. See the section on Transactions in the project [readme](index.html). This approach can work work for many tests, however, sometimes you may not want to rollback transactions to be sure that no exceptions would have been raised on the commit. For more information about Spring testing with JDBC see the [Spring Reference Documentation](http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/testing.html#integration-testing-support-jdbc). 23 | 24 | 2) Use the @DatabaseTearDown annotation on the test class and provide a reset DBUnit XML file. Creating a reset script can often be difficult if you have foreign key constraints (see 25 | [this blog post](http://www.andrewspencer.net/2011/solve-foreign-key-problems-in-dbunit-test-data/)). Any empty table element tells DBUnit to delete all data, so a reset script is generally a list of tables in the order that they can be deleted without causing foreign key constraints, e.g.: 26 | 27 | ``` 28 |

29 | 30 | ``` 31 | -------------------------------------------------------------------------------- /spring-test-dbunit-sample/src/test/java/com/github/springtestdbunit/sample/service/PersonServiceXmlTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.springtestdbunit.sample.service; 17 | 18 | import static org.junit.jupiter.api.Assertions.assertEquals; 19 | 20 | import java.util.List; 21 | 22 | import org.junit.jupiter.api.Test; 23 | import org.springframework.test.context.TestExecutionListeners; 24 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 25 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 26 | 27 | import com.github.springtestdbunit.DbUnitTestExecutionListener; 28 | import com.github.springtestdbunit.annotation.DatabaseSetup; 29 | import com.github.springtestdbunit.annotation.ExpectedDatabase; 30 | import com.github.springtestdbunit.sample.config.SampleTestConfiguration; 31 | import com.github.springtestdbunit.sample.entity.Person; 32 | 33 | import jakarta.annotation.Resource; 34 | 35 | @SpringJUnitConfig(SampleTestConfiguration.class) 36 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class }) 37 | public class PersonServiceXmlTest { 38 | 39 | @Resource 40 | private PersonService personService; 41 | 42 | @Test 43 | @DatabaseSetup("sampleData.xml") 44 | public void testFind() throws Exception { 45 | List personList = personService.find("hil"); 46 | assertEquals(1, personList.size()); 47 | assertEquals("Phillip", personList.get(0).getFirstName()); 48 | } 49 | 50 | @Test 51 | @DatabaseSetup("sampleData.xml") 52 | @ExpectedDatabase("expectedData.xml") 53 | public void testRemove() throws Exception { 54 | personService.remove(1); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/dataset/SqlLoaderControlDataSetLoaderTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | package com.github.springtestdbunit.dataset; 18 | 19 | import static org.junit.jupiter.api.Assertions.assertEquals; 20 | import static org.junit.jupiter.api.Assertions.assertNull; 21 | 22 | import org.dbunit.dataset.IDataSet; 23 | import org.junit.jupiter.api.BeforeEach; 24 | import org.junit.jupiter.api.Test; 25 | import org.springframework.test.context.TestContext; 26 | 27 | import com.github.springtestdbunit.testutils.ExtendedTestContextManager; 28 | 29 | /** 30 | * Tests for {@link SqlLoaderControlDataSetLoader}. 31 | * 32 | * @author Paul Podgorsek 33 | */ 34 | public class SqlLoaderControlDataSetLoaderTest { 35 | 36 | private TestContext testContext; 37 | 38 | private SqlLoaderControlDataSetLoader loader; 39 | 40 | @BeforeEach 41 | public void setup() throws Exception { 42 | loader = new SqlLoaderControlDataSetLoader(); 43 | ExtendedTestContextManager manager = new ExtendedTestContextManager(getClass()); 44 | testContext = manager.accessTestContext(); 45 | } 46 | 47 | @Test 48 | public void shouldLoadFromRelativeFile() throws Exception { 49 | IDataSet dataset = loader.loadDataSet(testContext.getTestClass(), "sql/"); 50 | assertEquals(2, dataset.getTableNames().length, "Wrong number of tables"); 51 | assertEquals("Sample_2", dataset.getTableNames()[0]); 52 | assertEquals("Sample_1", dataset.getTableNames()[1]); 53 | } 54 | 55 | @Test 56 | public void shouldReturnNullOnMissingFile() throws Exception { 57 | IDataSet dataset = loader.loadDataSet(testContext.getTestClass(), "doesnotexist/"); 58 | assertNull(dataset); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/setup/MixedSetupOnClassAndMethodTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.setup; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.TransactionDbUnitTestExecutionListener; 27 | import com.github.springtestdbunit.annotation.DatabaseOperation; 28 | import com.github.springtestdbunit.annotation.DatabaseSetup; 29 | import com.github.springtestdbunit.config.CoreTestConfiguration; 30 | import com.github.springtestdbunit.entity.EntityAssert; 31 | 32 | @SpringJUnitConfig(CoreTestConfiguration.class) 33 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 34 | TransactionDbUnitTestExecutionListener.class }) 35 | @DatabaseSetup("/META-INF/db/insert.xml") 36 | @Transactional 37 | public class MixedSetupOnClassAndMethodTest { 38 | 39 | @Autowired 40 | private EntityAssert entityAssert; 41 | 42 | @Test 43 | @DatabaseSetup(value = "/META-INF/db/insert2.xml", type = DatabaseOperation.INSERT) 44 | public void testInsert() throws Exception { 45 | entityAssert.assertValues("fromDbUnit", "fromDbUnit2"); 46 | } 47 | 48 | @Test 49 | @DatabaseSetup(type = DatabaseOperation.REFRESH, value = "/META-INF/db/refresh.xml") 50 | public void testRefresh() throws Exception { 51 | entityAssert.assertValues("addedFromDbUnit", "replacedFromDbUnit"); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/annotation/DatabaseSetupTearDownAnnotationAttributes.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.springtestdbunit.annotation; 17 | 18 | import java.lang.annotation.Annotation; 19 | import java.util.ArrayList; 20 | import java.util.Collection; 21 | import java.util.List; 22 | import java.util.Map; 23 | 24 | import org.springframework.core.annotation.AnnotationUtils; 25 | import org.springframework.util.Assert; 26 | 27 | public class DatabaseSetupTearDownAnnotationAttributes extends AbstractDatabaseAnnotationAttributes { 28 | 29 | private final DatabaseOperation type; 30 | 31 | private final String[] value; 32 | 33 | public DatabaseSetupTearDownAnnotationAttributes(final Annotation annotation) { 34 | 35 | super(annotation); 36 | 37 | Assert.state((annotation instanceof DatabaseSetup) || (annotation instanceof DatabaseTearDown), 38 | "Only DatabaseSetup and DatabaseTearDown annotations are supported"); 39 | 40 | Map attributes = AnnotationUtils.getAnnotationAttributes(annotation); 41 | type = (DatabaseOperation) attributes.get("type"); 42 | value = (String[]) attributes.get("value"); 43 | } 44 | 45 | public static Collection get( 46 | final Annotations annotations) { 47 | 48 | List annotationAttributes = new ArrayList(); 49 | 50 | for (T annotation : annotations) { 51 | annotationAttributes.add(new DatabaseSetupTearDownAnnotationAttributes(annotation)); 52 | } 53 | 54 | return annotationAttributes; 55 | } 56 | 57 | public DatabaseOperation getType() { 58 | return type; 59 | } 60 | 61 | public String[] getValue() { 62 | return value; 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/annotation/DatabaseOperation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.annotation; 18 | 19 | /** 20 | * Database test operations that can be performed to configure database tables. 21 | * 22 | * @see DatabaseSetup 23 | * @see DatabaseTearDown 24 | * 25 | * @author Phillip Webb 26 | */ 27 | public enum DatabaseOperation { 28 | 29 | /** 30 | * Updates the contents of existing database tables from the dataset. 31 | */ 32 | UPDATE, 33 | 34 | /** 35 | * Inserts new database tables and contents from the dataset. 36 | */ 37 | INSERT, 38 | 39 | /** 40 | * Refresh the contents of existing database tables. Rows from the dataset will insert or replace existing data. Any 41 | * database rows that are not in the dataset remain unaffected. 42 | */ 43 | REFRESH, 44 | 45 | /** 46 | * Deletes database table rows that matches rows from the dataset. 47 | */ 48 | DELETE, 49 | 50 | /** 51 | * Deletes all rows from a database table when the table is specified in the dataset. Tables in the database but not 52 | * in the dataset remain unaffected. 53 | * @see #TRUNCATE_TABLE 54 | */ 55 | DELETE_ALL, 56 | 57 | /** 58 | * Deletes all rows from a database table when the table is specified in the dataset. Tables in the database but not 59 | * in the dataset are unaffected. Identical to {@link #DELETE_ALL} expect this operation cannot be rolled back and 60 | * is supported by less database vendors. 61 | * @see #DELETE_ALL 62 | */ 63 | TRUNCATE_TABLE, 64 | 65 | /** 66 | * Deletes all rows from a database table when the tables is specified in the dataset and subsequently insert new 67 | * contents. Equivalent to calling {@link #DELETE_ALL} followed by {@link #INSERT}. 68 | */ 69 | CLEAN_INSERT; 70 | 71 | } 72 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/teardown/DeleteAllTearDownOnClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.teardown; 18 | 19 | import org.junit.jupiter.api.Test; 20 | import org.springframework.beans.factory.annotation.Autowired; 21 | import org.springframework.test.context.TestExecutionListeners; 22 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 23 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 24 | import org.springframework.transaction.annotation.Transactional; 25 | 26 | import com.github.springtestdbunit.DbUnitTestExecutionListener; 27 | import com.github.springtestdbunit.annotation.DatabaseOperation; 28 | import com.github.springtestdbunit.annotation.DatabaseTearDown; 29 | import com.github.springtestdbunit.annotation.DbUnitConfiguration; 30 | import com.github.springtestdbunit.config.CoreTestConfiguration; 31 | import com.github.springtestdbunit.entity.EntityAssert; 32 | import com.github.springtestdbunit.testutils.AfterTearDownDbUnitTestExecutionListener; 33 | 34 | @SpringJUnitConfig(CoreTestConfiguration.class) 35 | @DbUnitConfiguration(databaseConnection = DbUnitTestExecutionListener.DEFAULT_DBUNIT_DATABASE_CONNECTION_BEAN_NAME) 36 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 37 | AfterTearDownDbUnitTestExecutionListener.class }) 38 | @DatabaseTearDown(type = DatabaseOperation.DELETE, value = "/META-INF/db/delete.xml") 39 | @Transactional 40 | public class DeleteAllTearDownOnClass { 41 | 42 | @Autowired 43 | private EntityAssert entityAssert; 44 | 45 | @Test 46 | public void test() { 47 | entityAssert.assertValues("existing1", "existing2"); 48 | } 49 | 50 | public void afterTest() { 51 | entityAssert.assertValues("existing2"); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/dataset/FlatXmlDataSetLoaderTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.dataset; 18 | 19 | import static org.junit.jupiter.api.Assertions.assertEquals; 20 | import static org.junit.jupiter.api.Assertions.assertNull; 21 | 22 | import org.dbunit.dataset.IDataSet; 23 | import org.junit.jupiter.api.BeforeEach; 24 | import org.junit.jupiter.api.Test; 25 | import org.springframework.test.context.TestContext; 26 | 27 | import com.github.springtestdbunit.testutils.ExtendedTestContextManager; 28 | 29 | /** 30 | * Tests for {@link FlatXmlDataSetLoader}. 31 | * 32 | * @author Phillip Webb 33 | */ 34 | public class FlatXmlDataSetLoaderTest { 35 | 36 | private TestContext testContext; 37 | 38 | private FlatXmlDataSetLoader loader; 39 | 40 | @BeforeEach 41 | public void setup() throws Exception { 42 | loader = new FlatXmlDataSetLoader(); 43 | ExtendedTestContextManager manager = new ExtendedTestContextManager(getClass()); 44 | testContext = manager.accessTestContext(); 45 | } 46 | 47 | @Test 48 | public void shouldSenseColumns() throws Exception { 49 | IDataSet dataset = loader.loadDataSet(testContext.getTestClass(), "test-column-sensing.xml"); 50 | assertEquals(null, dataset.getTable("Sample").getValue(0, "name")); 51 | assertEquals("test", dataset.getTable("Sample").getValue(1, "name")); 52 | } 53 | 54 | @Test 55 | public void shouldLoadFromRelativeFile() throws Exception { 56 | IDataSet dataset = loader.loadDataSet(testContext.getTestClass(), "test.xml"); 57 | assertEquals("Sample", dataset.getTableNames()[0]); 58 | } 59 | 60 | @Test 61 | public void shouldReturnNullOnMissingFile() throws Exception { 62 | IDataSet dataset = loader.loadDataSet(testContext.getTestClass(), "doesnotexist.xml"); 63 | assertNull(dataset); 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/DbUnitTestContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit; 18 | 19 | import java.lang.reflect.Method; 20 | 21 | import org.dbunit.database.IDatabaseConnection; 22 | import org.dbunit.dataset.IDataSet; 23 | 24 | import com.github.springtestdbunit.dataset.DataSetLoader; 25 | import com.github.springtestdbunit.operation.DatabaseOperationLookup; 26 | 27 | /** 28 | * Provides context for the {@link DbUnitRunner}. 29 | * 30 | * @author Phillip Webb 31 | */ 32 | public interface DbUnitTestContext { 33 | 34 | /** 35 | * Returns the {@link IDatabaseConnection} that should be used when performing database setup and teardown. 36 | * @return The connection 37 | */ 38 | DatabaseConnections getConnections(); 39 | 40 | /** 41 | * Returns the {@link DataSetLoader} that should be used to load {@link IDataSet}s. 42 | * @return The dataset loader 43 | */ 44 | DataSetLoader getDataSetLoader(); 45 | 46 | /** 47 | * Returns the {@link DatabaseOperationLookup} that should be used to lookup database operations. 48 | * @return the database operation lookup 49 | */ 50 | DatabaseOperationLookup getDatabaseOperationLookup(); 51 | 52 | /** 53 | * Returns the class that is under test. 54 | * @return The class under test 55 | */ 56 | Class getTestClass(); 57 | 58 | /** 59 | * Returns the instance that is under test. 60 | * @return The instance under test 61 | */ 62 | Object getTestInstance(); 63 | 64 | /** 65 | * Returns the method that is under test. 66 | * @return The method under test 67 | */ 68 | Method getTestMethod(); 69 | 70 | /** 71 | * Returns any exception that was thrown during the test or {@code null} if no test exception occurred. 72 | * @return the test exception or {@code null} 73 | */ 74 | Throwable getTestException(); 75 | 76 | } 77 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/main/java/com/github/springtestdbunit/dataset/FlatXmlDataSetLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.dataset; 18 | 19 | import java.io.IOException; 20 | import java.io.InputStream; 21 | import java.net.URL; 22 | 23 | import org.dbunit.dataset.DataSetException; 24 | import org.dbunit.dataset.IDataSet; 25 | import org.dbunit.dataset.xml.FlatXmlDataSet; 26 | import org.dbunit.dataset.xml.FlatXmlDataSetBuilder; 27 | import org.springframework.core.io.Resource; 28 | 29 | /** 30 | * A {@link DataSetLoader data set loader} that can be used to load {@link FlatXmlDataSet FlatXmlDataSets} 31 | * 32 | * @author Phillip Webb 33 | */ 34 | public class FlatXmlDataSetLoader extends AbstractDataSetLoader { 35 | 36 | @Override 37 | protected IDataSet createDataSet(Resource resource) throws DataSetException, IOException { 38 | FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder(); 39 | builder.setColumnSensing(true); 40 | return buildDataSet(builder, resource); 41 | } 42 | 43 | private IDataSet buildDataSet(FlatXmlDataSetBuilder builder, Resource resource) 44 | throws DataSetException, IOException { 45 | try { 46 | // Prefer URL loading if possible so that DTDs can be resolved 47 | return buildDataSetFromUrl(builder, resource.getURL()); 48 | } catch (Exception ex) { 49 | return buildDataSetFromStream(builder, resource); 50 | } 51 | } 52 | 53 | private IDataSet buildDataSetFromUrl(FlatXmlDataSetBuilder builder, URL url) throws DataSetException { 54 | return builder.build(url); 55 | } 56 | 57 | private IDataSet buildDataSetFromStream(FlatXmlDataSetBuilder builder, Resource resource) 58 | throws IOException, DataSetException { 59 | InputStream inputStream = resource.getInputStream(); 60 | try { 61 | return builder.build(inputStream); 62 | } finally { 63 | inputStream.close(); 64 | } 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /spring-test-dbunit-sample/src/test/java/com/github/springtestdbunit/sample/service/PersonServiceCsvUrlTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.springtestdbunit.sample.service; 17 | 18 | import static org.junit.jupiter.api.Assertions.assertEquals; 19 | 20 | import java.util.List; 21 | 22 | import org.junit.jupiter.api.Test; 23 | import org.springframework.test.context.TestExecutionListeners; 24 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 25 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 26 | 27 | import com.github.springtestdbunit.DbUnitTestExecutionListener; 28 | import com.github.springtestdbunit.annotation.DatabaseSetup; 29 | import com.github.springtestdbunit.annotation.DbUnitConfiguration; 30 | import com.github.springtestdbunit.annotation.ExpectedDatabase; 31 | import com.github.springtestdbunit.dataset.CsvUrlDataSetLoader; 32 | import com.github.springtestdbunit.sample.config.SampleTestConfiguration; 33 | import com.github.springtestdbunit.sample.entity.Person; 34 | 35 | import jakarta.annotation.Resource; 36 | 37 | @SpringJUnitConfig(SampleTestConfiguration.class) 38 | @DbUnitConfiguration(dataSetLoader = CsvUrlDataSetLoader.class) 39 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class }) 40 | public class PersonServiceCsvUrlTest { 41 | 42 | @Resource 43 | private PersonService personService; 44 | 45 | @Test 46 | @DatabaseSetup("csv/sample/") 47 | public void testFind() throws Exception { 48 | List personList = personService.find("gor"); 49 | assertEquals(1, personList.size(), "Wrong number of results"); 50 | assertEquals("Paul", personList.get(0).getFirstName(), "Wrong user"); 51 | } 52 | 53 | @Test 54 | @DatabaseSetup("csv/sample/") 55 | @ExpectedDatabase("csv/expected/") 56 | public void testRemove() throws Exception { 57 | personService.remove(1); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /spring-test-dbunit-sample/src/test/java/com/github/springtestdbunit/sample/service/PersonServiceXlsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.springtestdbunit.sample.service; 17 | 18 | import static org.junit.jupiter.api.Assertions.assertEquals; 19 | 20 | import java.util.List; 21 | 22 | import org.junit.jupiter.api.Test; 23 | import org.springframework.test.context.TestExecutionListeners; 24 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 25 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 26 | 27 | import com.github.springtestdbunit.DbUnitTestExecutionListener; 28 | import com.github.springtestdbunit.annotation.DatabaseSetup; 29 | import com.github.springtestdbunit.annotation.DbUnitConfiguration; 30 | import com.github.springtestdbunit.annotation.ExpectedDatabase; 31 | import com.github.springtestdbunit.dataset.XlsDataSetLoader; 32 | import com.github.springtestdbunit.sample.config.SampleTestConfiguration; 33 | import com.github.springtestdbunit.sample.entity.Person; 34 | 35 | import jakarta.annotation.Resource; 36 | 37 | @SpringJUnitConfig(SampleTestConfiguration.class) 38 | @DbUnitConfiguration(dataSetLoader = XlsDataSetLoader.class) 39 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class }) 40 | public class PersonServiceXlsTest { 41 | 42 | @Resource 43 | private PersonService personService; 44 | 45 | @Test 46 | @DatabaseSetup("sampleData.xls") 47 | public void testFind() throws Exception { 48 | List personList = personService.find("gor"); 49 | assertEquals(1, personList.size(), "Wrong number of results"); 50 | assertEquals("Paul", personList.get(0).getFirstName(), "Wrong user"); 51 | } 52 | 53 | @Test 54 | @DatabaseSetup("sampleData.xls") 55 | @ExpectedDatabase("expectedData.xls") 56 | public void testRemove() throws Exception { 57 | personService.remove(1); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /spring-test-dbunit-core/src/test/java/com/github/springtestdbunit/expected/ExpectedNonStrictWithDtdTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2016 the original author or authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.github.springtestdbunit.expected; 18 | 19 | import org.dbunit.dataset.Column; 20 | import org.dbunit.dataset.filter.IColumnFilter; 21 | import org.junit.jupiter.api.Test; 22 | import org.springframework.beans.factory.annotation.Autowired; 23 | import org.springframework.test.context.TestExecutionListeners; 24 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 25 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 26 | import org.springframework.transaction.annotation.Transactional; 27 | 28 | import com.github.springtestdbunit.DbUnitTestExecutionListener; 29 | import com.github.springtestdbunit.annotation.ExpectedDatabase; 30 | import com.github.springtestdbunit.assertion.DatabaseAssertionMode; 31 | import com.github.springtestdbunit.config.CoreTestConfiguration; 32 | import com.github.springtestdbunit.entity.EntityAssert; 33 | 34 | @SpringJUnitConfig(CoreTestConfiguration.class) 35 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class }) 36 | @Transactional 37 | public class ExpectedNonStrictWithDtdTest { 38 | 39 | @Autowired 40 | private EntityAssert entityAssert; 41 | 42 | @Test 43 | @ExpectedDatabase(value = "/META-INF/db/expected_nonstrict_with_dtd.xml", assertionMode = DatabaseAssertionMode.NON_STRICT, columnFilters = { 44 | SampleEntityIdExclusionFilter.class }) 45 | public void shouldNotFailEvenThoughExpectedTableDoesNotSpecifyAllColumns() { 46 | entityAssert.assertValues("existing1", "existing2"); 47 | } 48 | 49 | public static class SampleEntityIdExclusionFilter implements IColumnFilter { 50 | 51 | public boolean accept(String tableName, Column column) { 52 | return !(tableName.equals("SampleEntity") && column.getColumnName().equals("id")); 53 | } 54 | 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /spring-test-dbunit-sample/src/test/java/com/github/springtestdbunit/sample/service/PersonServiceSqlLoaderControlTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.github.springtestdbunit.sample.service; 17 | 18 | import static org.junit.jupiter.api.Assertions.assertEquals; 19 | 20 | import java.util.List; 21 | 22 | import org.junit.jupiter.api.Test; 23 | import org.springframework.test.context.TestExecutionListeners; 24 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 25 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; 26 | 27 | import com.github.springtestdbunit.DbUnitTestExecutionListener; 28 | import com.github.springtestdbunit.annotation.DatabaseSetup; 29 | import com.github.springtestdbunit.annotation.DbUnitConfiguration; 30 | import com.github.springtestdbunit.annotation.ExpectedDatabase; 31 | import com.github.springtestdbunit.dataset.SqlLoaderControlDataSetLoader; 32 | import com.github.springtestdbunit.sample.config.SampleTestConfiguration; 33 | import com.github.springtestdbunit.sample.entity.Person; 34 | 35 | import jakarta.annotation.Resource; 36 | 37 | @SpringJUnitConfig(SampleTestConfiguration.class) 38 | @DbUnitConfiguration(dataSetLoader = SqlLoaderControlDataSetLoader.class) 39 | @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class }) 40 | public class PersonServiceSqlLoaderControlTest { 41 | 42 | @Resource 43 | private PersonService personService; 44 | 45 | @Test 46 | @DatabaseSetup("sql/sample/") 47 | public void testFind() throws Exception { 48 | List personList = personService.find("gor"); 49 | assertEquals(1, personList.size(), "Wrong number of results"); 50 | assertEquals("Paul", personList.get(0).getFirstName(), "Wrong user"); 51 | } 52 | 53 | @Test 54 | @DatabaseSetup("sql/sample/") 55 | @ExpectedDatabase("sql/expected/") 56 | public void testRemove() throws Exception { 57 | personService.remove(1); 58 | } 59 | 60 | } 61 | --------------------------------------------------------------------------------