001: package com.mockrunner.example.ejb;
002:
003: import org.mockejb.TransactionPolicy;
004:
005: import com.mockrunner.ejb.EJBTestCaseAdapter;
006: import com.mockrunner.example.ejb.interfaces.PaySession;
007: import com.mockrunner.jdbc.JDBCTestModule;
008: import com.mockrunner.jdbc.StatementResultSetHandler;
009: import com.mockrunner.mock.jdbc.MockResultSet;
010:
011: /**
012: * Example test for {@link PaySessionBean}. This example demonstrates
013: * how to use {@link com.mockrunner.jdbc.JDBCTestModule} and
014: * {@link com.mockrunner.ejb.EJBTestModule} in conjunction.
015: * The tests are similar to {@link com.mockrunner.example.jdbc.PayActionTest}
016: * but instead of an action we test an EJB. This example works with the simulated
017: * JDBC environment of Mockrunner.
018: */
019: public class PaySessionTest extends EJBTestCaseAdapter {
020: private JDBCTestModule jdbcModule;
021: private PaySession bean;
022: private StatementResultSetHandler statementHandler;
023:
024: protected void setUp() throws Exception {
025: super .setUp();
026: jdbcModule = createJDBCTestModule();
027: setInterfacePackage("com.mockrunner.example.ejb.interfaces");
028: deploySessionBean("com/mockrunner/example/PaySession",
029: PaySessionBean.class, TransactionPolicy.REQUIRED);
030: bindToContext("java:comp/env/jdbc/MySQLDB",
031: getJDBCMockObjectFactory().getMockDataSource());
032: bean = (PaySession) createBean("com/mockrunner/example/PaySession");
033: statementHandler = getJDBCMockObjectFactory()
034: .getMockConnection().getStatementResultSetHandler();
035: }
036:
037: private void createValidCustomerResult() {
038: MockResultSet result = statementHandler.createResultSet();
039: result.addColumn("name", new String[] { "MyName" });
040: statementHandler.prepareResultSet("select name", result);
041: }
042:
043: private void createValidBillResult() {
044: MockResultSet result = statementHandler.createResultSet();
045: result.addColumn("id", new String[] { "1" });
046: result.addColumn("customerid", new String[] { "1" });
047: result.addColumn("amount", new Double[] { new Double(100) });
048: statementHandler.prepareResultSet("select * from openbills",
049: result);
050: }
051:
052: public void testUnknownCustomer() throws Exception {
053: MockResultSet result = statementHandler.createResultSet();
054: result.addColumn("name");
055: statementHandler.prepareResultSet("select name", result);
056: try {
057: bean.payBill("1", "1", 100);
058: fail();
059: } catch (PaySessionException exc) {
060: assertEquals(PaySessionException.UNKNOWN_CUSTOMER, exc
061: .getCode());
062: }
063: verifyMarkedForRollback();
064: verifyRolledBack();
065: jdbcModule.verifySQLStatementExecuted("select name");
066: jdbcModule
067: .verifySQLStatementNotExecuted("delete from openbills");
068: jdbcModule
069: .verifySQLStatementNotExecuted("insert into paidbills");
070: jdbcModule.verifyAllResultSetsClosed();
071: jdbcModule.verifyAllStatementsClosed();
072: jdbcModule.verifyConnectionClosed();
073: }
074:
075: public void testUnknownBill() throws Exception {
076: createValidCustomerResult();
077: MockResultSet result = statementHandler.createResultSet();
078: result.addColumn("id");
079: result.addColumn("customerid");
080: result.addColumn("amount");
081: statementHandler.prepareResultSet("select * from openbills",
082: result);
083: try {
084: bean.payBill("1", "1", 100);
085: fail();
086: } catch (PaySessionException exc) {
087: assertEquals(PaySessionException.UNKNOWN_BILL, exc
088: .getCode());
089: }
090: verifyMarkedForRollback();
091: verifyRolledBack();
092: jdbcModule
093: .verifySQLStatementExecuted("select * from openbills");
094: jdbcModule
095: .verifySQLStatementNotExecuted("delete from openbills");
096: jdbcModule
097: .verifySQLStatementNotExecuted("insert into paidbills");
098: jdbcModule.verifyAllResultSetsClosed();
099: jdbcModule.verifyAllStatementsClosed();
100: jdbcModule.verifyConnectionClosed();
101: }
102:
103: public void testCustomerIdMismatch() throws Exception {
104: createValidCustomerResult();
105: createValidBillResult();
106: try {
107: bean.payBill("2", "1", 100);
108: fail();
109: } catch (PaySessionException exc) {
110: assertEquals(PaySessionException.WRONG_BILL_FOR_CUSTOMER,
111: exc.getCode());
112: }
113: verifyMarkedForRollback();
114: verifyRolledBack();
115: jdbcModule
116: .verifySQLStatementExecuted("select * from openbills");
117: jdbcModule
118: .verifySQLStatementNotExecuted("delete from openbills");
119: jdbcModule
120: .verifySQLStatementNotExecuted("insert into paidbills");
121: jdbcModule.verifyAllResultSetsClosed();
122: jdbcModule.verifyAllStatementsClosed();
123: jdbcModule.verifyConnectionClosed();
124: }
125:
126: public void testAmountMismatch() throws Exception {
127: createValidCustomerResult();
128: createValidBillResult();
129: try {
130: bean.payBill("1", "1", 200);
131: fail();
132: } catch (PaySessionException exc) {
133: assertEquals(PaySessionException.WRONG_AMOUNT_FOR_BILL, exc
134: .getCode());
135: }
136: verifyMarkedForRollback();
137: verifyRolledBack();
138: jdbcModule
139: .verifySQLStatementExecuted("select * from openbills");
140: jdbcModule
141: .verifySQLStatementNotExecuted("delete from openbills");
142: jdbcModule
143: .verifySQLStatementNotExecuted("insert into paidbills");
144: jdbcModule.verifyAllResultSetsClosed();
145: jdbcModule.verifyAllStatementsClosed();
146: jdbcModule.verifyConnectionClosed();
147: }
148:
149: public void testValidTransaction() throws Exception {
150: createValidCustomerResult();
151: createValidBillResult();
152: bean.payBill("1", "1", 100);
153: verifyNotMarkedForRollback();
154: verifyCommitted();
155: jdbcModule
156: .verifySQLStatementExecuted("delete from openbills where id='1'");
157: jdbcModule
158: .verifySQLStatementExecuted("insert into paidbills values('1','1',100.0)");
159: jdbcModule.verifyAllResultSetsClosed();
160: jdbcModule.verifyAllStatementsClosed();
161: jdbcModule.verifyConnectionClosed();
162: }
163: }
|