01: package com.mockrunner.example.connector;
02:
03: import java.util.ArrayList;
04: import java.util.List;
05:
06: import com.mockrunner.connector.ConnectorTestCaseAdapter;
07: import com.mockrunner.connector.GenericFailureInteraction;
08: import com.mockrunner.connector.IndexedRecordInteraction;
09: import com.mockrunner.ejb.EJBTestModule;
10:
11: /**
12: * Example test for {@link AccountDAO}. The involved resource adaper
13: * works with indexed records, so we can use the
14: * {@link com.mockrunner.connector.IndexedRecordInteraction} for
15: * the test. We simply prepare some names and the corresponding
16: * ids, the simulated resource adapter should return. The
17: * {@link com.mockrunner.connector.IndexedRecordInteraction} creates
18: * suitable <code>IndexedRecord</code> implementations for the provided
19: * lists. If the actual request matches the prepared list data, the
20: * corresponding response <code>IndexedRecord</code> is returned.
21: * This example test also demonstrates how to test the failure case.
22: * The {@link com.mockrunner.connector.GenericFailureInteraction}
23: * throws an exception which causes the {@link AccountDAO} to roll back the
24: * transaction and to return -1.
25: */
26: public class AccountDAOTest extends ConnectorTestCaseAdapter {
27: private EJBTestModule ejbModule;
28: private AccountDAO dao;
29:
30: protected void setUp() throws Exception {
31: super .setUp();
32: ejbModule = createEJBTestModule();
33: ejbModule.bindToContext("java:ra/db/ConnectionFactory",
34: getConnectorMockObjectFactory()
35: .getMockConnectionFactory());
36: dao = new AccountDAO();
37: }
38:
39: private void prepareValidInteraction(String firstName,
40: String lastName, int id) {
41: List request = new ArrayList();
42: request.add(firstName);
43: request.add(lastName);
44: List response = new ArrayList();
45: response.add(new Integer(id));
46: IndexedRecordInteraction indexedInteraction = new IndexedRecordInteraction(
47: request, response);
48: getInteractionHandler().addImplementor(indexedInteraction);
49: }
50:
51: public void testCreateAccountVariousUsers() {
52: prepareValidInteraction("John", "Doe", 500);
53: prepareValidInteraction("Jane", "Doe", 1000);
54: prepareValidInteraction("Brian", "Doe", 1500);
55: assertEquals(500, dao.createAccount("John", "Doe"));
56: assertEquals(1000, dao.createAccount("Jane", "Doe"));
57: assertEquals(1500, dao.createAccount("Brian", "Doe"));
58: assertEquals(-1, dao.createAccount("Judith", "Doe"));
59: verifyAllInteractionsClosed();
60: verifyConnectionClosed();
61: }
62:
63: public void testCreateAccountSuccessful() {
64: prepareValidInteraction("John", "Doe", 500);
65: assertEquals(500, dao.createAccount("John", "Doe"));
66: verifyLocalTransactionCommitted();
67: verifyNumberCreatedIndexedRecords(1);
68: verifyAllInteractionsClosed();
69: verifyConnectionClosed();
70: }
71:
72: public void testCreateAccountFailure() {
73: getInteractionHandler().addImplementor(
74: new GenericFailureInteraction());
75: assertEquals(-1, dao.createAccount("John", "Doe"));
76: verifyLocalTransactionRolledBack();
77: verifyNumberCreatedIndexedRecords(1);
78: verifyAllInteractionsClosed();
79: verifyConnectionClosed();
80: }
81: }
|