Skip to content

Commit 21ebbb9

Browse files
committed
Support session & request scoped beans in the TCF
This commit introduces RequestAndSessionScopedBeansWacTests which verifies support for request and session scoped beans in the Spring TestContext Framework (TCF). This support was actually introduced as an intentional side effect of the work performed for SPR-5243 through the addition of the new WebTestExecutionListener. Issue: SPR-4588
1 parent 0bb24f2 commit 21ebbb9

File tree

5 files changed

+101
-4
lines changed

5 files changed

+101
-4
lines changed

spring-test/.springBeans

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<beansProjectDescription>
33
<version>1</version>
4-
<pluginVersion><![CDATA[3.0.0.201208090952-RELEASE]]></pluginVersion>
4+
<pluginVersion><![CDATA[3.1.0.201210040510-RELEASE]]></pluginVersion>
55
<configSuffixes>
66
<configSuffix><![CDATA[xml]]></configSuffix>
77
</configSuffixes>
88
<enableImports><![CDATA[false]]></enableImports>
99
<configs>
1010
<config>src/test/java/org/springframework/test/context/junit4/profile/xml/DefaultProfileXmlConfigTests-context.xml</config>
1111
<config>src/test/java/org/springframework/test/context/junit4/aci/xml/MultipleInitializersXmlConfigTests-context.xml</config>
12-
<config>src/test/resources/org/springframework/test/context/web/BasicXmlWacTests-config.xml</config>
12+
<config>src/test/resources/org/springframework/test/context/web/RequestAndSessionScopedBeansWacTests-context.xml</config>
1313
</configs>
1414
<configSets>
1515
</configSets>

spring-test/src/test/java/org/springframework/test/context/web/AbstractBasicWacTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.beans.factory.annotation.Autowired;
2828
import org.springframework.mock.web.MockHttpServletRequest;
2929
import org.springframework.mock.web.MockHttpServletResponse;
30+
import org.springframework.mock.web.MockHttpSession;
3031
import org.springframework.mock.web.MockServletContext;
3132
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3233
import org.springframework.web.context.ServletContextAware;
@@ -55,6 +56,9 @@ public abstract class AbstractBasicWacTests implements ServletContextAware {
5556
@Autowired
5657
protected MockHttpServletResponse response;
5758

59+
@Autowired
60+
protected MockHttpSession session;
61+
5862
@Autowired
5963
protected ServletWebRequest webRequest;
6064

@@ -75,6 +79,7 @@ public void basicWacFeatures() throws Exception {
7579
assertNotNull("ServletContext should have been autowired from the WAC.", mockServletContext);
7680
assertNotNull("MockHttpServletRequest should have been autowired from the WAC.", request);
7781
assertNotNull("MockHttpServletResponse should have been autowired from the WAC.", response);
82+
assertNotNull("MockHttpSession should have been autowired from the WAC.", session);
7883
assertNotNull("ServletWebRequest should have been autowired from the WAC.", webRequest);
7984

8085
Object rootWac = mockServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.web;
18+
19+
import static org.junit.Assert.*;
20+
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.springframework.beans.TestBean;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.mock.web.MockHttpServletRequest;
26+
import org.springframework.mock.web.MockHttpSession;
27+
import org.springframework.test.context.ContextConfiguration;
28+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
29+
import org.springframework.web.context.WebApplicationContext;
30+
31+
/**
32+
* Integration tests that verify support for request and session scoped beans
33+
* in conjunction with the TestContext Framework.
34+
*
35+
* @author Sam Brannen
36+
* @since 3.2
37+
*/
38+
@RunWith(SpringJUnit4ClassRunner.class)
39+
@ContextConfiguration
40+
@WebAppConfiguration
41+
public class RequestAndSessionScopedBeansWacTests {
42+
43+
@Autowired
44+
private WebApplicationContext wac;
45+
46+
@Autowired
47+
private MockHttpServletRequest request;
48+
49+
@Autowired
50+
private MockHttpSession session;
51+
52+
53+
@Test
54+
public void requestScope() throws Exception {
55+
final String beanName = "requestScopedTestBean";
56+
final String contextPath = "/path";
57+
58+
assertNull(request.getAttribute(beanName));
59+
60+
request.setContextPath(contextPath);
61+
TestBean testBean = wac.getBean(beanName, TestBean.class);
62+
63+
assertEquals(contextPath, testBean.getName());
64+
assertSame(testBean, request.getAttribute(beanName));
65+
assertSame(testBean, wac.getBean(beanName, TestBean.class));
66+
}
67+
68+
@Test
69+
public void sessionScope() throws Exception {
70+
final String beanName = "sessionScopedTestBean";
71+
72+
assertNull(session.getAttribute(beanName));
73+
74+
TestBean testBean = wac.getBean(beanName, TestBean.class);
75+
76+
assertSame(testBean, session.getAttribute(beanName));
77+
assertSame(testBean, wac.getBean(beanName, TestBean.class));
78+
}
79+
80+
}

spring-test/src/test/java/org/springframework/test/context/web/WebContextLoaderTestSuite.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@
3333
@RunWith(Suite.class)
3434
// Note: the following 'multi-line' layout is for enhanced code readability.
3535
@SuiteClasses({//
36-
BasicXmlWacTests.class,//
37-
BasicAnnotationConfigWacTests.class //
36+
BasicXmlWacTests.class,//
37+
BasicAnnotationConfigWacTests.class,//
38+
RequestAndSessionScopedBeansWacTests.class //
3839
})
3940
public class WebContextLoaderTestSuite {
4041
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
4+
5+
<bean id="requestScopedTestBean" class="org.springframework.beans.TestBean" scope="request">
6+
<property name="name" value="#{request.contextPath}" />
7+
</bean>
8+
9+
<bean id="sessionScopedTestBean" class="org.springframework.beans.TestBean" scope="session" />
10+
11+
</beans>

0 commit comments

Comments
 (0)