use of org.springframework.beans.testfixture.beans.DerivedTestBean 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.DerivedTestBean in project spring-framework by spring-projects.
the class BeanFactoryTransactionTests method testGetsAreNotTransactionalWithProxyFactory3.
@Test
public void testGetsAreNotTransactionalWithProxyFactory3() {
ITestBean testBean = (ITestBean) factory.getBean("proxyFactory3");
boolean condition = testBean instanceof DerivedTestBean;
assertThat(condition).as("testBean is a full proxy").isTrue();
boolean condition1 = testBean instanceof TransactionalProxy;
assertThat(condition1).isTrue();
InvocationCounterPointcut txnCounter = (InvocationCounterPointcut) factory.getBean("txnInvocationCounterPointcut");
InvocationCounterInterceptor preCounter = (InvocationCounterInterceptor) factory.getBean("preInvocationCounterInterceptor");
InvocationCounterInterceptor postCounter = (InvocationCounterInterceptor) factory.getBean("postInvocationCounterInterceptor");
txnCounter.counter = 0;
preCounter.counter = 0;
postCounter.counter = 0;
doTestGetsAreNotTransactional(testBean);
// Can't assert it's equal to 4 as the pointcut may be optimized and only invoked once
assertThat(0 < txnCounter.counter && txnCounter.counter <= 4).isTrue();
assertThat(preCounter.counter).isEqualTo(4);
assertThat(postCounter.counter).isEqualTo(4);
}
use of org.springframework.beans.testfixture.beans.DerivedTestBean in project spring-framework by spring-projects.
the class WebApplicationContextScopeTests method testRequestScope.
@Test
public void testRequestScope() {
WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_REQUEST);
MockHttpServletRequest request = new MockHttpServletRequest();
ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(requestAttributes);
try {
assertThat(request.getAttribute(NAME)).isNull();
DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class);
assertThat(request.getAttribute(NAME)).isSameAs(bean);
assertThat(ac.getBean(NAME)).isSameAs(bean);
requestAttributes.requestCompleted();
assertThat(bean.wasDestroyed()).isTrue();
} finally {
RequestContextHolder.setRequestAttributes(null);
}
}
use of org.springframework.beans.testfixture.beans.DerivedTestBean in project spring-framework by spring-projects.
the class RequestScopeTests method destructionAtRequestCompletion.
@Test
public void destructionAtRequestCompletion() {
MockHttpServletRequest request = new MockHttpServletRequest();
ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(requestAttributes);
String name = "requestScopedDisposableObject";
assertThat(request.getAttribute(name)).isNull();
DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name);
assertThat(request.getAttribute(name)).isSameAs(bean);
assertThat(this.beanFactory.getBean(name)).isSameAs(bean);
requestAttributes.requestCompleted();
assertThat(bean.wasDestroyed()).isTrue();
}
use of org.springframework.beans.testfixture.beans.DerivedTestBean in project spring-framework by spring-projects.
the class DataBinderTests method testDirectBindingToEmptyIndexedFieldWithRegisteredGenericEditor.
@Test
void testDirectBindingToEmptyIndexedFieldWithRegisteredGenericEditor() {
IndexedTestBean tb = new IndexedTestBean();
DataBinder binder = new DataBinder(tb, "tb");
binder.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
DerivedTestBean tb = new DerivedTestBean();
tb.setName("array" + text);
setValue(tb);
}
@Override
public String getAsText() {
return ((TestBean) getValue()).getName();
}
});
Errors errors = binder.getBindingResult();
errors.rejectValue("map[key0]", "NOT_NULL", "should not be null");
assertThat(errors.getFieldErrorCount("map[key0]")).isEqualTo(1);
assertThat(errors.getFieldError("map[key0]").getCode()).isEqualTo("NOT_NULL");
assertThat(errors.getFieldError("map[key0]").getCodes()[0]).isEqualTo("NOT_NULL.tb.map[key0]");
assertThat(errors.getFieldError("map[key0]").getCodes()[1]).isEqualTo("NOT_NULL.tb.map");
assertThat(errors.getFieldError("map[key0]").getCodes()[2]).isEqualTo("NOT_NULL.map[key0]");
assertThat(errors.getFieldError("map[key0]").getCodes()[3]).isEqualTo("NOT_NULL.map");
// This next code is only generated because of the registered editor, using the
// registered type of the editor as guess for the content type of the collection.
assertThat(errors.getFieldError("map[key0]").getCodes()[4]).isEqualTo("NOT_NULL.org.springframework.beans.testfixture.beans.TestBean");
assertThat(errors.getFieldError("map[key0]").getCodes()[5]).isEqualTo("NOT_NULL");
}
Aggregations