[BAEL-2749] Code refactoring

This commit is contained in:
Joao Esperancinha 2020-03-29 21:47:00 +02:00
parent 71ba591868
commit e4f4f7110b
14 changed files with 108 additions and 102 deletions

View File

@ -8,6 +8,6 @@
</appender> </appender>
<root level="INFO"> <root level="INFO">
<appender-ref ref="STDOUT" /> <appender-ref ref="STDOUT"/>
</root> </root>
</configuration> </configuration>

View File

@ -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 = "";
}

View File

@ -1,6 +1,5 @@
package com.baeldung.dbunit; package com.baeldung.dbunit;
import org.dbunit.Assertion;
import org.dbunit.IDatabaseTester; import org.dbunit.IDatabaseTester;
import org.dbunit.JdbcDatabaseTester; import org.dbunit.JdbcDatabaseTester;
import org.dbunit.dataset.IDataSet; import org.dbunit.dataset.IDataSet;
@ -17,14 +16,14 @@ import java.io.InputStream;
import java.sql.Connection; import java.sql.Connection;
import java.sql.ResultSet; 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.assertj.core.api.Assertions.assertThat;
import static org.dbunit.Assertion.assertEquals; import static org.dbunit.Assertion.assertEquals;
public class OldSchoolDbUnitTest { 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; private static IDatabaseTester tester = null;
@ -99,20 +98,31 @@ public class OldSchoolDbUnitTest {
@Test @Test
public void testUpdate() throws Exception { public void testUpdate() throws Exception {
// Arrange
final Connection connection = tester.getConnection().getConnection(); final Connection connection = tester.getConnection().getConnection();
final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("items_exp_rename.xml"); final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("items_exp_rename.xml");
ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("items"); final ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("items");
//expectedTable = DefaultColumnFilter.excludedColumnsTable(expectedTable, new String[]{"produced"});
// Act
connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1"); connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1");
// Assert
final IDataSet databaseDataSet = tester.getConnection().createDataSet(); final IDataSet databaseDataSet = tester.getConnection().createDataSet();
ITable actualTable = databaseDataSet.getTable("items"); 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); assertEquals(expectedTable, actualTable);
} }

View File

@ -16,13 +16,13 @@ import java.io.InputStream;
import java.sql.Connection; import java.sql.Connection;
import java.sql.ResultSet; 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.assertj.core.api.Assertions.assertThat;
public class PrepAndExpectedDbUnitTest extends DefaultPrepAndExpectedTestCase { 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 @Override
public void setUp() throws Exception { public void setUp() throws Exception {
@ -49,60 +49,46 @@ public class PrepAndExpectedDbUnitTest extends DefaultPrepAndExpectedTestCase {
@Test @Test
public void testSelect() throws Exception { public void testSelect() throws Exception {
// Arrange
final Connection connection = getConnection().getConnection(); final Connection connection = getConnection().getConnection();
final VerifyTableDefinition[] verifyTables = {new VerifyTableDefinition("USERS", new String[]{})}; final VerifyTableDefinition[] verifyTables = {new VerifyTableDefinition("USERS", new String[]{})};
final String[] prepDataFiles = {"/users.xml"}; final String[] prepDataFiles = {"/users.xml"};
final String[] expectedDataFiles = {"/users.xml"}; final String[] expectedDataFiles = {"/users.xml"};
final PrepAndExpectedTestCaseSteps testSteps = () -> { final PrepAndExpectedTestCaseSteps testSteps =
// invoke the method being tested here; for the sake of simplicity we use JDBC API directly in this example () -> connection
final ResultSet rs = connection.createStatement().executeQuery("select * from USERS where id = 1"); .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); final ResultSet rs = (ResultSet) super.runTest(verifyTables, prepDataFiles, expectedDataFiles, testSteps);
// or place assertions at the end
assertThat(rs.next()).isTrue(); assertThat(rs.next()).isTrue();
assertThat(rs.getString("last_name")).isEqualTo("Xavier"); assertThat(rs.getString("last_name")).isEqualTo("Xavier");
} }
@Test @Test
public void testUpdate() throws Exception { public void testUpdate() throws Exception {
// Arrange
final Connection connection = getConnection().getConnection(); final Connection connection = getConnection().getConnection();
final VerifyTableDefinition[] verifyTables = {new VerifyTableDefinition("USERS", new String[]{})}; // define tables to verify final VerifyTableDefinition[] verifyTables = {new VerifyTableDefinition("USERS", new String[]{})}; // define tables to verify
final String[] prepDataFiles = {"/users.xml"}; // define prep files final String[] prepDataFiles = {"/users.xml"};
final String[] expectedDataFiles = {"/users_exp_rename.xml"}; // define expected files final String[] expectedDataFiles = {"/users_exp_rename.xml"};
final PrepAndExpectedTestCaseSteps testSteps = () -> { final PrepAndExpectedTestCaseSteps testSteps =
// invoke the method being tested here; for the sake of simplicity we use JDBC API directly in this example () -> connection
return connection.createStatement().executeUpdate("update USERS set first_name = 'new name' where id = 1"); .createStatement()
// after this method exits, dbUnit will: .executeUpdate("update USERS set first_name = 'new name' where id = 1");
// * verify configured tables
// * cleanup tables as configured
};
// Act
super.runTest(verifyTables, prepDataFiles, expectedDataFiles, testSteps); super.runTest(verifyTables, prepDataFiles, expectedDataFiles, testSteps);
} }
@Test @Test
public void testDelete() throws Exception { public void testDelete() throws Exception {
// Arrange
final Connection connection = getConnection().getConnection(); final Connection connection = getConnection().getConnection();
final VerifyTableDefinition[] verifyTables = {new VerifyTableDefinition("USERS", new String[]{})}; final VerifyTableDefinition[] verifyTables = {new VerifyTableDefinition("USERS", new String[]{})};
final String[] prepDataFiles = {"/users.xml"}; final String[] prepDataFiles = {"/users.xml"};
final String[] expectedDataFiles = {"/users_exp_delete.xml"}; final String[] expectedDataFiles = {"/users_exp_delete.xml"};
final PrepAndExpectedTestCaseSteps testSteps = 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); super.runTest(verifyTables, prepDataFiles, expectedDataFiles, testSteps);
} }

View File

@ -13,11 +13,13 @@ import java.io.InputStream;
import java.sql.Connection; import java.sql.Connection;
import java.sql.ResultSet; 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 { 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) { public SampleDbUnitTest(String name) {
super(name); super(name);
@ -46,29 +48,23 @@ public class SampleDbUnitTest extends DBTestCase {
@Test @Test
public void testSelect() throws Exception { public void testSelect() throws Exception {
// Arrange
final Connection connection = getConnection().getConnection(); final Connection connection = getConnection().getConnection();
// Act
final ResultSet rs = connection.createStatement().executeQuery("select * from iTEMS where id = 1"); final ResultSet rs = connection.createStatement().executeQuery("select * from iTEMS where id = 1");
// Assert assertThat(rs.next()).isTrue();
assertTrue(rs.next()); assertThat(rs.getString("title")).isEqualTo("Grey T-Shirt");
assertEquals("Grey T-Shirt", rs.getString("title"));
} }
@Test @Test
public void testDelete() throws Exception { public void testDelete() throws Exception {
// Arrange
final Connection connection = getConnection().getConnection(); final Connection connection = getConnection().getConnection();
final InputStream is = SampleDbUnitTest.class.getClassLoader().getResourceAsStream("items_exp_delete.xml"); final InputStream is = SampleDbUnitTest.class.getClassLoader().getResourceAsStream("items_exp_delete.xml");
ITable expectedTable = (new FlatXmlDataSetBuilder().build(is)).getTable("items"); ITable expectedTable = (new FlatXmlDataSetBuilder().build(is)).getTable("items");
// Act
connection.createStatement().executeUpdate("delete from ITEMS where id = 2"); connection.createStatement().executeUpdate("delete from ITEMS where id = 2");
// Assert
final IDataSet databaseDataSet = getConnection().createDataSet(); final IDataSet databaseDataSet = getConnection().createDataSet();
ITable actualTable = databaseDataSet.getTable("items"); ITable actualTable = databaseDataSet.getTable("items");
@ -77,16 +73,13 @@ public class SampleDbUnitTest extends DBTestCase {
@Test @Test
public void testUpdate() throws Exception { public void testUpdate() throws Exception {
// Arrange
final Connection connection = getConnection().getConnection(); final Connection connection = getConnection().getConnection();
final InputStream is = SampleDbUnitTest.class.getClassLoader().getResourceAsStream("items_exp_rename.xml"); final InputStream is = SampleDbUnitTest.class.getClassLoader().getResourceAsStream("items_exp_rename.xml");
ITable expectedTable = (new FlatXmlDataSetBuilder().build(is)).getTable("items"); ITable expectedTable = (new FlatXmlDataSetBuilder().build(is)).getTable("items");
// Act
connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1"); connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1");
// Assert
final IDataSet databaseDataSet = getConnection().createDataSet(); final IDataSet databaseDataSet = getConnection().createDataSet();
ITable actualTable = databaseDataSet.getTable("items"); ITable actualTable = databaseDataSet.getTable("items");

View File

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<dataset> <dataset>
<USERS id='1' first_name='Charles' last_name='Xavier' /> <USERS id='1' first_name='Charles' last_name='Xavier'/>
<ITEMS id='1' title='Grey T-Shirt' price='17.99' produced='2019-03-20'/> <ITEMS id='1' title='Grey T-Shirt' price='17.99' produced='2019-03-20'/>
<ITEMS id='2' title='Fitted Hat' price='29.99' produced='2019-03-21'/> <ITEMS id='2' title='Fitted Hat' price='29.99' produced='2019-03-21'/>
<ITEMS id='3' title='Backpack' price='54.99' produced='2019-03-22'/> <ITEMS id='3' title='Backpack' price='54.99' produced='2019-03-22'/>
<ITEMS id='4' title='Earrings' price='14.99' produced='2019-03-23'/> <ITEMS id='4' title='Earrings' price='14.99' produced='2019-03-23'/>
<ITEMS id='5' title='Socks' price='9.99' /> <ITEMS id='5' title='Socks' price='9.99'/>
</dataset> </dataset>

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<dataset> <dataset>
<ITEMS id='1' title='Grey T-Shirt' price='17.99' produced='2019-03-20'/> <ITEMS id='1' title='Grey T-Shirt' price='17.99' produced='2019-03-20'/>
<ITEMS id='3' title='Backpack' price='54.99' produced='2019-03-22'/> <ITEMS id='3' title='Backpack' price='54.99' produced='2019-03-22'/>
<ITEMS id='4' title='Earrings' price='14.99' produced='2019-03-23'/> <ITEMS id='4' title='Earrings' price='14.99' produced='2019-03-23'/>
<ITEMS id='5' title='Socks' price='9.99' /> <ITEMS id='5' title='Socks' price='9.99'/>
</dataset> </dataset>

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<dataset> <dataset>
<ITEMS id='1' title='Grey T-Shirt' price='17.99'/> <ITEMS id='1' title='Grey T-Shirt' price='17.99'/>
<ITEMS id='3' title='Backpack' price='54.99'/> <ITEMS id='3' title='Backpack' price='54.99'/>
<ITEMS id='4' title='Earrings' price='14.99'/> <ITEMS id='4' title='Earrings' price='14.99'/>
<ITEMS id='5' title='Socks' price='9.99' /> <ITEMS id='5' title='Socks' price='9.99'/>
</dataset> </dataset>

View File

@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<dataset> <dataset>
<ITEMS id='1' title='new name' price='17.99' produced='2019-03-20'/> <ITEMS id='1' title='new name' price='17.99' produced='2019-03-20'/>
<ITEMS id='2' title='Fitted Hat' price='29.99' produced='2019-03-21'/> <ITEMS id='2' title='Fitted Hat' price='29.99' produced='2019-03-21'/>
<ITEMS id='3' title='Backpack' price='54.99' produced='2019-03-22'/> <ITEMS id='3' title='Backpack' price='54.99' produced='2019-03-22'/>
<ITEMS id='4' title='Earrings' price='14.99' produced='2019-03-23'/> <ITEMS id='4' title='Earrings' price='14.99' produced='2019-03-23'/>
<ITEMS id='5' title='Socks' price='9.99' /> <ITEMS id='5' title='Socks' price='9.99'/>
</dataset> </dataset>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<ITEMS id='1' title='new name' price='17.99'/>
<ITEMS id='2' title='Fitted Hat' price='29.99'/>
<ITEMS id='3' title='Backpack' price='54.99'/>
<ITEMS id='4' title='Earrings' price='14.99'/>
<ITEMS id='5' title='Socks' price='9.99'/>
</dataset>

View File

@ -1,28 +1,28 @@
CREATE TABLE IF NOT EXISTS USERS CREATE TABLE IF NOT EXISTS USERS
( (
`id` int AUTO_INCREMENT NOT NULL, `id` int AUTO_INCREMENT NOT NULL,
`first_name` varchar(100) NOT NULL, `first_name` varchar(100) NOT NULL,
`last_name` varchar(100) NOT NULL, `last_name` varchar(100) NOT NULL,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
); );
CREATE TABLE IF NOT EXISTS ITEMS CREATE TABLE IF NOT EXISTS ITEMS
( (
`id` int AUTO_INCREMENT NOT NULL, `id` int AUTO_INCREMENT NOT NULL,
`title` varchar(100) NOT NULL, `title` varchar(100) NOT NULL,
`produced` date, `produced` date,
`price` float, `price` float,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
); );
CREATE TABLE IF NOT EXISTS PURCHASES CREATE TABLE IF NOT EXISTS PURCHASES
( (
`id` int NOT NULL AUTO_INCREMENT, `id` int NOT NULL AUTO_INCREMENT,
`id_user` int NOT NULL, `id_user` int NOT NULL,
`id_item` int NOT NULL, `id_item` int NOT NULL,
`total_price` float NOT NULL, `total_price` float NOT NULL,
`quantity` int(11) NOT NULL, `quantity` int(11) NOT NULL,
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
FOREIGN KEY (`id_user`) REFERENCES USERS (`id`) ON DELETE CASCADE, FOREIGN KEY (`id_user`) REFERENCES USERS (`id`) ON DELETE CASCADE,
FOREIGN KEY (`id_item`) REFERENCES ITEMS (`id`) ON DELETE CASCADE ON UPDATE CASCADE FOREIGN KEY (`id_item`) REFERENCES ITEMS (`id`) ON DELETE CASCADE ON UPDATE CASCADE
); );

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<dataset> <dataset>
<USERS id='1' first_name='Charles' last_name='Xavier' /> <USERS id='1' first_name='Charles' last_name='Xavier'/>
<USERS id='2' first_name='Scott' last_name='Summers' /> <USERS id='2' first_name='Scott' last_name='Summers'/>
<USERS id='3' first_name='Jean' last_name='Grey' /> <USERS id='3' first_name='Jean' last_name='Grey'/>
</dataset> </dataset>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<dataset> <dataset>
<USERS id='1' first_name='Charles' last_name='Xavier' /> <USERS id='1' first_name='Charles' last_name='Xavier'/>
<USERS id='3' first_name='Jean' last_name='Grey' /> <USERS id='3' first_name='Jean' last_name='Grey'/>
</dataset> </dataset>

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<dataset> <dataset>
<USERS id='1' first_name='new name' last_name='Xavier' /> <USERS id='1' first_name='new name' last_name='Xavier'/>
<USERS id='2' first_name='Scott' last_name='Summers' /> <USERS id='2' first_name='Scott' last_name='Summers'/>
<USERS id='3' first_name='Jean' last_name='Grey' /> <USERS id='3' first_name='Jean' last_name='Grey'/>
</dataset> </dataset>