001: package com.mockrunner.test.connector;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.IOException;
005: import java.io.InputStream;
006: import java.io.OutputStream;
007: import java.util.Arrays;
008:
009: import javax.resource.cci.InteractionSpec;
010: import javax.resource.cci.Record;
011: import javax.resource.cci.Streamable;
012:
013: import junit.framework.TestCase;
014:
015: import com.mockrunner.connector.StreamableRecordByteArrayInteraction;
016: import com.mockrunner.mock.connector.cci.MockIndexedRecord;
017: import com.mockrunner.mock.connector.cci.MockMappedRecord;
018: import com.mockrunner.mock.connector.cci.MockStreamableByteArrayRecord;
019:
020: public class StreamableRecordByteArrayInteractionTest extends TestCase {
021: public void testSetResponseArguments() {
022: StreamableRecordByteArrayInteraction interaction = new StreamableRecordByteArrayInteraction();
023: try {
024: interaction.setResponse(new byte[0], String.class);
025: fail();
026: } catch (IllegalArgumentException exc) {
027: //should throw exception
028: }
029: try {
030: interaction.setResponse(new byte[0], Streamable.class);
031: fail();
032: } catch (IllegalArgumentException exc) {
033: //should throw exception
034: }
035: try {
036: interaction.setResponse(new ByteArrayInputStream(
037: new byte[0]), Record.class);
038: fail();
039: } catch (IllegalArgumentException exc) {
040: //should throw exception
041: }
042: interaction.setResponse(new ByteArrayInputStream(new byte[0]),
043: null);
044: interaction.setResponse(new ByteArrayInputStream(new byte[0]),
045: TestRecord.class);
046: interaction.setResponse(new ByteArrayInputStream(new byte[0]),
047: MockStreamableByteArrayRecord.class);
048: try {
049: interaction = new StreamableRecordByteArrayInteraction(
050: new byte[0], Integer.class);
051: fail();
052: } catch (IllegalArgumentException exc) {
053: //should throw exception
054: }
055: try {
056: interaction = new StreamableRecordByteArrayInteraction(
057: new byte[0], Record.class);
058: fail();
059: } catch (IllegalArgumentException exc) {
060: //should throw exception
061: }
062: interaction = new StreamableRecordByteArrayInteraction(
063: new byte[0], (Class) null);
064: }
065:
066: public void testCanHandle() {
067: StreamableRecordByteArrayInteraction interaction = new StreamableRecordByteArrayInteraction();
068: InteractionSpec spec = new InteractionSpec() {
069: };
070: assertTrue(interaction.canHandle(spec, null, null));
071: assertTrue(interaction.canHandle(spec, null,
072: new MockStreamableByteArrayRecord()));
073: assertFalse(interaction.canHandle(spec, null,
074: new MockIndexedRecord()));
075: assertTrue(interaction.canHandle(spec,
076: new MockStreamableByteArrayRecord(),
077: new MockStreamableByteArrayRecord()));
078: interaction.setExpectedRequest(new byte[] { 1, 2, 3 });
079: assertFalse(interaction.canHandle(spec, null,
080: new MockStreamableByteArrayRecord()));
081: assertFalse(interaction.canHandle(spec,
082: new MockStreamableByteArrayRecord(),
083: new MockStreamableByteArrayRecord()));
084: MockStreamableByteArrayRecord request = new MockStreamableByteArrayRecord();
085: request.setContent(new byte[] { 1, 2, 3 });
086: assertTrue(interaction.canHandle(spec, request,
087: new MockStreamableByteArrayRecord()));
088: request.setContent(new byte[] { 1, 2, 3, 4 });
089: assertFalse(interaction.canHandle(spec, request,
090: new MockStreamableByteArrayRecord()));
091: interaction.setExpectedRequest(new ByteArrayInputStream(
092: new byte[] { 1, 2, 3, 4 }));
093: assertTrue(interaction.canHandle(spec, request,
094: new MockStreamableByteArrayRecord()));
095: interaction = new StreamableRecordByteArrayInteraction(
096: new byte[] { 5, 6, 7 },
097: new MockStreamableByteArrayRecord());
098: assertFalse(interaction.canHandle(spec, request,
099: new MockStreamableByteArrayRecord()));
100: request.setContent(new byte[] { 5, 6, 7 });
101: assertTrue(interaction.canHandle(spec, request,
102: new MockStreamableByteArrayRecord()));
103: interaction = new StreamableRecordByteArrayInteraction(
104: new byte[] { 1 }, MockStreamableByteArrayRecord.class);
105: assertTrue(interaction.canHandle(spec, request,
106: new MockStreamableByteArrayRecord()));
107: }
108:
109: public void testEnableAndDisable() {
110: StreamableRecordByteArrayInteraction interaction = new StreamableRecordByteArrayInteraction();
111: InteractionSpec spec = new InteractionSpec() {
112: };
113: assertTrue(interaction.canHandle(spec, null, null));
114: interaction.disable();
115: assertFalse(interaction.canHandle(spec, null, null));
116: interaction.enable();
117: assertTrue(interaction.canHandle(spec, null, null));
118: }
119:
120: public void testExecuteReturnsRecord() throws Exception {
121: StreamableRecordByteArrayInteraction interaction = new StreamableRecordByteArrayInteraction();
122: InteractionSpec spec = new InteractionSpec() {
123: };
124: interaction.setExpectedRequest(new byte[] { 1 });
125: assertNull(interaction.execute(spec, new MockIndexedRecord()));
126: MockStreamableByteArrayRecord request = new MockStreamableByteArrayRecord();
127: request.setContent(new byte[] { 1 });
128: MockStreamableByteArrayRecord response = (MockStreamableByteArrayRecord) interaction
129: .execute(spec, request);
130: assertNull(response.getContent());
131: MockMappedRecord mappedResponse = new MockMappedRecord();
132: interaction.setResponse(mappedResponse);
133: assertSame(mappedResponse, interaction.execute(spec, request));
134: interaction.setResponse(new byte[] { 1, 2, 3 },
135: TestRecord.class);
136: interaction.setResponse((Record) null);
137: assertTrue(interaction.execute(spec, request) instanceof TestRecord);
138: interaction.setResponse(new byte[] { 1, 2, 3, 4, 5 });
139: response = (MockStreamableByteArrayRecord) interaction.execute(
140: spec, request);
141: assertTrue(Arrays.equals(new byte[] { 1, 2, 3, 4, 5 }, response
142: .getContent()));
143: interaction.setResponse(new TestRecord());
144: assertTrue(interaction.execute(spec, request) instanceof TestRecord);
145: assertNull(interaction.execute(spec,
146: new MockStreamableByteArrayRecord()));
147: interaction = new StreamableRecordByteArrayInteraction(
148: new byte[] { 1 }, MockStreamableByteArrayRecord.class);
149: response = (MockStreamableByteArrayRecord) interaction.execute(
150: spec, request);
151: assertTrue(Arrays.equals(new byte[] { 1 }, response
152: .getContent()));
153: interaction = new StreamableRecordByteArrayInteraction(
154: new byte[] { 1, 2 }, (Class) null);
155: response = (MockStreamableByteArrayRecord) interaction.execute(
156: spec, request);
157: assertTrue(Arrays.equals(new byte[] { 1, 2 }, response
158: .getContent()));
159: interaction.setResponse(mappedResponse);
160: assertSame(mappedResponse, interaction.execute(spec, request));
161: interaction.setResponse((Record) null);
162: response = (MockStreamableByteArrayRecord) interaction.execute(
163: spec, request);
164: assertTrue(Arrays.equals(new byte[] { 1, 2 }, response
165: .getContent()));
166: }
167:
168: public void testExecuteReturnsBoolean() throws Exception {
169: StreamableRecordByteArrayInteraction interaction = new StreamableRecordByteArrayInteraction();
170: InteractionSpec spec = new InteractionSpec() {
171: };
172: interaction.setExpectedRequest(new byte[] { 1 });
173: assertFalse(interaction.execute(spec, new MockIndexedRecord(),
174: null));
175: MockStreamableByteArrayRecord request = new MockStreamableByteArrayRecord();
176: request.setContent(new byte[] { 1 });
177: assertTrue(interaction.execute(spec, request, null));
178: MockStreamableByteArrayRecord response = new MockStreamableByteArrayRecord();
179: assertTrue(interaction.execute(spec, request, response));
180: assertNull(response.getContent());
181: interaction.setResponse(new byte[] { 1, 2, 3 });
182: assertTrue(interaction.execute(spec, request, response));
183: assertTrue(Arrays.equals(new byte[] { 1, 2, 3 }, response
184: .getContent()));
185: assertFalse(interaction.execute(spec, request,
186: new MockMappedRecord()));
187: assertTrue(interaction.execute(spec, request, new TestRecord()));
188: interaction.setResponse((byte[]) null);
189: interaction.setResponse(new TestRecord());
190: response = new MockStreamableByteArrayRecord();
191: assertTrue(interaction.execute(spec, request, response));
192: assertNull(response.getContent());
193: interaction = new StreamableRecordByteArrayInteraction(null,
194: new byte[] { 1 });
195: assertTrue(interaction.execute(spec, request, response));
196: assertTrue(Arrays.equals(new byte[] { 1 }, response
197: .getContent()));
198: interaction.setResponse(new TestRecord());
199: assertTrue(interaction.execute(spec, request, response));
200: assertTrue(Arrays.equals(new byte[] { 1 }, response
201: .getContent()));
202: }
203:
204: public static class TestRecord implements Record, Streamable {
205: public String getRecordName() {
206: return "";
207: }
208:
209: public String getRecordShortDescription() {
210: return "";
211: }
212:
213: public void setRecordName(String name) {
214:
215: }
216:
217: public void setRecordShortDescription(String description) {
218:
219: }
220:
221: public void read(InputStream stream) throws IOException {
222:
223: }
224:
225: public void write(OutputStream stream) throws IOException {
226:
227: }
228:
229: public Object clone() throws CloneNotSupportedException {
230: return super.clone();
231: }
232: }
233: }
|