001: package com.mockrunner.test.connector;
002:
003: import java.util.ArrayList;
004: import java.util.Arrays;
005: import java.util.HashMap;
006: import java.util.List;
007: import java.util.Map;
008:
009: import javax.resource.cci.Interaction;
010: import javax.resource.cci.Record;
011:
012: import junit.framework.TestCase;
013:
014: import com.mockrunner.connector.IndexedRecordInteraction;
015: import com.mockrunner.connector.MappedRecordInteraction;
016: import com.mockrunner.connector.StreamableRecordByteArrayInteraction;
017: import com.mockrunner.mock.connector.cci.ConnectorMockObjectFactory;
018: import com.mockrunner.mock.connector.cci.MockMappedRecord;
019: import com.mockrunner.mock.connector.cci.MockStreamableByteArrayRecord;
020:
021: public class MockInteractionTest extends TestCase {
022: private ConnectorMockObjectFactory mockFactory;
023:
024: protected void setUp() throws Exception {
025: mockFactory = new ConnectorMockObjectFactory();
026: }
027:
028: protected void tearDown() throws Exception {
029: mockFactory = null;
030: }
031:
032: public void testExecuteReturnsBoolean() throws Exception {
033: StreamableRecordByteArrayInteraction byteArrayInteraction1 = new StreamableRecordByteArrayInteraction();
034: byteArrayInteraction1
035: .setExpectedRequest(new byte[] { 1, 2, 3 });
036: byteArrayInteraction1.setResponse(new byte[] { 1 });
037: StreamableRecordByteArrayInteraction byteArrayInteraction2 = new StreamableRecordByteArrayInteraction();
038: byteArrayInteraction2.setResponse(new byte[] { 2 });
039: mockFactory.getInteractionHandler().addImplementor(
040: byteArrayInteraction1);
041: mockFactory.getInteractionHandler().addImplementor(
042: byteArrayInteraction2);
043: Interaction interaction = mockFactory
044: .getMockConnectionFactory().getConnection()
045: .createInteraction();
046: assertTrue(interaction.execute(null, null, null));
047: MockStreamableByteArrayRecord actualRequest = new MockStreamableByteArrayRecord();
048: MockStreamableByteArrayRecord actualResponse = new MockStreamableByteArrayRecord();
049: actualRequest.setContent(new byte[] { 1, 2, 3 });
050: assertTrue(interaction.execute(null, actualRequest,
051: actualResponse));
052: assertTrue(Arrays.equals(new byte[] { 1 }, actualResponse
053: .getContent()));
054: actualRequest.setContent(new byte[] { 1, 2, 4 });
055: assertTrue(interaction.execute(null, actualRequest,
056: actualResponse));
057: assertTrue(Arrays.equals(new byte[] { 2 }, actualResponse
058: .getContent()));
059: }
060:
061: public void testExecuteReturnsRecord() throws Exception {
062: IndexedRecordInteraction indexedInteraction = new IndexedRecordInteraction();
063: TestRecord testRecord = new TestRecord();
064: indexedInteraction.setResponse(testRecord);
065: mockFactory.getInteractionHandler().addImplementor(
066: indexedInteraction);
067: Interaction interaction = mockFactory
068: .getMockConnectionFactory().getConnection()
069: .createInteraction();
070: assertSame(testRecord, interaction.execute(null, null));
071: List expectedList = new ArrayList();
072: expectedList.add("1");
073: indexedInteraction.setExpectedRequest(expectedList);
074: assertNull(interaction.execute(null, null));
075: MappedRecordInteraction mappedInteraction = new MappedRecordInteraction();
076: TestRecord anotherTestRecord = new TestRecord();
077: mappedInteraction.setResponse(anotherTestRecord);
078: Map expectedMap = new HashMap();
079: expectedMap.put("1", "1");
080: mappedInteraction.setExpectedRequest(expectedMap);
081: MockMappedRecord actualRequest = new MockMappedRecord();
082: actualRequest.put("1", "1");
083: mockFactory.getInteractionHandler().addImplementor(
084: mappedInteraction);
085: assertSame(anotherTestRecord, interaction.execute(null,
086: actualRequest));
087: }
088:
089: private class TestRecord implements Record {
090: public String getRecordName() {
091: return null;
092: }
093:
094: public String getRecordShortDescription() {
095: return null;
096: }
097:
098: public void setRecordName(String name) {
099:
100: }
101:
102: public void setRecordShortDescription(String description) {
103:
104: }
105:
106: public Object clone() throws CloneNotSupportedException {
107: return null;
108: }
109: }
110: }
|