use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class RequestScopedProxyTests method testGetFromScopeThroughDynamicProxy.
@Test
public void testGetFromScopeThroughDynamicProxy() {
String name = "requestScopedProxy";
ITestBean bean = (ITestBean) this.beanFactory.getBean(name);
// assertTrue(AopUtils.isJdkDynamicProxy(bean));
MockHttpServletRequest request = new MockHttpServletRequest();
RequestAttributes requestAttributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(requestAttributes);
try {
assertThat(request.getAttribute("scopedTarget." + name)).isNull();
assertThat(bean.getName()).isEqualTo("scoped");
assertThat(request.getAttribute("scopedTarget." + name)).isNotNull();
TestBean target = (TestBean) request.getAttribute("scopedTarget." + name);
assertThat(target.getClass()).isEqualTo(TestBean.class);
assertThat(target.getName()).isEqualTo("scoped");
assertThat(this.beanFactory.getBean(name)).isSameAs(bean);
assertThat(target.toString()).isEqualTo(bean.toString());
} finally {
RequestContextHolder.setRequestAttributes(null);
}
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class TxNamespaceHandlerTests method invokeTransactional.
@Test
public void invokeTransactional() {
ITestBean testBean = getTestBean();
CallCountingTransactionManager ptm = (CallCountingTransactionManager) context.getBean("transactionManager");
// try with transactional
assertThat(ptm.begun).as("Should not have any started transactions").isEqualTo(0);
testBean.getName();
assertThat(ptm.lastDefinition.isReadOnly()).isTrue();
assertThat(ptm.lastDefinition.getTimeout()).isEqualTo(5);
assertThat(ptm.begun).as("Should have 1 started transaction").isEqualTo(1);
assertThat(ptm.commits).as("Should have 1 committed transaction").isEqualTo(1);
// try with non-transaction
testBean.haveBirthday();
assertThat(ptm.begun).as("Should not have started another transaction").isEqualTo(1);
// try with exceptional
assertThatExceptionOfType(Throwable.class).isThrownBy(() -> testBean.exceptional(new IllegalArgumentException("foo")));
assertThat(ptm.begun).as("Should have another started transaction").isEqualTo(2);
assertThat(ptm.rollbacks).as("Should have 1 rolled back transaction").isEqualTo(1);
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class TxNamespaceHandlerTests method isProxy.
@Test
public void isProxy() {
ITestBean bean = getTestBean();
assertThat(AopUtils.isAopProxy(bean)).as("testBean is not a proxy").isTrue();
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class AbstractTransactionAspectTests method programmaticRollback.
/**
* Test that TransactionStatus.setRollbackOnly works.
*/
@Test
public void programmaticRollback() throws Exception {
TransactionAttribute txatt = new DefaultTransactionAttribute();
Method m = getNameMethod;
MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
tas.register(m, txatt);
TransactionStatus status = mock(TransactionStatus.class);
PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
given(ptm.getTransaction(txatt)).willReturn(status);
final String name = "jenny";
TestBean tb = new TestBean() {
@Override
public String getName() {
TransactionStatus txStatus = TransactionInterceptor.currentTransactionStatus();
txStatus.setRollbackOnly();
return name;
}
};
ITestBean itb = (ITestBean) advised(tb, ptm, tas);
// verification!?
assertThat(name.equals(itb.getName())).isTrue();
verify(ptm).commit(status);
}
use of org.springframework.beans.testfixture.beans.ITestBean in project spring-framework by spring-projects.
the class AbstractTransactionAspectTests method doTestRollbackOnException.
/**
* 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
*/
@SuppressWarnings("serial")
protected void doTestRollbackOnException(final Exception ex, final boolean shouldRollback, boolean rollbackException) throws Exception {
TransactionAttribute txatt = new DefaultTransactionAttribute() {
@Override
public boolean rollbackOn(Throwable t) {
assertThat(t == ex).isTrue();
return shouldRollback;
}
};
Method m = exceptionalMethod;
MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
tas.register(m, txatt);
TransactionStatus status = mock(TransactionStatus.class);
PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
// Gets additional call(s) from TransactionControl
given(ptm.getTransaction(txatt)).willReturn(status);
TransactionSystemException tex = new TransactionSystemException("system exception");
if (rollbackException) {
if (shouldRollback) {
willThrow(tex).given(ptm).rollback(status);
} else {
willThrow(tex).given(ptm).commit(status);
}
}
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) {
assertThat(t).as("Caught wrong exception").isEqualTo(tex);
} else {
assertThat(t).as("Caught wrong exception").isEqualTo(ex);
}
}
if (!rollbackException) {
if (shouldRollback) {
verify(ptm).rollback(status);
} else {
verify(ptm).commit(status);
}
}
}
Aggregations