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 extends DataSetModifier> 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 extends DataSetModifier> 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 |
--------------------------------------------------------------------------------