[BAEL-2749] USERS table name changed to CLIENTS

This commit is contained in:
Joao Esperancinha 2020-03-31 12:38:39 +02:00
parent d5d1cc7d58
commit ef19d83db5
8 changed files with 32 additions and 21 deletions

View File

@ -1,7 +1,9 @@
package com.baeldung.dbunit;
import org.dbunit.Assertion;
import org.dbunit.DataSourceBasedDBTestCase;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ITable;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.h2.jdbcx.JdbcDataSource;
import org.junit.Test;
@ -11,16 +13,17 @@ import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import static com.baeldung.dbunit.ConnectionSettings.JDBC_URL;
import static org.assertj.core.api.Assertions.assertThat;
public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase {
@Override
protected DataSource getDataSource() {
JdbcDataSource dataSource = new JdbcDataSource();
dataSource.setURL(
"jdbc:h2:mem:default;DB_CLOSE_DELAY=-1;init=runscript from 'classpath:schema.sql'");
dataSource.setURL(JDBC_URL);
dataSource.setUser("sa");
dataSource.setPassword("sa");
dataSource.setPassword("");
return dataSource;
}
@ -43,4 +46,13 @@ public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase {
assertThat(rs.next()).isTrue();
assertThat(rs.getString("title")).isEqualTo("Grey T-Shirt");
}
@Test
public void testEmptySchema() throws Exception {
IDataSet expectedDataSet = getDataSet();
ITable expectedTable = expectedDataSet.getTable("CLIENTS");
IDataSet databaseDataSet = getConnection().createDataSet();
ITable actualTable = databaseDataSet.getTable("CLIENTS");
Assertion.assertEquals(expectedTable, actualTable);
}
}

View File

@ -27,7 +27,6 @@ public class DbUnitTest extends DBTestCase {
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, JDBC_URL);
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, USER);
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, PASSWORD);
// System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_SCHEMA, "");
}
@Override

View File

@ -50,13 +50,13 @@ public class PrepAndExpectedDbUnitTest extends DefaultPrepAndExpectedTestCase {
@Test
public void testSelect() throws Exception {
final Connection connection = getConnection().getConnection();
final VerifyTableDefinition[] verifyTables = {new VerifyTableDefinition("USERS", new String[]{})};
final VerifyTableDefinition[] verifyTables = {new VerifyTableDefinition("CLIENTS", new String[]{})};
final String[] prepDataFiles = {"/users.xml"};
final String[] expectedDataFiles = {"/users.xml"};
final PrepAndExpectedTestCaseSteps testSteps =
() -> connection
.createStatement()
.executeQuery("select * from USERS where id = 1");
.executeQuery("select * from CLIENTS where id = 1");
final ResultSet rs = (ResultSet) super.runTest(verifyTables, prepDataFiles, expectedDataFiles, testSteps);
@ -67,13 +67,13 @@ public class PrepAndExpectedDbUnitTest extends DefaultPrepAndExpectedTestCase {
@Test
public void testUpdate() throws Exception {
final Connection connection = getConnection().getConnection();
final VerifyTableDefinition[] verifyTables = {new VerifyTableDefinition("USERS", new String[]{})}; // define tables to verify
final VerifyTableDefinition[] verifyTables = {new VerifyTableDefinition("CLIENTS", new String[]{})}; // define tables to verify
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");
.executeUpdate("update CLIENTS set first_name = 'new name' where id = 1");
super.runTest(verifyTables, prepDataFiles, expectedDataFiles, testSteps);
}
@ -81,13 +81,13 @@ public class PrepAndExpectedDbUnitTest extends DefaultPrepAndExpectedTestCase {
@Test
public void testDelete() throws Exception {
final Connection connection = getConnection().getConnection();
final VerifyTableDefinition[] verifyTables = {new VerifyTableDefinition("USERS", new String[]{})};
final VerifyTableDefinition[] verifyTables = {new VerifyTableDefinition("CLIENTS", 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");
.executeUpdate("delete from CLIENTS where id = 2");
super.runTest(verifyTables, prepDataFiles, expectedDataFiles, testSteps);
}

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<USERS id='1' first_name='Charles' last_name='Xavier'/>
<CLIENTS 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'/>

View File

@ -1,4 +1,4 @@
CREATE TABLE IF NOT EXISTS USERS
CREATE TABLE IF NOT EXISTS CLIENTS
(
`id` int AUTO_INCREMENT NOT NULL,
`first_name` varchar(100) NOT NULL,
@ -23,6 +23,6 @@ CREATE TABLE IF NOT EXISTS PURCHASES
`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_user`) REFERENCES CLIENTS (`id`) ON DELETE 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"?>
<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'/>
<CLIENTS id='1' first_name='Charles' last_name='Xavier'/>
<CLIENTS id='2' first_name='Scott' last_name='Summers'/>
<CLIENTS id='3' first_name='Jean' last_name='Grey'/>
</dataset>

View File

@ -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'/>
<CLIENTS id='1' first_name='Charles' last_name='Xavier'/>
<CLIENTS id='3' first_name='Jean' last_name='Grey'/>
</dataset>

View File

@ -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'/>
<CLIENTS id='1' first_name='new name' last_name='Xavier'/>
<CLIENTS id='2' first_name='Scott' last_name='Summers'/>
<CLIENTS id='3' first_name='Jean' last_name='Grey'/>
</dataset>