Search in sources :

Example 26 with DerivedTestBean

use of org.springframework.beans.testfixture.beans.DerivedTestBean in project spring-framework by spring-projects.

the class SessionScopeTests method doTestDestructionWithSessionSerialization.

private void doTestDestructionWithSessionSerialization(boolean beanNameReset) throws Exception {
    Serializable serializedState = null;
    MockHttpSession session = new MockHttpSession();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(requestAttributes);
    String name = "sessionScopedDisposableObject";
    assertThat(session.getAttribute(name)).isNull();
    DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name);
    assertThat(bean).isEqualTo(session.getAttribute(name));
    assertThat(this.beanFactory.getBean(name)).isSameAs(bean);
    requestAttributes.requestCompleted();
    serializedState = session.serializeState();
    assertThat(bean.wasDestroyed()).isFalse();
    serializedState = SerializationTestUtils.serializeAndDeserialize(serializedState);
    session = new MockHttpSession();
    session.deserializeState(serializedState);
    request = new MockHttpServletRequest();
    request.setSession(session);
    requestAttributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(requestAttributes);
    name = "sessionScopedDisposableObject";
    assertThat(session.getAttribute(name)).isNotNull();
    bean = (DerivedTestBean) this.beanFactory.getBean(name);
    assertThat(bean).isEqualTo(session.getAttribute(name));
    assertThat(this.beanFactory.getBean(name)).isSameAs(bean);
    requestAttributes.requestCompleted();
    session.invalidate();
    assertThat(bean.wasDestroyed()).isTrue();
    if (beanNameReset) {
        assertThat(bean.getBeanName()).isNull();
    } else {
        assertThat(bean.getBeanName()).isNotNull();
    }
}
Also used : Serializable(java.io.Serializable) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpSession(org.springframework.web.testfixture.servlet.MockHttpSession) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean)

Example 27 with DerivedTestBean

use of org.springframework.beans.testfixture.beans.DerivedTestBean in project spring-framework by spring-projects.

the class SessionScopeTests method destructionAtSessionTermination.

@Test
public void destructionAtSessionTermination() throws Exception {
    MockHttpSession session = new MockHttpSession();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(requestAttributes);
    String name = "sessionScopedDisposableObject";
    assertThat(session.getAttribute(name)).isNull();
    DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name);
    assertThat(bean).isEqualTo(session.getAttribute(name));
    assertThat(this.beanFactory.getBean(name)).isSameAs(bean);
    requestAttributes.requestCompleted();
    session.invalidate();
    assertThat(bean.wasDestroyed()).isTrue();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpSession(org.springframework.web.testfixture.servlet.MockHttpSession) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) Test(org.junit.jupiter.api.Test)

Aggregations

DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)27 Test (org.junit.jupiter.api.Test)26 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)7 TestBean (org.springframework.beans.testfixture.beans.TestBean)7 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)6 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)6 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)6 PropertyEditorSupport (java.beans.PropertyEditorSupport)4 WebApplicationContext (org.springframework.web.context.WebApplicationContext)3 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)2 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)2 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)2 ResourceTestBean (org.springframework.tests.sample.beans.ResourceTestBean)2 MockHttpSession (org.springframework.web.testfixture.servlet.MockHttpSession)2 ServletContextEvent (jakarta.servlet.ServletContextEvent)1 InputStream (java.io.InputStream)1 Serializable (java.io.Serializable)1