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));
}
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));
}
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());
}
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());
}
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());
}
}
Aggregations