001: package com.mockrunner.example.jdbc;
002:
003: import com.mockrunner.jdbc.JDBCTestCaseAdapter;
004: import com.mockrunner.jdbc.StatementResultSetHandler;
005: import com.mockrunner.mock.jdbc.MockResultSet;
006: import com.mockrunner.struts.ActionTestModule;
007:
008: /**
009: * Example test for {@link PayAction}. Demonstrates the usage of
010: * {@link com.mockrunner.jdbc.JDBCTestModule}
011: * and {@link com.mockrunner.jdbc.JDBCTestCaseAdapter}.
012: * This is also a good example how to combine the Struts and
013: * the JDBC testframework.
014: */
015: public class PayActionTest extends JDBCTestCaseAdapter {
016: private ActionTestModule actionModule;
017: private StatementResultSetHandler statementHandler;
018:
019: protected void setUp() throws Exception {
020: super .setUp();
021: actionModule = createActionTestModule();
022: statementHandler = getJDBCMockObjectFactory()
023: .getMockConnection().getStatementResultSetHandler();
024: }
025:
026: private void createValidCustomerResult() {
027: MockResultSet result = statementHandler.createResultSet();
028: result.addColumn("name", new String[] { "MyName" });
029: statementHandler.prepareResultSet("select name", result);
030: }
031:
032: private void createValidBillResult() {
033: MockResultSet result = statementHandler.createResultSet();
034: result.addColumn("id", new String[] { "1" });
035: result.addColumn("customerid", new String[] { "1" });
036: result.addColumn("amount", new Double[] { new Double(100) });
037: statementHandler.prepareResultSet("select * from openbills",
038: result);
039: }
040:
041: public void testUnknownCustomer() {
042: MockResultSet result = statementHandler.createResultSet();
043: result.addColumn("name");
044: statementHandler.prepareResultSet("select name", result);
045: actionModule.addRequestParameter("customerId", "1");
046: actionModule.actionPerform(PayAction.class, PayForm.class);
047: actionModule.verifyActionErrorPresent("unknown.customer.error");
048: verifyRolledBack();
049: verifyNotCommitted();
050: verifySQLStatementExecuted("select name");
051: verifySQLStatementNotExecuted("delete from openbills");
052: verifySQLStatementNotExecuted("insert into paidbills");
053: verifyAllResultSetsClosed();
054: verifyAllStatementsClosed();
055: verifyConnectionClosed();
056: }
057:
058: public void testUnknownBill() {
059: createValidCustomerResult();
060: MockResultSet result = statementHandler.createResultSet();
061: result.addColumn("id");
062: result.addColumn("customerid");
063: result.addColumn("amount");
064: statementHandler.prepareResultSet("select * from openbills",
065: result);
066: actionModule.addRequestParameter("customerId", "1");
067: actionModule.addRequestParameter("billId", "1");
068: actionModule.actionPerform(PayAction.class, PayForm.class);
069: actionModule.verifyActionErrorPresent("unknown.bill.error");
070: verifyRolledBack();
071: verifyNotCommitted();
072: verifySQLStatementExecuted("select * from openbills");
073: verifySQLStatementNotExecuted("delete from openbills");
074: verifySQLStatementNotExecuted("insert into paidbills");
075: verifyAllResultSetsClosed();
076: verifyAllStatementsClosed();
077: verifyConnectionClosed();
078: }
079:
080: public void testCustomerIdMismatch() {
081: createValidCustomerResult();
082: createValidBillResult();
083: actionModule.addRequestParameter("customerId", "2");
084: actionModule.addRequestParameter("billId", "1");
085: actionModule.actionPerform(PayAction.class, PayForm.class);
086: actionModule
087: .verifyActionErrorPresent("wrong.bill.for.customer");
088: verifyRolledBack();
089: verifyNotCommitted();
090: verifySQLStatementExecuted("select * from openbills");
091: verifySQLStatementNotExecuted("delete from openbills");
092: verifySQLStatementNotExecuted("insert into paidbills");
093: verifyAllResultSetsClosed();
094: verifyAllStatementsClosed();
095: verifyConnectionClosed();
096: }
097:
098: public void testAmountMismatch() {
099: createValidCustomerResult();
100: createValidBillResult();
101: actionModule.addRequestParameter("customerId", "1");
102: actionModule.addRequestParameter("billId", "1");
103: actionModule.addRequestParameter("amount", "200");
104: actionModule.actionPerform(PayAction.class, PayForm.class);
105: actionModule.verifyActionErrorPresent("wrong.amount.for.bill");
106: verifyRolledBack();
107: verifyNotCommitted();
108: verifySQLStatementExecuted("select * from openbills");
109: verifySQLStatementNotExecuted("delete from openbills");
110: verifySQLStatementNotExecuted("insert into paidbills");
111: verifyAllResultSetsClosed();
112: verifyAllStatementsClosed();
113: verifyConnectionClosed();
114: }
115:
116: public void testValidTransaction() {
117: createValidCustomerResult();
118: createValidBillResult();
119: actionModule.addRequestParameter("customerId", "1");
120: actionModule.addRequestParameter("billId", "1");
121: actionModule.addRequestParameter("amount", "100");
122: actionModule.actionPerform(PayAction.class, PayForm.class);
123: actionModule.verifyNoActionErrors();
124: verifyCommitted();
125: verifyNotRolledBack();
126: verifySQLStatementExecuted("delete from openbills where id='1'");
127: verifySQLStatementExecuted("insert into paidbills values('1','1',100.0)");
128: verifyAllResultSetsClosed();
129: verifyAllStatementsClosed();
130: verifyConnectionClosed();
131: }
132: }
|