[BAEL-2749] Code refactoring
This commit is contained in:
parent
71ba591868
commit
e4f4f7110b
|
@ -8,6 +8,6 @@
|
|||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
</configuration>
|
|
@ -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 = "";
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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");
|
||||
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<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='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='4' title='Earrings' price='14.99' produced='2019-03-23'/>
|
||||
<ITEMS id='5' title='Socks' price='9.99' />
|
||||
<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='3' title='Backpack' price='54.99' produced='2019-03-22'/>
|
||||
<ITEMS id='4' title='Earrings' price='14.99' produced='2019-03-23'/>
|
||||
<ITEMS id='5' title='Socks' price='9.99'/>
|
||||
</dataset>
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<dataset>
|
||||
<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='4' title='Earrings' price='14.99' produced='2019-03-23'/>
|
||||
<ITEMS id='5' title='Socks' price='9.99' />
|
||||
<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='4' title='Earrings' price='14.99' produced='2019-03-23'/>
|
||||
<ITEMS id='5' title='Socks' price='9.99'/>
|
||||
</dataset>
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<dataset>
|
||||
<ITEMS id='1' title='Grey T-Shirt' price='17.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' />
|
||||
<ITEMS id='1' title='Grey T-Shirt' price='17.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>
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<dataset>
|
||||
<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='3' title='Backpack' price='54.99' produced='2019-03-22'/>
|
||||
<ITEMS id='4' title='Earrings' price='14.99' produced='2019-03-23'/>
|
||||
<ITEMS id='5' title='Socks' price='9.99' />
|
||||
<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='3' title='Backpack' price='54.99' produced='2019-03-22'/>
|
||||
<ITEMS id='4' title='Earrings' price='14.99' produced='2019-03-23'/>
|
||||
<ITEMS id='5' title='Socks' price='9.99'/>
|
||||
</dataset>
|
||||
|
|
|
@ -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>
|
|
@ -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
|
||||
);
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<dataset>
|
||||
<USERS id='1' first_name='Charles' last_name='Xavier' />
|
||||
<USERS id='2' first_name='Scott' last_name='Summers' />
|
||||
<USERS id='3' first_name='Jean' last_name='Grey' />
|
||||
<USERS id='1' first_name='Charles' last_name='Xavier'/>
|
||||
<USERS id='2' first_name='Scott' last_name='Summers'/>
|
||||
<USERS id='3' first_name='Jean' last_name='Grey'/>
|
||||
</dataset>
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<dataset>
|
||||
<USERS id='1' first_name='Charles' last_name='Xavier' />
|
||||
<USERS id='3' first_name='Jean' last_name='Grey' />
|
||||
<USERS id='1' first_name='Charles' last_name='Xavier'/>
|
||||
<USERS id='3' first_name='Jean' last_name='Grey'/>
|
||||
</dataset>
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<dataset>
|
||||
<USERS id='1' first_name='new name' last_name='Xavier' />
|
||||
<USERS id='2' first_name='Scott' last_name='Summers' />
|
||||
<USERS id='3' first_name='Jean' last_name='Grey' />
|
||||
<USERS id='1' first_name='new name' last_name='Xavier'/>
|
||||
<USERS id='2' first_name='Scott' last_name='Summers'/>
|
||||
<USERS id='3' first_name='Jean' last_name='Grey'/>
|
||||
</dataset>
|
||||
|
|
Loading…
Reference in New Issue