use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class RequestScopeTests method innerBeanInheritsContainingBeanScopeByDefault.
@Test
public void innerBeanInheritsContainingBeanScopeByDefault() {
MockHttpServletRequest request = new MockHttpServletRequest();
ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(requestAttributes);
String outerBeanName = "requestScopedOuterBean";
assertThat(request.getAttribute(outerBeanName)).isNull();
TestBean outer1 = (TestBean) this.beanFactory.getBean(outerBeanName);
assertThat(request.getAttribute(outerBeanName)).isNotNull();
TestBean inner1 = (TestBean) outer1.getSpouse();
assertThat(this.beanFactory.getBean(outerBeanName)).isSameAs(outer1);
requestAttributes.requestCompleted();
assertThat(outer1.wasDestroyed()).isTrue();
assertThat(inner1.wasDestroyed()).isTrue();
request = new MockHttpServletRequest();
requestAttributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(requestAttributes);
TestBean outer2 = (TestBean) this.beanFactory.getBean(outerBeanName);
assertThat(outer2).isNotSameAs(outer1);
assertThat(outer2.getSpouse()).isNotSameAs(inner1);
}
use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class RequestScopeTests method getFromScope.
@Test
public void getFromScope() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setContextPath("/path");
RequestAttributes requestAttributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(requestAttributes);
String name = "requestScopedObject";
assertThat(request.getAttribute(name)).isNull();
TestBean bean = (TestBean) this.beanFactory.getBean(name);
assertThat(bean.getName()).isEqualTo("/path");
assertThat(request.getAttribute(name)).isSameAs(bean);
assertThat(this.beanFactory.getBean(name)).isSameAs(bean);
}
use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class RequestScopeTests method requestScopedInnerBeanDestroyedWhileContainedBySingleton.
@Test
public void requestScopedInnerBeanDestroyedWhileContainedBySingleton() {
MockHttpServletRequest request = new MockHttpServletRequest();
ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
RequestContextHolder.setRequestAttributes(requestAttributes);
String outerBeanName = "singletonOuterBean";
TestBean outer1 = (TestBean) this.beanFactory.getBean(outerBeanName);
assertThat(request.getAttribute(outerBeanName)).isNull();
TestBean inner1 = (TestBean) outer1.getSpouse();
TestBean outer2 = (TestBean) this.beanFactory.getBean(outerBeanName);
assertThat(outer2).isSameAs(outer1);
assertThat(outer2.getSpouse()).isSameAs(inner1);
requestAttributes.requestCompleted();
assertThat(inner1.wasDestroyed()).isTrue();
assertThat(outer1.wasDestroyed()).isFalse();
}
use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class SessionAttributesHandlerTests method cleanupAttributes.
@Test
public void cleanupAttributes() throws Exception {
sessionAttributeStore.storeAttribute(request, "attr1", "value1");
sessionAttributeStore.storeAttribute(request, "attr2", "value2");
sessionAttributeStore.storeAttribute(request, "attr3", new TestBean());
sessionAttributesHandler.cleanupAttributes(request);
assertThat(sessionAttributeStore.retrieveAttribute(request, "attr1")).isNull();
assertThat(sessionAttributeStore.retrieveAttribute(request, "attr2")).isNull();
assertThat(sessionAttributeStore.retrieveAttribute(request, "attr3")).isNotNull();
// Resolve 'attr3' by type
sessionAttributesHandler.isHandlerSessionAttribute("attr3", TestBean.class);
sessionAttributesHandler.cleanupAttributes(request);
assertThat(sessionAttributeStore.retrieveAttribute(request, "attr3")).isNull();
}
use of org.springframework.beans.testfixture.beans.TestBean in project spring-framework by spring-projects.
the class SessionAttributesHandlerTests method storeAttributes.
@Test
public void storeAttributes() throws Exception {
ModelMap model = new ModelMap();
model.put("attr1", "value1");
model.put("attr2", "value2");
model.put("attr3", new TestBean());
sessionAttributesHandler.storeAttributes(request, model);
assertThat(sessionAttributeStore.retrieveAttribute(request, "attr1")).isEqualTo("value1");
assertThat(sessionAttributeStore.retrieveAttribute(request, "attr2")).isEqualTo("value2");
boolean condition = sessionAttributeStore.retrieveAttribute(request, "attr3") instanceof TestBean;
assertThat(condition).isTrue();
}
Aggregations