1 17 package org.alfresco.util.transaction; 18 19 import javax.transaction.RollbackException ; 20 import javax.transaction.Status ; 21 import javax.transaction.UserTransaction ; 22 23 import junit.framework.TestCase; 24 25 import org.springframework.transaction.NoTransactionException; 26 import org.springframework.transaction.TransactionDefinition; 27 import org.springframework.transaction.interceptor.TransactionAspectSupport; 28 import org.springframework.transaction.support.AbstractPlatformTransactionManager; 29 import org.springframework.transaction.support.DefaultTransactionStatus; 30 31 36 public class SpringAwareUserTransactionTest extends TestCase 37 { 38 private DummyTransactionManager transactionManager; 39 private UserTransaction txn; 40 41 public SpringAwareUserTransactionTest() 42 { 43 super(); 44 } 45 46 @Override 47 protected void setUp() throws Exception 48 { 49 transactionManager = new DummyTransactionManager(); 50 txn = getTxn(); 51 } 52 53 private UserTransaction getTxn() 54 { 55 return new SpringAwareUserTransaction( 56 transactionManager, 57 false, 58 TransactionDefinition.ISOLATION_DEFAULT, 59 TransactionDefinition.PROPAGATION_REQUIRED, 60 TransactionDefinition.TIMEOUT_DEFAULT); 61 } 62 63 public void testSetUp() throws Exception 64 { 65 assertNotNull(transactionManager); 66 assertNotNull(txn); 67 } 68 69 private void checkNoStatusOnThread() 70 { 71 try 72 { 73 TransactionAspectSupport.currentTransactionStatus(); 74 fail("Spring transaction info is present outside of transaction boundaries"); 75 } 76 catch (NoTransactionException e) 77 { 78 } 80 } 81 82 public void testNoTxnStatus() throws Exception 83 { 84 checkNoStatusOnThread(); 85 assertEquals("Transaction status is not correct", 86 Status.STATUS_NO_TRANSACTION, 87 txn.getStatus()); 88 assertEquals("Transaction manager not set up correctly", 89 txn.getStatus(), 90 transactionManager.getStatus()); 91 } 92 93 public void testSimpleTxnWithCommit() throws Throwable 94 { 95 testNoTxnStatus(); 96 try 97 { 98 txn.begin(); 99 assertEquals("Transaction status is not correct", 100 Status.STATUS_ACTIVE, 101 txn.getStatus()); 102 assertEquals("Transaction manager not called correctly", 103 txn.getStatus(), 104 transactionManager.getStatus()); 105 106 txn.commit(); 107 assertEquals("Transaction status is not correct", 108 Status.STATUS_COMMITTED, 109 txn.getStatus()); 110 assertEquals("Transaction manager not called correctly", 111 txn.getStatus(), 112 transactionManager.getStatus()); 113 } 114 catch (Throwable e) 115 { 116 try 118 { 119 txn.rollback(); 120 } 121 catch (Throwable ee) 122 { 123 e.printStackTrace(); 124 } 125 throw e; 126 } 127 checkNoStatusOnThread(); 128 } 129 130 public void testSimpleTxnWithRollback() throws Exception 131 { 132 testNoTxnStatus(); 133 try 134 { 135 txn.begin(); 136 137 throw new Exception ("Blah"); 138 } 139 catch (Throwable e) 140 { 141 txn.rollback(); 142 } 143 assertEquals("Transaction status is not correct", 144 Status.STATUS_ROLLEDBACK, 145 txn.getStatus()); 146 assertEquals("Transaction manager not called correctly", 147 txn.getStatus(), 148 transactionManager.getStatus()); 149 checkNoStatusOnThread(); 150 } 151 152 public void testNoBeginCommit() throws Exception 153 { 154 testNoTxnStatus(); 155 try 156 { 157 txn.commit(); 158 fail("Failed to detected no begin"); 159 } 160 catch (IllegalStateException e) 161 { 162 } 164 checkNoStatusOnThread(); 165 } 166 167 public void testPostRollbackCommitDetection() throws Exception 168 { 169 testNoTxnStatus(); 170 171 txn.begin(); 172 txn.rollback(); 173 try 174 { 175 txn.commit(); 176 fail("Failed to detect rolled back txn"); 177 } 178 catch (RollbackException e) 179 { 180 } 182 checkNoStatusOnThread(); 183 } 184 185 public void testPostSetRollbackOnlyCommitDetection() throws Exception 186 { 187 testNoTxnStatus(); 188 189 txn.begin(); 190 txn.setRollbackOnly(); 191 try 192 { 193 txn.commit(); 194 fail("Failed to detect set rollback"); 195 } 196 catch (RollbackException e) 197 { 198 txn.rollback(); 200 } 201 checkNoStatusOnThread(); 202 } 203 204 public void testMismatchedBeginCommit() throws Exception 205 { 206 UserTransaction txn1 = getTxn(); 207 UserTransaction txn2 = getTxn(); 208 209 testNoTxnStatus(); 210 211 txn1.begin(); 212 txn2.begin(); 213 214 txn2.commit(); 215 txn1.commit(); 216 217 checkNoStatusOnThread(); 218 219 txn1 = getTxn(); 220 txn2 = getTxn(); 221 222 txn1.begin(); 223 txn2.begin(); 224 225 try 226 { 227 txn1.commit(); 228 fail("Failure to detect mismatched transaction begin/commit"); 229 } 230 catch (RuntimeException e) 231 { 232 } 234 txn2.commit(); 235 txn1.commit(); 236 237 checkNoStatusOnThread(); 238 } 239 240 245 private static class DummyTransactionManager extends AbstractPlatformTransactionManager 246 { 247 private int status = Status.STATUS_NO_TRANSACTION; 248 private Object txn = new Object (); 249 250 253 public int getStatus() 254 { 255 return status; 256 } 257 258 protected void doBegin(Object arg0, TransactionDefinition arg1) 259 { 260 status = Status.STATUS_ACTIVE; 261 } 262 263 protected void doCommit(DefaultTransactionStatus arg0) 264 { 265 status = Status.STATUS_COMMITTED; 266 } 267 268 protected Object doGetTransaction() 269 { 270 return txn; 271 } 272 273 protected void doRollback(DefaultTransactionStatus arg0) 274 { 275 status = Status.STATUS_ROLLEDBACK; 276 } 277 } 278 } 279 | Popular Tags |