Search in sources :

Example 6 with MockHttpSession

use of org.springframework.mock.web.test.MockHttpSession in project spring-framework by spring-projects.

the class ServletRequestAttributesTests method setSessionScopedAttribute.

@Test
public void setSessionScopedAttribute() throws Exception {
    MockHttpSession session = new MockHttpSession();
    session.setAttribute(KEY, VALUE);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    ServletRequestAttributes attrs = new ServletRequestAttributes(request);
    attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_SESSION);
    assertSame(VALUE, session.getAttribute(KEY));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.test.MockHttpSession) Test(org.junit.Test)

Example 7 with MockHttpSession

use of org.springframework.mock.web.test.MockHttpSession in project spring-framework by spring-projects.

the class ServletRequestAttributesTests method setSessionScopedAttributeAfterCompletion.

@Test
public void setSessionScopedAttributeAfterCompletion() throws Exception {
    MockHttpSession session = new MockHttpSession();
    session.setAttribute(KEY, VALUE);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);
    ServletRequestAttributes attrs = new ServletRequestAttributes(request);
    assertSame(VALUE, attrs.getAttribute(KEY, RequestAttributes.SCOPE_SESSION));
    attrs.requestCompleted();
    request.close();
    attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_SESSION);
    assertSame(VALUE, session.getAttribute(KEY));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.test.MockHttpSession) Test(org.junit.Test)

Example 8 with MockHttpSession

use of org.springframework.mock.web.test.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";
    assertNull(session.getAttribute(name));
    TestBean bean = (TestBean) this.beanFactory.getBean(name);
    assertEquals(1, count.intValue());
    // should re-propagate updated attribute
    requestAttributes.requestCompleted();
    assertEquals(session.getAttribute(name), bean);
    assertEquals(2, count.intValue());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.test.MockHttpSession) Test(org.junit.Test)

Example 9 with MockHttpSession

use of org.springframework.mock.web.test.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";
    assertNull(session.getAttribute(name));
    DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name);
    assertEquals(session.getAttribute(name), bean);
    assertSame(bean, this.beanFactory.getBean(name));
    requestAttributes.requestCompleted();
    session.invalidate();
    assertTrue(bean.wasDestroyed());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.test.MockHttpSession) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) Test(org.junit.Test)

Example 10 with MockHttpSession

use of org.springframework.mock.web.test.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";
    assertNull(session.getAttribute(name));
    DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name);
    assertEquals(session.getAttribute(name), bean);
    assertSame(bean, this.beanFactory.getBean(name));
    requestAttributes.requestCompleted();
    serializedState = session.serializeState();
    assertFalse(bean.wasDestroyed());
    serializedState = (Serializable) SerializationTestUtils.serializeAndDeserialize(serializedState);
    session = new MockHttpSession();
    session.deserializeState(serializedState);
    request = new MockHttpServletRequest();
    request.setSession(session);
    requestAttributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(requestAttributes);
    name = "sessionScopedDisposableObject";
    assertNotNull(session.getAttribute(name));
    bean = (DerivedTestBean) this.beanFactory.getBean(name);
    assertEquals(session.getAttribute(name), bean);
    assertSame(bean, this.beanFactory.getBean(name));
    requestAttributes.requestCompleted();
    session.invalidate();
    assertTrue(bean.wasDestroyed());
    if (beanNameReset) {
        assertNull(bean.getBeanName());
    } else {
        assertNotNull(bean.getBeanName());
    }
}
Also used : Serializable(java.io.Serializable) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.test.MockHttpSession) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean)

Aggregations

MockHttpSession (org.springframework.mock.web.test.MockHttpSession)12 Test (org.junit.Test)11 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)7 HashMap (java.util.HashMap)4 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)4 WebSocketHandler (org.springframework.web.socket.WebSocketHandler)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 TestBean (org.springframework.tests.sample.beans.TestBean)2 Serializable (java.io.Serializable)1 MethodParameter (org.springframework.core.MethodParameter)1