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