1 16 17 package org.springframework.jca.cci; 18 19 import javax.resource.ResourceException ; 20 import javax.resource.cci.Connection ; 21 import javax.resource.cci.ConnectionFactory ; 22 import javax.resource.cci.Interaction ; 23 import javax.resource.cci.InteractionSpec ; 24 import javax.resource.cci.Record ; 25 import javax.resource.cci.RecordFactory ; 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 37 public class EisOperationTests extends TestCase { 38 39 public void testSimpleRecordOperation() throws ResourceException { 40 MockControl connectionFactoryControl = MockControl.createControl(ConnectionFactory .class); 41 ConnectionFactory connectionFactory = (ConnectionFactory ) connectionFactoryControl.getMock(); 42 MockControl connectionControl = MockControl.createControl(Connection .class); 43 Connection connection = (Connection ) connectionControl.getMock(); 44 MockControl interactionControl = MockControl.createControl(Interaction .class); 45 Interaction interaction = (Interaction ) interactionControl.getMock(); 46 47 MockControl inputRecordControl = MockControl.createControl(Record .class); 48 Record inputRecord = (Record ) inputRecordControl.getMock(); 49 MockControl outputRecordControl = MockControl.createControl(Record .class); 50 Record outputRecord = (Record ) outputRecordControl.getMock(); 51 52 MockControl interactionSpecControl = MockControl.createControl(InteractionSpec .class); 53 InteractionSpec interactionSpec = (InteractionSpec ) 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 { 84 MockControl connectionFactoryControl = MockControl.createControl(ConnectionFactory .class); 85 ConnectionFactory connectionFactory = (ConnectionFactory ) connectionFactoryControl.getMock(); 86 MockControl connectionControl = MockControl.createControl(Connection .class); 87 Connection connection = (Connection ) connectionControl.getMock(); 88 MockControl interactionControl = MockControl.createControl(Interaction .class); 89 Interaction interaction = (Interaction ) interactionControl.getMock(); 90 91 MockControl inputRecordControl = MockControl.createControl(Record .class); 92 Record inputRecord = (Record ) inputRecordControl.getMock(); 93 MockControl outputRecordControl = MockControl.createControl(Record .class); 94 Record outputRecord = (Record ) outputRecordControl.getMock(); 95 96 MockControl interactionSpecControl = MockControl.createControl(InteractionSpec .class); 97 InteractionSpec interactionSpec = (InteractionSpec ) 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 { 128 MockControl connectionFactoryControl = MockControl.createControl(ConnectionFactory .class); 129 final ConnectionFactory connectionFactory = (ConnectionFactory ) connectionFactoryControl.getMock(); 130 MockControl connectionControl = MockControl.createControl(Connection .class); 131 Connection connection = (Connection ) connectionControl.getMock(); 132 MockControl interactionControl = MockControl.createControl(Interaction .class); 133 Interaction interaction = (Interaction ) interactionControl.getMock(); 134 135 MockControl inputOutputRecordControl = MockControl.createControl(Record .class); 136 Record inputOutputRecord = (Record ) inputOutputRecordControl.getMock(); 137 138 MockControl interactionSpecControl = MockControl.createControl(InteractionSpec .class); 139 InteractionSpec interactionSpec = (InteractionSpec ) 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 { 170 MockControl connectionFactoryControl = MockControl.createControl(ConnectionFactory .class); 171 final ConnectionFactory connectionFactory = (ConnectionFactory ) connectionFactoryControl.getMock(); 172 MockControl recordFactoryControl = MockControl.createStrictControl(RecordFactory .class); 173 RecordFactory recordFactory = (RecordFactory ) recordFactoryControl.getMock(); 174 MockControl connectionControl = MockControl.createControl(Connection .class); 175 Connection connection = (Connection ) connectionControl.getMock(); 176 MockControl interactionControl = MockControl.createControl(Interaction .class); 177 Interaction interaction = (Interaction ) interactionControl.getMock(); 178 179 MockControl inputRecordControl = MockControl.createControl(Record .class); 180 Record inputRecord = (Record ) inputRecordControl.getMock(); 181 MockControl outputRecordControl = MockControl.createControl(Record .class); 182 Record outputRecord = (Record ) outputRecordControl.getMock(); 183 184 MockControl interactionSpecControl = MockControl.createControl(InteractionSpec .class); 185 InteractionSpec interactionSpec = (InteractionSpec ) 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 inObj = new Object (); 194 Object outObj = new Object (); 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 { 234 MockControl connectionFactoryControl = MockControl.createControl(ConnectionFactory .class); 235 final ConnectionFactory connectionFactory = (ConnectionFactory ) connectionFactoryControl.getMock(); 236 MockControl recordFactoryControl = MockControl.createStrictControl(RecordFactory .class); 237 RecordFactory recordFactory = (RecordFactory ) recordFactoryControl.getMock(); 238 MockControl connectionControl = MockControl.createControl(Connection .class); 239 Connection connection = (Connection ) connectionControl.getMock(); 240 MockControl interactionControl = MockControl.createControl(Interaction .class); 241 Interaction interaction = (Interaction ) interactionControl.getMock(); 242 243 MockControl inputRecordControl = MockControl.createControl(Record .class); 244 Record inputRecord = (Record ) inputRecordControl.getMock(); 245 MockControl outputRecordControl = MockControl.createControl(Record .class); 246 Record outputRecord = (Record ) outputRecordControl.getMock(); 247 MockControl outputCreatorControl = MockControl.createControl(RecordCreator.class); 248 RecordCreator outputCreator = (RecordCreator) outputCreatorControl.getMock(); 249 250 MockControl interactionSpecControl = MockControl.createControl(InteractionSpec .class); 251 InteractionSpec interactionSpec = (InteractionSpec ) 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 inObj = new Object (); 261 Object outObj = new Object (); 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 connectionFactory, InteractionSpec interactionSpec) { 314 super(connectionFactory, interactionSpec); 315 } 316 317 public void setCallDetector(QueryCallDetector callDetector) { 318 this.callDetector = callDetector; 319 } 320 321 protected Record createInputRecord(RecordFactory recordFactory, Object inputObject) { 322 return this.callDetector.callCreateInputRecord(recordFactory, inputObject); 323 } 324 325 protected Object extractOutputData(Record outputRecord) throws ResourceException { 326 return this.callDetector.callExtractOutputData(outputRecord); 327 } 328 } 329 330 331 private interface QueryCallDetector { 332 333 Record callCreateInputRecord(RecordFactory recordFactory, Object inputObject); 334 335 Object callExtractOutputData(Record outputRecord); 336 } 337 338 } 339 | Popular Tags |