diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java index 12a7e8a965..347ad8291e 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java @@ -40,8 +40,9 @@ import org.springframework.web.util.WebUtils; /** * Mock implementation of the {@link javax.servlet.http.HttpServletResponse} interface. * - *

As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline. Beyond that, - * this MockHttpServletResponse is also compatible with Servlet 3.1's setContentLengthLong. + *

As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline. + * Beyond that, {@code MockHttpServletResponse} is also compatible with Servlet + * 3.1's {@code setContentLengthLong()} method. * * @author Juergen Hoeller * @author Rod Johnson diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java index 513f64ad56..d59c5e70f3 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java @@ -35,7 +35,7 @@ import org.springframework.util.Assert; /** * Mock implementation of the {@link javax.servlet.http.HttpSession} interface. * - *

Compatible with Servlet 2.5 as well as Servlet 3.0. + *

As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline. * *

Used for testing the web framework; also useful for testing application * controllers. diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockMultipartHttpServletRequest.java b/spring-test/src/main/java/org/springframework/mock/web/MockMultipartHttpServletRequest.java index 01abe7665f..ac0cb75e64 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockMultipartHttpServletRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockMultipartHttpServletRequest.java @@ -35,6 +35,8 @@ import org.springframework.web.multipart.MultipartHttpServletRequest; * Mock implementation of the * {@link org.springframework.web.multipart.MultipartHttpServletRequest} interface. * + *

As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline. + * *

Useful for testing application controllers that access multipart uploads. * The {@link MockMultipartFile} can be used to populate these mock requests * with files. diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockServletContext.java b/spring-test/src/main/java/org/springframework/mock/web/MockServletContext.java index 1ab7a63e5c..23f5010d7c 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockServletContext.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockServletContext.java @@ -56,7 +56,9 @@ import org.springframework.web.util.WebUtils; /** * Mock implementation of the {@link javax.servlet.ServletContext} interface. * - *

Compatible with Servlet 3.0. Can be configured to expose a specific version + *

As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline. + * + *

Compatible with Servlet 3.0 but can be configured to expose a specific version * through {@link #setMajorVersion}/{@link #setMinorVersion}; default is 3.0. * Note that Servlet 3.0 support is limited: servlet, filter and listener * registration methods are not supported; neither is JSP configuration. diff --git a/spring-test/src/main/java/org/springframework/mock/web/package-info.java b/spring-test/src/main/java/org/springframework/mock/web/package-info.java index be8cb8601c..ce02d00c83 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/package-info.java +++ b/spring-test/src/main/java/org/springframework/mock/web/package-info.java @@ -1,15 +1,14 @@ - /** + * A comprehensive set of Servlet API 3.0 mock objects, targeted at usage with + * Spring's web MVC framework. * - * A comprehensive set of Servlet API 2.5 mock objects, - * targeted at usage with Spring's web MVC framework. - * Useful for testing web contexts and controllers. + *

Useful for testing web contexts and controllers. * *

More convenient to use than dynamic mock objects * (EasyMock) or * existing Servlet API mock objects * (MockObjects). - * */ + package org.springframework.mock.web; diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java index 203b5143b6..7e87dfb891 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletRequest.java @@ -36,6 +36,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; + import javax.servlet.AsyncContext; import javax.servlet.DispatcherType; import javax.servlet.RequestDispatcher; @@ -52,11 +53,12 @@ import javax.servlet.http.Part; import org.springframework.util.Assert; import org.springframework.util.LinkedCaseInsensitiveMap; +import org.springframework.util.StringUtils; /** * Mock implementation of the {@link javax.servlet.http.HttpServletRequest} interface. * - *

As of Spring 4.0, this set of mocks is entirely based on Servlet 3.0. + *

As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline. * * @author Juergen Hoeller * @author Rod Johnson @@ -313,7 +315,7 @@ public class MockHttpServletRequest implements HttpServletRequest { @Override public Enumeration getAttributeNames() { checkActive(); - return Collections.enumeration(this.attributes.keySet()); + return Collections.enumeration(new LinkedHashSet(this.attributes.keySet())); } @Override @@ -946,9 +948,17 @@ public class MockHttpServletRequest implements HttpServletRequest { @Override public StringBuffer getRequestURL() { - StringBuffer url = new StringBuffer(this.scheme); - url.append("://").append(this.serverName).append(':').append(this.serverPort); - url.append(getRequestURI()); + StringBuffer url = new StringBuffer(this.scheme).append("://").append(this.serverName); + + if (this.serverPort > 0 + && (("http".equalsIgnoreCase(scheme) && this.serverPort != 80) || ("https".equalsIgnoreCase(scheme) && this.serverPort != 443))) { + url.append(':').append(this.serverPort); + } + + if (StringUtils.hasText(getRequestURI())) { + url.append(getRequestURI()); + } + return url; } diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java index 96a84ed5c7..080428883b 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java @@ -40,8 +40,9 @@ import org.springframework.web.util.WebUtils; /** * Mock implementation of the {@link javax.servlet.http.HttpServletResponse} interface. * - *

As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline. Beyond that, - * this MockHttpServletResponse is also compatible with Servlet 3.1's setContentLengthLong. + *

As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline. + * Beyond that, {@code MockHttpServletResponse} is also compatible with Servlet + * 3.1's {@code setContentLengthLong()} method. * * @author Juergen Hoeller * @author Rod Johnson @@ -533,13 +534,17 @@ public class MockHttpServletResponse implements HttpServletResponse { @Override public void setStatus(int status) { - this.status = status; + if(!this.isCommitted()) { + this.status = status; + } } @Override public void setStatus(int status, String errorMessage) { - this.status = status; - this.errorMessage = errorMessage; + if(!this.isCommitted()) { + this.status = status; + this.errorMessage = errorMessage; + } } @Override diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpSession.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpSession.java index 12301763b6..43357bd314 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpSession.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpSession.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,7 @@ import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.Map; import javax.servlet.ServletContext; import javax.servlet.http.HttpSession; @@ -34,7 +35,7 @@ import org.springframework.util.Assert; /** * Mock implementation of the {@link javax.servlet.http.HttpSession} interface. * - *

Compatible with Servlet 2.5 as well as Servlet 3.0. + *

As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline. * *

Used for testing the web framework; also useful for testing application * controllers. @@ -50,6 +51,7 @@ public class MockHttpSession implements HttpSession { public static final String SESSION_COOKIE_NAME = "JSESSION"; + private static int nextId = 1; private final String id; @@ -100,6 +102,7 @@ public class MockHttpSession implements HttpSession { @Override public long getCreationTime() { + assertIsValid(); return this.creationTime; } @@ -115,6 +118,7 @@ public class MockHttpSession implements HttpSession { @Override public long getLastAccessedTime() { + assertIsValid(); return this.lastAccessedTime; } @@ -140,6 +144,7 @@ public class MockHttpSession implements HttpSession { @Override public Object getAttribute(String name) { + assertIsValid(); Assert.notNull(name, "Attribute name must not be null"); return this.attributes.get(name); } @@ -151,16 +156,19 @@ public class MockHttpSession implements HttpSession { @Override public Enumeration getAttributeNames() { - return Collections.enumeration(this.attributes.keySet()); + assertIsValid(); + return Collections.enumeration(new LinkedHashSet(this.attributes.keySet())); } @Override public String[] getValueNames() { + assertIsValid(); return this.attributes.keySet().toArray(new String[this.attributes.size()]); } @Override public void setAttribute(String name, Object value) { + assertIsValid(); Assert.notNull(name, "Attribute name must not be null"); if (value != null) { this.attributes.put(name, value); @@ -180,6 +188,7 @@ public class MockHttpSession implements HttpSession { @Override public void removeAttribute(String name) { + assertIsValid(); Assert.notNull(name, "Attribute name must not be null"); Object value = this.attributes.remove(name); if (value instanceof HttpSessionBindingListener) { @@ -214,11 +223,7 @@ public class MockHttpSession implements HttpSession { */ @Override public void invalidate() { - if (this.invalid) { - throw new IllegalStateException("The session has already been invalidated"); - } - - // else + assertIsValid(); this.invalid = true; clearAttributes(); } @@ -227,12 +232,25 @@ public class MockHttpSession implements HttpSession { return this.invalid; } + /** + * Convenience method for asserting that this session has not been + * {@linkplain #invalidate() invalidated}. + * + * @throws IllegalStateException if this session has been invalidated + */ + private void assertIsValid() { + if (isInvalid()) { + throw new IllegalStateException("The session has already been invalidated"); + } + } + public void setNew(boolean value) { this.isNew = value; } @Override public boolean isNew() { + assertIsValid(); return this.isNew; } diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockMultipartHttpServletRequest.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockMultipartHttpServletRequest.java index 825ee8b539..a0c87ea3ac 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockMultipartHttpServletRequest.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockMultipartHttpServletRequest.java @@ -35,6 +35,8 @@ import org.springframework.web.multipart.MultipartHttpServletRequest; * Mock implementation of the * {@link org.springframework.web.multipart.MultipartHttpServletRequest} interface. * + *

As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline. + * *

Useful for testing application controllers that access multipart uploads. * The {@link MockMultipartFile} can be used to populate these mock requests * with files. diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockServletContext.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockServletContext.java index 0878f4d246..57e6f024ef 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockServletContext.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockServletContext.java @@ -56,7 +56,9 @@ import org.springframework.web.util.WebUtils; /** * Mock implementation of the {@link javax.servlet.ServletContext} interface. * - *

Compatible with Servlet 3.0. Can be configured to expose a specific version + *

As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline. + * + *

Compatible with Servlet 3.0 but can be configured to expose a specific version * through {@link #setMajorVersion}/{@link #setMinorVersion}; default is 3.0. * Note that Servlet 3.0 support is limited: servlet, filter and listener * registration methods are not supported; neither is JSP configuration.