001: package com.mockrunner.example.jdbc;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005:
006: import com.mockrunner.jdbc.BasicJDBCTestCaseAdapter;
007: import com.mockrunner.jdbc.FileResultSetFactory;
008: import com.mockrunner.mock.jdbc.MockResultSet;
009:
010: /**
011: * Example test for {@link Bookstore}. Demonstrates the usage of
012: * {@link com.mockrunner.jdbc.JDBCTestModule}
013: * and {@link com.mockrunner.jdbc.BasicJDBCTestCaseAdapter}.
014: * This is an example for the handling of {@link com.mockrunner.mock.jdbc.MockResultSet}.
015: * The data that the JDBC code should receive when executing the <i>select</i>
016: * statement is specified in the file <i>bookstore.txt</i>. Please note that we
017: * do not pass a filled <code>List</code> to the order method in the
018: * succesful order test, because the choice if a table row should
019: * be in the result is done by SQL. The framework does not execute any
020: * SQL. In the second test, we check the correct SQL string.
021: * In the third test, we specify that the statement should raise an
022: * SQL exception (to simulate a database error) and verify, that
023: * the transaction is rolled back.
024: *
025: * This example uses regular expressions. Per default, regular expressions
026: * are disabled, i.e. the preparation of result sets and verification of
027: * executed SQL statements is based on simple string comparison.
028: * With<br><br>
029: * <code>setUseRegularExpressions(true);</code>
030: * <br><br>and<br><br>
031: * <code>getStatementResultSetHandler().setUseRegularExpressions(true);</code>
032: * <br>
033: * you enable regular expressions for the preparation of result sets and
034: * verification of executed SQL statements (note that for prepared and
035: * callable statements you would have to enable it seperately).
036: * E.g. <code>prepareResultSet("select.*isbn,.*quantity.*", result)</code>
037: * means that only the words <i>select</i>, <i>isbn,</i>, and <i>quantity</i>
038: * must appear in the specified order, all other characters are irrelevant.
039: * Besides that simple example, you can use any Perl5 compatible expression.
040: */
041: public class BookstoreTest extends BasicJDBCTestCaseAdapter {
042: protected void setUp() throws Exception {
043: super .setUp();
044: setUseRegularExpressions(true);
045: getStatementResultSetHandler().setUseRegularExpressions(true);
046: }
047:
048: public void testSuccessfulOrder() throws Exception {
049: FileResultSetFactory factory = new FileResultSetFactory(
050: "src/com/mockrunner/example/jdbc/bookstore.txt");
051: factory.setFirstLineContainsColumnNames(true);
052: MockResultSet result = getStatementResultSetHandler()
053: .createResultSet("bookresult", factory);
054: //System.out.println(result.toString());
055: getStatementResultSetHandler().prepareResultSet(
056: "select.*isbn,.*quantity.*", result);
057: List resultList = Bookstore.order(getJDBCMockObjectFactory()
058: .getMockConnection(), new ArrayList());
059: assertEquals(4, resultList.size());
060: assertTrue(resultList.contains("1234567890"));
061: assertTrue(resultList.contains("1111111111"));
062: assertTrue(resultList.contains("1212121212"));
063: assertTrue(resultList.contains("3333333333"));
064: verifyResultSetRow("bookresult", 1, new String[] {
065: "1234567890", "0" });
066: verifyResultSetRow("bookresult", 2, new String[] {
067: "1111111111", "4" });
068: verifyResultSetRow("bookresult", 3, new String[] {
069: "0987654321", "0" });
070: verifyResultSetRow("bookresult", 4, new String[] {
071: "1212121212", "2" });
072: verifyResultSetRow("bookresult", 5, new String[] {
073: "3333333333", "0" });
074: verifyCommitted();
075: verifyAllResultSetsClosed();
076: verifyAllStatementsClosed();
077: }
078:
079: public void testCorrectSQL() throws Exception {
080: MockResultSet result = getStatementResultSetHandler()
081: .createResultSet();
082: getStatementResultSetHandler().prepareResultSet(
083: "select.*isbn,.*quantity.*", result);
084: List orderList = new ArrayList();
085: orderList.add("1234567890");
086: orderList.add("1111111111");
087: Bookstore.order(getJDBCMockObjectFactory().getMockConnection(),
088: orderList);
089: verifySQLStatementExecuted("select.*isbn,.*quantity.*\\(isbn='1234567890'.*or.*isbn='1111111111'\\)");
090: }
091:
092: public void testException() throws Exception {
093: getStatementResultSetHandler().prepareThrowsSQLException(
094: "select.*isbn,.*quantity.*");
095: Bookstore.order(getJDBCMockObjectFactory().getMockConnection(),
096: new ArrayList());
097: verifyRolledBack();
098: verifyAllResultSetsClosed();
099: verifyAllStatementsClosed();
100: }
101: }
|