use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class AbstractTransactionAspectTests method noTransaction.
@Test
public void noTransaction() throws Exception {
PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
TestBean tb = new TestBean();
TransactionAttributeSource tas = new MapTransactionAttributeSource();
// All the methods in this class use the advised() template method
// to obtain a transaction object, configured with the given PlatformTransactionManager
// and transaction attribute source
ITestBean itb = (ITestBean) advised(tb, ptm, tas);
checkTransactionStatus(false);
itb.getName();
checkTransactionStatus(false);
// expect no calls
verifyNoInteractions(ptm);
}
use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class AbstractTransactionAspectTests method enclosingTransactionWithNonTransactionMethodOnAdvisedInside.
@Test
public void enclosingTransactionWithNonTransactionMethodOnAdvisedInside() throws Throwable {
TransactionAttribute txatt = new DefaultTransactionAttribute();
MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
tas.register(exceptionalMethod, txatt);
TransactionStatus status = mock(TransactionStatus.class);
PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
// Expect a transaction
given(ptm.getTransaction(txatt)).willReturn(status);
final String spouseName = "innerName";
TestBean outer = new TestBean() {
@Override
public void exceptional(Throwable t) throws Throwable {
TransactionInfo ti = TransactionAspectSupport.currentTransactionInfo();
assertThat(ti.hasTransaction()).isTrue();
assertThat(getSpouse().getName()).isEqualTo(spouseName);
}
};
TestBean inner = new TestBean() {
@Override
public String getName() {
// Assert that we're in the inner proxy
TransactionInfo ti = TransactionAspectSupport.currentTransactionInfo();
assertThat(ti.hasTransaction()).isFalse();
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);
verify(ptm).commit(status);
}
use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class SimpleTransactionScopeTests method getFromScope.
@Test
@SuppressWarnings("resource")
public void getFromScope() throws Exception {
GenericApplicationContext context = new GenericApplicationContext();
context.getBeanFactory().registerScope("tx", new SimpleTransactionScope());
GenericBeanDefinition bd1 = new GenericBeanDefinition();
bd1.setBeanClass(TestBean.class);
bd1.setScope("tx");
bd1.setPrimary(true);
context.registerBeanDefinition("txScopedObject1", bd1);
GenericBeanDefinition bd2 = new GenericBeanDefinition();
bd2.setBeanClass(DerivedTestBean.class);
bd2.setScope("tx");
context.registerBeanDefinition("txScopedObject2", bd2);
context.refresh();
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> context.getBean(TestBean.class)).withCauseInstanceOf(IllegalStateException.class);
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> context.getBean(DerivedTestBean.class)).withCauseInstanceOf(IllegalStateException.class);
TestBean bean1 = null;
DerivedTestBean bean2 = null;
DerivedTestBean bean2a = null;
DerivedTestBean bean2b = null;
TransactionSynchronizationManager.initSynchronization();
try {
bean1 = context.getBean(TestBean.class);
assertThat(context.getBean(TestBean.class)).isSameAs(bean1);
bean2 = context.getBean(DerivedTestBean.class);
assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2);
context.getBeanFactory().destroyScopedBean("txScopedObject2");
assertThat(TransactionSynchronizationManager.hasResource("txScopedObject2")).isFalse();
assertThat(bean2.wasDestroyed()).isTrue();
bean2a = context.getBean(DerivedTestBean.class);
assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2a);
assertThat(bean2a).isNotSameAs(bean2);
context.getBeanFactory().getRegisteredScope("tx").remove("txScopedObject2");
assertThat(TransactionSynchronizationManager.hasResource("txScopedObject2")).isFalse();
assertThat(bean2a.wasDestroyed()).isFalse();
bean2b = context.getBean(DerivedTestBean.class);
assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2b);
assertThat(bean2b).isNotSameAs(bean2);
assertThat(bean2b).isNotSameAs(bean2a);
} finally {
TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_COMMITTED);
TransactionSynchronizationManager.clearSynchronization();
}
assertThat(bean2a.wasDestroyed()).isFalse();
assertThat(bean2b.wasDestroyed()).isTrue();
assertThat(TransactionSynchronizationManager.getResourceMap().isEmpty()).isTrue();
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> context.getBean(TestBean.class)).withCauseInstanceOf(IllegalStateException.class);
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> context.getBean(DerivedTestBean.class)).withCauseInstanceOf(IllegalStateException.class);
}
use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class SimpleTransactionScopeTests method getWithTransactionManager.
@Test
public void getWithTransactionManager() throws Exception {
try (GenericApplicationContext context = new GenericApplicationContext()) {
context.getBeanFactory().registerScope("tx", new SimpleTransactionScope());
GenericBeanDefinition bd1 = new GenericBeanDefinition();
bd1.setBeanClass(TestBean.class);
bd1.setScope("tx");
bd1.setPrimary(true);
context.registerBeanDefinition("txScopedObject1", bd1);
GenericBeanDefinition bd2 = new GenericBeanDefinition();
bd2.setBeanClass(DerivedTestBean.class);
bd2.setScope("tx");
context.registerBeanDefinition("txScopedObject2", bd2);
context.refresh();
CallCountingTransactionManager tm = new CallCountingTransactionManager();
TransactionTemplate tt = new TransactionTemplate(tm);
Set<DerivedTestBean> finallyDestroy = new HashSet<>();
tt.execute(status -> {
TestBean bean1 = context.getBean(TestBean.class);
assertThat(context.getBean(TestBean.class)).isSameAs(bean1);
DerivedTestBean bean2 = context.getBean(DerivedTestBean.class);
assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2);
context.getBeanFactory().destroyScopedBean("txScopedObject2");
assertThat(TransactionSynchronizationManager.hasResource("txScopedObject2")).isFalse();
assertThat(bean2.wasDestroyed()).isTrue();
DerivedTestBean bean2a = context.getBean(DerivedTestBean.class);
assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2a);
assertThat(bean2a).isNotSameAs(bean2);
context.getBeanFactory().getRegisteredScope("tx").remove("txScopedObject2");
assertThat(TransactionSynchronizationManager.hasResource("txScopedObject2")).isFalse();
assertThat(bean2a.wasDestroyed()).isFalse();
DerivedTestBean bean2b = context.getBean(DerivedTestBean.class);
finallyDestroy.add(bean2b);
assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2b);
assertThat(bean2b).isNotSameAs(bean2);
assertThat(bean2b).isNotSameAs(bean2a);
Set<DerivedTestBean> immediatelyDestroy = new HashSet<>();
TransactionTemplate tt2 = new TransactionTemplate(tm);
tt2.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRED);
tt2.execute(status2 -> {
DerivedTestBean bean2c = context.getBean(DerivedTestBean.class);
immediatelyDestroy.add(bean2c);
assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2c);
assertThat(bean2c).isNotSameAs(bean2);
assertThat(bean2c).isNotSameAs(bean2a);
assertThat(bean2c).isNotSameAs(bean2b);
return null;
});
assertThat(immediatelyDestroy.iterator().next().wasDestroyed()).isTrue();
assertThat(bean2b.wasDestroyed()).isFalse();
return null;
});
assertThat(finallyDestroy.iterator().next().wasDestroyed()).isTrue();
}
}
use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class AopNamespaceHandlerScopeIntegrationTests method testSessionScoping.
@Test
void testSessionScoping() throws Exception {
MockHttpSession oldSession = new MockHttpSession();
MockHttpSession newSession = new MockHttpSession();
MockHttpServletRequest request = new MockHttpServletRequest();
request.setSession(oldSession);
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
assertThat(AopUtils.isAopProxy(sessionScoped)).as("Should be AOP proxy").isTrue();
boolean condition1 = sessionScoped instanceof TestBean;
assertThat(condition1).as("Should not be target class proxy").isFalse();
assertThat(sessionScopedAlias).isSameAs(sessionScoped);
assertThat(AopUtils.isAopProxy(testBean)).as("Should be AOP proxy").isTrue();
boolean condition = testBean instanceof TestBean;
assertThat(condition).as("Regular bean should be JDK proxy").isFalse();
String rob = "Rob Harrop";
String bram = "Bram Smeets";
assertThat(sessionScoped.getName()).isEqualTo(rob);
sessionScoped.setName(bram);
request.setSession(newSession);
assertThat(sessionScoped.getName()).isEqualTo(rob);
request.setSession(oldSession);
assertThat(sessionScoped.getName()).isEqualTo(bram);
assertThat(((Advised) sessionScoped).getAdvisors().length > 0).as("Should have advisors").isTrue();
}
Aggregations