[BAEL-2749] Code refactoring
This commit is contained in:
parent
71ba591868
commit
e4f4f7110b
|
@ -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");
|
||||
|
||||
|
|
|
@ -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>
|
Loading…
Reference in New Issue