Java Code Examples for org.springframework.mock.web.test.MockHttpServletRequest#setSession()

The following examples show how to use org.springframework.mock.web.test.MockHttpServletRequest#setSession() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SessionScopeTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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());
}
 
Example 2
Source File: SessionScopeTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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());
}
 
Example 3
Source File: SessionScopeTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example 4
Source File: ServletRequestAttributesTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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));
}
 
Example 5
Source File: ServletRequestAttributesTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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));
}
 
Example 6
Source File: ServletRequestAttributesTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void removeSessionScopedAttribute() throws Exception {
	MockHttpSession session = new MockHttpSession();
	session.setAttribute(KEY, VALUE);
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setSession(session);
	ServletRequestAttributes attrs = new ServletRequestAttributes(request);
	attrs.removeAttribute(KEY, RequestAttributes.SCOPE_SESSION);
	Object value = session.getAttribute(KEY);
	assertNull(value);
}
 
Example 7
Source File: ServletRequestAttributesTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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));
}
 
Example 8
Source File: ServletRequestAttributesTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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));
}
 
Example 9
Source File: ServletRequestAttributesTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void removeSessionScopedAttribute() throws Exception {
	MockHttpSession session = new MockHttpSession();
	session.setAttribute(KEY, VALUE);
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setSession(session);
	ServletRequestAttributes attrs = new ServletRequestAttributes(request);
	attrs.removeAttribute(KEY, RequestAttributes.SCOPE_SESSION);
	Object value = session.getAttribute(KEY);
	assertNull(value);
}
 
Example 10
Source File: ServletRequestAttributesTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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);
	Object value = session.getAttribute(KEY);
	assertSame(VALUE, value);
}
 
Example 11
Source File: ServletRequestAttributesTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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);
	attrs.requestCompleted();
	request.close();
	attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_SESSION);
	Object value = session.getAttribute(KEY);
	assertSame(VALUE, value);
}
 
Example 12
Source File: ServletRequestAttributesTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void setGlobalSessionScopedAttribute() 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_GLOBAL_SESSION);
	Object value = session.getAttribute(KEY);
	assertSame(VALUE, value);
}
 
Example 13
Source File: ServletRequestAttributesTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void setGlobalSessionScopedAttributeAfterCompletion() throws Exception {
	MockHttpSession session = new MockHttpSession();
	session.setAttribute(KEY, VALUE);
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setSession(session);
	ServletRequestAttributes attrs = new ServletRequestAttributes(request);
	attrs.requestCompleted();
	request.close();
	attrs.setAttribute(KEY, VALUE, RequestAttributes.SCOPE_GLOBAL_SESSION);
	Object value = session.getAttribute(KEY);
	assertSame(VALUE, value);
}
 
Example 14
Source File: ServletRequestAttributesTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void removeSessionScopedAttribute() throws Exception {
	MockHttpSession session = new MockHttpSession();
	session.setAttribute(KEY, VALUE);
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setSession(session);
	ServletRequestAttributes attrs = new ServletRequestAttributes(request);
	attrs.removeAttribute(KEY, RequestAttributes.SCOPE_SESSION);
	Object value = session.getAttribute(KEY);
	assertNull(value);
}
 
Example 15
Source File: SessionScopeTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void getFromScope() throws Exception {
	MockHttpSession session = new MockHttpSession();
	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(session.getAttribute(name), bean);
	assertSame(bean, this.beanFactory.getBean(name));
}