01: package com.mockrunner.example.jdbc;
02:
03: import java.sql.SQLException;
04:
05: import com.mockrunner.jdbc.BasicJDBCTestCaseAdapter;
06: import com.mockrunner.jdbc.StatementResultSetHandler;
07: import com.mockrunner.mock.jdbc.MockResultSet;
08:
09: /**
10: * Example test for {@link Bank}. Demonstrates the usage of
11: * {@link com.mockrunner.jdbc.JDBCTestModule}
12: * and {@link com.mockrunner.jdbc.BasicJDBCTestCaseAdapter}.
13: * Please note that the framework does not execute any SQL statements.
14: * You have to specify the <code>MockResultSet</code> of the <i>select</i>
15: * statement. Since there is only one <i>select</i> in this test, the
16: * <code>MockResultSet</code> is set as the global one.
17: * The Java API is used to add one row to the <code>MockResultSet</code>.
18: * It's also possible to read the test tables from text files.
19: * This test covers a valid transaction test
20: * (there's enough money on the source account) and the failure case.
21: * You do not have to specify the exact SQL statements,
22: * <i>select balance</i> is ok for <i>select balance from account where id=1</i>.
23: * You can specify the search parameters of SQL statements with
24: * {@link com.mockrunner.jdbc.JDBCTestModule#setExactMatch} and
25: * {@link com.mockrunner.jdbc.JDBCTestModule#setCaseSensitive}.
26: * The default is <code>false</code> for both.
27: */
28: public class BankTest extends BasicJDBCTestCaseAdapter {
29: private void prepareEmptyResultSet() {
30: StatementResultSetHandler statementHandler = getJDBCMockObjectFactory()
31: .getMockConnection().getStatementResultSetHandler();
32: MockResultSet result = statementHandler.createResultSet();
33: statementHandler.prepareGlobalResultSet(result);
34: }
35:
36: private void prepareResultSet() {
37: StatementResultSetHandler statementHandler = getJDBCMockObjectFactory()
38: .getMockConnection().getStatementResultSetHandler();
39: MockResultSet result = statementHandler.createResultSet();
40: result.addRow(new Integer[] { new Integer(10000) });
41: statementHandler.prepareGlobalResultSet(result);
42: }
43:
44: public void testWrongId() throws SQLException {
45: prepareEmptyResultSet();
46: Bank bank = new Bank();
47: bank.connect();
48: bank.transfer(1, 2, 5000);
49: bank.disconnect();
50: verifySQLStatementExecuted("select balance");
51: verifySQLStatementNotExecuted("update account");
52: verifyNotCommitted();
53: verifyRolledBack();
54: verifyAllResultSetsClosed();
55: verifyAllStatementsClosed();
56: verifyConnectionClosed();
57: }
58:
59: public void testTransferOk() throws SQLException {
60: prepareResultSet();
61: Bank bank = new Bank();
62: bank.connect();
63: bank.transfer(1, 2, 5000);
64: bank.disconnect();
65: verifySQLStatementExecuted("select balance");
66: verifySQLStatementExecuted("update account");
67: verifySQLStatementParameter("update account", 0, 1,
68: new Integer(-5000));
69: verifySQLStatementParameter("update account", 0, 2,
70: new Integer(1));
71: verifySQLStatementParameter("update account", 1, 1,
72: new Integer(5000));
73: verifySQLStatementParameter("update account", 1, 2,
74: new Integer(2));
75: verifyCommitted();
76: verifyNotRolledBack();
77: verifyAllResultSetsClosed();
78: verifyAllStatementsClosed();
79: verifyConnectionClosed();
80: }
81:
82: public void testTransferFailure() throws SQLException {
83: prepareResultSet();
84: Bank bank = new Bank();
85: bank.connect();
86: bank.transfer(1, 2, 20000);
87: bank.disconnect();
88: verifySQLStatementExecuted("select balance");
89: verifySQLStatementNotExecuted("update account");
90: verifyNotCommitted();
91: verifyRolledBack();
92: verifyAllResultSetsClosed();
93: verifyAllStatementsClosed();
94: verifyConnectionClosed();
95: }
96: }
|