Search in sources :

Example 41 with TestBean

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);
}
Also used : DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) CountingTestBean(org.springframework.beans.testfixture.beans.CountingTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 42 with TestBean

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);
}
Also used : DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) CountingTestBean(org.springframework.beans.testfixture.beans.CountingTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 43 with TestBean

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();
}
Also used : DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) CountingTestBean(org.springframework.beans.testfixture.beans.CountingTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 44 with TestBean

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();
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) Test(org.junit.jupiter.api.Test)

Example 45 with TestBean

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();
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) ModelMap(org.springframework.ui.ModelMap) Test(org.junit.jupiter.api.Test)

Aggregations

TestBean (org.springframework.beans.testfixture.beans.TestBean)808 Test (org.junit.jupiter.api.Test)765 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)472 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)268 NestedTestBean (org.springframework.beans.testfixture.beans.NestedTestBean)183 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)167 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)162 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)118 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)96 BooleanTestBean (org.springframework.beans.testfixture.beans.BooleanTestBean)40 NumberTestBean (org.springframework.beans.testfixture.beans.NumberTestBean)40 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)39 Properties (java.util.Properties)38 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)37 HashMap (java.util.HashMap)36 Errors (org.springframework.validation.Errors)35 PropertyEditorSupport (java.beans.PropertyEditorSupport)34 SerializableNopInterceptor (org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor)29 List (java.util.List)28 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)28