[BAEL-2749] Refactoring and cleanup

This commit is contained in:
Joao Esperancinha 2020-05-11 18:23:14 +02:00
parent 7350a9377e
commit bd588e7321
2 changed files with 98 additions and 81 deletions

View File

@ -24,6 +24,8 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import static com.baeldung.dbunit.ConnectionSettings.JDBC_URL;
import static com.baeldung.dbunit.ConnectionSettings.PASSWORD;
import static com.baeldung.dbunit.ConnectionSettings.USER;
import static java.util.stream.Collectors.joining;
import static org.assertj.core.api.Assertions.assertThat;
import static org.dbunit.Assertion.assertEqualsIgnoreCols;
@ -33,12 +35,14 @@ public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase {
private static final Logger logger = LoggerFactory.getLogger(DataSourceDBUnitTest.class);
private Connection connection;
@Override
protected DataSource getDataSource() {
JdbcDataSource dataSource = new JdbcDataSource();
dataSource.setURL(JDBC_URL);
dataSource.setUser("sa");
dataSource.setPassword("");
dataSource.setUser(USER);
dataSource.setPassword(PASSWORD);
return dataSource;
}
@ -62,6 +66,7 @@ public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase {
@Before
public void setUp() throws Exception {
super.setUp();
connection = getConnection().getConnection();
}
@After
@ -71,9 +76,7 @@ public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase {
@Test
public void givenDataSet_whenSelect_thenFirstTitleIsGreyTShirt() throws SQLException {
final Connection connection = getDataSource().getConnection();
final ResultSet rs = connection.createStatement().executeQuery("select * from ITEMS where id = 1");
ResultSet rs = connection.createStatement().executeQuery("select * from ITEMS where id = 1");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("title")).isEqualTo("Grey T-Shirt");
@ -81,58 +84,59 @@ public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase {
@Test
public void givenDataSetEmptySchema_whenDataSetCreated_thenTablesAreEqual() throws Exception {
final IDataSet expectedDataSet = getDataSet();
final ITable expectedTable = expectedDataSet.getTable("CLIENTS");
final IDataSet databaseDataSet = getConnection().createDataSet();
final ITable actualTable = databaseDataSet.getTable("CLIENTS");
IDataSet expectedDataSet = getDataSet();
ITable expectedTable = expectedDataSet.getTable("CLIENTS");
IDataSet databaseDataSet = getConnection().createDataSet();
ITable actualTable = databaseDataSet.getTable("CLIENTS");
Assertion.assertEquals(expectedTable, actualTable);
}
@Test
public void givenDataSet_whenInsert_thenTableHasNewClient() throws Exception {
try (final InputStream is = getClass().getClassLoader().getResourceAsStream("dbunit/expected-user.xml")) {
final IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(is);
final ITable expectedTable = expectedDataSet.getTable("CLIENTS");
final Connection conn = getDataSource().getConnection();
try (InputStream is = getClass().getClassLoader().getResourceAsStream("dbunit/expected-user.xml")) {
// given
IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(is);
ITable expectedTable = expectedDataSet.getTable("CLIENTS");
Connection conn = getDataSource().getConnection();
// when
conn.createStatement()
.executeUpdate(
"INSERT INTO CLIENTS (first_name, last_name) VALUES ('John', 'Jansen')");
final ITable actualData = getConnection()
.createQueryTable(
"result_name",
"SELECT * FROM CLIENTS WHERE last_name='Jansen'");
.executeUpdate("INSERT INTO CLIENTS (first_name, last_name) VALUES ('John', 'Jansen')");
ITable actualData = getConnection()
.createQueryTable("result_name", "SELECT * FROM CLIENTS WHERE last_name='Jansen'");
// then
assertEqualsIgnoreCols(expectedTable, actualData, new String[] { "id" });
}
}
@Test
public void givenDataSet_whenDelete_thenItemIsDeleted() throws Exception {
final Connection connection = getConnection().getConnection();
try (final InputStream is = DataSourceDBUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_delete.xml")) {
try (InputStream is = DataSourceDBUnitTest.class.getClassLoader()
.getResourceAsStream("dbunit/items_exp_delete.xml")) {
// given
ITable expectedTable = (new FlatXmlDataSetBuilder().build(is)).getTable("ITEMS");
// when
connection.createStatement().executeUpdate("delete from ITEMS where id = 2");
final IDataSet databaseDataSet = getConnection().createDataSet();
// then
IDataSet databaseDataSet = getConnection().createDataSet();
ITable actualTable = databaseDataSet.getTable("ITEMS");
Assertion.assertEquals(expectedTable, actualTable);
}
}
@Test
public void givenDataSet_whenUpdate_thenItemHasNewName() throws Exception {
final Connection connection = getConnection().getConnection();
try (final InputStream is = DataSourceDBUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_rename.xml")) {
try (InputStream is = DataSourceDBUnitTest.class.getClassLoader()
.getResourceAsStream("dbunit/items_exp_rename.xml")) {
// given
ITable expectedTable = (new FlatXmlDataSetBuilder().build(is)).getTable("ITEMS");
connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1");
final IDataSet databaseDataSet = getConnection().createDataSet();
IDataSet databaseDataSet = getConnection().createDataSet();
ITable actualTable = databaseDataSet.getTable("ITEMS");
Assertion.assertEquals(expectedTable, actualTable);
@ -140,22 +144,24 @@ public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase {
}
@Test
public void givenDataSet_whenInsertUnexpectedData_thenFailOnAllUnexpectedValues() throws Exception {
try (final InputStream is = getClass().getClassLoader().getResourceAsStream("dbunit/expected-multiple-failures.xml")) {
final IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(is);
final ITable expectedTable = expectedDataSet.getTable("ITEMS");
final Connection conn = getDataSource().getConnection();
final DiffCollectingFailureHandler collectingHandler = new DiffCollectingFailureHandler();
public void givenDataSet_whenInsertUnexpectedData_thenFail() throws Exception {
try (InputStream is = getClass().getClassLoader()
.getResourceAsStream("dbunit/expected-multiple-failures.xml")) {
conn.createStatement().executeUpdate(
"INSERT INTO ITEMS (title, price) VALUES ('Battery', '1000000')");
final ITable actualData = getConnection().createDataSet().getTable("ITEMS");
// given
IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(is);
ITable expectedTable = expectedDataSet.getTable("ITEMS");
Connection conn = getDataSource().getConnection();
DiffCollectingFailureHandler collectingHandler = new DiffCollectingFailureHandler();
// when
conn.createStatement().executeUpdate("INSERT INTO ITEMS (title, price) VALUES ('Battery', '1000000')");
ITable actualData = getConnection().createDataSet().getTable("ITEMS");
// then
Assertion.assertEquals(expectedTable, actualData, collectingHandler);
if (!collectingHandler.getDiffList().isEmpty()) {
String message = (String) collectingHandler
.getDiffList()
.stream()
String message = (String) collectingHandler.getDiffList().stream()
.map(d -> formatDifference((Difference) d)).collect(joining("\n"));
logger.error(() -> message);
}
@ -163,6 +169,8 @@ public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase {
}
private static String formatDifference(Difference diff) {
return "expected value in " + diff.getExpectedTable().getTableMetaData().getTableName() + "." + diff.getColumnName() + " row " + diff.getRowIndex() + ":" + diff.getExpectedValue() + ", but was: " + diff.getActualValue();
return "expected value in " + diff.getExpectedTable().getTableMetaData().getTableName() + "." + diff
.getColumnName() + " row " + diff.getRowIndex() + ":" + diff.getExpectedValue() + ", but was: " + diff
.getActualValue();
}
}

View File

@ -31,13 +31,15 @@ public class OldSchoolDbUnitTest {
private static IDatabaseTester tester = null;
private Connection connection;
@BeforeClass
public static void setUp() throws Exception {
tester = initDatabaseTester();
}
private static IDatabaseTester initDatabaseTester() throws Exception {
final JdbcDatabaseTester tester = new JdbcDatabaseTester(JDBC_DRIVER, JDBC_URL, USER, PASSWORD);
JdbcDatabaseTester tester = new JdbcDatabaseTester(JDBC_DRIVER, JDBC_URL, USER, PASSWORD);
tester.setDataSet(initDataSet());
tester.setSetUpOperation(DatabaseOperation.REFRESH);
tester.setTearDownOperation(DatabaseOperation.DELETE_ALL);
@ -45,7 +47,7 @@ public class OldSchoolDbUnitTest {
}
private static IDataSet initDataSet() throws Exception {
try (final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/data.xml")) {
try (InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/data.xml")) {
return new FlatXmlDataSetBuilder().build(is);
}
}
@ -53,6 +55,7 @@ public class OldSchoolDbUnitTest {
@Before
public void setup() throws Exception {
tester.onSetup();
connection = tester.getConnection().getConnection();
}
@After
@ -62,94 +65,100 @@ public class OldSchoolDbUnitTest {
@Test
public void givenDataSet_whenSelect_thenFirstTitleIsGreyTShirt() throws Exception {
final Connection connection = tester.getConnection().getConnection();
final ResultSet rs = connection.createStatement().executeQuery("select * from ITEMS where id = 1");
ResultSet rs = connection.createStatement().executeQuery("select * from ITEMS where id = 1");
assertThat(rs.next()).isTrue();
assertThat(rs.getString("title")).isEqualTo("Grey T-Shirt");
}
@Test
public void givenDataSet_whenInsert_thenGetResultsAreStillEqualIfIgnoringColumnsWithDifferentProduced() throws Exception {
final Connection connection = tester.getConnection().getConnection();
final String[] excludedColumns = { "id", "produced" };
try (final InputStream is = getClass().getClassLoader().getResourceAsStream("dbunit/expected-ignoring-registered_at.xml")) {
final IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(is);
final ITable expectedTable = DefaultColumnFilter.excludedColumnsTable(
expectedDataSet.getTable("ITEMS"), excludedColumns);
public void givenDataSet_whenInsert_thenGetResultsAreStillEqualIfIgnoringColumnsWithDifferentProduced()
throws Exception {
String[] excludedColumns = { "id", "produced" };
try (InputStream is = getClass().getClassLoader()
.getResourceAsStream("dbunit/expected-ignoring-registered_at.xml")) {
// given
IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(is);
ITable expectedTable = DefaultColumnFilter
.excludedColumnsTable(expectedDataSet.getTable("ITEMS"), excludedColumns);
connection.createStatement().executeUpdate(
"INSERT INTO ITEMS (title, price, produced) VALUES('Necklace', 199.99, now())");
final IDataSet databaseDataSet = tester.getConnection().createDataSet();
final ITable actualTable = DefaultColumnFilter.excludedColumnsTable(
databaseDataSet.getTable("ITEMS"), excludedColumns);
// when
connection.createStatement()
.executeUpdate("INSERT INTO ITEMS (title, price, produced) VALUES('Necklace', 199.99, now())");
// then
IDataSet databaseDataSet = tester.getConnection().createDataSet();
ITable actualTable = DefaultColumnFilter
.excludedColumnsTable(databaseDataSet.getTable("ITEMS"), excludedColumns);
Assertion.assertEquals(expectedTable, actualTable);
}
}
@Test
public void givenDataSet_whenDelete_thenItemIsRemoved() throws Exception {
final Connection connection = tester.getConnection().getConnection();
try (final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_delete.xml")) {
try (InputStream is = OldSchoolDbUnitTest.class.getClassLoader()
.getResourceAsStream("dbunit/items_exp_delete.xml")) {
// given
ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS");
// when
connection.createStatement().executeUpdate("delete from ITEMS where id = 2");
final IDataSet databaseDataSet = tester.getConnection().createDataSet();
// then
IDataSet databaseDataSet = tester.getConnection().createDataSet();
ITable actualTable = databaseDataSet.getTable("ITEMS");
assertEquals(expectedTable, actualTable);
}
}
@Test
public void givenDataSet_whenDelete_thenItemIsRemovedAndResultsEqualIfProducedIsIgnored() throws Exception {
final Connection connection = tester.getConnection().getConnection();
try (final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_delete_no_produced.xml")) {
final ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS");
public void givenDataSet_whenProductIgnoredAndDelete_thenItemIsRemoved() throws Exception {
try (InputStream is = OldSchoolDbUnitTest.class.getClassLoader()
.getResourceAsStream("dbunit/items_exp_delete_no_produced.xml")) {
// given
ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS");
// when
connection.createStatement().executeUpdate("delete from ITEMS where id = 2");
final IDataSet databaseDataSet = tester.getConnection().createDataSet();
// then
IDataSet databaseDataSet = tester.getConnection().createDataSet();
ITable actualTable = databaseDataSet.getTable("ITEMS");
actualTable = DefaultColumnFilter.excludedColumnsTable(actualTable, new String[] { "produced" });
assertEquals(expectedTable, actualTable);
}
}
@Test
public void givenDataSet_whenUpdate_thenItemHasNewName() throws Exception {
final Connection connection = tester.getConnection().getConnection();
try (final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_rename.xml")) {
final ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS");
try (InputStream is = OldSchoolDbUnitTest.class.getClassLoader()
.getResourceAsStream("dbunit/items_exp_rename.xml")) {
// given
ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS");
// when
connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1");
final IDataSet databaseDataSet = tester.getConnection().createDataSet();
// then
IDataSet databaseDataSet = tester.getConnection().createDataSet();
ITable actualTable = databaseDataSet.getTable("ITEMS");
assertEquals(expectedTable, actualTable);
}
}
@Test
public void givenDataSet_whenUpdateWithNoProduced_thenItemHasNewName() throws Exception {
final Connection connection = tester.getConnection().getConnection();
try (final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_rename_no_produced.xml")) {
try (InputStream is = OldSchoolDbUnitTest.class.getClassLoader()
.getResourceAsStream("dbunit/items_exp_rename_no_produced.xml")) {
// given
ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS");
expectedTable = DefaultColumnFilter.excludedColumnsTable(expectedTable, new String[] { "produced" });
// when
connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1");
final IDataSet databaseDataSet = tester.getConnection().createDataSet();
// then
IDataSet databaseDataSet = tester.getConnection().createDataSet();
ITable actualTable = databaseDataSet.getTable("ITEMS");
actualTable = DefaultColumnFilter.excludedColumnsTable(actualTable, new String[] { "produced" });
assertEquals(expectedTable, actualTable);