1 5 6 package org.springframework.orm.toplink; 7 8 import java.util.ArrayList ; 9 import java.util.List ; 10 11 import junit.framework.TestCase; 12 import oracle.toplink.sessions.Session; 13 import oracle.toplink.sessions.UnitOfWork; 14 import org.easymock.MockControl; 15 16 import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator; 17 import org.springframework.transaction.PlatformTransactionManager; 18 import org.springframework.transaction.TransactionDefinition; 19 import org.springframework.transaction.TransactionStatus; 20 import org.springframework.transaction.UnexpectedRollbackException; 21 import org.springframework.transaction.support.TransactionCallback; 22 import org.springframework.transaction.support.TransactionSynchronizationManager; 23 import org.springframework.transaction.support.TransactionTemplate; 24 25 30 public class TopLinkTransactionManagerTests extends TestCase { 31 32 public void testTransactionCommit() { 33 MockControl sessionControl = MockControl.createControl(Session.class); 34 final Session session = (Session) sessionControl.getMock(); 35 MockControl uowControl = MockControl.createControl(UnitOfWork.class); 36 UnitOfWork uow = (UnitOfWork) uowControl.getMock(); 37 38 final SessionFactory sf = new MockSessionFactory(session); 39 40 session.getActiveUnitOfWork(); 42 sessionControl.setReturnValue(uow, 2); 43 uow.beginEarlyTransaction(); 44 uowControl.setVoidCallable(1); 45 uow.commit(); 46 uowControl.setVoidCallable(); 47 session.release(); 49 sessionControl.setVoidCallable(); 50 51 sessionControl.replay(); 52 uowControl.replay(); 53 54 TopLinkTransactionManager tm = new TopLinkTransactionManager(); 55 tm.setJdbcExceptionTranslator(new SQLStateSQLExceptionTranslator()); 56 tm.setSessionFactory(sf); 57 TransactionTemplate tt = new TransactionTemplate(tm); 58 tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE); 59 tt.setTimeout(10); 60 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 61 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 62 63 Object result = tt.execute(new TransactionCallback() { 64 public Object doInTransaction(TransactionStatus status) { 65 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 66 TopLinkTemplate template = new TopLinkTemplate(sf); 67 return template.execute(new TopLinkCallback() { 68 public Object doInTopLink(Session session) { 69 return null; 70 } 71 }); 72 } 73 }); 74 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 75 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 76 sessionControl.verify(); 77 uowControl.verify(); 78 } 79 80 public void testTransactionRollback() { 81 MockControl sessionControl = MockControl.createControl(Session.class); 82 final Session session = (Session) sessionControl.getMock(); 83 MockControl uowControl = MockControl.createControl(UnitOfWork.class); 84 UnitOfWork uow = (UnitOfWork) uowControl.getMock(); 85 86 final SessionFactory sf = new MockSessionFactory(session); 87 88 session.getActiveUnitOfWork(); 89 sessionControl.setReturnValue(uow, 1); 90 uow.beginEarlyTransaction(); 91 uowControl.setVoidCallable(1); 92 session.release(); 93 sessionControl.setVoidCallable(1); 94 95 sessionControl.replay(); 96 uowControl.replay(); 97 98 TopLinkTransactionManager tm = new TopLinkTransactionManager(); 99 tm.setSessionFactory(sf); 100 tm.setJdbcExceptionTranslator(new SQLStateSQLExceptionTranslator()); 101 TransactionTemplate tt = new TransactionTemplate(tm); 102 tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE); 103 tt.setTimeout(10); 104 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 105 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 106 107 try { 108 Object result = tt.execute(new TransactionCallback() { 109 public Object doInTransaction(TransactionStatus status) { 110 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 111 TopLinkTemplate template = new TopLinkTemplate(sf); 112 return template.execute(new TopLinkCallback() { 113 public Object doInTopLink(Session session) { 114 throw new RuntimeException ("failure"); 115 } 116 }); 117 } 118 }); 119 fail("Should have propagated RuntimeException"); 120 } 121 catch (RuntimeException ex) { 122 assertTrue(ex.getMessage().equals("failure")); 123 } 124 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 125 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 126 127 sessionControl.verify(); 128 uowControl.verify(); 129 } 130 131 public void testTransactionRollbackOnly() { 132 MockControl sessionControl = MockControl.createControl(Session.class); 133 final Session session = (Session) sessionControl.getMock(); 134 135 final SessionFactory sf = new MockSessionFactory(session); 136 session.release(); 137 sessionControl.setVoidCallable(); 138 sessionControl.replay(); 139 140 TopLinkTransactionManager tm = new TopLinkTransactionManager(); 141 tm.setSessionFactory(sf); 142 tm.setLazyDatabaseTransaction(true); 143 tm.setJdbcExceptionTranslator(new SQLStateSQLExceptionTranslator()); 144 TransactionTemplate tt = new TransactionTemplate(tm); 145 tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE); 146 tt.setTimeout(10); 147 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 148 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 149 150 Object result = tt.execute(new TransactionCallback() { 151 public Object doInTransaction(TransactionStatus status) { 152 assertTrue("Has thread session", 153 TransactionSynchronizationManager.hasResource(sf)); 154 TopLinkTemplate template = new TopLinkTemplate(sf); 155 template.execute(new TopLinkCallback() { 156 public Object doInTopLink(Session session) { 157 return null; 158 } 159 }); 160 status.setRollbackOnly(); 161 return null; 162 } 163 }); 164 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 165 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 166 sessionControl.verify(); 167 } 168 169 public void testParticipatingTransactionWithCommit() { 170 MockControl sessionControl = MockControl.createControl(Session.class); 171 final Session session = (Session) sessionControl.getMock(); 172 MockControl uowControl = MockControl.createControl(UnitOfWork.class); 173 UnitOfWork uow = (UnitOfWork) uowControl.getMock(); 174 175 final SessionFactory sf = new MockSessionFactory(session); 176 177 session.getActiveUnitOfWork(); 178 sessionControl.setReturnValue(uow, 2); 179 uow.beginEarlyTransaction(); 180 uowControl.setVoidCallable(1); 181 uow.commit(); 182 uowControl.setVoidCallable(); 183 session.release(); 184 sessionControl.setVoidCallable(); 185 186 sessionControl.replay(); 187 uowControl.replay(); 188 189 PlatformTransactionManager tm = new TopLinkTransactionManager(sf); 190 final TransactionTemplate tt = new TransactionTemplate(tm); 191 192 Object result = tt.execute(new TransactionCallback() { 193 public Object doInTransaction(TransactionStatus status) { 194 return tt.execute(new TransactionCallback() { 195 public Object doInTransaction(TransactionStatus status) { 196 TopLinkTemplate ht = new TopLinkTemplate(sf); 197 return ht.executeFind(new TopLinkCallback() { 198 public Object doInTopLink(Session injectedSession) { 199 assertTrue(session == injectedSession); 200 return null; 201 } 202 }); 203 } 204 }); 205 } 206 }); 207 208 sessionControl.verify(); 209 uowControl.verify(); 210 } 211 212 public void testParticipatingTransactionWithRollback() { 213 MockControl sessionControl = MockControl.createControl(Session.class); 214 final Session session = (Session) sessionControl.getMock(); 215 216 final SessionFactory sf = new MockSessionFactory(session); 217 218 session.release(); 219 sessionControl.setVoidCallable(); 220 221 sessionControl.replay(); 222 223 TopLinkTransactionManager tm = new TopLinkTransactionManager(sf); 224 tm.setLazyDatabaseTransaction(true); 225 final TransactionTemplate tt = new TransactionTemplate(tm); 226 try { 227 tt.execute(new TransactionCallback() { 228 public Object doInTransaction(TransactionStatus status) { 229 return tt.execute(new TransactionCallback() { 230 public Object doInTransaction(TransactionStatus status) { 231 TopLinkTemplate ht = new TopLinkTemplate(sf); 232 return ht.executeFind(new TopLinkCallback() { 233 public Object doInTopLink(Session session) { 234 throw new RuntimeException ("application exception"); 235 } 236 }); 237 } 238 }); 239 } 240 }); 241 fail("Should not thrown RuntimeException"); 242 } 243 catch (RuntimeException ex) { 244 assertTrue(ex.getMessage().equals("application exception")); 245 } 246 sessionControl.verify(); 247 } 248 249 public void testParticipatingTransactionWithRollbackOnly() { 250 MockControl sessionControl = MockControl.createControl(Session.class); 251 final Session session = (Session) sessionControl.getMock(); 252 final SessionFactory sf = new MockSessionFactory(session); 253 254 session.release(); 255 sessionControl.setVoidCallable(); 256 257 sessionControl.replay(); 258 259 TopLinkTransactionManager tm = new TopLinkTransactionManager(sf); 260 tm.setLazyDatabaseTransaction(true); 261 final TransactionTemplate tt = new TransactionTemplate(tm); 262 263 try { 264 tt.execute(new TransactionCallback() { 265 public Object doInTransaction(TransactionStatus status) { 266 tt.execute(new TransactionCallback() { 267 public Object doInTransaction(TransactionStatus status) { 268 TopLinkTemplate ht = new TopLinkTemplate(sf); 269 ht.execute(new TopLinkCallback() { 270 public Object doInTopLink(Session session) { 271 return null; 272 } 273 }); 274 status.setRollbackOnly(); 275 return null; 276 } 277 }); 278 return null; 279 } 280 }); 281 fail("Should have thrown UnexpectedRollbackException"); 282 } 283 catch (UnexpectedRollbackException ex) { 284 } 286 287 sessionControl.verify(); 288 } 289 290 public void testParticipatingTransactionWithWithRequiresNew() { 291 MockControl session1Control = MockControl.createControl(Session.class); 292 final Session session1 = (Session) session1Control.getMock(); 293 MockControl session2Control = MockControl.createControl(Session.class); 294 final Session session2 = (Session) session2Control.getMock(); 295 296 MockControl uow1Control = MockControl.createControl(UnitOfWork.class); 297 UnitOfWork uow1 = (UnitOfWork) uow1Control.getMock(); 298 MockControl uow2Control = MockControl.createControl(UnitOfWork.class); 299 UnitOfWork uow2 = (UnitOfWork) uow2Control.getMock(); 300 301 final MockSessionFactory sf = new MockSessionFactory(session1); 302 303 session2.getActiveUnitOfWork(); 304 session2Control.setReturnValue(uow2, 2); 305 uow2.beginEarlyTransaction(); 306 uow2Control.setVoidCallable(1); 307 uow2.commit(); 308 uow2Control.setVoidCallable(); 309 session2.release(); 310 session2Control.setVoidCallable(); 311 312 session1.getActiveUnitOfWork(); 313 session1Control.setReturnValue(uow1, 2); 314 uow1.beginEarlyTransaction(); 315 uow1Control.setVoidCallable(1); 316 uow1.commit(); 317 uow1Control.setVoidCallable(); 318 session1.release(); 319 session1Control.setVoidCallable(); 320 321 session1Control.replay(); 322 uow1Control.replay(); 323 session2Control.replay(); 324 uow2Control.replay(); 325 326 PlatformTransactionManager tm = new TopLinkTransactionManager(sf); 327 final TransactionTemplate tt = new TransactionTemplate(tm); 328 tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); 329 330 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 331 tt.execute(new TransactionCallback() { 332 public Object doInTransaction(TransactionStatus status) { 333 final SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sf); 334 assertTrue("Has thread session", holder != null); 335 sf.setSession(session2); 336 tt.execute(new TransactionCallback() { 337 public Object doInTransaction(TransactionStatus status) { 338 TopLinkTemplate ht = new TopLinkTemplate(sf); 339 return ht.execute(new TopLinkCallback() { 340 public Object doInTopLink(Session session) { 341 assertTrue("Not enclosing session", session != holder.getSession()); 342 return null; 343 } 344 }); 345 } 346 }); 347 assertTrue("Same thread session as before", 348 holder.getSession() == SessionFactoryUtils.getSession(sf, false)); 349 return null; 350 } 351 }); 352 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 353 354 session1Control.verify(); 355 session2Control.verify(); 356 uow1Control.verify(); 357 uow2Control.verify(); 358 } 359 360 public void testParticipatingTransactionWithWithNotSupported() { 361 MockControl sessionControl = MockControl.createControl(Session.class); 362 final Session session = (Session) sessionControl.getMock(); 363 MockControl uowControl = MockControl.createControl(UnitOfWork.class); 364 UnitOfWork uow = (UnitOfWork) uowControl.getMock(); 365 366 final SessionFactory sf = new MockSessionFactory(session); 367 368 session.getActiveUnitOfWork(); 369 sessionControl.setReturnValue(uow, 2); 370 uow.beginEarlyTransaction(); 371 uowControl.setVoidCallable(1); 372 uow.commit(); 373 uowControl.setVoidCallable(); 374 session.release(); 375 sessionControl.setVoidCallable(2); 376 377 sessionControl.replay(); 378 uowControl.replay(); 379 380 TopLinkTransactionManager tm = new TopLinkTransactionManager(sf); 381 final TransactionTemplate tt = new TransactionTemplate(tm); 382 tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); 383 384 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 385 tt.execute(new TransactionCallback() { 386 public Object doInTransaction(TransactionStatus status) { 387 SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sf); 388 assertTrue("Has thread session", holder != null); 389 tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED); 390 tt.execute(new TransactionCallback() { 391 public Object doInTransaction(TransactionStatus status) { 392 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 393 TopLinkTemplate ht = new TopLinkTemplate(sf); 394 395 return ht.execute(new TopLinkCallback() { 396 public Object doInTopLink(Session session) { 397 return null; 398 } 399 }); 400 } 401 }); 402 assertTrue("Same thread session as before", holder.getSession() == SessionFactoryUtils.getSession(sf, false)); 403 return null; 404 } 405 }); 406 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 407 408 sessionControl.verify(); 409 uowControl.verify(); 410 } 411 412 public void testTransactionWithPropagationSupports() { 413 MockControl sessionControl = MockControl.createControl(Session.class); 414 final Session session = (Session) sessionControl.getMock(); 415 416 final SessionFactory sf = new MockSessionFactory(session); 417 418 session.release(); 420 sessionControl.setVoidCallable(); 421 422 sessionControl.replay(); 423 424 PlatformTransactionManager tm = new TopLinkTransactionManager(sf); 425 TransactionTemplate tt = new TransactionTemplate(tm); 426 tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS); 427 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 428 429 tt.execute(new TransactionCallback() { 430 public Object doInTransaction(TransactionStatus status) { 431 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 432 assertTrue("Is not new transaction", !status.isNewTransaction()); 433 TopLinkTemplate ht = new TopLinkTemplate(sf); 434 ht.execute(new TopLinkCallback() { 435 public Object doInTopLink(Session session) { 436 return null; 437 } 438 }); 439 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 440 return null; 441 } 442 }); 443 444 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 445 sessionControl.verify(); 446 } 447 448 public void testTransactionCommitWithReadOnly() { 449 MockControl sessionControl = MockControl.createControl(Session.class); 450 final Session session = (Session) sessionControl.getMock(); 451 MockControl uowControl = MockControl.createControl(UnitOfWork.class); 452 UnitOfWork uow = (UnitOfWork) uowControl.getMock(); 453 454 final SessionFactory sf = new MockSessionFactory(session); 455 456 session.release(); 457 sessionControl.setVoidCallable(); 458 459 sessionControl.replay(); 460 uowControl.replay(); 461 462 TopLinkTransactionManager tm = new TopLinkTransactionManager(sf); 463 TransactionTemplate tt = new TransactionTemplate(tm); 464 tt.setReadOnly(true); 465 final List l = new ArrayList (); 466 l.add("test"); 467 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 468 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 469 470 Object result = tt.execute(new TransactionCallback() { 471 public Object doInTransaction(TransactionStatus status) { 472 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 473 TopLinkTemplate ht = new TopLinkTemplate(sf); 474 return ht.executeFind(new TopLinkCallback() { 475 public Object doInTopLink(Session session) { 476 return l; 477 } 478 }); 479 } 480 }); 481 assertTrue("Correct result list", result == l); 482 483 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 484 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 485 sessionControl.verify(); 486 uowControl.verify(); 487 } 488 489 protected void tearDown() { 490 assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty()); 491 assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); 492 assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly()); 493 assertFalse(TransactionSynchronizationManager.isActualTransactionActive()); 494 } 495 496 } 497 | Popular Tags |