[BAEL-2749] Indentation and annotations format

This commit is contained in:
Joao Esperancinha 2020-04-02 11:20:34 +02:00
parent 064e16251b
commit f06af64998
5 changed files with 310 additions and 281 deletions

View File

@ -1,8 +1,8 @@
package com.baeldung.dbunit; package com.baeldung.dbunit;
public class ConnectionSettings { public class ConnectionSettings {
public static final String JDBC_DRIVER = org.h2.Driver.class.getName(); 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:dbunit/schema.sql'"; public static final String JDBC_URL = "jdbc:h2:mem:default;DB_CLOSE_DELAY=-1;init=runscript from 'classpath:dbunit/schema.sql'";
public static final String USER = "sa"; public static final String USER = "sa";
public static final String PASSWORD = ""; public static final String PASSWORD = "";
} }

View File

@ -26,87 +26,97 @@ import static org.assertj.core.api.Assertions.assertThat;
public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase { public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase {
private static final Logger logger = LoggerFactory.getLogger(DataSourceDBUnitTest.class); private static final Logger logger = LoggerFactory.getLogger(DataSourceDBUnitTest.class);
@Override protected javax.sql.DataSource getDataSource() { @Override
JdbcDataSource dataSource = new JdbcDataSource(); protected javax.sql.DataSource getDataSource() {
dataSource.setURL(JDBC_URL); JdbcDataSource dataSource = new JdbcDataSource();
dataSource.setUser("sa"); dataSource.setURL(JDBC_URL);
dataSource.setPassword(""); dataSource.setUser("sa");
return dataSource; dataSource.setPassword("");
return dataSource;
}
@Override
protected IDataSet getDataSet() throws Exception {
try (InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream("dbunit/data.xml")) {
return new FlatXmlDataSetBuilder().build(resourceAsStream);
} }
}
@Override protected IDataSet getDataSet() throws Exception { @Override
try (InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream("dbunit/data.xml")) { protected DatabaseOperation getSetUpOperation() {
return new FlatXmlDataSetBuilder().build(resourceAsStream); return DatabaseOperation.REFRESH;
} }
@Override
protected DatabaseOperation getTearDownOperation() {
return DatabaseOperation.DELETE_ALL;
}
@Before
public void setUp() throws Exception {
super.setUp();
}
@After
public void tearDown() throws Exception {
super.tearDown();
}
@Test
public void testSimpleDataSet() throws SQLException {
final Connection connection = getDataSource().getConnection();
final 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 testEmptySchema() throws Exception {
final IDataSet expectedDataSet = getDataSet();
final ITable expectedTable = expectedDataSet.getTable("CLIENTS");
final IDataSet databaseDataSet = getConnection().createDataSet();
final ITable actualTable = databaseDataSet.getTable("CLIENTS");
Assertion.assertEquals(expectedTable, actualTable);
}
@Test
public void testAssertByQuery() 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();
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'");
Assertion.assertEqualsIgnoreCols(expectedTable, actualData, new String[] { "id" });
} }
}
@Override protected DatabaseOperation getSetUpOperation() { @Test
return DatabaseOperation.REFRESH; public void testMultipleFailures() 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();
conn.createStatement().executeUpdate("INSERT INTO ITEMS (title, price) VALUES ('Battery', '1000000')");
final ITable actualData = getConnection().createDataSet().getTable("ITEMS");
Assertion.assertEquals(expectedTable, actualData, collectingHandler);
if (!collectingHandler.getDiffList().isEmpty()) {
String message = (String) collectingHandler.getDiffList().stream().map(d -> formatDifference((Difference) d)).collect(joining("\n"));
logger.error(() -> message);
}
} }
}
@Override protected DatabaseOperation getTearDownOperation() { private static String formatDifference(Difference diff) {
return DatabaseOperation.DELETE_ALL; return "expected value in " + diff.getExpectedTable().getTableMetaData().getTableName() + "." + diff.getColumnName() + " row " + diff.getRowIndex() + ":" + diff.getExpectedValue() + ", but was: " + diff.getActualValue();
} }
@Before public void setUp() throws Exception {
super.setUp();
}
@After public void tearDown() throws Exception {
super.tearDown();
}
@Test public void testSimpleDataSet() throws SQLException {
final Connection connection = getDataSource().getConnection();
final 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 testEmptySchema() throws Exception {
final IDataSet expectedDataSet = getDataSet();
final ITable expectedTable = expectedDataSet.getTable("CLIENTS");
final IDataSet databaseDataSet = getConnection().createDataSet();
final ITable actualTable = databaseDataSet.getTable("CLIENTS");
Assertion.assertEquals(expectedTable, actualTable);
}
@Test public void testAssertByQuery() 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();
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'");
Assertion.assertEqualsIgnoreCols(expectedTable, actualData, new String[] { "id" });
}
}
@Test public void testMultipleFailures() 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();
conn.createStatement().executeUpdate("INSERT INTO ITEMS (title, price) VALUES ('Battery', '1000000')");
final ITable actualData = getConnection().createDataSet().getTable("ITEMS");
Assertion.assertEquals(expectedTable, actualData, collectingHandler);
if (!collectingHandler.getDiffList().isEmpty()) {
String message = (String) collectingHandler.getDiffList().stream().map(d -> formatDifference((Difference) d)).collect(joining("\n"));
logger.error(() -> message);
}
}
}
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();
}
} }

View File

@ -21,65 +21,71 @@ import static org.assertj.core.api.Assertions.assertThat;
public class DbUnitTest extends DBTestCase { public class DbUnitTest extends DBTestCase {
public DbUnitTest(String name) { public DbUnitTest(String name) {
super(name); super(name);
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, JDBC_DRIVER); System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, JDBC_DRIVER);
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, JDBC_URL); System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, JDBC_URL);
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, USER); System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, USER);
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, PASSWORD); System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, PASSWORD);
}
@Override
protected IDataSet getDataSet() throws Exception {
try (final InputStream is = DbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/data.xml")) {
return new FlatXmlDataSetBuilder().build(is);
} }
}
@Override protected IDataSet getDataSet() throws Exception { @Override
try (final InputStream is = DbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/data.xml")) { protected DatabaseOperation getSetUpOperation() {
return new FlatXmlDataSetBuilder().build(is); return DatabaseOperation.REFRESH;
} }
@Override
protected DatabaseOperation getTearDownOperation() {
return DatabaseOperation.DELETE_ALL;
}
@Test
public void testSelect() throws Exception {
final Connection connection = getConnection().getConnection();
final 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 testDelete() throws Exception {
final Connection connection = getConnection().getConnection();
try (final InputStream is = DbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_delete.xml")) {
ITable expectedTable = (new FlatXmlDataSetBuilder().build(is)).getTable("ITEMS");
connection.createStatement().executeUpdate("delete from ITEMS where id = 2");
final IDataSet databaseDataSet = getConnection().createDataSet();
ITable actualTable = databaseDataSet.getTable("ITEMS");
Assertion.assertEquals(expectedTable, actualTable);
} }
}
@Override protected DatabaseOperation getSetUpOperation() { @Test
return DatabaseOperation.REFRESH; public void testUpdate() throws Exception {
} final Connection connection = getConnection().getConnection();
@Override protected DatabaseOperation getTearDownOperation() { try (final InputStream is = DbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_rename.xml")) {
return DatabaseOperation.DELETE_ALL; ITable expectedTable = (new FlatXmlDataSetBuilder().build(is)).getTable("ITEMS");
}
connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1");
@Test public void testSelect() throws Exception {
final Connection connection = getConnection().getConnection(); final IDataSet databaseDataSet = getConnection().createDataSet();
ITable actualTable = databaseDataSet.getTable("ITEMS");
final ResultSet rs = connection.createStatement().executeQuery("select * from ITEMS where id = 1");
Assertion.assertEquals(expectedTable, actualTable);
assertThat(rs.next()).isTrue();
assertThat(rs.getString("title")).isEqualTo("Grey T-Shirt");
}
@Test public void testDelete() throws Exception {
final Connection connection = getConnection().getConnection();
try (final InputStream is = DbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_delete.xml")) {
ITable expectedTable = (new FlatXmlDataSetBuilder().build(is)).getTable("ITEMS");
connection.createStatement().executeUpdate("delete from ITEMS where id = 2");
final IDataSet databaseDataSet = getConnection().createDataSet();
ITable actualTable = databaseDataSet.getTable("ITEMS");
Assertion.assertEquals(expectedTable, actualTable);
}
}
@Test public void testUpdate() throws Exception {
final Connection connection = getConnection().getConnection();
try (final InputStream is = DbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_rename.xml")) {
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();
ITable actualTable = databaseDataSet.getTable("ITEMS");
Assertion.assertEquals(expectedTable, actualTable);
}
} }
}
} }

View File

@ -26,119 +26,128 @@ import static org.dbunit.Assertion.assertEquals;
public class OldSchoolDbUnitTest { public class OldSchoolDbUnitTest {
private static IDatabaseTester tester = null; private static IDatabaseTester tester = null;
@BeforeClass public static void setUp() throws Exception { @BeforeClass
tester = initDatabaseTester(); 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);
tester.setDataSet(initDataSet());
tester.setSetUpOperation(DatabaseOperation.REFRESH);
tester.setTearDownOperation(DatabaseOperation.DELETE_ALL);
return tester;
}
private static IDataSet initDataSet() throws Exception {
try (final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/data.xml")) {
return new FlatXmlDataSetBuilder().build(is);
} }
}
private static IDatabaseTester initDatabaseTester() throws Exception { @Before
final JdbcDatabaseTester tester = new JdbcDatabaseTester(JDBC_DRIVER, JDBC_URL, USER, PASSWORD); public void setup() throws Exception {
tester.setDataSet(initDataSet()); tester.onSetup();
tester.setSetUpOperation(DatabaseOperation.REFRESH); }
tester.setTearDownOperation(DatabaseOperation.DELETE_ALL);
return tester; @After
public void tearDown() throws Exception {
tester.onTearDown();
}
@Test
public void testSelect() throws Exception {
final Connection connection = tester.getConnection().getConnection();
final 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 testIgnoringProduced() 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);
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);
Assertion.assertEquals(expectedTable, actualTable);
} }
}
private static IDataSet initDataSet() throws Exception { @Test
try (final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/data.xml")) { public void testDelete() throws Exception {
return new FlatXmlDataSetBuilder().build(is); final Connection connection = tester.getConnection().getConnection();
}
try (final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_delete.xml")) {
ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS");
connection.createStatement().executeUpdate("delete from ITEMS where id = 2");
final IDataSet databaseDataSet = tester.getConnection().createDataSet();
ITable actualTable = databaseDataSet.getTable("ITEMS");
assertEquals(expectedTable, actualTable);
} }
}
@Before public void setup() throws Exception { @Test
tester.onSetup(); public void testDeleteWithExcludedColumns() 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");
connection.createStatement().executeUpdate("delete from ITEMS where id = 2");
final IDataSet databaseDataSet = tester.getConnection().createDataSet();
ITable actualTable = databaseDataSet.getTable("ITEMS");
actualTable = DefaultColumnFilter.excludedColumnsTable(actualTable, new String[] { "produced" });
assertEquals(expectedTable, actualTable);
} }
}
@After public void tearDown() throws Exception { @Test
tester.onTearDown(); public void testUpdate() 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");
connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1");
final IDataSet databaseDataSet = tester.getConnection().createDataSet();
ITable actualTable = databaseDataSet.getTable("ITEMS");
assertEquals(expectedTable, actualTable);
} }
}
@Test public void testSelect() throws Exception { @Test
final Connection connection = tester.getConnection().getConnection(); public void testUpdateWithExcludedColumns() throws Exception {
final Connection connection = tester.getConnection().getConnection();
final ResultSet rs = connection.createStatement().executeQuery("select * from ITEMS where id = 1"); try (final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_rename_no_produced.xml")) {
ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS");
assertThat(rs.next()).isTrue(); connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1");
assertThat(rs.getString("title")).isEqualTo("Grey T-Shirt");
} final IDataSet databaseDataSet = tester.getConnection().createDataSet();
ITable actualTable = databaseDataSet.getTable("ITEMS");
@Test public void testIgnoringProduced() throws Exception { actualTable = DefaultColumnFilter.excludedColumnsTable(actualTable, new String[] { "produced" });
final Connection connection = tester.getConnection().getConnection();
final String[] excludedColumns = { "id", "produced" }; assertEquals(expectedTable, actualTable);
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);
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);
Assertion.assertEquals(expectedTable, actualTable);
}
}
@Test public void testDelete() throws Exception {
final Connection connection = tester.getConnection().getConnection();
try (final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_delete.xml")) {
ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS");
connection.createStatement().executeUpdate("delete from ITEMS where id = 2");
final IDataSet databaseDataSet = tester.getConnection().createDataSet();
ITable actualTable = databaseDataSet.getTable("ITEMS");
assertEquals(expectedTable, actualTable);
}
}
@Test public void testDeleteWithExcludedColumns() 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");
connection.createStatement().executeUpdate("delete from ITEMS where id = 2");
final IDataSet databaseDataSet = tester.getConnection().createDataSet();
ITable actualTable = databaseDataSet.getTable("ITEMS");
actualTable = DefaultColumnFilter.excludedColumnsTable(actualTable, new String[] { "produced" });
assertEquals(expectedTable, actualTable);
}
}
@Test public void testUpdate() 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");
connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1");
final IDataSet databaseDataSet = tester.getConnection().createDataSet();
ITable actualTable = databaseDataSet.getTable("ITEMS");
assertEquals(expectedTable, actualTable);
}
}
@Test public void testUpdateWithExcludedColumns() throws Exception {
final Connection connection = tester.getConnection().getConnection();
try (final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/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);
}
} }
}
} }

View File

@ -24,60 +24,64 @@ import static org.assertj.core.api.Assertions.assertThat;
public class PrepAndExpectedDbUnitTest extends DefaultPrepAndExpectedTestCase { public class PrepAndExpectedDbUnitTest extends DefaultPrepAndExpectedTestCase {
@Override public void setUp() throws Exception { @Override
setDatabaseTester(initDatabaseTester()); public void setUp() throws Exception {
setDataFileLoader(initDataFileLoader()); setDatabaseTester(initDatabaseTester());
super.setUp(); setDataFileLoader(initDataFileLoader());
super.setUp();
}
private IDatabaseTester initDatabaseTester() throws Exception {
final JdbcDatabaseTester tester = new JdbcDatabaseTester(JDBC_DRIVER, JDBC_URL, USER, PASSWORD);
tester.setDataSet(initDataSet());
tester.setSetUpOperation(DatabaseOperation.REFRESH);
return tester;
}
private IDataSet initDataSet() throws Exception {
try (final InputStream is = getClass().getClassLoader().getResourceAsStream("dbunit/data.xml")) {
return new FlatXmlDataSetBuilder().build(is);
} }
}
private IDatabaseTester initDatabaseTester() throws Exception { private DataFileLoader initDataFileLoader() {
final JdbcDatabaseTester tester = new JdbcDatabaseTester(JDBC_DRIVER, JDBC_URL, USER, PASSWORD); return new FlatXmlDataFileLoader();
tester.setDataSet(initDataSet()); }
tester.setSetUpOperation(DatabaseOperation.REFRESH);
return tester;
}
private IDataSet initDataSet() throws Exception { @Test
try (final InputStream is = getClass().getClassLoader().getResourceAsStream("dbunit/data.xml")) { public void testSelect() throws Exception {
return new FlatXmlDataSetBuilder().build(is); final Connection connection = getConnection().getConnection();
} final VerifyTableDefinition[] verifyTables = { new VerifyTableDefinition("CLIENTS", new String[] {}) };
} final String[] prepDataFiles = { "/dbunit/users.xml" };
final String[] expectedDataFiles = { "/dbunit/users.xml" };
final PrepAndExpectedTestCaseSteps testSteps = () -> connection.createStatement().executeQuery("select * from CLIENTS where id = 1");
private DataFileLoader initDataFileLoader() { final ResultSet rs = (ResultSet) super.runTest(verifyTables, prepDataFiles, expectedDataFiles, testSteps);
return new FlatXmlDataFileLoader();
}
@Test public void testSelect() throws Exception { assertThat(rs.next()).isTrue();
final Connection connection = getConnection().getConnection(); assertThat(rs.getString("last_name")).isEqualTo("Xavier");
final VerifyTableDefinition[] verifyTables = { new VerifyTableDefinition("CLIENTS", new String[] {}) }; }
final String[] prepDataFiles = { "/dbunit/users.xml" };
final String[] expectedDataFiles = { "/dbunit/users.xml" };
final PrepAndExpectedTestCaseSteps testSteps = () -> connection.createStatement().executeQuery("select * from CLIENTS where id = 1");
final ResultSet rs = (ResultSet) super.runTest(verifyTables, prepDataFiles, expectedDataFiles, testSteps); @Test
public void testUpdate() throws Exception {
final Connection connection = getConnection().getConnection();
final VerifyTableDefinition[] verifyTables = { new VerifyTableDefinition("CLIENTS", new String[] {}) }; // define tables to verify
final String[] prepDataFiles = { "/dbunit/users.xml" };
final String[] expectedDataFiles = { "/dbunit/users_exp_rename.xml" };
final PrepAndExpectedTestCaseSteps testSteps = () -> connection.createStatement().executeUpdate("update CLIENTS set first_name = 'new name' where id = 1");
assertThat(rs.next()).isTrue(); super.runTest(verifyTables, prepDataFiles, expectedDataFiles, testSteps);
assertThat(rs.getString("last_name")).isEqualTo("Xavier"); }
}
@Test public void testUpdate() throws Exception { @Test
final Connection connection = getConnection().getConnection(); public void testDelete() throws Exception {
final VerifyTableDefinition[] verifyTables = { new VerifyTableDefinition("CLIENTS", new String[] {}) }; // define tables to verify final Connection connection = getConnection().getConnection();
final String[] prepDataFiles = { "/dbunit/users.xml" }; final VerifyTableDefinition[] verifyTables = { new VerifyTableDefinition("CLIENTS", new String[] {}) };
final String[] expectedDataFiles = { "/dbunit/users_exp_rename.xml" }; final String[] prepDataFiles = { "/dbunit/users.xml" };
final PrepAndExpectedTestCaseSteps testSteps = () -> connection.createStatement().executeUpdate("update CLIENTS set first_name = 'new name' where id = 1"); final String[] expectedDataFiles = { "/dbunit/users_exp_delete.xml" };
final PrepAndExpectedTestCaseSteps testSteps = () -> connection.createStatement().executeUpdate("delete from CLIENTS where id = 2");
super.runTest(verifyTables, prepDataFiles, expectedDataFiles, testSteps); super.runTest(verifyTables, prepDataFiles, expectedDataFiles, testSteps);
} }
@Test public void testDelete() throws Exception {
final Connection connection = getConnection().getConnection();
final VerifyTableDefinition[] verifyTables = { new VerifyTableDefinition("CLIENTS", new String[] {}) };
final String[] prepDataFiles = { "/dbunit/users.xml" };
final String[] expectedDataFiles = { "/dbunit/users_exp_delete.xml" };
final PrepAndExpectedTestCaseSteps testSteps = () -> connection.createStatement().executeUpdate("delete from CLIENTS where id = 2");
super.runTest(verifyTables, prepDataFiles, expectedDataFiles, testSteps);
}
} }