1 16 package org.springframework.jms.connection; 17 18 import javax.jms.Connection ; 19 import javax.jms.ConnectionFactory ; 20 import javax.jms.JMSException ; 21 import javax.jms.QueueConnection ; 22 import javax.jms.QueueConnectionFactory ; 23 import javax.jms.QueueSession ; 24 import javax.jms.Session ; 25 import javax.jms.TopicConnection ; 26 import javax.jms.TopicConnectionFactory ; 27 import javax.jms.TopicSession ; 28 29 import junit.framework.TestCase; 30 import org.easymock.MockControl; 31 32 import org.springframework.jms.core.JmsTemplate; 33 import org.springframework.jms.core.JmsTemplate102; 34 import org.springframework.jms.core.SessionCallback; 35 import org.springframework.transaction.TransactionDefinition; 36 import org.springframework.transaction.TransactionStatus; 37 import org.springframework.transaction.UnexpectedRollbackException; 38 import org.springframework.transaction.support.DefaultTransactionDefinition; 39 import org.springframework.transaction.support.TransactionCallbackWithoutResult; 40 import org.springframework.transaction.support.TransactionSynchronizationManager; 41 import org.springframework.transaction.support.TransactionTemplate; 42 43 47 public class JmsTransactionManagerTests extends TestCase { 48 49 public void testTransactionCommit() throws JMSException { 50 MockControl cfControl = MockControl.createControl(ConnectionFactory .class); 51 ConnectionFactory cf = (ConnectionFactory ) cfControl.getMock(); 52 MockControl conControl = MockControl.createControl(Connection .class); 53 Connection con = (Connection ) conControl.getMock(); 54 MockControl sessionControl = MockControl.createControl(Session .class); 55 final Session session = (Session ) sessionControl.getMock(); 56 57 cf.createConnection(); 58 cfControl.setReturnValue(con, 1); 59 con.createSession(true, Session.AUTO_ACKNOWLEDGE); 60 conControl.setReturnValue(session, 1); 61 session.commit(); 62 sessionControl.setVoidCallable(1); 63 session.close(); 64 sessionControl.setVoidCallable(1); 65 con.close(); 66 conControl.setVoidCallable(1); 67 68 sessionControl.replay(); 69 conControl.replay(); 70 cfControl.replay(); 71 72 JmsTransactionManager tm = new JmsTransactionManager(cf); 73 TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition()); 74 JmsTemplate jt = new JmsTemplate(cf); 75 jt.execute(new SessionCallback() { 76 public Object doInJms(Session sess) { 77 assertTrue(sess == session); 78 return null; 79 } 80 }); 81 tm.commit(ts); 82 83 sessionControl.verify(); 84 conControl.verify(); 85 cfControl.verify(); 86 } 87 88 public void testTransactionRollback() throws JMSException { 89 MockControl cfControl = MockControl.createControl(ConnectionFactory .class); 90 ConnectionFactory cf = (ConnectionFactory ) cfControl.getMock(); 91 MockControl conControl = MockControl.createControl(Connection .class); 92 Connection con = (Connection ) conControl.getMock(); 93 MockControl sessionControl = MockControl.createControl(Session .class); 94 final Session session = (Session ) sessionControl.getMock(); 95 96 cf.createConnection(); 97 cfControl.setReturnValue(con, 1); 98 con.createSession(true, Session.AUTO_ACKNOWLEDGE); 99 conControl.setReturnValue(session, 1); 100 session.rollback(); 101 sessionControl.setVoidCallable(1); 102 session.close(); 103 sessionControl.setVoidCallable(1); 104 con.close(); 105 conControl.setVoidCallable(1); 106 107 sessionControl.replay(); 108 conControl.replay(); 109 cfControl.replay(); 110 111 JmsTransactionManager tm = new JmsTransactionManager(cf); 112 TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition()); 113 JmsTemplate jt = new JmsTemplate(cf); 114 jt.execute(new SessionCallback() { 115 public Object doInJms(Session sess) { 116 assertTrue(sess == session); 117 return null; 118 } 119 }); 120 tm.rollback(ts); 121 122 sessionControl.verify(); 123 conControl.verify(); 124 cfControl.verify(); 125 } 126 127 public void testParticipatingTransactionWithCommit() throws JMSException { 128 MockControl cfControl = MockControl.createControl(ConnectionFactory .class); 129 final ConnectionFactory cf = (ConnectionFactory ) cfControl.getMock(); 130 MockControl conControl = MockControl.createControl(Connection .class); 131 Connection con = (Connection ) conControl.getMock(); 132 MockControl sessionControl = MockControl.createControl(Session .class); 133 final Session session = (Session ) sessionControl.getMock(); 134 135 cf.createConnection(); 136 cfControl.setReturnValue(con, 1); 137 con.createSession(true, Session.AUTO_ACKNOWLEDGE); 138 conControl.setReturnValue(session, 1); 139 session.commit(); 140 sessionControl.setVoidCallable(1); 141 session.close(); 142 sessionControl.setVoidCallable(1); 143 con.close(); 144 conControl.setVoidCallable(1); 145 146 sessionControl.replay(); 147 conControl.replay(); 148 cfControl.replay(); 149 150 JmsTransactionManager tm = new JmsTransactionManager(cf); 151 TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition()); 152 final JmsTemplate jt = new JmsTemplate(cf); 153 jt.execute(new SessionCallback() { 154 public Object doInJms(Session sess) { 155 assertTrue(sess == session); 156 return null; 157 } 158 }); 159 TransactionTemplate tt = new TransactionTemplate(tm); 160 tt.execute(new TransactionCallbackWithoutResult() { 161 protected void doInTransactionWithoutResult(TransactionStatus status) { 162 jt.execute(new SessionCallback() { 163 public Object doInJms(Session sess) { 164 assertTrue(sess == session); 165 return null; 166 } 167 }); 168 } 169 }); 170 tm.commit(ts); 171 172 sessionControl.verify(); 173 conControl.verify(); 174 cfControl.verify(); 175 } 176 177 public void testParticipatingTransactionWithRollbackOnly() throws JMSException { 178 MockControl cfControl = MockControl.createControl(ConnectionFactory .class); 179 final ConnectionFactory cf = (ConnectionFactory ) cfControl.getMock(); 180 MockControl conControl = MockControl.createControl(Connection .class); 181 Connection con = (Connection ) conControl.getMock(); 182 MockControl sessionControl = MockControl.createControl(Session .class); 183 final Session session = (Session ) sessionControl.getMock(); 184 185 cf.createConnection(); 186 cfControl.setReturnValue(con, 1); 187 con.createSession(true, Session.AUTO_ACKNOWLEDGE); 188 conControl.setReturnValue(session, 1); 189 session.rollback(); 190 sessionControl.setVoidCallable(1); 191 session.close(); 192 sessionControl.setVoidCallable(1); 193 con.close(); 194 conControl.setVoidCallable(1); 195 196 sessionControl.replay(); 197 conControl.replay(); 198 cfControl.replay(); 199 200 JmsTransactionManager tm = new JmsTransactionManager(cf); 201 TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition()); 202 final JmsTemplate jt = new JmsTemplate(cf); 203 jt.execute(new SessionCallback() { 204 public Object doInJms(Session sess) { 205 assertTrue(sess == session); 206 return null; 207 } 208 }); 209 TransactionTemplate tt = new TransactionTemplate(tm); 210 tt.execute(new TransactionCallbackWithoutResult() { 211 protected void doInTransactionWithoutResult(TransactionStatus status) { 212 jt.execute(new SessionCallback() { 213 public Object doInJms(Session sess) { 214 assertTrue(sess == session); 215 return null; 216 } 217 }); 218 status.setRollbackOnly(); 219 } 220 }); 221 try { 222 tm.commit(ts); 223 fail("Should have thrown UnexpectedRollbackException"); 224 } 225 catch (UnexpectedRollbackException ex) { 226 } 228 229 sessionControl.verify(); 230 conControl.verify(); 231 cfControl.verify(); 232 } 233 234 public void testSuspendedTransaction() throws JMSException { 235 MockControl cfControl = MockControl.createControl(ConnectionFactory .class); 236 final ConnectionFactory cf = (ConnectionFactory ) cfControl.getMock(); 237 MockControl conControl = MockControl.createControl(Connection .class); 238 Connection con = (Connection ) conControl.getMock(); 239 MockControl sessionControl = MockControl.createControl(Session .class); 240 final Session session = (Session ) sessionControl.getMock(); 241 MockControl session2Control = MockControl.createControl(Session .class); 242 final Session session2 = (Session ) session2Control.getMock(); 243 244 cf.createConnection(); 245 cfControl.setReturnValue(con, 2); 246 con.createSession(true, Session.AUTO_ACKNOWLEDGE); 247 conControl.setReturnValue(session, 1); 248 con.createSession(false, Session.AUTO_ACKNOWLEDGE); 249 conControl.setReturnValue(session2, 1); 250 session.commit(); 251 sessionControl.setVoidCallable(1); 252 session.close(); 253 sessionControl.setVoidCallable(1); 254 session2.close(); 255 session2Control.setVoidCallable(1); 256 con.close(); 257 conControl.setVoidCallable(2); 258 259 sessionControl.replay(); 260 conControl.replay(); 261 cfControl.replay(); 262 263 JmsTransactionManager tm = new JmsTransactionManager(cf); 264 TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition()); 265 final JmsTemplate jt = new JmsTemplate(cf); 266 jt.execute(new SessionCallback() { 267 public Object doInJms(Session sess) { 268 assertTrue(sess == session); 269 return null; 270 } 271 }); 272 TransactionTemplate tt = new TransactionTemplate(tm); 273 tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED); 274 tt.execute(new TransactionCallbackWithoutResult() { 275 protected void doInTransactionWithoutResult(TransactionStatus status) { 276 jt.execute(new SessionCallback() { 277 public Object doInJms(Session sess) { 278 assertTrue(sess != session); 279 return null; 280 } 281 }); 282 } 283 }); 284 jt.execute(new SessionCallback() { 285 public Object doInJms(Session sess) { 286 assertTrue(sess == session); 287 return null; 288 } 289 }); 290 tm.commit(ts); 291 292 sessionControl.verify(); 293 conControl.verify(); 294 cfControl.verify(); 295 } 296 297 public void testNestedTransaction() throws JMSException { 298 MockControl cfControl = MockControl.createControl(ConnectionFactory .class); 299 final ConnectionFactory cf = (ConnectionFactory ) cfControl.getMock(); 300 MockControl conControl = MockControl.createControl(Connection .class); 301 Connection con = (Connection ) conControl.getMock(); 302 MockControl sessionControl = MockControl.createControl(Session .class); 303 final Session session = (Session ) sessionControl.getMock(); 304 MockControl session2Control = MockControl.createControl(Session .class); 305 final Session session2 = (Session ) session2Control.getMock(); 306 307 cf.createConnection(); 308 cfControl.setReturnValue(con, 2); 309 con.createSession(true, Session.AUTO_ACKNOWLEDGE); 310 conControl.setReturnValue(session, 1); 311 con.createSession(true, Session.AUTO_ACKNOWLEDGE); 312 conControl.setReturnValue(session2, 1); 313 session.commit(); 314 sessionControl.setVoidCallable(1); 315 session2.commit(); 316 session2Control.setVoidCallable(1); 317 session.close(); 318 sessionControl.setVoidCallable(1); 319 session2.close(); 320 session2Control.setVoidCallable(1); 321 con.close(); 322 conControl.setVoidCallable(2); 323 324 sessionControl.replay(); 325 conControl.replay(); 326 cfControl.replay(); 327 328 JmsTransactionManager tm = new JmsTransactionManager(cf); 329 TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition()); 330 final JmsTemplate jt = new JmsTemplate(cf); 331 jt.execute(new SessionCallback() { 332 public Object doInJms(Session sess) { 333 assertTrue(sess == session); 334 return null; 335 } 336 }); 337 TransactionTemplate tt = new TransactionTemplate(tm); 338 tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); 339 tt.execute(new TransactionCallbackWithoutResult() { 340 protected void doInTransactionWithoutResult(TransactionStatus status) { 341 jt.execute(new SessionCallback() { 342 public Object doInJms(Session sess) { 343 assertTrue(sess != session); 344 return null; 345 } 346 }); 347 } 348 }); 349 jt.execute(new SessionCallback() { 350 public Object doInJms(Session sess) { 351 assertTrue(sess == session); 352 return null; 353 } 354 }); 355 tm.commit(ts); 356 357 sessionControl.verify(); 358 conControl.verify(); 359 cfControl.verify(); 360 } 361 362 public void testTransactionCommit102WithQueue() throws JMSException { 363 MockControl cfControl = MockControl.createControl(QueueConnectionFactory .class); 364 QueueConnectionFactory cf = (QueueConnectionFactory ) cfControl.getMock(); 365 MockControl conControl = MockControl.createControl(QueueConnection .class); 366 QueueConnection con = (QueueConnection ) conControl.getMock(); 367 MockControl sessionControl = MockControl.createControl(QueueSession .class); 368 final QueueSession session = (QueueSession ) sessionControl.getMock(); 369 370 cf.createQueueConnection(); 371 cfControl.setReturnValue(con, 1); 372 con.createQueueSession(true, Session.AUTO_ACKNOWLEDGE); 373 conControl.setReturnValue(session, 1); 374 session.commit(); 375 sessionControl.setVoidCallable(1); 376 session.close(); 377 sessionControl.setVoidCallable(1); 378 con.close(); 379 conControl.setVoidCallable(1); 380 381 sessionControl.replay(); 382 conControl.replay(); 383 cfControl.replay(); 384 385 JmsTransactionManager tm = new JmsTransactionManager102(cf, false); 386 TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition()); 387 JmsTemplate jt = new JmsTemplate102(cf, false); 388 jt.execute(new SessionCallback() { 389 public Object doInJms(Session sess) { 390 assertTrue(sess == session); 391 return null; 392 } 393 }); 394 tm.commit(ts); 395 396 sessionControl.verify(); 397 conControl.verify(); 398 cfControl.verify(); 399 } 400 401 public void testTransactionCommit102WithTopic() throws JMSException { 402 MockControl cfControl = MockControl.createControl(TopicConnectionFactory .class); 403 TopicConnectionFactory cf = (TopicConnectionFactory ) cfControl.getMock(); 404 MockControl conControl = MockControl.createControl(TopicConnection .class); 405 TopicConnection con = (TopicConnection ) conControl.getMock(); 406 MockControl sessionControl = MockControl.createControl(TopicSession .class); 407 final TopicSession session = (TopicSession ) sessionControl.getMock(); 408 409 cf.createTopicConnection(); 410 cfControl.setReturnValue(con, 1); 411 con.createTopicSession(true, Session.AUTO_ACKNOWLEDGE); 412 conControl.setReturnValue(session, 1); 413 session.commit(); 414 sessionControl.setVoidCallable(1); 415 session.close(); 416 sessionControl.setVoidCallable(1); 417 con.close(); 418 conControl.setVoidCallable(1); 419 420 sessionControl.replay(); 421 conControl.replay(); 422 cfControl.replay(); 423 424 JmsTransactionManager tm = new JmsTransactionManager102(cf, true); 425 TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition()); 426 JmsTemplate jt = new JmsTemplate102(cf, true); 427 jt.execute(new SessionCallback() { 428 public Object doInJms(Session sess) { 429 assertTrue(sess == session); 430 return null; 431 } 432 }); 433 tm.commit(ts); 434 435 sessionControl.verify(); 436 conControl.verify(); 437 cfControl.verify(); 438 } 439 440 protected void tearDown() { 441 assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty()); 442 assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); 443 } 444 445 } 446 | Popular Tags |