Search in sources :

Example 11 with MockHttpSession

use of org.springframework.web.testfixture.servlet.MockHttpSession in project spring-framework by spring-projects.

the class SessionScopeTests method getFromScopeWithSingleAccess.

@Test
public void getFromScopeWithSingleAccess() throws Exception {
    AtomicInteger count = new AtomicInteger();
    MockHttpSession session = new MockHttpSession() {

        @Override
        public void setAttribute(String name, Object value) {
            super.setAttribute(name, value);
            count.incrementAndGet();
        }
    };
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(requestAttributes);
    String name = "sessionScopedObject";
    assertThat(session.getAttribute(name)).isNull();
    TestBean bean = (TestBean) this.beanFactory.getBean(name);
    assertThat(count.intValue()).isEqualTo(1);
    // should re-propagate updated attribute
    requestAttributes.requestCompleted();
    assertThat(bean).isEqualTo(session.getAttribute(name));
    assertThat(count.intValue()).isEqualTo(2);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpSession(org.springframework.web.testfixture.servlet.MockHttpSession) Test(org.junit.jupiter.api.Test)

Example 12 with MockHttpSession

use of org.springframework.web.testfixture.servlet.MockHttpSession in project spring-framework by spring-projects.

the class SessionScopeTests method getFromScope.

@Test
public void getFromScope() throws Exception {
    AtomicInteger count = new AtomicInteger();
    MockHttpSession session = new MockHttpSession() {

        @Override
        public void setAttribute(String name, Object value) {
            super.setAttribute(name, value);
            count.incrementAndGet();
        }
    };
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(requestAttributes);
    String name = "sessionScopedObject";
    assertThat(session.getAttribute(name)).isNull();
    TestBean bean = (TestBean) this.beanFactory.getBean(name);
    assertThat(count.intValue()).isEqualTo(1);
    assertThat(bean).isEqualTo(session.getAttribute(name));
    assertThat(this.beanFactory.getBean(name)).isSameAs(bean);
    assertThat(count.intValue()).isEqualTo(1);
    // should re-propagate updated attribute
    requestAttributes.requestCompleted();
    assertThat(bean).isEqualTo(session.getAttribute(name));
    assertThat(count.intValue()).isEqualTo(2);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpSession(org.springframework.web.testfixture.servlet.MockHttpSession) Test(org.junit.jupiter.api.Test)

Example 13 with MockHttpSession

use of org.springframework.web.testfixture.servlet.MockHttpSession 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 14 with MockHttpSession

use of org.springframework.web.testfixture.servlet.MockHttpSession 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)

Example 15 with MockHttpSession

use of org.springframework.web.testfixture.servlet.MockHttpSession in project spring-framework by spring-projects.

the class ServletRequestMethodArgumentResolverTests method session.

@Test
public void session() throws Exception {
    MockHttpSession session = new MockHttpSession();
    servletRequest.setSession(session);
    MethodParameter sessionParameter = new MethodParameter(method, 2);
    assertThat(resolver.supportsParameter(sessionParameter)).as("Session not supported").isTrue();
    Object result = resolver.resolveArgument(sessionParameter, mavContainer, webRequest, null);
    assertThat(result).as("Invalid result").isSameAs(session);
    assertThat(mavContainer.isRequestHandled()).as("The requestHandled flag shouldn't change").isFalse();
}
Also used : MockHttpSession(org.springframework.web.testfixture.servlet.MockHttpSession) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.jupiter.api.Test)

Aggregations

MockHttpSession (org.springframework.web.testfixture.servlet.MockHttpSession)15 Test (org.junit.jupiter.api.Test)14 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)9 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 TestBean (org.springframework.beans.testfixture.beans.TestBean)2 FilterChain (jakarta.servlet.FilterChain)1 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)1 Serializable (java.io.Serializable)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodParameter (org.springframework.core.MethodParameter)1 FileCopyUtils (org.springframework.util.FileCopyUtils)1 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)1 ContentCachingRequestWrapper (org.springframework.web.util.ContentCachingRequestWrapper)1 WebUtils (org.springframework.web.util.WebUtils)1