1 16 17 package org.springframework.test; 18 19 import org.springframework.transaction.PlatformTransactionManager; 20 import org.springframework.transaction.TransactionDefinition; 21 import org.springframework.transaction.TransactionException; 22 import org.springframework.transaction.TransactionStatus; 23 import org.springframework.transaction.support.DefaultTransactionDefinition; 24 25 75 public abstract class AbstractTransactionalSpringContextTests extends AbstractDependencyInjectionSpringContextTests { 76 77 78 protected PlatformTransactionManager transactionManager; 79 80 81 private boolean defaultRollback = true; 82 83 84 private boolean complete = false; 85 86 87 private int transactionsStarted = 0; 88 89 93 protected TransactionDefinition transactionDefinition = new DefaultTransactionDefinition(); 94 95 98 protected TransactionStatus transactionStatus; 99 100 101 104 public AbstractTransactionalSpringContextTests() { 105 } 106 107 110 public AbstractTransactionalSpringContextTests(String name) { 111 super(name); 112 } 113 114 115 121 public void setTransactionManager(PlatformTransactionManager transactionManager) { 122 this.transactionManager = transactionManager; 123 } 124 125 129 public void setDefaultRollback(boolean defaultRollback) { 130 this.defaultRollback = defaultRollback; 131 } 132 133 134 138 protected void preventTransaction() { 139 this.transactionDefinition = null; 140 } 141 142 148 protected void setTransactionDefinition(TransactionDefinition customDefinition) { 149 this.transactionDefinition = customDefinition; 150 } 151 152 153 158 protected final void onSetUp() throws Exception { 159 this.complete = !this.defaultRollback; 160 161 if (this.transactionManager == null) { 162 logger.info("No transaction manager set: test will NOT run within a transaction"); 163 } 164 else if (this.transactionDefinition == null) { 165 logger.info("No transaction definition set: test will NOT run within a transaction"); 166 } 167 else { 168 onSetUpBeforeTransaction(); 169 startNewTransaction(); 170 try { 171 onSetUpInTransaction(); 172 } 173 catch (Exception ex) { 174 endTransaction(); 175 throw ex; 176 } 177 } 178 } 179 180 188 protected void onSetUpBeforeTransaction() throws Exception { 189 } 190 191 203 protected void onSetUpInTransaction() throws Exception { 204 } 205 206 207 217 protected final void onTearDown() throws Exception { 218 if (this.transactionStatus != null && !this.transactionStatus.isCompleted()) { 220 try { 221 onTearDownInTransaction(); 222 } 223 finally { 224 endTransaction(); 225 } 226 } 227 if (this.transactionsStarted > 0) { 230 onTearDownAfterTransaction(); 231 } 232 } 233 234 243 protected void onTearDownInTransaction() throws Exception { 244 } 245 246 251 protected void onTearDownAfterTransaction() throws Exception { 252 } 253 254 255 261 protected void setComplete() { 262 if (this.transactionManager == null) { 263 throw new IllegalStateException ("No transaction manager set"); 264 } 265 this.complete = true; 266 } 267 268 276 protected void endTransaction() { 277 if (this.transactionStatus != null) { 278 try { 279 if (!this.complete) { 280 this.transactionManager.rollback(this.transactionStatus); 281 logger.info("Rolled back transaction after test execution"); 282 } 283 else { 284 this.transactionManager.commit(this.transactionStatus); 285 logger.info("Committed transaction after test execution"); 286 } 287 } 288 finally { 289 this.transactionStatus = null; 290 } 291 } 292 } 293 294 300 protected void startNewTransaction() throws TransactionException { 301 if (this.transactionStatus != null) { 302 throw new IllegalStateException ("Cannot start new transaction without ending existing transaction: " + 303 "Invoke endTransaction() before startNewTransaction()"); 304 } 305 if (this.transactionManager == null) { 306 throw new IllegalStateException ("No transaction manager set"); 307 } 308 309 this.transactionStatus = this.transactionManager.getTransaction(this.transactionDefinition); 310 ++this.transactionsStarted; 311 this.complete = !this.defaultRollback; 312 313 if (logger.isInfoEnabled()) { 314 logger.info("Began transaction (" + this.transactionsStarted + "): transaction manager [" + 315 this.transactionManager + "]; default rollback = " + this.defaultRollback); 316 } 317 } 318 319 } 320 | Popular Tags |