Skip to content

Commit 51d8288

Browse files
committed
Throw ISEs in MockHttpSession for invalid session
The Javadoc for several methods in HttpSession specifies that an IllegalStateException must be thrown if the method is called on an invalidated session; however, Spring's MockHttpSession did not implement this behavior consistently prior to this commit. This commit therefore ensures that the following methods in MockHttpSession properly throw an IllegalStateException as defined in the Servlet specification. - long getCreationTime() - long getLastAccessedTime() - Object getAttribute(String) - Object getValue(String) - Enumeration<String> getAttributeNames() - String[] getValueNames() - void setAttribute(String, Object) - void putValue(String , Object) - void removeAttribute(String) - void removeValue(String) - void invalidate() - boolean isNew() Issue: SPR-7659
1 parent ec5d81e commit 51d8288

File tree

2 files changed

+122
-7
lines changed

2 files changed

+122
-7
lines changed

spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public MockHttpSession(ServletContext servletContext, String id) {
102102

103103
@Override
104104
public long getCreationTime() {
105+
assertIsValid();
105106
return this.creationTime;
106107
}
107108

@@ -117,6 +118,7 @@ public void access() {
117118

118119
@Override
119120
public long getLastAccessedTime() {
121+
assertIsValid();
120122
return this.lastAccessedTime;
121123
}
122124

@@ -142,6 +144,7 @@ public HttpSessionContext getSessionContext() {
142144

143145
@Override
144146
public Object getAttribute(String name) {
147+
assertIsValid();
145148
Assert.notNull(name, "Attribute name must not be null");
146149
return this.attributes.get(name);
147150
}
@@ -153,16 +156,19 @@ public Object getValue(String name) {
153156

154157
@Override
155158
public Enumeration<String> getAttributeNames() {
159+
assertIsValid();
156160
return Collections.enumeration(new LinkedHashSet<String>(this.attributes.keySet()));
157161
}
158162

159163
@Override
160164
public String[] getValueNames() {
165+
assertIsValid();
161166
return this.attributes.keySet().toArray(new String[this.attributes.size()]);
162167
}
163168

164169
@Override
165170
public void setAttribute(String name, Object value) {
171+
assertIsValid();
166172
Assert.notNull(name, "Attribute name must not be null");
167173
if (value != null) {
168174
this.attributes.put(name, value);
@@ -182,6 +188,7 @@ public void putValue(String name, Object value) {
182188

183189
@Override
184190
public void removeAttribute(String name) {
191+
assertIsValid();
185192
Assert.notNull(name, "Attribute name must not be null");
186193
Object value = this.attributes.remove(name);
187194
if (value instanceof HttpSessionBindingListener) {
@@ -216,11 +223,7 @@ public void clearAttributes() {
216223
*/
217224
@Override
218225
public void invalidate() {
219-
if (this.invalid) {
220-
throw new IllegalStateException("The session has already been invalidated");
221-
}
222-
223-
// else
226+
assertIsValid();
224227
this.invalid = true;
225228
clearAttributes();
226229
}
@@ -229,12 +232,25 @@ public boolean isInvalid() {
229232
return this.invalid;
230233
}
231234

235+
/**
236+
* Convenience method for asserting that this session has not been
237+
* {@linkplain #invalidate() invalidated}.
238+
*
239+
* @throws IllegalStateException if this session has been invalidated
240+
*/
241+
private void assertIsValid() {
242+
if (isInvalid()) {
243+
throw new IllegalStateException("The session has already been invalidated");
244+
}
245+
}
246+
232247
public void setNew(boolean value) {
233248
this.isNew = value;
234249
}
235250

236251
@Override
237252
public boolean isNew() {
253+
assertIsValid();
238254
return this.isNew;
239255
}
240256

spring-test/src/test/java/org/springframework/mock/web/MockHttpSessionTests.java

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,7 +28,7 @@
2828
*/
2929
public class MockHttpSessionTests {
3030

31-
private MockHttpSession session = new MockHttpSession();
31+
private final MockHttpSession session = new MockHttpSession();
3232

3333

3434
@Test
@@ -44,4 +44,103 @@ public void invalidateTwice() {
4444
session.invalidate();
4545
}
4646

47+
/**
48+
* @since 4.0
49+
*/
50+
@Test(expected = IllegalStateException.class)
51+
public void getCreationTimeOnInvalidatedSession() {
52+
session.invalidate();
53+
session.getCreationTime();
54+
}
55+
56+
/**
57+
* @since 4.0
58+
*/
59+
@Test(expected = IllegalStateException.class)
60+
public void getLastAccessedTimeOnInvalidatedSession() {
61+
session.invalidate();
62+
session.getLastAccessedTime();
63+
}
64+
65+
/**
66+
* @since 4.0
67+
*/
68+
@Test(expected = IllegalStateException.class)
69+
public void getAttributeOnInvalidatedSession() {
70+
session.invalidate();
71+
session.getAttribute("foo");
72+
}
73+
74+
/**
75+
* @since 4.0
76+
*/
77+
@Test(expected = IllegalStateException.class)
78+
public void getAttributeNamesOnInvalidatedSession() {
79+
session.invalidate();
80+
session.getAttributeNames();
81+
}
82+
83+
/**
84+
* @since 4.0
85+
*/
86+
@Test(expected = IllegalStateException.class)
87+
public void getValueOnInvalidatedSession() {
88+
session.invalidate();
89+
session.getValue("foo");
90+
}
91+
92+
/**
93+
* @since 4.0
94+
*/
95+
@Test(expected = IllegalStateException.class)
96+
public void getValueNamesOnInvalidatedSession() {
97+
session.invalidate();
98+
session.getValueNames();
99+
}
100+
101+
/**
102+
* @since 4.0
103+
*/
104+
@Test(expected = IllegalStateException.class)
105+
public void setAttributeOnInvalidatedSession() {
106+
session.invalidate();
107+
session.setAttribute("name", "value");
108+
}
109+
110+
/**
111+
* @since 4.0
112+
*/
113+
@Test(expected = IllegalStateException.class)
114+
public void putValueOnInvalidatedSession() {
115+
session.invalidate();
116+
session.putValue("name", "value");
117+
}
118+
119+
/**
120+
* @since 4.0
121+
*/
122+
@Test(expected = IllegalStateException.class)
123+
public void removeAttributeOnInvalidatedSession() {
124+
session.invalidate();
125+
session.removeAttribute("name");
126+
}
127+
128+
/**
129+
* @since 4.0
130+
*/
131+
@Test(expected = IllegalStateException.class)
132+
public void removeValueOnInvalidatedSession() {
133+
session.invalidate();
134+
session.removeValue("name");
135+
}
136+
137+
/**
138+
* @since 4.0
139+
*/
140+
@Test(expected = IllegalStateException.class)
141+
public void isNewOnInvalidatedSession() {
142+
session.invalidate();
143+
session.isNew();
144+
}
145+
47146
}

0 commit comments

Comments
 (0)