01: package com.mockrunner.example.connector;
02:
03: import java.io.FileInputStream;
04:
05: import com.mockrunner.connector.ConnectorTestCaseAdapter;
06: import com.mockrunner.connector.StreamableRecordByteArrayInteraction;
07: import com.mockrunner.ejb.EJBTestModule;
08:
09: /**
10: * Example test for {@link PersonSearchDAO}. The two files
11: * <i>personin.bin</i> and <i>personout.bin</i> are snapshots
12: * from a real mainframe communication. Once created, the snapshot
13: * files can be used to simulate mainframe access in tests.
14: * The <i>personin.bin</i> file represents an empty person with an id of
15: * <i>1</i>, which is the request. The <i>personout.bin</i> contains the
16: * user data for the person with id <i>1</i>. If we search for a user with id
17: * <i>1</i>, the framework recognizes that the actual request matches the expected
18: * request and returns the actual response (the <i>personout.bin</i> data).
19: * If we pass <i>2</i> as the id, no response is found and <code>findPersonById</code>
20: * returns an empty person. This example uses the
21: * {@link com.mockrunner.connector.StreamableRecordByteArrayInteraction} which
22: * works with byte data. {@link com.mockrunner.connector.StreamableRecordByteArrayInteraction}
23: * can always be used when the involved <code>Record</code> classes implement
24: * <code>Streamable</code>.
25: */
26: public class PersonSearchDAOTest extends ConnectorTestCaseAdapter {
27: private EJBTestModule ejbModule;
28: private PersonSearchDAO dao;
29:
30: protected void setUp() throws Exception {
31: super .setUp();
32: ejbModule = createEJBTestModule();
33: ejbModule.bindToContext("java:ra/cics/ConnectionFactory",
34: getConnectorMockObjectFactory()
35: .getMockConnectionFactory());
36: dao = new PersonSearchDAO();
37: }
38:
39: private void prepareInteraction() throws Exception {
40: StreamableRecordByteArrayInteraction interaction = new StreamableRecordByteArrayInteraction();
41: FileInputStream request = new FileInputStream(
42: "src/com/mockrunner/example/connector/personin.bin");
43: FileInputStream response = new FileInputStream(
44: "src/com/mockrunner/example/connector/personout.bin");
45: interaction.setExpectedRequest(request);
46: interaction.setResponse(response);
47: getInteractionHandler().addImplementor(interaction);
48: request.close();
49: response.close();
50: }
51:
52: public void testFindPersonByIdFound() throws Exception {
53: prepareInteraction();
54: Person response = dao.findPersonById("1");
55: assertEquals("1", response.getId());
56: assertEquals("Jane", response.getFirstName());
57: assertEquals("Doe", response.getLastName());
58: assertEquals(30, response.getAge());
59: verifyConnectionClosed();
60: verifyAllInteractionsClosed();
61: }
62:
63: public void testFindPersonByIdNotFound() throws Exception {
64: prepareInteraction();
65: Person response = dao.findPersonById("2");
66: assertNull(response.getId());
67: verifyConnectionClosed();
68: verifyAllInteractionsClosed();
69: }
70: }
|