From e9f64cf9ae919800690fb53b2c3484e57a80b26e Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sun, 19 Jul 2015 20:23:51 +0200 Subject: [PATCH] Sync Servlet mocks between spring-test & spring-web --- .../mock/web/MockAsyncContext.java | 3 +- .../mock/web/MockHttpServletRequest.java | 19 ++-- .../mock/web/MockPageContext.java | 9 +- .../mock/web/test/MockAsyncContext.java | 14 ++- .../web/test/MockExpressionEvaluator.java | 48 +++++----- .../mock/web/test/MockFilterConfig.java | 2 +- .../mock/web/test/MockHttpServletRequest.java | 87 ++++++++++++++++++- .../web/test/MockHttpServletResponse.java | 21 +++-- .../mock/web/test/MockHttpSession.java | 5 +- .../mock/web/test/MockPageContext.java | 15 ++-- .../mock/web/test/MockServletContext.java | 73 +++++++++++----- 11 files changed, 217 insertions(+), 79 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockAsyncContext.java b/spring-test/src/main/java/org/springframework/mock/web/MockAsyncContext.java index 60dc2663d7..0d9f917818 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockAsyncContext.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockAsyncContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2015 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. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.mock.web; import java.io.IOException; diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java index 6c2873c4c0..4211452929 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletRequest.java @@ -53,6 +53,8 @@ import javax.servlet.http.Part; import org.springframework.http.MediaType; import org.springframework.util.Assert; import org.springframework.util.LinkedCaseInsensitiveMap; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; /** @@ -70,6 +72,7 @@ import org.springframework.util.StringUtils; * @author Mark Fisher * @author Chris Beams * @author Sam Brannen + * @author Brian Clozel * @since 1.0.2 */ public class MockHttpServletRequest implements HttpServletRequest { @@ -209,7 +212,7 @@ public class MockHttpServletRequest implements HttpServletRequest { private boolean requestedSessionIdFromURL = false; - private final Map parts = new LinkedHashMap(); + private final MultiValueMap parts = new LinkedMultiValueMap(); // --------------------------------------------------------------------- @@ -1044,8 +1047,8 @@ public class MockHttpServletRequest implements HttpServletRequest { public StringBuffer getRequestURL() { StringBuffer url = new StringBuffer(this.scheme).append("://").append(this.serverName); - if (this.serverPort > 0 && ((HTTP.equalsIgnoreCase(this.scheme) && this.serverPort != 80) || - (HTTPS.equalsIgnoreCase(this.scheme) && this.serverPort != 443))) { + if (this.serverPort > 0 + && ((HTTP.equalsIgnoreCase(this.scheme) && this.serverPort != 80) || (HTTPS.equalsIgnoreCase(this.scheme) && this.serverPort != 443))) { url.append(':').append(this.serverPort); } @@ -1157,17 +1160,21 @@ public class MockHttpServletRequest implements HttpServletRequest { } public void addPart(Part part) { - this.parts.put(part.getName(), part); + this.parts.add(part.getName(), part); } @Override public Part getPart(String name) throws IOException, IllegalStateException, ServletException { - return this.parts.get(name); + return this.parts.getFirst(name); } @Override public Collection getParts() throws IOException, IllegalStateException, ServletException { - return this.parts.values(); + List result = new LinkedList(); + for(List list : this.parts.values()) { + result.addAll(list); + } + return result; } } diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockPageContext.java b/spring-test/src/main/java/org/springframework/mock/web/MockPageContext.java index b1fbe21cd3..0e95d460b5 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockPageContext.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockPageContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2015 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. @@ -45,13 +45,12 @@ import org.springframework.util.Assert; * applications when testing custom JSP tags. * *

Note: Expects initialization via the constructor rather than via the - * {@code PageContext.initialize} method. Does not support writing to - * a JspWriter, request dispatching, and {@code handlePageException} calls. + * {@code PageContext.initialize} method. Does not support writing to a + * JspWriter, request dispatching, or {@code handlePageException} calls. * * @author Juergen Hoeller * @since 1.0.2 */ -@SuppressWarnings("deprecation") public class MockPageContext extends PageContext { private final ServletContext servletContext; @@ -287,6 +286,7 @@ public class MockPageContext extends PageContext { } @Override + @Deprecated public javax.servlet.jsp.el.ExpressionEvaluator getExpressionEvaluator() { return new MockExpressionEvaluator(this); } @@ -297,6 +297,7 @@ public class MockPageContext extends PageContext { } @Override + @Deprecated public javax.servlet.jsp.el.VariableResolver getVariableResolver() { return null; } diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockAsyncContext.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockAsyncContext.java index 69a4958582..7fb4f8ec95 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockAsyncContext.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockAsyncContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2015 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. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.mock.web.test; import java.io.IOException; @@ -29,6 +30,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.BeanUtils; +import org.springframework.util.Assert; import org.springframework.web.util.WebUtils; /** @@ -49,6 +51,8 @@ public class MockAsyncContext implements AsyncContext { private long timeout = 10 * 1000L; // 10 seconds is Tomcat's default + private final List dispatchHandlers = new ArrayList(); + public MockAsyncContext(ServletRequest request, ServletResponse response) { this.request = (HttpServletRequest) request; @@ -56,6 +60,11 @@ public class MockAsyncContext implements AsyncContext { } + public void addDispatchHandler(Runnable handler) { + Assert.notNull(handler); + this.dispatchHandlers.add(handler); + } + @Override public ServletRequest getRequest() { return this.request; @@ -84,6 +93,9 @@ public class MockAsyncContext implements AsyncContext { @Override public void dispatch(ServletContext context, String path) { this.dispatchedPath = path; + for (Runnable r : this.dispatchHandlers) { + r.run(); + } } public String getDispatchedPath() { diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockExpressionEvaluator.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockExpressionEvaluator.java index 95e5c5d436..80b3365f47 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockExpressionEvaluator.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockExpressionEvaluator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2015 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. @@ -18,30 +18,25 @@ package org.springframework.mock.web.test; import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; -import javax.servlet.jsp.el.ELException; -import javax.servlet.jsp.el.Expression; -import javax.servlet.jsp.el.ExpressionEvaluator; -import javax.servlet.jsp.el.FunctionMapper; -import javax.servlet.jsp.el.VariableResolver; import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager; /** * Mock implementation of the JSP 2.0 {@link javax.servlet.jsp.el.ExpressionEvaluator} - * interface, delegating to the Jakarta JSTL ExpressionEvaluatorManager. + * interface, delegating to the Apache JSTL ExpressionEvaluatorManager. * - *

Used for testing the web framework; only necessary for testing - * applications when testing custom JSP tags. + *

Used for testing the web framework; only necessary for testing applications + * when testing custom JSP tags. * - *

Note that the Jakarta JSTL implementation (jstl.jar, standard.jar) - * has to be available on the class path to use this expression evaluator. + *

Note that the Apache JSTL implementation (jstl.jar, standard.jar) has to be + * available on the class path to use this expression evaluator. * * @author Juergen Hoeller * @since 1.1.5 * @see org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager */ -@Deprecated -public class MockExpressionEvaluator extends ExpressionEvaluator { +@SuppressWarnings("deprecation") +public class MockExpressionEvaluator extends javax.servlet.jsp.el.ExpressionEvaluator { private final PageContext pageContext; @@ -54,23 +49,24 @@ public class MockExpressionEvaluator extends ExpressionEvaluator { this.pageContext = pageContext; } - @Override - public Expression parseExpression( - final String expression, final Class expectedType, final FunctionMapper functionMapper) - throws ELException { - return new Expression() { + @Override + @SuppressWarnings("rawtypes") + public javax.servlet.jsp.el.Expression parseExpression(final String expression, final Class expectedType, + final javax.servlet.jsp.el.FunctionMapper functionMapper) throws javax.servlet.jsp.el.ELException { + + return new javax.servlet.jsp.el.Expression() { @Override - public Object evaluate(VariableResolver variableResolver) throws ELException { + public Object evaluate(javax.servlet.jsp.el.VariableResolver variableResolver) throws javax.servlet.jsp.el.ELException { return doEvaluate(expression, expectedType, functionMapper); } }; } @Override - public Object evaluate( - String expression, Class expectedType, VariableResolver variableResolver, FunctionMapper functionMapper) - throws ELException { + @SuppressWarnings("rawtypes") + public Object evaluate(String expression, Class expectedType, javax.servlet.jsp.el.VariableResolver variableResolver, + javax.servlet.jsp.el.FunctionMapper functionMapper) throws javax.servlet.jsp.el.ELException { if (variableResolver != null) { throw new IllegalArgumentException("Custom VariableResolver not supported"); @@ -78,9 +74,9 @@ public class MockExpressionEvaluator extends ExpressionEvaluator { return doEvaluate(expression, expectedType, functionMapper); } - protected Object doEvaluate( - String expression, Class expectedType, FunctionMapper functionMapper) - throws ELException { + @SuppressWarnings("rawtypes") + protected Object doEvaluate(String expression, Class expectedType, javax.servlet.jsp.el.FunctionMapper functionMapper) + throws javax.servlet.jsp.el.ELException { if (functionMapper != null) { throw new IllegalArgumentException("Custom FunctionMapper not supported"); @@ -89,7 +85,7 @@ public class MockExpressionEvaluator extends ExpressionEvaluator { return ExpressionEvaluatorManager.evaluate("JSP EL expression", expression, expectedType, this.pageContext); } catch (JspException ex) { - throw new ELException("Parsing of JSP EL expression \"" + expression + "\" failed", ex); + throw new javax.servlet.jsp.el.ELException("Parsing of JSP EL expression \"" + expression + "\" failed", ex); } } diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockFilterConfig.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockFilterConfig.java index 12fbe613ec..116312d4c4 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockFilterConfig.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockFilterConfig.java @@ -28,7 +28,7 @@ import org.springframework.util.Assert; /** * Mock implementation of the {@link javax.servlet.FilterConfig} interface. * - *

Used for testing the web framework; also usefol for testing + *

Used for testing the web framework; also useful for testing * custom {@link javax.servlet.Filter} implementations. * * @author Juergen Hoeller 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 144977e6e4..40b9a8ce1f 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 @@ -60,7 +60,11 @@ 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 designed on a Servlet 3.0 baseline. + *

The default, preferred {@link Locale} for the server mocked by this request + * is {@link Locale#ENGLISH}. This value can be changed via {@link #addPreferredLocale} + * or {@link #setPreferredLocales}. + * + *

As of Spring Framework 4.0, this set of mocks is designed on a Servlet 3.0 baseline. * * @author Juergen Hoeller * @author Rod Johnson @@ -73,10 +77,14 @@ import org.springframework.util.StringUtils; */ public class MockHttpServletRequest implements HttpServletRequest { + private static final String HTTP = "http"; + + private static final String HTTPS = "https"; + /** * The default protocol: 'http'. */ - public static final String DEFAULT_PROTOCOL = "http"; + public static final String DEFAULT_PROTOCOL = HTTP; /** * The default server address: '127.0.0.1'. @@ -105,6 +113,8 @@ public class MockHttpServletRequest implements HttpServletRequest { private static final String CONTENT_TYPE_HEADER = "Content-Type"; + private static final String HOST_HEADER = "Host"; + private static final String CHARSET_PREFIX = "charset="; private static final ServletInputStream EMPTY_SERVLET_INPUT_STREAM = @@ -551,6 +561,19 @@ public class MockHttpServletRequest implements HttpServletRequest { @Override public String getServerName() { + String host = getHeader(HOST_HEADER); + if (host != null) { + host = host.trim(); + if (host.startsWith("[")) { + host = host.substring(1, host.indexOf(']')); + } + else if (host.contains(":")) { + host = host.substring(0, host.indexOf(':')); + } + return host; + } + + // else return this.serverName; } @@ -560,6 +583,22 @@ public class MockHttpServletRequest implements HttpServletRequest { @Override public int getServerPort() { + String host = getHeader(HOST_HEADER); + if (host != null) { + host = host.trim(); + int idx; + if (host.startsWith("[")) { + idx = host.indexOf(':', host.indexOf(']')); + } + else { + idx = host.indexOf(':'); + } + if (idx != -1) { + return Integer.parseInt(host.substring(idx + 1)); + } + } + + // else return this.serverPort; } @@ -641,23 +680,61 @@ public class MockHttpServletRequest implements HttpServletRequest { this.locales.addAll(locales); } + /** + * Returns the first preferred {@linkplain Locale locale} configured + * in this mock request. + *

If no locales have been explicitly configured, the default, + * preferred {@link Locale} for the server mocked by this + * request is {@link Locale#ENGLISH}. + *

In contrast to the Servlet specification, this mock implementation + * does not take into consideration any locales + * specified via the {@code Accept-Language} header. + * @see javax.servlet.ServletRequest#getLocale() + * @see #addPreferredLocale(Locale) + * @see #setPreferredLocales(List) + */ @Override public Locale getLocale() { return this.locales.get(0); } + /** + * Returns an {@linkplain Enumeration enumeration} of the preferred + * {@linkplain Locale locales} configured in this mock request. + *

If no locales have been explicitly configured, the default, + * preferred {@link Locale} for the server mocked by this + * request is {@link Locale#ENGLISH}. + *

In contrast to the Servlet specification, this mock implementation + * does not take into consideration any locales + * specified via the {@code Accept-Language} header. + * @see javax.servlet.ServletRequest#getLocales() + * @see #addPreferredLocale(Locale) + * @see #setPreferredLocales(List) + */ @Override public Enumeration getLocales() { return Collections.enumeration(this.locales); } + /** + * Set the boolean {@code secure} flag indicating whether the mock request + * was made using a secure channel, such as HTTPS. + * @see #isSecure() + * @see #getScheme() + * @see #setScheme(String) + */ public void setSecure(boolean secure) { this.secure = secure; } + /** + * Returns {@code true} if the {@link #setSecure secure} flag has been set + * to {@code true} or if the {@link #getScheme scheme} is {@code https}. + * @see javax.servlet.ServletRequest#isSecure() + */ @Override public boolean isSecure() { - return this.secure; + return (this.secure || HTTPS.equalsIgnoreCase(this.scheme)); } @Override @@ -666,6 +743,7 @@ public class MockHttpServletRequest implements HttpServletRequest { } @Override + @Deprecated public String getRealPath(String path) { return this.servletContext.getRealPath(path); } @@ -970,7 +1048,7 @@ public class MockHttpServletRequest implements HttpServletRequest { 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))) { + && ((HTTP.equalsIgnoreCase(this.scheme) && this.serverPort != 80) || (HTTPS.equalsIgnoreCase(this.scheme) && this.serverPort != 443))) { url.append(':').append(this.serverPort); } @@ -1059,6 +1137,7 @@ public class MockHttpServletRequest implements HttpServletRequest { } @Override + @Deprecated public boolean isRequestedSessionIdFromUrl() { return isRequestedSessionIdFromURL(); } 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 a1e7e243e1..937209957d 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 @@ -72,7 +72,7 @@ public class MockHttpServletResponse implements HttpServletResponse { private boolean charset = false; - private final ByteArrayOutputStream content = new ByteArrayOutputStream(); + private final ByteArrayOutputStream content = new ByteArrayOutputStream(1024); private final ServletOutputStream outputStream = new ResponseServletOutputStream(this.content); @@ -140,6 +140,14 @@ public class MockHttpServletResponse implements HttpServletResponse { return this.writerAccessAllowed; } + /** + * Return whether the character encoding has been set. + *

If {@code false}, {@link #getCharacterEncoding()} will return a default encoding value. + */ + public boolean isCharset() { + return charset; + } + @Override public void setCharacterEncoding(String characterEncoding) { this.characterEncoding = characterEncoding; @@ -190,8 +198,8 @@ public class MockHttpServletResponse implements HttpServletResponse { public String getContentAsString() throws UnsupportedEncodingException { flushBuffer(); - return (this.characterEncoding != null) ? - this.content.toString(this.characterEncoding) : this.content.toString(); + return (this.characterEncoding != null ? + this.content.toString(this.characterEncoding) : this.content.toString()); } @Override @@ -426,11 +434,13 @@ public class MockHttpServletResponse implements HttpServletResponse { } @Override + @Deprecated public String encodeUrl(String url) { return encodeURL(url); } @Override + @Deprecated public String encodeRedirectUrl(String url) { return encodeRedirectURL(url); } @@ -544,14 +554,15 @@ public class MockHttpServletResponse implements HttpServletResponse { @Override public void setStatus(int status) { - if(!this.isCommitted()) { + if (!this.isCommitted()) { this.status = status; } } @Override + @Deprecated public void setStatus(int status, String errorMessage) { - if(!this.isCommitted()) { + if (!this.isCommitted()) { this.status = status; this.errorMessage = errorMessage; } 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 5883ed063c..6e1b21f0be 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-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -28,7 +28,6 @@ import javax.servlet.ServletContext; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; -import javax.servlet.http.HttpSessionContext; import org.springframework.util.Assert; @@ -148,7 +147,7 @@ public class MockHttpSession implements HttpSession { } @Override - public HttpSessionContext getSessionContext() { + public javax.servlet.http.HttpSessionContext getSessionContext() { throw new UnsupportedOperationException("getSessionContext"); } diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockPageContext.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockPageContext.java index 54a0fc0025..95acca3f91 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockPageContext.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockPageContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2015 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. @@ -21,6 +21,7 @@ import java.io.UnsupportedEncodingException; import java.util.Collections; import java.util.Enumeration; import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.Map; import javax.el.ELContext; import javax.servlet.Servlet; @@ -34,8 +35,6 @@ import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.PageContext; -import javax.servlet.jsp.el.ExpressionEvaluator; -import javax.servlet.jsp.el.VariableResolver; import org.springframework.util.Assert; @@ -46,8 +45,8 @@ import org.springframework.util.Assert; * applications when testing custom JSP tags. * *

Note: Expects initialization via the constructor rather than via the - * {@code PageContext.initialize} method. Does not support writing to - * a JspWriter, request dispatching, and {@code handlePageException} calls. + * {@code PageContext.initialize} method. Does not support writing to a + * JspWriter, request dispatching, or {@code handlePageException} calls. * * @author Juergen Hoeller * @since 1.0.2 @@ -258,7 +257,7 @@ public class MockPageContext extends PageContext { } public Enumeration getAttributeNames() { - return Collections.enumeration(this.attributes.keySet()); + return Collections.enumeration(new LinkedHashSet(this.attributes.keySet())); } @Override @@ -288,7 +287,7 @@ public class MockPageContext extends PageContext { @Override @Deprecated - public ExpressionEvaluator getExpressionEvaluator() { + public javax.servlet.jsp.el.ExpressionEvaluator getExpressionEvaluator() { return new MockExpressionEvaluator(this); } @@ -299,7 +298,7 @@ public class MockPageContext extends PageContext { @Override @Deprecated - public VariableResolver getVariableResolver() { + public javax.servlet.jsp.el.VariableResolver getVariableResolver() { return null; } 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 57e6f024ef..0dcecd73e5 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2015 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. @@ -62,7 +62,7 @@ import org.springframework.web.util.WebUtils; * 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. - * We generally do not recommend to unit-test your ServletContainerInitializers and + * We generally do not recommend to unit test your ServletContainerInitializers and * WebApplicationInitializers which is where those registration methods would be used. * *

Used for testing the Spring web framework; only rarely necessary for testing @@ -152,8 +152,8 @@ public class MockServletContext implements ServletContext { /** - * Create a new MockServletContext, using no base path and a - * DefaultResourceLoader (i.e. the classpath root as WAR root). + * Create a new {@code MockServletContext}, using no base path and a + * {@link DefaultResourceLoader} (i.e. the classpath root as WAR root). * @see org.springframework.core.io.DefaultResourceLoader */ public MockServletContext() { @@ -161,7 +161,7 @@ public class MockServletContext implements ServletContext { } /** - * Create a new MockServletContext, using a DefaultResourceLoader. + * Create a new {@code MockServletContext}, using a {@link DefaultResourceLoader}. * @param resourceBasePath the root directory of the WAR (should not end with a slash) * @see org.springframework.core.io.DefaultResourceLoader */ @@ -170,7 +170,7 @@ public class MockServletContext implements ServletContext { } /** - * Create a new MockServletContext, using the specified ResourceLoader + * Create a new {@code MockServletContext}, using the specified {@link ResourceLoader} * and no base path. * @param resourceLoader the ResourceLoader to use (or null for the default) */ @@ -179,10 +179,10 @@ public class MockServletContext implements ServletContext { } /** - * Create a new MockServletContext using the supplied resource base path and - * resource loader. + * Create a new {@code MockServletContext} using the supplied resource base + * path and resource loader. *

Registers a {@link MockRequestDispatcher} for the Servlet named - * {@value #COMMON_DEFAULT_SERVLET_NAME}. + * {@literal 'default'}. * @param resourceBasePath the root directory of the WAR (should not end with a slash) * @param resourceLoader the ResourceLoader to use (or null for the default) * @see #registerNamedDispatcher @@ -201,8 +201,8 @@ public class MockServletContext implements ServletContext { } /** - * Build a full resource location for the given path, - * prepending the resource base path of this MockServletContext. + * Build a full resource location for the given path, prepending the resource + * base path of this {@code MockServletContext}. * @param path the path as specified * @return the full resource path */ @@ -271,10 +271,23 @@ public class MockServletContext implements ServletContext { } /** - * This method uses the Java Activation framework, which returns "application/octet-stream" - * when the mime type is unknown (i.e. it never returns {@code null}). In order to maintain - * the {@link ServletContext#getMimeType(String)} contract, this method returns {@code null} - * if the mimeType is "application/octet-stream", as of Spring 3.2.2. + * This method uses the default + * {@link javax.activation.FileTypeMap#getDefaultFileTypeMap() FileTypeMap} + * from the Java Activation Framework to resolve MIME types. + *

The Java Activation Framework returns {@code "application/octet-stream"} + * if the MIME type is unknown (i.e., it never returns {@code null}). Thus, in + * order to honor the {@link ServletContext#getMimeType(String)} contract, + * this method returns {@code null} if the MIME type is + * {@code "application/octet-stream"}. + *

{@code MockServletContext} does not provide a direct mechanism for + * setting a custom MIME type; however, if the default {@code FileTypeMap} + * is an instance of {@code javax.activation.MimetypesFileTypeMap}, a custom + * MIME type named {@code text/enigma} can be registered for a custom + * {@code .puzzle} file extension in the following manner: + *

+	 * MimetypesFileTypeMap mimetypesFileTypeMap = (MimetypesFileTypeMap) FileTypeMap.getDefaultFileTypeMap();
+	 * mimetypesFileTypeMap.addMimeTypes("text/enigma    puzzle");
+	 * 
*/ @Override public String getMimeType(String filePath) { @@ -383,7 +396,7 @@ public class MockServletContext implements ServletContext { /** * Get the name of the default {@code Servlet}. - *

Defaults to {@value #COMMON_DEFAULT_SERVLET_NAME}. + *

Defaults to {@literal 'default'}. * @see #setDefaultServletName */ public String getDefaultServletName() { @@ -408,16 +421,19 @@ public class MockServletContext implements ServletContext { } @Override + @Deprecated public Servlet getServlet(String name) { return null; } @Override + @Deprecated public Enumeration getServlets() { return Collections.enumeration(new HashSet()); } @Override + @Deprecated public Enumeration getServletNames() { return Collections.enumeration(new HashSet()); } @@ -428,6 +444,7 @@ public class MockServletContext implements ServletContext { } @Override + @Deprecated public void log(Exception ex, String message) { logger.info(message, ex); } @@ -587,14 +604,22 @@ public class MockServletContext implements ServletContext { throw new UnsupportedOperationException(); } + /** + * This method always returns {@code null}. + * @see javax.servlet.ServletContext#getServletRegistration(java.lang.String) + */ @Override public ServletRegistration getServletRegistration(String servletName) { - throw new UnsupportedOperationException(); + return null; } + /** + * This method always returns an {@linkplain Collections#emptyMap empty map}. + * @see javax.servlet.ServletContext#getServletRegistrations() + */ @Override public Map getServletRegistrations() { - throw new UnsupportedOperationException(); + return Collections.emptyMap(); } @Override @@ -617,14 +642,22 @@ public class MockServletContext implements ServletContext { throw new UnsupportedOperationException(); } + /** + * This method always returns {@code null}. + * @see javax.servlet.ServletContext#getFilterRegistration(java.lang.String) + */ @Override public FilterRegistration getFilterRegistration(String filterName) { - throw new UnsupportedOperationException(); + return null; } + /** + * This method always returns an {@linkplain Collections#emptyMap empty map}. + * @see javax.servlet.ServletContext#getFilterRegistrations() + */ @Override public Map getFilterRegistrations() { - throw new UnsupportedOperationException(); + return Collections.emptyMap(); } @Override