Skip to content

Commit 2e6c998

Browse files
committed
Update Javadoc for mocks regarding Servlet 3.0
Commit deba32c upgraded the Servlet API mocks to Servlet 3.0; however, not all of the Javadoc was updated accordingly. This commit updates the remaining Javadoc with regard to Servlet 3.0 as the baseline for mocks in the spring-test module. In addition, this commit syncs up the mocks used for internal testing in the spring-web module with the most current versions from spring-test. Issue: SPR-11049
1 parent d371886 commit 2e6c998

File tree

10 files changed

+69
-28
lines changed

10 files changed

+69
-28
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@
4040
/**
4141
* Mock implementation of the {@link javax.servlet.http.HttpServletResponse} interface.
4242
*
43-
* <p>As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline. Beyond that,
44-
* this MockHttpServletResponse is also compatible with Servlet 3.1's setContentLengthLong.
43+
* <p>As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline.
44+
* Beyond that, {@code MockHttpServletResponse} is also compatible with Servlet
45+
* 3.1's {@code setContentLengthLong()} method.
4546
*
4647
* @author Juergen Hoeller
4748
* @author Rod Johnson

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
/**
3636
* Mock implementation of the {@link javax.servlet.http.HttpSession} interface.
3737
*
38-
* <p>Compatible with Servlet 2.5 as well as Servlet 3.0.
38+
* <p>As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline.
3939
*
4040
* <p>Used for testing the web framework; also useful for testing application
4141
* controllers.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
* Mock implementation of the
3636
* {@link org.springframework.web.multipart.MultipartHttpServletRequest} interface.
3737
*
38+
* <p>As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline.
39+
*
3840
* <p>Useful for testing application controllers that access multipart uploads.
3941
* The {@link MockMultipartFile} can be used to populate these mock requests
4042
* with files.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@
5656
/**
5757
* Mock implementation of the {@link javax.servlet.ServletContext} interface.
5858
*
59-
* <p>Compatible with Servlet 3.0. Can be configured to expose a specific version
59+
* <p>As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline.
60+
*
61+
* <p>Compatible with Servlet 3.0 but can be configured to expose a specific version
6062
* through {@link #setMajorVersion}/{@link #setMinorVersion}; default is 3.0.
6163
* Note that Servlet 3.0 support is limited: servlet, filter and listener
6264
* registration methods are not supported; neither is JSP configuration.
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
21
/**
2+
* A comprehensive set of Servlet API 3.0 mock objects, targeted at usage with
3+
* Spring's web MVC framework.
34
*
4-
* A comprehensive set of Servlet API 2.5 mock objects,
5-
* targeted at usage with Spring's web MVC framework.
6-
* Useful for testing web contexts and controllers.
5+
* <p>Useful for testing web contexts and controllers.
76
*
87
* <p>More convenient to use than dynamic mock objects
98
* (<a href="http://www.easymock.org">EasyMock</a>) or
109
* existing Servlet API mock objects
1110
* (<a href="http://www.mockobjects.com">MockObjects</a>).
12-
*
1311
*/
12+
1413
package org.springframework.mock.web;
1514

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.Locale;
3737
import java.util.Map;
3838
import java.util.Set;
39+
3940
import javax.servlet.AsyncContext;
4041
import javax.servlet.DispatcherType;
4142
import javax.servlet.RequestDispatcher;
@@ -52,11 +53,12 @@
5253

5354
import org.springframework.util.Assert;
5455
import org.springframework.util.LinkedCaseInsensitiveMap;
56+
import org.springframework.util.StringUtils;
5557

5658
/**
5759
* Mock implementation of the {@link javax.servlet.http.HttpServletRequest} interface.
5860
*
59-
* <p>As of Spring 4.0, this set of mocks is entirely based on Servlet 3.0.
61+
* <p>As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline.
6062
*
6163
* @author Juergen Hoeller
6264
* @author Rod Johnson
@@ -313,7 +315,7 @@ public Object getAttribute(String name) {
313315
@Override
314316
public Enumeration<String> getAttributeNames() {
315317
checkActive();
316-
return Collections.enumeration(this.attributes.keySet());
318+
return Collections.enumeration(new LinkedHashSet<String>(this.attributes.keySet()));
317319
}
318320

319321
@Override
@@ -946,9 +948,17 @@ public String getRequestURI() {
946948

947949
@Override
948950
public StringBuffer getRequestURL() {
949-
StringBuffer url = new StringBuffer(this.scheme);
950-
url.append("://").append(this.serverName).append(':').append(this.serverPort);
951-
url.append(getRequestURI());
951+
StringBuffer url = new StringBuffer(this.scheme).append("://").append(this.serverName);
952+
953+
if (this.serverPort > 0
954+
&& (("http".equalsIgnoreCase(scheme) && this.serverPort != 80) || ("https".equalsIgnoreCase(scheme) && this.serverPort != 443))) {
955+
url.append(':').append(this.serverPort);
956+
}
957+
958+
if (StringUtils.hasText(getRequestURI())) {
959+
url.append(getRequestURI());
960+
}
961+
952962
return url;
953963
}
954964

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@
4040
/**
4141
* Mock implementation of the {@link javax.servlet.http.HttpServletResponse} interface.
4242
*
43-
* <p>As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline. Beyond that,
44-
* this MockHttpServletResponse is also compatible with Servlet 3.1's setContentLengthLong.
43+
* <p>As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline.
44+
* Beyond that, {@code MockHttpServletResponse} is also compatible with Servlet
45+
* 3.1's {@code setContentLengthLong()} method.
4546
*
4647
* @author Juergen Hoeller
4748
* @author Rod Johnson
@@ -533,13 +534,17 @@ private void doAddHeaderValue(String name, Object value, boolean replace) {
533534

534535
@Override
535536
public void setStatus(int status) {
536-
this.status = status;
537+
if(!this.isCommitted()) {
538+
this.status = status;
539+
}
537540
}
538541

539542
@Override
540543
public void setStatus(int status, String errorMessage) {
541-
this.status = status;
542-
this.errorMessage = errorMessage;
544+
if(!this.isCommitted()) {
545+
this.status = status;
546+
this.errorMessage = errorMessage;
547+
}
543548
}
544549

545550
@Override

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

Lines changed: 26 additions & 8 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.
@@ -22,6 +22,7 @@
2222
import java.util.HashMap;
2323
import java.util.Iterator;
2424
import java.util.LinkedHashMap;
25+
import java.util.LinkedHashSet;
2526
import java.util.Map;
2627
import javax.servlet.ServletContext;
2728
import javax.servlet.http.HttpSession;
@@ -34,7 +35,7 @@
3435
/**
3536
* Mock implementation of the {@link javax.servlet.http.HttpSession} interface.
3637
*
37-
* <p>Compatible with Servlet 2.5 as well as Servlet 3.0.
38+
* <p>As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline.
3839
*
3940
* <p>Used for testing the web framework; also useful for testing application
4041
* controllers.
@@ -50,6 +51,7 @@ public class MockHttpSession implements HttpSession {
5051

5152
public static final String SESSION_COOKIE_NAME = "JSESSION";
5253

54+
5355
private static int nextId = 1;
5456

5557
private final String id;
@@ -100,6 +102,7 @@ public MockHttpSession(ServletContext servletContext, String id) {
100102

101103
@Override
102104
public long getCreationTime() {
105+
assertIsValid();
103106
return this.creationTime;
104107
}
105108

@@ -115,6 +118,7 @@ public void access() {
115118

116119
@Override
117120
public long getLastAccessedTime() {
121+
assertIsValid();
118122
return this.lastAccessedTime;
119123
}
120124

@@ -140,6 +144,7 @@ public HttpSessionContext getSessionContext() {
140144

141145
@Override
142146
public Object getAttribute(String name) {
147+
assertIsValid();
143148
Assert.notNull(name, "Attribute name must not be null");
144149
return this.attributes.get(name);
145150
}
@@ -151,16 +156,19 @@ public Object getValue(String name) {
151156

152157
@Override
153158
public Enumeration<String> getAttributeNames() {
154-
return Collections.enumeration(this.attributes.keySet());
159+
assertIsValid();
160+
return Collections.enumeration(new LinkedHashSet<String>(this.attributes.keySet()));
155161
}
156162

157163
@Override
158164
public String[] getValueNames() {
165+
assertIsValid();
159166
return this.attributes.keySet().toArray(new String[this.attributes.size()]);
160167
}
161168

162169
@Override
163170
public void setAttribute(String name, Object value) {
171+
assertIsValid();
164172
Assert.notNull(name, "Attribute name must not be null");
165173
if (value != null) {
166174
this.attributes.put(name, value);
@@ -180,6 +188,7 @@ public void putValue(String name, Object value) {
180188

181189
@Override
182190
public void removeAttribute(String name) {
191+
assertIsValid();
183192
Assert.notNull(name, "Attribute name must not be null");
184193
Object value = this.attributes.remove(name);
185194
if (value instanceof HttpSessionBindingListener) {
@@ -214,11 +223,7 @@ public void clearAttributes() {
214223
*/
215224
@Override
216225
public void invalidate() {
217-
if (this.invalid) {
218-
throw new IllegalStateException("The session has already been invalidated");
219-
}
220-
221-
// else
226+
assertIsValid();
222227
this.invalid = true;
223228
clearAttributes();
224229
}
@@ -227,12 +232,25 @@ public boolean isInvalid() {
227232
return this.invalid;
228233
}
229234

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+
230247
public void setNew(boolean value) {
231248
this.isNew = value;
232249
}
233250

234251
@Override
235252
public boolean isNew() {
253+
assertIsValid();
236254
return this.isNew;
237255
}
238256

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
* Mock implementation of the
3636
* {@link org.springframework.web.multipart.MultipartHttpServletRequest} interface.
3737
*
38+
* <p>As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline.
39+
*
3840
* <p>Useful for testing application controllers that access multipart uploads.
3941
* The {@link MockMultipartFile} can be used to populate these mock requests
4042
* with files.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@
5656
/**
5757
* Mock implementation of the {@link javax.servlet.ServletContext} interface.
5858
*
59-
* <p>Compatible with Servlet 3.0. Can be configured to expose a specific version
59+
* <p>As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline.
60+
*
61+
* <p>Compatible with Servlet 3.0 but can be configured to expose a specific version
6062
* through {@link #setMajorVersion}/{@link #setMinorVersion}; default is 3.0.
6163
* Note that Servlet 3.0 support is limited: servlet, filter and listener
6264
* registration methods are not supported; neither is JSP configuration.

0 commit comments

Comments
 (0)