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