KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jca > cci > EisOperationTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jca.cci;
18
19 import javax.resource.ResourceException JavaDoc;
20 import javax.resource.cci.Connection JavaDoc;
21 import javax.resource.cci.ConnectionFactory JavaDoc;
22 import javax.resource.cci.Interaction JavaDoc;
23 import javax.resource.cci.InteractionSpec JavaDoc;
24 import javax.resource.cci.Record JavaDoc;
25 import javax.resource.cci.RecordFactory JavaDoc;
26
27 import junit.framework.TestCase;
28 import org.easymock.MockControl;
29
30 import org.springframework.jca.cci.core.RecordCreator;
31 import org.springframework.jca.cci.object.MappingRecordOperation;
32 import org.springframework.jca.cci.object.SimpleRecordOperation;
33
34 /**
35  * @author Thierry TEMPLIER
36  */

37 public class EisOperationTests extends TestCase {
38
39     public void testSimpleRecordOperation() throws ResourceException JavaDoc {
40         MockControl connectionFactoryControl = MockControl.createControl(ConnectionFactory JavaDoc.class);
41         ConnectionFactory JavaDoc connectionFactory = (ConnectionFactory JavaDoc) connectionFactoryControl.getMock();
42         MockControl connectionControl = MockControl.createControl(Connection JavaDoc.class);
43         Connection JavaDoc connection = (Connection JavaDoc) connectionControl.getMock();
44         MockControl interactionControl = MockControl.createControl(Interaction JavaDoc.class);
45         Interaction JavaDoc interaction = (Interaction JavaDoc) interactionControl.getMock();
46
47         MockControl inputRecordControl = MockControl.createControl(Record JavaDoc.class);
48         Record JavaDoc inputRecord = (Record JavaDoc) inputRecordControl.getMock();
49         MockControl outputRecordControl = MockControl.createControl(Record JavaDoc.class);
50         Record JavaDoc outputRecord = (Record JavaDoc) outputRecordControl.getMock();
51
52         MockControl interactionSpecControl = MockControl.createControl(InteractionSpec JavaDoc.class);
53         InteractionSpec JavaDoc interactionSpec = (InteractionSpec JavaDoc) interactionSpecControl.getMock();
54
55         SimpleRecordOperation query = new SimpleRecordOperation(connectionFactory, interactionSpec);
56
57         connectionFactory.getConnection();
58         connectionFactoryControl.setReturnValue(connection);
59
60         connection.createInteraction();
61         connectionControl.setReturnValue(interaction);
62
63         interaction.execute(interactionSpec, inputRecord);
64         interactionControl.setReturnValue(outputRecord, 1);
65
66         interaction.close();
67         interactionControl.setVoidCallable(1);
68
69         connection.close();
70         connectionControl.setVoidCallable(1);
71
72         connectionFactoryControl.replay();
73         connectionControl.replay();
74         interactionControl.replay();
75
76         query.execute(inputRecord);
77
78         connectionFactoryControl.verify();
79         connectionControl.verify();
80         interactionControl.verify();
81     }
82
83     public void testSimpleRecordOperationWithExplicitOutputRecord() throws ResourceException JavaDoc {
84         MockControl connectionFactoryControl = MockControl.createControl(ConnectionFactory JavaDoc.class);
85         ConnectionFactory JavaDoc connectionFactory = (ConnectionFactory JavaDoc) connectionFactoryControl.getMock();
86         MockControl connectionControl = MockControl.createControl(Connection JavaDoc.class);
87         Connection JavaDoc connection = (Connection JavaDoc) connectionControl.getMock();
88         MockControl interactionControl = MockControl.createControl(Interaction JavaDoc.class);
89         Interaction JavaDoc interaction = (Interaction JavaDoc) interactionControl.getMock();
90
91         MockControl inputRecordControl = MockControl.createControl(Record JavaDoc.class);
92         Record JavaDoc inputRecord = (Record JavaDoc) inputRecordControl.getMock();
93         MockControl outputRecordControl = MockControl.createControl(Record JavaDoc.class);
94         Record JavaDoc outputRecord = (Record JavaDoc) outputRecordControl.getMock();
95
96         MockControl interactionSpecControl = MockControl.createControl(InteractionSpec JavaDoc.class);
97         InteractionSpec JavaDoc interactionSpec = (InteractionSpec JavaDoc) interactionSpecControl.getMock();
98
99         SimpleRecordOperation operation = new SimpleRecordOperation(connectionFactory, interactionSpec);
100
101         connectionFactory.getConnection();
102         connectionFactoryControl.setReturnValue(connection, 1);
103
104         connection.createInteraction();
105         connectionControl.setReturnValue(interaction, 1);
106
107         interaction.execute(interactionSpec, inputRecord, outputRecord);
108         interactionControl.setReturnValue(true, 1);
109
110         interaction.close();
111         interactionControl.setVoidCallable(1);
112
113         connection.close();
114         connectionControl.setVoidCallable(1);
115
116         connectionFactoryControl.replay();
117         connectionControl.replay();
118         interactionControl.replay();
119
120         operation.execute(inputRecord, outputRecord);
121
122         connectionFactoryControl.verify();
123         connectionControl.verify();
124         interactionControl.verify();
125     }
126
127     public void testSimpleRecordOperationWithInputOutputRecord() throws ResourceException JavaDoc {
128         MockControl connectionFactoryControl = MockControl.createControl(ConnectionFactory JavaDoc.class);
129         final ConnectionFactory JavaDoc connectionFactory = (ConnectionFactory JavaDoc) connectionFactoryControl.getMock();
130         MockControl connectionControl = MockControl.createControl(Connection JavaDoc.class);
131         Connection JavaDoc connection = (Connection JavaDoc) connectionControl.getMock();
132         MockControl interactionControl = MockControl.createControl(Interaction JavaDoc.class);
133         Interaction JavaDoc interaction = (Interaction JavaDoc) interactionControl.getMock();
134
135         MockControl inputOutputRecordControl = MockControl.createControl(Record JavaDoc.class);
136         Record JavaDoc inputOutputRecord = (Record JavaDoc) inputOutputRecordControl.getMock();
137
138         MockControl interactionSpecControl = MockControl.createControl(InteractionSpec JavaDoc.class);
139         InteractionSpec JavaDoc interactionSpec = (InteractionSpec JavaDoc) interactionSpecControl.getMock();
140
141         SimpleRecordOperation query = new SimpleRecordOperation(connectionFactory, interactionSpec);
142
143         connectionFactory.getConnection();
144         connectionFactoryControl.setReturnValue(connection);
145
146         connection.createInteraction();
147         connectionControl.setReturnValue(interaction);
148
149         interaction.execute(interactionSpec, inputOutputRecord, inputOutputRecord);
150         interactionControl.setReturnValue(true, 1);
151
152         interaction.close();
153         interactionControl.setVoidCallable(1);
154
155         connection.close();
156         connectionControl.setVoidCallable(1);
157
158         connectionFactoryControl.replay();
159         connectionControl.replay();
160         interactionControl.replay();
161
162         query.execute(inputOutputRecord, inputOutputRecord);
163
164         connectionFactoryControl.verify();
165         connectionControl.verify();
166         interactionControl.verify();
167     }
168
169     public void testMappingRecordOperation() throws ResourceException JavaDoc {
170         MockControl connectionFactoryControl = MockControl.createControl(ConnectionFactory JavaDoc.class);
171         final ConnectionFactory JavaDoc connectionFactory = (ConnectionFactory JavaDoc) connectionFactoryControl.getMock();
172         MockControl recordFactoryControl = MockControl.createStrictControl(RecordFactory JavaDoc.class);
173         RecordFactory JavaDoc recordFactory = (RecordFactory JavaDoc) recordFactoryControl.getMock();
174         MockControl connectionControl = MockControl.createControl(Connection JavaDoc.class);
175         Connection JavaDoc connection = (Connection JavaDoc) connectionControl.getMock();
176         MockControl interactionControl = MockControl.createControl(Interaction JavaDoc.class);
177         Interaction JavaDoc interaction = (Interaction JavaDoc) interactionControl.getMock();
178
179         MockControl inputRecordControl = MockControl.createControl(Record JavaDoc.class);
180         Record JavaDoc inputRecord = (Record JavaDoc) inputRecordControl.getMock();
181         MockControl outputRecordControl = MockControl.createControl(Record JavaDoc.class);
182         Record JavaDoc outputRecord = (Record JavaDoc) outputRecordControl.getMock();
183
184         MockControl interactionSpecControl = MockControl.createControl(InteractionSpec JavaDoc.class);
185         InteractionSpec JavaDoc interactionSpec = (InteractionSpec JavaDoc) interactionSpecControl.getMock();
186
187         MockControl callDetectorControl = MockControl.createControl(QueryCallDetector.class);
188         QueryCallDetector callDetector = (QueryCallDetector) callDetectorControl.getMock();
189
190         MappingRecordOperationImpl query = new MappingRecordOperationImpl(connectionFactory, interactionSpec);
191         query.setCallDetector(callDetector);
192
193         Object JavaDoc inObj = new Object JavaDoc();
194         Object JavaDoc outObj = new Object JavaDoc();
195
196         connectionFactory.getRecordFactory();
197         connectionFactoryControl.setReturnValue(recordFactory, 1);
198
199         callDetector.callCreateInputRecord(recordFactory, inObj);
200         callDetectorControl.setReturnValue(inputRecord, 1);
201
202         connectionFactory.getConnection();
203         connectionFactoryControl.setReturnValue(connection, 1);
204
205         connection.createInteraction();
206         connectionControl.setReturnValue(interaction, 1);
207
208         interaction.execute(interactionSpec, inputRecord);
209         interactionControl.setReturnValue(outputRecord, 1);
210
211         callDetector.callExtractOutputData(outputRecord);
212         callDetectorControl.setReturnValue(outObj, 1);
213
214         interaction.close();
215         interactionControl.setVoidCallable(1);
216
217         connection.close();
218         connectionControl.setVoidCallable(1);
219
220         connectionFactoryControl.replay();
221         connectionControl.replay();
222         interactionControl.replay();
223         callDetectorControl.replay();
224
225         assertSame(outObj, query.execute(inObj));
226
227         connectionFactoryControl.verify();
228         connectionControl.verify();
229         interactionControl.verify();
230         callDetectorControl.verify();
231     }
232
233     public void testMappingRecordOperationWithOutputRecordCreator() throws ResourceException JavaDoc {
234         MockControl connectionFactoryControl = MockControl.createControl(ConnectionFactory JavaDoc.class);
235         final ConnectionFactory JavaDoc connectionFactory = (ConnectionFactory JavaDoc) connectionFactoryControl.getMock();
236         MockControl recordFactoryControl = MockControl.createStrictControl(RecordFactory JavaDoc.class);
237         RecordFactory JavaDoc recordFactory = (RecordFactory JavaDoc) recordFactoryControl.getMock();
238         MockControl connectionControl = MockControl.createControl(Connection JavaDoc.class);
239         Connection JavaDoc connection = (Connection JavaDoc) connectionControl.getMock();
240         MockControl interactionControl = MockControl.createControl(Interaction JavaDoc.class);
241         Interaction JavaDoc interaction = (Interaction JavaDoc) interactionControl.getMock();
242
243         MockControl inputRecordControl = MockControl.createControl(Record JavaDoc.class);
244         Record JavaDoc inputRecord = (Record JavaDoc) inputRecordControl.getMock();
245         MockControl outputRecordControl = MockControl.createControl(Record JavaDoc.class);
246         Record JavaDoc outputRecord = (Record JavaDoc) outputRecordControl.getMock();
247         MockControl outputCreatorControl = MockControl.createControl(RecordCreator.class);
248         RecordCreator outputCreator = (RecordCreator) outputCreatorControl.getMock();
249
250         MockControl interactionSpecControl = MockControl.createControl(InteractionSpec JavaDoc.class);
251         InteractionSpec JavaDoc interactionSpec = (InteractionSpec JavaDoc) interactionSpecControl.getMock();
252
253         MockControl callDetectorControl = MockControl.createControl(QueryCallDetector.class);
254         QueryCallDetector callDetector = (QueryCallDetector) callDetectorControl.getMock();
255
256         MappingRecordOperationImpl query = new MappingRecordOperationImpl(connectionFactory, interactionSpec);
257         query.setOutputRecordCreator(outputCreator);
258         query.setCallDetector(callDetector);
259
260         Object JavaDoc inObj = new Object JavaDoc();
261         Object JavaDoc outObj = new Object JavaDoc();
262
263         connectionFactory.getRecordFactory();
264         connectionFactoryControl.setReturnValue(recordFactory, 1);
265
266         callDetector.callCreateInputRecord(recordFactory, inObj);
267         callDetectorControl.setReturnValue(inputRecord, 1);
268
269         connectionFactory.getConnection();
270         connectionFactoryControl.setReturnValue(connection, 1);
271
272         connection.createInteraction();
273         connectionControl.setReturnValue(interaction, 1);
274
275         connectionFactory.getRecordFactory();
276         connectionFactoryControl.setReturnValue(recordFactory, 1);
277
278         outputCreator.createRecord(recordFactory);
279         outputCreatorControl.setReturnValue(outputRecord, 1);
280
281         interaction.execute(interactionSpec, inputRecord, outputRecord);
282         interactionControl.setReturnValue(true, 1);
283
284         callDetector.callExtractOutputData(outputRecord);
285         callDetectorControl.setReturnValue(outObj, 1);
286
287         interaction.close();
288         interactionControl.setVoidCallable(1);
289
290         connection.close();
291         connectionControl.setVoidCallable(1);
292
293         connectionFactoryControl.replay();
294         connectionControl.replay();
295         interactionControl.replay();
296         outputCreatorControl.replay();
297         callDetectorControl.replay();
298
299         assertSame(outObj, query.execute(inObj));
300
301         connectionFactoryControl.verify();
302         connectionControl.verify();
303         interactionControl.verify();
304         outputCreatorControl.verify();
305         callDetectorControl.verify();
306     }
307
308
309     private class MappingRecordOperationImpl extends MappingRecordOperation {
310
311         private QueryCallDetector callDetector;
312
313         public MappingRecordOperationImpl(ConnectionFactory JavaDoc connectionFactory, InteractionSpec JavaDoc interactionSpec) {
314             super(connectionFactory, interactionSpec);
315         }
316
317         public void setCallDetector(QueryCallDetector callDetector) {
318             this.callDetector = callDetector;
319         }
320
321         protected Record JavaDoc createInputRecord(RecordFactory JavaDoc recordFactory, Object JavaDoc inputObject) {
322             return this.callDetector.callCreateInputRecord(recordFactory, inputObject);
323         }
324
325         protected Object JavaDoc extractOutputData(Record JavaDoc outputRecord) throws ResourceException JavaDoc {
326             return this.callDetector.callExtractOutputData(outputRecord);
327         }
328     }
329
330
331     private interface QueryCallDetector {
332
333         Record JavaDoc callCreateInputRecord(RecordFactory JavaDoc recordFactory, Object JavaDoc inputObject);
334
335         Object JavaDoc callExtractOutputData(Record JavaDoc outputRecord);
336     }
337
338 }
339
Popular Tags