001: package com.mockrunner.test.connector;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.Iterator;
006: import java.util.List;
007: import java.util.ListIterator;
008:
009: import javax.resource.cci.IndexedRecord;
010: import javax.resource.cci.InteractionSpec;
011: import javax.resource.cci.Record;
012: import javax.resource.cci.Streamable;
013:
014: import junit.framework.TestCase;
015:
016: import com.mockrunner.connector.IndexedRecordInteraction;
017: import com.mockrunner.mock.connector.cci.MockIndexedRecord;
018: import com.mockrunner.mock.connector.cci.MockMappedRecord;
019:
020: public class IndexedRecordInteractionTest extends TestCase {
021: public void testSetResponseArguments() {
022: IndexedRecordInteraction interaction = new IndexedRecordInteraction();
023: try {
024: interaction.setResponse(new ArrayList(), String.class);
025: fail();
026: } catch (IllegalArgumentException exc) {
027: //should throw exception
028: }
029: try {
030: interaction.setResponse(new ArrayList(), Streamable.class);
031: fail();
032: } catch (IllegalArgumentException exc) {
033: //should throw exception
034: }
035: interaction.setResponse(new ArrayList(), null);
036: interaction.setResponse(new ArrayList(), TestRecord.class);
037: interaction.setResponse(new ArrayList(),
038: MockIndexedRecord.class);
039: try {
040: interaction = new IndexedRecordInteraction(new ArrayList(),
041: Integer.class);
042: fail();
043: } catch (IllegalArgumentException exc) {
044: //should throw exception
045: }
046: try {
047: interaction = new IndexedRecordInteraction(new ArrayList(),
048: Record.class);
049: fail();
050: } catch (IllegalArgumentException exc) {
051: //should throw exception
052: }
053: interaction = new IndexedRecordInteraction(new ArrayList(),
054: (Class) null);
055: }
056:
057: public void testCanHandle() {
058: IndexedRecordInteraction interaction = new IndexedRecordInteraction();
059: InteractionSpec spec = new InteractionSpec() {
060: };
061: assertTrue(interaction.canHandle(spec, null, null));
062: assertTrue(interaction.canHandle(spec, null,
063: new MockIndexedRecord()));
064: assertFalse(interaction.canHandle(spec, null,
065: new MockMappedRecord()));
066: assertTrue(interaction.canHandle(spec, new MockIndexedRecord(),
067: new MockIndexedRecord()));
068: List expectedRequestList = new ArrayList();
069: expectedRequestList.add(null);
070: expectedRequestList.add("2");
071: expectedRequestList.add("3");
072: interaction.setExpectedRequest(expectedRequestList);
073: expectedRequestList.add("4");
074: assertFalse(interaction.canHandle(spec, null,
075: new MockIndexedRecord()));
076: assertFalse(interaction.canHandle(spec,
077: new MockIndexedRecord(), new MockIndexedRecord()));
078: MockIndexedRecord request = new MockIndexedRecord();
079: request.add(null);
080: request.add("2");
081: request.add("3");
082: assertTrue(interaction.canHandle(spec, request,
083: new MockIndexedRecord()));
084: request.add("4");
085: assertFalse(interaction.canHandle(spec, request,
086: new MockIndexedRecord()));
087: interaction.setExpectedRequest(request);
088: assertTrue(interaction.canHandle(spec, request,
089: new MockIndexedRecord()));
090: expectedRequestList = new ArrayList();
091: expectedRequestList.add(new Integer(5));
092: expectedRequestList.add(new Integer(6));
093: expectedRequestList.add(new Integer(7));
094: interaction = new IndexedRecordInteraction(expectedRequestList,
095: (Record) new MockIndexedRecord());
096: assertFalse(interaction.canHandle(spec, request,
097: new MockIndexedRecord()));
098: request = new MockIndexedRecord();
099: request.add(new Integer(5));
100: request.add(new Integer(1));
101: request.add(new Integer(7));
102: request.add(new Integer(8));
103: assertFalse(interaction.canHandle(spec, request,
104: new MockIndexedRecord()));
105: request.remove(new Integer(8));
106: assertFalse(interaction.canHandle(spec, request,
107: new MockIndexedRecord()));
108: request.set(1, new Integer(6));
109: assertTrue(interaction.canHandle(spec, request,
110: new MockIndexedRecord()));
111: interaction = new IndexedRecordInteraction(new ArrayList(),
112: MockIndexedRecord.class);
113: assertTrue(interaction.canHandle(spec, request,
114: new MockIndexedRecord()));
115: }
116:
117: public void testEnableAndDisable() {
118: IndexedRecordInteraction interaction = new IndexedRecordInteraction();
119: InteractionSpec spec = new InteractionSpec() {
120: };
121: assertTrue(interaction.canHandle(spec, null, null));
122: interaction.disable();
123: assertFalse(interaction.canHandle(spec, null, null));
124: interaction.enable();
125: assertTrue(interaction.canHandle(spec, null, null));
126: }
127:
128: public void testExecuteReturnsRecord() throws Exception {
129: IndexedRecordInteraction interaction = new IndexedRecordInteraction();
130: InteractionSpec spec = new InteractionSpec() {
131: };
132: List expectedRequestList = new ArrayList();
133: expectedRequestList.add(new Long(1));
134: interaction.setExpectedRequest(expectedRequestList);
135: assertNull(interaction.execute(spec, new MockMappedRecord()));
136: MockIndexedRecord request = new MockIndexedRecord();
137: request.add(new Long(1));
138: MockIndexedRecord response = (MockIndexedRecord) interaction
139: .execute(spec, request);
140: assertEquals(0, response.size());
141: MockMappedRecord mappedResponse = new MockMappedRecord();
142: interaction.setResponse(mappedResponse);
143: assertSame(mappedResponse, interaction.execute(spec, request));
144: List responseList = new ArrayList();
145: responseList.add(new Integer(1));
146: responseList.add(new Integer(2));
147: responseList.add(new Integer(3));
148: interaction.setResponse(responseList, TestRecord.class);
149: interaction.setResponse((Record) null);
150: assertTrue(interaction.execute(spec, request) instanceof TestRecord);
151: responseList = new ArrayList();
152: responseList.add(new Integer(1));
153: responseList.add(new Integer(2));
154: responseList.add(new Integer(3));
155: responseList.add(new Integer(4));
156: responseList.add(new Integer(5));
157: interaction.setResponse(responseList);
158: response = (MockIndexedRecord) interaction.execute(spec,
159: request);
160: assertTrue(new ArrayList(response).equals(responseList));
161: interaction.setResponse((Record) new TestRecord());
162: assertTrue(interaction.execute(spec, request) instanceof TestRecord);
163: assertNull(interaction.execute(spec, new MockIndexedRecord()));
164: responseList = new ArrayList();
165: responseList.add(new Integer(1));
166: interaction = new IndexedRecordInteraction(responseList,
167: MockIndexedRecord.class);
168: response = (MockIndexedRecord) interaction.execute(spec,
169: request);
170: assertTrue(new ArrayList(response).equals(responseList));
171: responseList = new ArrayList();
172: responseList.add(new Integer(1));
173: responseList.add(new Integer(2));
174: interaction = new IndexedRecordInteraction(responseList,
175: (Class) null);
176: responseList.add(new Integer(3));
177: response = (MockIndexedRecord) interaction.execute(spec,
178: request);
179: responseList.remove(new Integer(3));
180: assertTrue(new ArrayList(response).equals(responseList));
181: interaction.setResponse(mappedResponse);
182: assertSame(mappedResponse, interaction.execute(spec, request));
183: interaction.setResponse((Record) null);
184: response = (MockIndexedRecord) interaction.execute(spec,
185: request);
186: assertTrue(new ArrayList(response).equals(responseList));
187: }
188:
189: public void testExecuteReturnsBoolean() throws Exception {
190: IndexedRecordInteraction interaction = new IndexedRecordInteraction();
191: InteractionSpec spec = new InteractionSpec() {
192: };
193: List expectedRequestList = new ArrayList();
194: expectedRequestList.add(new Long(1));
195: interaction.setExpectedRequest(expectedRequestList);
196: assertFalse(interaction.execute(spec, new MockMappedRecord(),
197: null));
198: MockIndexedRecord request = new MockIndexedRecord();
199: request.add(new Long(1));
200: assertTrue(interaction.execute(spec, request, null));
201: MockIndexedRecord response = new MockIndexedRecord();
202: assertTrue(interaction.execute(spec, request, response));
203: assertEquals(0, response.size());
204: List responseList = new ArrayList();
205: responseList.add(new Integer(1));
206: responseList.add(new Integer(2));
207: responseList.add(null);
208: interaction.setResponse(responseList);
209: assertTrue(interaction.execute(spec, request, response));
210: assertTrue(new ArrayList(response).equals(responseList));
211: assertFalse(interaction.execute(spec, request,
212: new MockMappedRecord()));
213: assertTrue(interaction.execute(spec, request, new TestRecord()));
214: interaction.setResponse((List) null);
215: interaction.setResponse((Record) new TestRecord());
216: response = new MockIndexedRecord();
217: assertTrue(interaction.execute(spec, request, response));
218: assertEquals(0, response.size());
219: responseList = new ArrayList();
220: responseList.add(new Integer(1));
221: interaction = new IndexedRecordInteraction(null, responseList);
222: assertTrue(interaction.execute(spec, request, response));
223: assertTrue(new ArrayList(response).equals(responseList));
224: interaction.setResponse((Record) new TestRecord());
225: assertTrue(interaction.execute(spec, request, response));
226: assertTrue(new ArrayList(response).equals(responseList));
227: }
228:
229: public static class TestRecord implements IndexedRecord {
230: public String getRecordName() {
231: return "";
232: }
233:
234: public String getRecordShortDescription() {
235: return "";
236: }
237:
238: public void setRecordName(String name) {
239:
240: }
241:
242: public void setRecordShortDescription(String description) {
243:
244: }
245:
246: public void add(int index, Object value) {
247:
248: }
249:
250: public boolean add(Object value) {
251: return false;
252: }
253:
254: public boolean addAll(Collection values) {
255: return false;
256: }
257:
258: public boolean addAll(int index, Collection values) {
259: return false;
260: }
261:
262: public void clear() {
263:
264: }
265:
266: public boolean contains(Object object) {
267: return false;
268: }
269:
270: public boolean containsAll(Collection collection) {
271: return false;
272: }
273:
274: public Object get(int index) {
275: return null;
276: }
277:
278: public int indexOf(Object object) {
279: return 0;
280: }
281:
282: public boolean isEmpty() {
283: return false;
284: }
285:
286: public Iterator iterator() {
287: return null;
288: }
289:
290: public int lastIndexOf(Object object) {
291: return 0;
292: }
293:
294: public ListIterator listIterator() {
295: return null;
296: }
297:
298: public ListIterator listIterator(int index) {
299: return null;
300: }
301:
302: public Object remove(int index) {
303: return null;
304: }
305:
306: public boolean remove(Object object) {
307: return false;
308: }
309:
310: public boolean removeAll(Collection collection) {
311: return false;
312: }
313:
314: public boolean retainAll(Collection collection) {
315: return false;
316: }
317:
318: public Object set(int index, Object value) {
319: return null;
320: }
321:
322: public int size() {
323: return 0;
324: }
325:
326: public List subList(int fromIndex, int toIndex) {
327: return null;
328: }
329:
330: public Object[] toArray() {
331: return null;
332: }
333:
334: public Object[] toArray(Object[] array) {
335: return null;
336: }
337:
338: public Object clone() throws CloneNotSupportedException {
339: return super.clone();
340: }
341: }
342: }
|