From e4f4f7110b33f753a50b5f56443b8dff8ab014b5 Mon Sep 17 00:00:00 2001 From: Joao Esperancinha Date: Sun, 29 Mar 2020 21:47:00 +0200 Subject: [PATCH] [BAEL-2749] Code refactoring --- dbunit/src/main/resources/logback.xml | 2 +- .../baeldung/dbunit/ConnectionSettings.java | 8 ++++ .../baeldung/dbunit/OldSchoolDbUnitTest.java | 32 ++++++++----- .../dbunit/PrepAndExpectedDbUnitTest.java | 48 +++++++------------ .../com/baeldung/dbunit/SampleDbUnitTest.java | 23 ++++----- dbunit/src/test/resources/data.xml | 12 ++--- .../src/test/resources/items_exp_delete.xml | 8 ++-- .../items_exp_delete_no_produced.xml | 8 ++-- .../src/test/resources/items_exp_rename.xml | 10 ++-- .../items_exp_rename_no_produced.xml | 9 ++++ dbunit/src/test/resources/schema.sql | 34 ++++++------- dbunit/src/test/resources/users.xml | 6 +-- .../src/test/resources/users_exp_delete.xml | 4 +- .../src/test/resources/users_exp_rename.xml | 6 +-- 14 files changed, 108 insertions(+), 102 deletions(-) create mode 100644 dbunit/src/test/java/com/baeldung/dbunit/ConnectionSettings.java create mode 100644 dbunit/src/test/resources/items_exp_rename_no_produced.xml diff --git a/dbunit/src/main/resources/logback.xml b/dbunit/src/main/resources/logback.xml index 7d900d8ea8..26beb6d5b4 100644 --- a/dbunit/src/main/resources/logback.xml +++ b/dbunit/src/main/resources/logback.xml @@ -8,6 +8,6 @@ - + \ No newline at end of file diff --git a/dbunit/src/test/java/com/baeldung/dbunit/ConnectionSettings.java b/dbunit/src/test/java/com/baeldung/dbunit/ConnectionSettings.java new file mode 100644 index 0000000000..e842022292 --- /dev/null +++ b/dbunit/src/test/java/com/baeldung/dbunit/ConnectionSettings.java @@ -0,0 +1,8 @@ +package com.baeldung.dbunit; + +public class ConnectionSettings { + public static final String JDBC_DRIVER = org.h2.Driver.class.getName(); + public static final String JDBC_URL = "jdbc:h2:mem:default;DB_CLOSE_DELAY=-1;init=runscript from 'classpath:schema.sql'"; + public static final String USER = "sa"; + public static final String PASSWORD = ""; +} diff --git a/dbunit/src/test/java/com/baeldung/dbunit/OldSchoolDbUnitTest.java b/dbunit/src/test/java/com/baeldung/dbunit/OldSchoolDbUnitTest.java index e64b49263a..0b63184951 100644 --- a/dbunit/src/test/java/com/baeldung/dbunit/OldSchoolDbUnitTest.java +++ b/dbunit/src/test/java/com/baeldung/dbunit/OldSchoolDbUnitTest.java @@ -1,6 +1,5 @@ package com.baeldung.dbunit; -import org.dbunit.Assertion; import org.dbunit.IDatabaseTester; import org.dbunit.JdbcDatabaseTester; import org.dbunit.dataset.IDataSet; @@ -17,14 +16,14 @@ import java.io.InputStream; import java.sql.Connection; import java.sql.ResultSet; +import static com.baeldung.dbunit.ConnectionSettings.JDBC_DRIVER; +import static com.baeldung.dbunit.ConnectionSettings.JDBC_URL; +import static com.baeldung.dbunit.ConnectionSettings.PASSWORD; +import static com.baeldung.dbunit.ConnectionSettings.USER; import static org.assertj.core.api.Assertions.assertThat; import static org.dbunit.Assertion.assertEquals; public class OldSchoolDbUnitTest { - private static final String JDBC_DRIVER = org.h2.Driver.class.getName(); - private static final String JDBC_URL = "jdbc:h2:mem:default;DB_CLOSE_DELAY=-1;init=runscript from 'classpath:schema.sql'"; - private static final String USER = "sa"; - private static final String PASSWORD = ""; private static IDatabaseTester tester = null; @@ -99,20 +98,31 @@ public class OldSchoolDbUnitTest { @Test public void testUpdate() throws Exception { - // Arrange final Connection connection = tester.getConnection().getConnection(); final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("items_exp_rename.xml"); - ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("items"); - //expectedTable = DefaultColumnFilter.excludedColumnsTable(expectedTable, new String[]{"produced"}); + final ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("items"); - // Act connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1"); - // Assert final IDataSet databaseDataSet = tester.getConnection().createDataSet(); ITable actualTable = databaseDataSet.getTable("items"); - //actualTable = DefaultColumnFilter.excludedColumnsTable(actualTable, new String[]{"produced"}); + + assertEquals(expectedTable, actualTable); + } + + @Test + public void testUpdateWithExcludedColumns() throws Exception { + final Connection connection = tester.getConnection().getConnection(); + + final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("items_exp_rename_no_produced.xml"); + ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("items"); + + connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1"); + + final IDataSet databaseDataSet = tester.getConnection().createDataSet(); + ITable actualTable = databaseDataSet.getTable("items"); + actualTable = DefaultColumnFilter.excludedColumnsTable(actualTable, new String[]{"produced"}); assertEquals(expectedTable, actualTable); } diff --git a/dbunit/src/test/java/com/baeldung/dbunit/PrepAndExpectedDbUnitTest.java b/dbunit/src/test/java/com/baeldung/dbunit/PrepAndExpectedDbUnitTest.java index c322cf0ae3..bb70136de2 100644 --- a/dbunit/src/test/java/com/baeldung/dbunit/PrepAndExpectedDbUnitTest.java +++ b/dbunit/src/test/java/com/baeldung/dbunit/PrepAndExpectedDbUnitTest.java @@ -16,13 +16,13 @@ import java.io.InputStream; import java.sql.Connection; import java.sql.ResultSet; +import static com.baeldung.dbunit.ConnectionSettings.JDBC_DRIVER; +import static com.baeldung.dbunit.ConnectionSettings.JDBC_URL; +import static com.baeldung.dbunit.ConnectionSettings.PASSWORD; +import static com.baeldung.dbunit.ConnectionSettings.USER; import static org.assertj.core.api.Assertions.assertThat; public class PrepAndExpectedDbUnitTest extends DefaultPrepAndExpectedTestCase { - private static final String JDBC_DRIVER = org.h2.Driver.class.getName(); - private static final String JDBC_URL = "jdbc:h2:mem:default;DB_CLOSE_DELAY=-1;init=runscript from 'classpath:schema.sql'"; - private static final String USER = "sa"; - private static final String PASSWORD = ""; @Override public void setUp() throws Exception { @@ -49,60 +49,46 @@ public class PrepAndExpectedDbUnitTest extends DefaultPrepAndExpectedTestCase { @Test public void testSelect() throws Exception { - // Arrange final Connection connection = getConnection().getConnection(); final VerifyTableDefinition[] verifyTables = {new VerifyTableDefinition("USERS", new String[]{})}; final String[] prepDataFiles = {"/users.xml"}; final String[] expectedDataFiles = {"/users.xml"}; - final PrepAndExpectedTestCaseSteps testSteps = () -> { - // invoke the method being tested here; for the sake of simplicity we use JDBC API directly in this example - final ResultSet rs = connection.createStatement().executeQuery("select * from USERS where id = 1"); + final PrepAndExpectedTestCaseSteps testSteps = + () -> connection + .createStatement() + .executeQuery("select * from USERS where id = 1"); - // either place assertions here - //assertTrue(rs.next()); - //assertEquals("Xavier", rs.getString("last_name")); - - return rs; - }; - - // Act final ResultSet rs = (ResultSet) super.runTest(verifyTables, prepDataFiles, expectedDataFiles, testSteps); - // or place assertions at the end assertThat(rs.next()).isTrue(); assertThat(rs.getString("last_name")).isEqualTo("Xavier"); } @Test public void testUpdate() throws Exception { - // Arrange final Connection connection = getConnection().getConnection(); final VerifyTableDefinition[] verifyTables = {new VerifyTableDefinition("USERS", new String[]{})}; // define tables to verify - final String[] prepDataFiles = {"/users.xml"}; // define prep files - final String[] expectedDataFiles = {"/users_exp_rename.xml"}; // define expected files - final PrepAndExpectedTestCaseSteps testSteps = () -> { - // invoke the method being tested here; for the sake of simplicity we use JDBC API directly in this example - return connection.createStatement().executeUpdate("update USERS set first_name = 'new name' where id = 1"); - // after this method exits, dbUnit will: - // * verify configured tables - // * cleanup tables as configured - }; + final String[] prepDataFiles = {"/users.xml"}; + final String[] expectedDataFiles = {"/users_exp_rename.xml"}; + final PrepAndExpectedTestCaseSteps testSteps = + () -> connection + .createStatement() + .executeUpdate("update USERS set first_name = 'new name' where id = 1"); - // Act super.runTest(verifyTables, prepDataFiles, expectedDataFiles, testSteps); } @Test public void testDelete() throws Exception { - // Arrange final Connection connection = getConnection().getConnection(); final VerifyTableDefinition[] verifyTables = {new VerifyTableDefinition("USERS", new String[]{})}; final String[] prepDataFiles = {"/users.xml"}; final String[] expectedDataFiles = {"/users_exp_delete.xml"}; final PrepAndExpectedTestCaseSteps testSteps = - () -> connection.createStatement().executeUpdate("delete from USERS where id = 2"); + () -> connection + .createStatement() + .executeUpdate("delete from USERS where id = 2"); - // Act super.runTest(verifyTables, prepDataFiles, expectedDataFiles, testSteps); } diff --git a/dbunit/src/test/java/com/baeldung/dbunit/SampleDbUnitTest.java b/dbunit/src/test/java/com/baeldung/dbunit/SampleDbUnitTest.java index cfe4fd4e11..f7970754e7 100644 --- a/dbunit/src/test/java/com/baeldung/dbunit/SampleDbUnitTest.java +++ b/dbunit/src/test/java/com/baeldung/dbunit/SampleDbUnitTest.java @@ -13,11 +13,13 @@ import java.io.InputStream; import java.sql.Connection; import java.sql.ResultSet; +import static com.baeldung.dbunit.ConnectionSettings.JDBC_DRIVER; +import static com.baeldung.dbunit.ConnectionSettings.JDBC_URL; +import static com.baeldung.dbunit.ConnectionSettings.PASSWORD; +import static com.baeldung.dbunit.ConnectionSettings.USER; +import static org.assertj.core.api.Assertions.assertThat; + public class SampleDbUnitTest extends DBTestCase { - private static final String JDBC_DRIVER = org.h2.Driver.class.getName(); - private static final String JDBC_URL = "jdbc:h2:mem:default;DB_CLOSE_DELAY=-1;init=runscript from 'classpath:schema.sql'"; - private static final String USER = "sa"; - private static final String PASSWORD = ""; public SampleDbUnitTest(String name) { super(name); @@ -46,29 +48,23 @@ public class SampleDbUnitTest extends DBTestCase { @Test public void testSelect() throws Exception { - // Arrange final Connection connection = getConnection().getConnection(); - // Act final ResultSet rs = connection.createStatement().executeQuery("select * from iTEMS where id = 1"); - // Assert - assertTrue(rs.next()); - assertEquals("Grey T-Shirt", rs.getString("title")); + assertThat(rs.next()).isTrue(); + assertThat(rs.getString("title")).isEqualTo("Grey T-Shirt"); } @Test public void testDelete() throws Exception { - // Arrange final Connection connection = getConnection().getConnection(); final InputStream is = SampleDbUnitTest.class.getClassLoader().getResourceAsStream("items_exp_delete.xml"); ITable expectedTable = (new FlatXmlDataSetBuilder().build(is)).getTable("items"); - // Act connection.createStatement().executeUpdate("delete from ITEMS where id = 2"); - // Assert final IDataSet databaseDataSet = getConnection().createDataSet(); ITable actualTable = databaseDataSet.getTable("items"); @@ -77,16 +73,13 @@ public class SampleDbUnitTest extends DBTestCase { @Test public void testUpdate() throws Exception { - // Arrange final Connection connection = getConnection().getConnection(); final InputStream is = SampleDbUnitTest.class.getClassLoader().getResourceAsStream("items_exp_rename.xml"); ITable expectedTable = (new FlatXmlDataSetBuilder().build(is)).getTable("items"); - // Act connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1"); - // Assert final IDataSet databaseDataSet = getConnection().createDataSet(); ITable actualTable = databaseDataSet.getTable("items"); diff --git a/dbunit/src/test/resources/data.xml b/dbunit/src/test/resources/data.xml index e4498513e0..ac413cfd23 100644 --- a/dbunit/src/test/resources/data.xml +++ b/dbunit/src/test/resources/data.xml @@ -1,11 +1,11 @@ - + - - - - - + + + + + diff --git a/dbunit/src/test/resources/items_exp_delete.xml b/dbunit/src/test/resources/items_exp_delete.xml index 1aaeff59ca..09b78da493 100644 --- a/dbunit/src/test/resources/items_exp_delete.xml +++ b/dbunit/src/test/resources/items_exp_delete.xml @@ -1,8 +1,8 @@ - - - - + + + + diff --git a/dbunit/src/test/resources/items_exp_delete_no_produced.xml b/dbunit/src/test/resources/items_exp_delete_no_produced.xml index c9b182a1b2..dd76e8c6ce 100644 --- a/dbunit/src/test/resources/items_exp_delete_no_produced.xml +++ b/dbunit/src/test/resources/items_exp_delete_no_produced.xml @@ -1,8 +1,8 @@ - - - - + + + + diff --git a/dbunit/src/test/resources/items_exp_rename.xml b/dbunit/src/test/resources/items_exp_rename.xml index 237280e758..830d83499f 100644 --- a/dbunit/src/test/resources/items_exp_rename.xml +++ b/dbunit/src/test/resources/items_exp_rename.xml @@ -1,9 +1,9 @@ - - - - - + + + + + diff --git a/dbunit/src/test/resources/items_exp_rename_no_produced.xml b/dbunit/src/test/resources/items_exp_rename_no_produced.xml new file mode 100644 index 0000000000..991c4726a0 --- /dev/null +++ b/dbunit/src/test/resources/items_exp_rename_no_produced.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/dbunit/src/test/resources/schema.sql b/dbunit/src/test/resources/schema.sql index 9b5018985a..dc4738ca76 100644 --- a/dbunit/src/test/resources/schema.sql +++ b/dbunit/src/test/resources/schema.sql @@ -1,28 +1,28 @@ CREATE TABLE IF NOT EXISTS USERS ( - `id` int AUTO_INCREMENT NOT NULL, - `first_name` varchar(100) NOT NULL, - `last_name` varchar(100) NOT NULL, - PRIMARY KEY (`id`) + `id` int AUTO_INCREMENT NOT NULL, + `first_name` varchar(100) NOT NULL, + `last_name` varchar(100) NOT NULL, + PRIMARY KEY (`id`) ); CREATE TABLE IF NOT EXISTS ITEMS ( - `id` int AUTO_INCREMENT NOT NULL, - `title` varchar(100) NOT NULL, - `produced` date, - `price` float, - PRIMARY KEY (`id`) + `id` int AUTO_INCREMENT NOT NULL, + `title` varchar(100) NOT NULL, + `produced` date, + `price` float, + PRIMARY KEY (`id`) ); CREATE TABLE IF NOT EXISTS PURCHASES ( - `id` int NOT NULL AUTO_INCREMENT, - `id_user` int NOT NULL, - `id_item` int NOT NULL, - `total_price` float NOT NULL, - `quantity` int(11) NOT NULL, - PRIMARY KEY (`id`), - FOREIGN KEY (`id_user`) REFERENCES USERS (`id`) ON DELETE CASCADE, - FOREIGN KEY (`id_item`) REFERENCES ITEMS (`id`) ON DELETE CASCADE ON UPDATE CASCADE + `id` int NOT NULL AUTO_INCREMENT, + `id_user` int NOT NULL, + `id_item` int NOT NULL, + `total_price` float NOT NULL, + `quantity` int(11) NOT NULL, + PRIMARY KEY (`id`), + FOREIGN KEY (`id_user`) REFERENCES USERS (`id`) ON DELETE CASCADE, + FOREIGN KEY (`id_item`) REFERENCES ITEMS (`id`) ON DELETE CASCADE ON UPDATE CASCADE ); diff --git a/dbunit/src/test/resources/users.xml b/dbunit/src/test/resources/users.xml index cebadf2e67..d8ff78024e 100644 --- a/dbunit/src/test/resources/users.xml +++ b/dbunit/src/test/resources/users.xml @@ -1,7 +1,7 @@ - - - + + + diff --git a/dbunit/src/test/resources/users_exp_delete.xml b/dbunit/src/test/resources/users_exp_delete.xml index 8b72ceef89..c4ea6e64e5 100644 --- a/dbunit/src/test/resources/users_exp_delete.xml +++ b/dbunit/src/test/resources/users_exp_delete.xml @@ -1,6 +1,6 @@ - - + + diff --git a/dbunit/src/test/resources/users_exp_rename.xml b/dbunit/src/test/resources/users_exp_rename.xml index 9bc1254967..c187e1ebe5 100644 --- a/dbunit/src/test/resources/users_exp_rename.xml +++ b/dbunit/src/test/resources/users_exp_rename.xml @@ -1,7 +1,7 @@ - - - + + +