Relational Database
FamilyName
GivenName
DOB
Address
Johnson
James
1960/10/20
125 King Ed
Doob
Larry
1972/05/10
656 Somerset
Relational Database
ISBN
AuthorID
765698754 1
Title
Year
1999
Object-Relational Mapping
DB
Object-Relational Mapping
Client
String name
Date birthdate
Account account
Common Mapping
A Class corresponds to 1 or
more Tables
JDBC
Accounts
Table
Database
Access
DB
Example
Suppose that we have customers with given names, family names, and an id number.
Java object:
public class Customer
{
private int id;
private String givenName;
private String familyName;
// get and set methods for each of these.
}
Object-Relational Mapping
(ORM)
Application
objects
ORM
Manager
SQL
DB
Class to be tested
Database Connections
Schema1
Java
Program
Table2
JDBC
Driver
DB
Table3
Schema2
Table4
JDBC handles the Java interface, while the driver is customized for
the particular database to be used.
For MySQL:
format:
jdbc:db_vendor://host:port/location
Loading a ResultSet
public class CustomerTableResultSet implements CustomerTable
{
private JdbcDataSource datasource;
private Connection connection;
private ResultSet results;
private String sqlCommand;
public CustomerTableRowSet( )
throws ClassNotFoundException, SQLException
{
datasource = new JdbcDataSource( );
connection = datasource.getConnection( );
Statement stmt = connection.createStatement( );
sqlCommand = "SELECT * FROM " + TABLE_NAME;
results = stmt.executeQuery( sqlCommand );
}
}
Servlet APIs,
JDBC,
JMS,
EJB,
Struts Actions
MockRunner Example
public class CustomerTableRowSetMockTest extends
BasicJDBCTestCaseAdapter {
/**
* Prepares an empty result set to be returned by
* the next SELECT statement
*
*/
MockRunner Example
@Test
public void testTableRowSet() {
prepareEmptyResultSet(); // we're not expecting any result
CustomerTable table = new CustomerTableRowSet();
table.close();
// check that select was issued
verifySQLStatementExecuted("SELECT * FROM CUSTOMER");
// check that no commit was issued
verifyNotCommitted();
// check that result set was closed
verifyAllResultSetsClosed();
}
MockRunner Example
private void prepareSingleResultSet() {
StatementResultSetHandler statementHandler =
getJDBCMockObjectFactory().
getMockConnection().
getStatementResultSetHandler();
MockResultSet result =
statementHandler.createResultSet();
// setup table
result.addColumn("CUSTOMER_ID");
result.addColumn("GIVEN_NAME");
result.addColumn("FAMILY_NAME");
// add data elements
result.addRow(new Object[] {"1", "Jane", "Deans"});
statementHandler.prepareGlobalResultSet(result);
}
MockRunner Example
@Test
public void testCustomerTableRowSet() throws
ClassNotFoundException,
SQLException {
prepareSingleResultSet();
CustomerTable table = new CustomerTableRowSet();
Customer cust = table.getCustomer(1);
table.close();
// check that right customer was created
String expGiven = "Jane";
String expFamily = "Deans";
assertEquals(expGiven,cust.getGivenName());
assertEquals(expFamily,cust.getFamilyName());
}
SQL script to delete rows from table, and then load two
rows into a MySQL database:
dbUnit
Functions:
The expected table can omit columns such as autogenerated primary keys, and the actual table can be
filtered based on the file.
Data generation
Data generation
CREATE TABLE CUSTOMER (
`id` mediumint(8) unsigned NOT NULL auto_increment,
`CUSTOMER_ID` MEDIUMINT default NULL,
`GIVEN_NAME` varchar(255) default NULL,
`FAMILY_NAME` varchar(255) default NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1;