1 16 17 package org.springframework.orm.toplink; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 22 import javax.transaction.Status ; 23 import javax.transaction.TransactionManager ; 24 import javax.transaction.UserTransaction ; 25 26 import junit.framework.TestCase; 27 import oracle.toplink.exceptions.TopLinkException; 28 import oracle.toplink.sessions.Session; 29 import org.easymock.MockControl; 30 31 import org.springframework.transaction.MockJtaTransaction; 32 import org.springframework.transaction.TransactionDefinition; 33 import org.springframework.transaction.TransactionStatus; 34 import org.springframework.transaction.jta.JtaTransactionManager; 35 import org.springframework.transaction.support.TransactionCallback; 36 import org.springframework.transaction.support.TransactionSynchronizationManager; 37 import org.springframework.transaction.support.TransactionTemplate; 38 39 44 public class TopLinkJtaTransactionTests extends TestCase { 45 46 public void testParticipatingJtaTransactionWithWithRequiresNew() throws Exception { 47 MockControl utControl = MockControl.createControl(UserTransaction .class); 48 UserTransaction ut = (UserTransaction ) utControl.getMock(); 49 MockControl tmControl = MockControl.createControl(TransactionManager .class); 50 TransactionManager tm = (TransactionManager ) tmControl.getMock(); 51 MockControl tx1Control = MockControl.createControl(javax.transaction.Transaction .class); 52 javax.transaction.Transaction tx1 = (javax.transaction.Transaction ) tx1Control.getMock(); 53 54 MockControl session1Control = MockControl.createControl(Session.class); 55 Session session1 = (Session) session1Control.getMock(); 56 MockControl session2Control = MockControl.createControl(Session.class); 57 final Session session2 = (Session) session2Control.getMock(); 58 final MockSessionFactory sf = new MockSessionFactory(session1); 59 60 ut.getStatus(); 61 utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1); 62 ut.getStatus(); 63 utControl.setReturnValue(Status.STATUS_ACTIVE, 3); 64 ut.begin(); 65 utControl.setVoidCallable(2); 66 tm.suspend(); 67 tmControl.setReturnValue(tx1, 1); 68 tm.resume(tx1); 69 tmControl.setVoidCallable(1); 70 ut.commit(); 71 utControl.setVoidCallable(2); 72 73 session1.release(); 76 session1Control.setVoidCallable(1); 77 session2.release(); 80 session2Control.setVoidCallable(1); 81 82 utControl.replay(); 83 tmControl.replay(); 84 session1Control.replay(); 85 session2Control.replay(); 86 87 JtaTransactionManager ptm = new JtaTransactionManager(); 88 ptm.setUserTransaction(ut); 89 ptm.setTransactionManager(tm); 90 final TransactionTemplate tt = new TransactionTemplate(ptm); 91 tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); 92 93 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 94 tt.execute(new TransactionCallback() { 95 public Object doInTransaction(TransactionStatus status) { 96 SessionFactoryUtils.getSession(sf, true); 97 final SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sf); 98 assertTrue("Has thread session", holder != null); 99 sf.setSession(session2); 100 101 tt.execute(new TransactionCallback() { 102 public Object doInTransaction(TransactionStatus status) { 103 TopLinkTemplate ht = new TopLinkTemplate(sf); 104 return ht.executeFind(new TopLinkCallback() { 105 public Object doInTopLink(Session session) { 106 assertTrue("Not enclosing session", session != holder.getSession()); 107 return null; 108 } 109 }); 110 } 111 }); 112 assertTrue("Same thread session as before", 113 holder.getSession() == SessionFactoryUtils.getSession(sf, false)); 114 return null; 115 } 116 }); 117 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 118 119 utControl.verify(); 120 tmControl.verify(); 121 session1Control.verify(); 122 session2Control.verify(); 123 } 124 125 public void testJtaTransactionCommit() throws Exception { 126 doTestJtaTransactionCommit(Status.STATUS_NO_TRANSACTION); 127 } 128 129 public void testJtaTransactionCommitWithExisting() throws Exception { 130 doTestJtaTransactionCommit(Status.STATUS_ACTIVE); 131 } 132 133 private void doTestJtaTransactionCommit(int status) throws Exception { 134 MockControl utControl = MockControl.createControl(UserTransaction .class); 135 UserTransaction ut = (UserTransaction ) utControl.getMock(); 136 ut.getStatus(); 137 utControl.setReturnValue(status, 1); 138 if (status == Status.STATUS_NO_TRANSACTION) { 139 ut.begin(); 140 utControl.setVoidCallable(1); 141 ut.getStatus(); 142 utControl.setReturnValue(Status.STATUS_ACTIVE, 1); 143 ut.commit(); 144 utControl.setVoidCallable(1); 145 } 146 utControl.replay(); 147 148 final MockControl sessionControl = MockControl.createControl(Session.class); 149 final Session session = (Session) sessionControl.getMock(); 150 final SessionFactory sf = new SingleSessionFactory(session); 151 152 sessionControl.replay(); 153 154 JtaTransactionManager ptm = new JtaTransactionManager(ut); 155 TransactionTemplate tt = new TransactionTemplate(ptm); 156 final List l = new ArrayList (); 157 l.add("test"); 158 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 159 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 160 161 Object result = tt.execute(new TransactionCallback() { 162 public Object doInTransaction(TransactionStatus status) { 163 try { 164 assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive()); 165 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 166 TopLinkTemplate ht = new TopLinkTemplate(sf); 167 List htl = ht.executeFind(new TopLinkCallback() { 168 public Object doInTopLink(Session sess) { 169 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 170 assertEquals(session, sess); 171 return l; 172 } 173 }); 174 175 ht = new TopLinkTemplate(sf); 176 htl = ht.executeFind(new TopLinkCallback() { 177 public Object doInTopLink(Session sess) { 178 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 179 assertEquals(session, sess); 180 return l; 181 } 182 }); 183 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 184 185 sessionControl.verify(); 186 sessionControl.reset(); 187 188 try { 189 session.release(); 190 sessionControl.setVoidCallable(1); 191 } 192 catch (TopLinkException e) { 193 } 194 sessionControl.replay(); 195 return htl; 196 } 197 catch (Error err) { 198 err.printStackTrace(); 199 throw err; 200 } 201 } 202 }); 203 204 assertTrue("Correct result list", result == l); 205 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 206 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 207 208 utControl.verify(); 209 sessionControl.verify(); 210 } 211 212 public void testJtaTransactionCommitWithJtaTm() throws Exception { 213 doTestJtaTransactionCommitWithJtaTm(Status.STATUS_NO_TRANSACTION); 214 } 215 216 public void testJtaTransactionCommitWithJtaTmAndExisting() throws Exception { 217 doTestJtaTransactionCommitWithJtaTm(Status.STATUS_ACTIVE); 218 } 219 220 private void doTestJtaTransactionCommitWithJtaTm(int status) throws Exception { 221 MockControl utControl = MockControl.createControl(UserTransaction .class); 222 UserTransaction ut = (UserTransaction ) utControl.getMock(); 223 ut.getStatus(); 224 utControl.setReturnValue(status, 1); 225 if (status == Status.STATUS_NO_TRANSACTION) { 226 ut.begin(); 227 utControl.setVoidCallable(1); 228 ut.getStatus(); 229 utControl.setReturnValue(Status.STATUS_ACTIVE, 1); 230 ut.commit(); 231 utControl.setVoidCallable(1); 232 } 233 234 MockControl tmControl = MockControl.createControl(TransactionManager .class); 235 TransactionManager tm = (TransactionManager ) tmControl.getMock(); 236 MockJtaTransaction transaction = new MockJtaTransaction(); 237 tm.getStatus(); 238 tmControl.setReturnValue(Status.STATUS_ACTIVE, 6); 239 tm.getTransaction(); 240 tmControl.setReturnValue(transaction, 6); 241 242 final MockControl sessionControl = MockControl.createControl(Session.class); 243 final Session session = (Session) sessionControl.getMock(); 244 final SessionFactory sf = new SingleSessionFactory(session); 245 246 utControl.replay(); 247 tmControl.replay(); 248 sessionControl.replay(); 249 250 JtaTransactionManager ptm = new JtaTransactionManager(ut); 251 TransactionTemplate tt = new TransactionTemplate(ptm); 252 final List l = new ArrayList (); 253 l.add("test"); 254 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 255 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 256 257 Object result = tt.execute(new TransactionCallback() { 258 public Object doInTransaction(TransactionStatus status) { 259 try { 260 assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive()); 261 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 262 263 TopLinkTemplate ht = new TopLinkTemplate(sf); 264 List htl = ht.executeFind(new TopLinkCallback() { 265 public Object doInTopLink(Session sess) { 266 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 267 assertEquals(session, sess); 268 return l; 269 } 270 }); 271 272 ht = new TopLinkTemplate(sf); 273 htl = ht.executeFind(new TopLinkCallback() { 274 public Object doInTopLink(Session sess) { 275 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 276 assertEquals(session, sess); 277 return l; 278 } 279 }); 280 281 assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf)); 282 sessionControl.verify(); 283 sessionControl.reset(); 284 try { 285 session.release(); 286 sessionControl.setVoidCallable(1); 287 } 288 catch (TopLinkException e) { 289 } 290 sessionControl.replay(); 291 return htl; 292 } 293 catch (Error err) { 294 err.printStackTrace(); 295 throw err; 296 } 297 } 298 }); 299 300 assertTrue("Correct result list", result == l); 301 assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf)); 302 assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive()); 303 304 utControl.verify(); 305 sessionControl.verify(); 306 } 307 1225 protected void tearDown() { 1226 assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty()); 1227 assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); 1228 } 1229 1230} 1231 | Popular Tags |