📄 abstracttransactionaspecttests.java
字号:
// Has nested transaction
assertTrue(ti.hasTransaction());
assertEquals(innerTxatt, ti.getTransactionAttribute());
return spouseName;
}
};
ITestBean outerProxy = (ITestBean) advised(outer, ptm, tas);
ITestBean innerProxy = (ITestBean) advised(inner, ptm, tas);
outer.setSpouse(innerProxy);
checkTransactionStatus(false);
// Will invoke inner.getName, which is non-transactional
outerProxy.exceptional(null);
checkTransactionStatus(false);
ptxControl.verify();
}
public void testRollbackOnCheckedException() throws Throwable {
doTestRollbackOnException(new Exception(), true, false);
}
public void testNoRollbackOnCheckedException() throws Throwable {
doTestRollbackOnException(new Exception(), false, false);
}
public void testRollbackOnUncheckedException() throws Throwable {
doTestRollbackOnException(new RuntimeException(), true, false);
}
public void testNoRollbackOnUncheckedException() throws Throwable {
doTestRollbackOnException(new RuntimeException(), false, false);
}
public void testRollbackOnCheckedExceptionWithRollbackException() throws Throwable {
doTestRollbackOnException(new Exception(), true, true);
}
public void testNoRollbackOnCheckedExceptionWithRollbackException() throws Throwable {
doTestRollbackOnException(new Exception(), false, true);
}
public void testRollbackOnUncheckedExceptionWithRollbackException() throws Throwable {
doTestRollbackOnException(new RuntimeException(), true, true);
}
public void testNoRollbackOnUncheckedExceptionWithRollbackException() throws Throwable {
doTestRollbackOnException(new RuntimeException(), false, true);
}
/**
* Check that the given exception thrown by the target can produce the
* desired behavior with the appropriate transaction attribute.
* @param ex exception to be thrown by the target
* @param shouldRollback whether this should cause a transaction rollback
*/
protected void doTestRollbackOnException(final Exception ex, final boolean shouldRollback,
boolean rollbackException) throws Exception {
TransactionAttribute txatt = new DefaultTransactionAttribute() {
public boolean rollbackOn(Throwable t) {
assertTrue(t == ex);
return shouldRollback;
}
};
Method m = exceptionalMethod;
MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
tas.register(m, txatt);
TransactionStatus status = new DefaultTransactionStatus(null, false, false, false, false, null);
MockControl ptxControl = MockControl.createControl(PlatformTransactionManager.class);
PlatformTransactionManager ptm = (PlatformTransactionManager) ptxControl.getMock();
// Gets additional call(s) from TransactionControl
ptm.getTransaction(txatt);
ptxControl.setReturnValue(status, 1);
if (shouldRollback) {
ptm.rollback(status);
}
else {
ptm.commit(status);
}
TransactionSystemException tex = new TransactionSystemException("system exception");
if (rollbackException) {
ptxControl.setThrowable(tex, 1);
}
else {
ptxControl.setVoidCallable(1);
}
ptxControl.replay();
TestBean tb = new TestBean();
ITestBean itb = (ITestBean) advised(tb, ptm, tas);
try {
itb.exceptional(ex);
fail("Should have thrown exception");
}
catch (Throwable t) {
if (rollbackException) {
assertEquals("Caught wrong exception", tex, t );
}
else {
assertEquals("Caught wrong exception", ex, t);
}
}
ptxControl.verify();
}
/**
* Test that TransactionControl.setRollbackOnly works
* @throws java.lang.Exception
*/
public void testProgrammaticRollback() throws Exception {
TransactionAttribute txatt = new DefaultTransactionAttribute();
Method m = getNameMethod;
MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
tas.register(m, txatt);
TransactionStatus status = transactionStatusForNewTransaction();
assertTrue(status.isNewTransaction());
MockControl ptxControl = MockControl.createControl(PlatformTransactionManager.class);
PlatformTransactionManager ptm = (PlatformTransactionManager) ptxControl.getMock();
ptm.getTransaction(txatt);
ptxControl.setReturnValue(status, 1);
ptm.commit(status);
ptxControl.setVoidCallable(1);
ptxControl.replay();
final String name = "jenny";
TestBean tb = new TestBean() {
public String getName() {
TransactionStatus txStatus = TransactionInterceptor.currentTransactionStatus();
txStatus.setRollbackOnly();
return name;
}
};
ITestBean itb = (ITestBean) advised(tb, ptm, tas);
// verification!?
assertTrue(name.equals(itb.getName()));
ptxControl.verify();
}
/**
* @return a TransactionStatus object configured
* for a new transaction
*/
private TransactionStatus transactionStatusForNewTransaction() {
return new DefaultTransactionStatus(new Object(), true, false, false, false, null);
}
/**
* Simulate a transaction infrastructure failure.
* Shouldn't invoke target method.
* @throws java.lang.Exception
*/
public void testCannotCreateTransaction() throws Exception {
TransactionAttribute txatt = new DefaultTransactionAttribute();
Method m = getNameMethod;
MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
tas.register(m, txatt);
MockControl ptxControl = MockControl.createControl(PlatformTransactionManager.class);
PlatformTransactionManager ptm = (PlatformTransactionManager) ptxControl.getMock();
// Expect a transaction
ptm.getTransaction(txatt);
CannotCreateTransactionException ex = new CannotCreateTransactionException("foobar", null);
ptxControl.setThrowable(ex);
ptxControl.replay();
TestBean tb = new TestBean() {
public String getName() {
throw new UnsupportedOperationException("Shouldn't have invoked target method when couldn't create transaction for transactional method");
}
};
ITestBean itb = (ITestBean) advised(tb, ptm, tas);
try {
itb.getName();
fail("Shouldn't have invoked method");
}
catch (CannotCreateTransactionException thrown) {
assertTrue(thrown == ex);
}
ptxControl.verify();
}
/**
* Simulate failure of the underlying transaction infrastructure to commit.
* Check that the target method was invoked, but that the transaction
* infrastructure exception was thrown to the client
*/
public void testCannotCommitTransaction() throws Exception {
TransactionAttribute txatt = new DefaultTransactionAttribute();
Method m = setNameMethod;
MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
tas.register(m, txatt);
Method m2 = getNameMethod;
// No attributes for m2
MockControl ptxControl = MockControl.createControl(PlatformTransactionManager.class);
PlatformTransactionManager ptm = (PlatformTransactionManager) ptxControl.getMock();
TransactionStatus status = transactionStatusForNewTransaction();
ptm.getTransaction(txatt);
ptxControl.setReturnValue(status);
UnexpectedRollbackException ex = new UnexpectedRollbackException("foobar", null);
ptm.commit(status);
ptxControl.setThrowable(ex);
ptxControl.replay();
TestBean tb = new TestBean();
ITestBean itb = (ITestBean) advised(tb, ptm, tas);
String name = "new name";
try {
itb.setName(name);
fail("Shouldn't have succeeded");
}
catch (UnexpectedRollbackException thrown) {
assertTrue(thrown == ex);
}
// Should have invoked target and changed name
assertTrue(itb.getName() == name);
ptxControl.verify();
}
protected void checkTransactionStatus(boolean expected) {
try {
TransactionInterceptor.currentTransactionStatus();
if (!expected) {
fail("Should have thrown NoTransactionException");
}
}
catch (NoTransactionException ex) {
if (expected) {
fail("Should have current TransactionStatus");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -