Upgraded all Servlet API mocks to Servlet 3.0 (with a little bit of Servlet 3.1 support in MockHttpServletResponse)
This commit is contained in:
parent
d03de21d62
commit
deba32cad9
|
@ -484,8 +484,7 @@ project("spring-orm") {
|
||||||
optional("org.hibernate:hibernate-entitymanager:3.6.9.Final")
|
optional("org.hibernate:hibernate-entitymanager:3.6.9.Final")
|
||||||
optional("org.apache.openjpa:openjpa:2.2.1")
|
optional("org.apache.openjpa:openjpa:2.2.1")
|
||||||
optional("javax.jdo:jdo-api:3.0")
|
optional("javax.jdo:jdo-api:3.0")
|
||||||
provided("javax.servlet:servlet-api:2.5")
|
provided("javax.servlet:javax.servlet-api:3.0.1")
|
||||||
testCompile("javax.servlet:javax.servlet-api:3.0.1")
|
|
||||||
testCompile("org.slf4j:slf4j-jcl:${slf4jVersion}")
|
testCompile("org.slf4j:slf4j-jcl:${slf4jVersion}")
|
||||||
testCompile("commons-dbcp:commons-dbcp:1.2.2")
|
testCompile("commons-dbcp:commons-dbcp:1.2.2")
|
||||||
testCompile("hsqldb:hsqldb:${hsqldbVersion}")
|
testCompile("hsqldb:hsqldb:${hsqldbVersion}")
|
||||||
|
@ -508,7 +507,7 @@ project("spring-orm-hibernate4") {
|
||||||
optional("org.hibernate:hibernate-core:4.1.9.Final")
|
optional("org.hibernate:hibernate-core:4.1.9.Final")
|
||||||
optional("org.hibernate:hibernate-entitymanager:4.1.9.Final")
|
optional("org.hibernate:hibernate-entitymanager:4.1.9.Final")
|
||||||
optional(project(":spring-web"))
|
optional(project(":spring-web"))
|
||||||
optional("javax.servlet:servlet-api:2.5")
|
optional("javax.servlet:javax.servlet-api:3.0.1")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -648,7 +647,7 @@ project("spring-test") {
|
||||||
optional(project(":spring-webmvc-portlet"), )
|
optional(project(":spring-webmvc-portlet"), )
|
||||||
optional("junit:junit:${junitVersion}")
|
optional("junit:junit:${junitVersion}")
|
||||||
optional("org.testng:testng:6.5.2")
|
optional("org.testng:testng:6.5.2")
|
||||||
optional("javax.servlet:servlet-api:2.5")
|
optional("javax.servlet:javax.servlet-api:3.0.1")
|
||||||
optional("javax.servlet.jsp:jsp-api:2.1")
|
optional("javax.servlet.jsp:jsp-api:2.1")
|
||||||
optional("javax.portlet:portlet-api:2.0")
|
optional("javax.portlet:portlet-api:2.0")
|
||||||
optional("org.eclipse.persistence:javax.persistence:2.0.0")
|
optional("org.eclipse.persistence:javax.persistence:2.0.0")
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -17,7 +17,6 @@
|
||||||
package org.springframework.test.web.servlet.request;
|
package org.springframework.test.web.servlet.request;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.lang.reflect.Constructor;
|
|
||||||
import java.security.Principal;
|
import java.security.Principal;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
@ -26,12 +25,10 @@ import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
import javax.servlet.ServletContext;
|
import javax.servlet.ServletContext;
|
||||||
import javax.servlet.ServletRequest;
|
import javax.servlet.ServletRequest;
|
||||||
import javax.servlet.http.Cookie;
|
import javax.servlet.http.Cookie;
|
||||||
|
|
||||||
import org.springframework.beans.BeanUtils;
|
|
||||||
import org.springframework.beans.Mergeable;
|
import org.springframework.beans.Mergeable;
|
||||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
|
@ -44,7 +41,6 @@ import org.springframework.test.web.servlet.MockMvc;
|
||||||
import org.springframework.test.web.servlet.RequestBuilder;
|
import org.springframework.test.web.servlet.RequestBuilder;
|
||||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.ClassUtils;
|
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
import org.springframework.util.ObjectUtils;
|
import org.springframework.util.ObjectUtils;
|
||||||
|
@ -69,12 +65,10 @@ import org.springframework.web.util.UriUtils;
|
||||||
*
|
*
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
* @author Arjen Poutsma
|
* @author Arjen Poutsma
|
||||||
* @since 3.2
|
* @since 3.2
|
||||||
*/
|
*/
|
||||||
public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable {
|
public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable {
|
||||||
|
|
||||||
static final boolean servlet3Present = ClassUtils.hasMethod(ServletRequest.class, "startAsync");
|
|
||||||
|
|
||||||
private final UriComponents uriComponents;
|
private final UriComponents uriComponents;
|
||||||
|
|
||||||
private final HttpMethod method;
|
private final HttpMethod method;
|
||||||
|
@ -547,7 +541,6 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
||||||
* Build a {@link MockHttpServletRequest}.
|
* Build a {@link MockHttpServletRequest}.
|
||||||
*/
|
*/
|
||||||
public final MockHttpServletRequest buildRequest(ServletContext servletContext) {
|
public final MockHttpServletRequest buildRequest(ServletContext servletContext) {
|
||||||
|
|
||||||
MockHttpServletRequest request = createServletRequest(servletContext);
|
MockHttpServletRequest request = createServletRequest(servletContext);
|
||||||
|
|
||||||
String requestUri = this.uriComponents.getPath();
|
String requestUri = this.uriComponents.getPath();
|
||||||
|
@ -645,23 +638,11 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new {@link MockHttpServletRequest} based on the given
|
* Create a new {@link MockHttpServletRequest} based on the given
|
||||||
* {@link ServletContext}. Can be overridden in sub-classes.
|
* {@link ServletContext}. Can be overridden in subclasses.
|
||||||
*/
|
*/
|
||||||
protected MockHttpServletRequest createServletRequest(ServletContext servletContext) {
|
protected MockHttpServletRequest createServletRequest(ServletContext servletContext) {
|
||||||
return servlet3Present ? createServlet3Request(servletContext) : new MockHttpServletRequest(servletContext);
|
return new MockHttpServletRequest(servletContext);
|
||||||
}
|
|
||||||
|
|
||||||
private MockHttpServletRequest createServlet3Request(ServletContext servletContext) {
|
|
||||||
try {
|
|
||||||
String className = "org.springframework.test.web.servlet.request.Servlet3MockHttpServletRequest";
|
|
||||||
Class<?> clazz = ClassUtils.forName(className, MockHttpServletRequestBuilder.class.getClassLoader());
|
|
||||||
Constructor<?> constructor = clazz.getConstructor(ServletContext.class);
|
|
||||||
return (MockHttpServletRequest) BeanUtils.instantiateClass(constructor, servletContext);
|
|
||||||
}
|
|
||||||
catch (Throwable t) {
|
|
||||||
throw new IllegalStateException("Failed to instantiate MockHttpServletRequest", t);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -16,19 +16,15 @@
|
||||||
|
|
||||||
package org.springframework.test.web.servlet.request;
|
package org.springframework.test.web.servlet.request;
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.servlet.ServletContext;
|
import javax.servlet.ServletContext;
|
||||||
|
|
||||||
import org.springframework.beans.BeanUtils;
|
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.mock.web.MockHttpServletRequest;
|
import org.springframework.mock.web.MockHttpServletRequest;
|
||||||
import org.springframework.mock.web.MockMultipartFile;
|
import org.springframework.mock.web.MockMultipartFile;
|
||||||
import org.springframework.mock.web.MockMultipartHttpServletRequest;
|
import org.springframework.mock.web.MockMultipartHttpServletRequest;
|
||||||
import org.springframework.util.ClassUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default builder for {@link MockMultipartHttpServletRequest}.
|
* Default builder for {@link MockMultipartHttpServletRequest}.
|
||||||
|
@ -99,24 +95,11 @@ public class MockMultipartHttpServletRequestBuilder extends MockHttpServletReque
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected final MockHttpServletRequest createServletRequest(ServletContext servletContext) {
|
protected final MockHttpServletRequest createServletRequest(ServletContext servletContext) {
|
||||||
MockMultipartHttpServletRequest request = servlet3Present ?
|
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(servletContext);
|
||||||
createServlet3Request() : new MockMultipartHttpServletRequest();
|
|
||||||
for (MockMultipartFile file : this.files) {
|
for (MockMultipartFile file : this.files) {
|
||||||
request.addFile(file);
|
request.addFile(file);
|
||||||
}
|
}
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
private MockMultipartHttpServletRequest createServlet3Request() {
|
|
||||||
try {
|
|
||||||
String className = "org.springframework.test.web.servlet.request.Servlet3MockMultipartHttpServletRequest";
|
|
||||||
Class<?> clazz = ClassUtils.forName(className, MockMultipartHttpServletRequestBuilder.class.getClassLoader());
|
|
||||||
Constructor<?> constructor = clazz.getDeclaredConstructor();
|
|
||||||
return (MockMultipartHttpServletRequest) BeanUtils.instantiateClass(constructor);
|
|
||||||
}
|
|
||||||
catch (Throwable t) {
|
|
||||||
throw new IllegalStateException("Failed to instantiate MockHttpServletRequest", t);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,130 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.springframework.mock.web;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import javax.servlet.AsyncContext;
|
||||||
|
import javax.servlet.AsyncEvent;
|
||||||
|
import javax.servlet.AsyncListener;
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.ServletRequest;
|
||||||
|
import javax.servlet.ServletResponse;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import org.springframework.beans.BeanUtils;
|
||||||
|
import org.springframework.web.util.WebUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mock implementation of the {@link AsyncContext} interface.
|
||||||
|
*
|
||||||
|
* @author Rossen Stoyanchev
|
||||||
|
* @since 3.2
|
||||||
|
*/
|
||||||
|
public class MockAsyncContext implements AsyncContext {
|
||||||
|
|
||||||
|
private final HttpServletRequest request;
|
||||||
|
|
||||||
|
private final HttpServletResponse response;
|
||||||
|
|
||||||
|
private final List<AsyncListener> listeners = new ArrayList<AsyncListener>();
|
||||||
|
|
||||||
|
private String dispatchedPath;
|
||||||
|
|
||||||
|
private long timeout = 10 * 1000L; // 10 seconds is Tomcat's default
|
||||||
|
|
||||||
|
|
||||||
|
public MockAsyncContext(ServletRequest request, ServletResponse response) {
|
||||||
|
this.request = (HttpServletRequest) request;
|
||||||
|
this.response = (HttpServletResponse) response;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public ServletRequest getRequest() {
|
||||||
|
return this.request;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServletResponse getResponse() {
|
||||||
|
return this.response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasOriginalRequestAndResponse() {
|
||||||
|
return (this.request instanceof MockHttpServletRequest) && (this.response instanceof MockHttpServletResponse);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void dispatch() {
|
||||||
|
dispatch(this.request.getRequestURI());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void dispatch(String path) {
|
||||||
|
dispatch(null, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void dispatch(ServletContext context, String path) {
|
||||||
|
this.dispatchedPath = path;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDispatchedPath() {
|
||||||
|
return this.dispatchedPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void complete() {
|
||||||
|
MockHttpServletRequest mockRequest = WebUtils.getNativeRequest(request, MockHttpServletRequest.class);
|
||||||
|
if (mockRequest != null) {
|
||||||
|
mockRequest.setAsyncStarted(false);
|
||||||
|
}
|
||||||
|
for (AsyncListener listener : this.listeners) {
|
||||||
|
try {
|
||||||
|
listener.onComplete(new AsyncEvent(this, this.request, this.response));
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
throw new IllegalStateException("AsyncListener failure", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start(Runnable runnable) {
|
||||||
|
runnable.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addListener(AsyncListener listener) {
|
||||||
|
this.listeners.add(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addListener(AsyncListener listener, ServletRequest request, ServletResponse response) {
|
||||||
|
this.listeners.add(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<AsyncListener> getListeners() {
|
||||||
|
return this.listeners;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T extends AsyncListener> T createListener(Class<T> clazz) throws ServletException {
|
||||||
|
return BeanUtils.instantiateClass(clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTimeout(long timeout) {
|
||||||
|
this.timeout = timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getTimeout() {
|
||||||
|
return this.timeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -36,14 +36,19 @@ import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import javax.servlet.AsyncContext;
|
||||||
|
import javax.servlet.DispatcherType;
|
||||||
import javax.servlet.RequestDispatcher;
|
import javax.servlet.RequestDispatcher;
|
||||||
import javax.servlet.ServletContext;
|
import javax.servlet.ServletContext;
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.ServletInputStream;
|
import javax.servlet.ServletInputStream;
|
||||||
|
import javax.servlet.ServletRequest;
|
||||||
|
import javax.servlet.ServletResponse;
|
||||||
import javax.servlet.http.Cookie;
|
import javax.servlet.http.Cookie;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import javax.servlet.http.HttpSession;
|
import javax.servlet.http.HttpSession;
|
||||||
|
import javax.servlet.http.Part;
|
||||||
|
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.LinkedCaseInsensitiveMap;
|
import org.springframework.util.LinkedCaseInsensitiveMap;
|
||||||
|
@ -51,13 +56,13 @@ import org.springframework.util.LinkedCaseInsensitiveMap;
|
||||||
/**
|
/**
|
||||||
* Mock implementation of the {@link javax.servlet.http.HttpServletRequest} interface.
|
* Mock implementation of the {@link javax.servlet.http.HttpServletRequest} interface.
|
||||||
*
|
*
|
||||||
* <p>Compatible with Servlet 2.5 and partially with Servlet 3.0 (notable exceptions:
|
* <p>As of Spring 4.0, this set of mocks is designed on a Servlet 3.0 baseline.
|
||||||
* the {@code getPart(s)} and {@code startAsync} families of methods).
|
|
||||||
*
|
*
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Rod Johnson
|
* @author Rod Johnson
|
||||||
* @author Rick Evans
|
* @author Rick Evans
|
||||||
* @author Mark Fisher
|
* @author Mark Fisher
|
||||||
|
* @author Chris Beams
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
* @since 1.0.2
|
* @since 1.0.2
|
||||||
*/
|
*/
|
||||||
|
@ -142,6 +147,14 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||||
|
|
||||||
private int localPort = DEFAULT_SERVER_PORT;
|
private int localPort = DEFAULT_SERVER_PORT;
|
||||||
|
|
||||||
|
private boolean asyncStarted = false;
|
||||||
|
|
||||||
|
private boolean asyncSupported = false;
|
||||||
|
|
||||||
|
private MockAsyncContext asyncContext;
|
||||||
|
|
||||||
|
private DispatcherType dispatcherType = DispatcherType.REQUEST;
|
||||||
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
// HttpServletRequest properties
|
// HttpServletRequest properties
|
||||||
|
@ -181,6 +194,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||||
|
|
||||||
private boolean requestedSessionIdFromURL = false;
|
private boolean requestedSessionIdFromURL = false;
|
||||||
|
|
||||||
|
private final Map<String, Part> parts = new LinkedHashMap<String, Part>();
|
||||||
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
// Constructors
|
// Constructors
|
||||||
|
@ -210,8 +225,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new {@code MockHttpServletRequest} with the supplied {@link ServletContext}.
|
* Create a new {@code MockHttpServletRequest} with the supplied {@link ServletContext}.
|
||||||
* @param servletContext the ServletContext that the request runs in (may be
|
* @param servletContext the ServletContext that the request runs in
|
||||||
* {@code null} to use a default {@link MockServletContext})
|
* (may be {@code null} to use a default {@link MockServletContext})
|
||||||
* @see #MockHttpServletRequest(ServletContext, String, String)
|
* @see #MockHttpServletRequest(ServletContext, String, String)
|
||||||
*/
|
*/
|
||||||
public MockHttpServletRequest(ServletContext servletContext) {
|
public MockHttpServletRequest(ServletContext servletContext) {
|
||||||
|
@ -640,6 +655,51 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||||
return this.localPort;
|
return this.localPort;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public AsyncContext startAsync() {
|
||||||
|
return startAsync(this, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AsyncContext startAsync(ServletRequest request, ServletResponse response) {
|
||||||
|
if (!this.asyncSupported) {
|
||||||
|
throw new IllegalStateException("Async not supported");
|
||||||
|
}
|
||||||
|
this.asyncStarted = true;
|
||||||
|
this.asyncContext = new MockAsyncContext(request, response);
|
||||||
|
return this.asyncContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAsyncStarted(boolean asyncStarted) {
|
||||||
|
this.asyncStarted = asyncStarted;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isAsyncStarted() {
|
||||||
|
return this.asyncStarted;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAsyncSupported(boolean asyncSupported) {
|
||||||
|
this.asyncSupported = asyncSupported;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isAsyncSupported() {
|
||||||
|
return this.asyncSupported;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAsyncContext(MockAsyncContext asyncContext) {
|
||||||
|
this.asyncContext = asyncContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AsyncContext getAsyncContext() {
|
||||||
|
return this.asyncContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDispatcherType(DispatcherType dispatcherType) {
|
||||||
|
this.dispatcherType = dispatcherType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DispatcherType getDispatcherType() {
|
||||||
|
return this.dispatcherType;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
// HttpServletRequest interface
|
// HttpServletRequest interface
|
||||||
|
@ -715,8 +775,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||||
return ((Number) value).longValue();
|
return ((Number) value).longValue();
|
||||||
}
|
}
|
||||||
else if (value != null) {
|
else if (value != null) {
|
||||||
throw new IllegalArgumentException("Value for header '" + name + "' is neither a Date nor a Number: "
|
throw new IllegalArgumentException(
|
||||||
+ value);
|
"Value for header '" + name + "' is neither a Date nor a Number: " + value);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return -1L;
|
return -1L;
|
||||||
|
@ -900,11 +960,11 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean authenticate(HttpServletResponse response) throws IOException, ServletException {
|
public boolean authenticate(HttpServletResponse response) throws IOException, ServletException {
|
||||||
return (this.userPrincipal != null && this.remoteUser != null && this.authType != null);
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void login(String username, String password) throws ServletException {
|
public void login(String username, String password) throws ServletException {
|
||||||
throw new ServletException("Username-password authentication not supported - override the login method");
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void logout() throws ServletException {
|
public void logout() throws ServletException {
|
||||||
|
@ -913,4 +973,16 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||||
this.authType = null;
|
this.authType = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addPart(Part part) {
|
||||||
|
this.parts.put(part.getName(), part);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Part getPart(String name) throws IOException, IllegalStateException, ServletException {
|
||||||
|
return this.parts.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Collection<Part> getParts() throws IOException, IllegalStateException, ServletException {
|
||||||
|
return this.parts.values();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,8 @@ import org.springframework.web.util.WebUtils;
|
||||||
/**
|
/**
|
||||||
* Mock implementation of the {@link javax.servlet.http.HttpServletResponse} interface.
|
* Mock implementation of the {@link javax.servlet.http.HttpServletResponse} interface.
|
||||||
*
|
*
|
||||||
* <p>Compatible with Servlet 2.5 as well as Servlet 3.0.
|
* <p>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.
|
||||||
*
|
*
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Rod Johnson
|
* @author Rod Johnson
|
||||||
|
@ -75,7 +76,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||||
|
|
||||||
private PrintWriter writer;
|
private PrintWriter writer;
|
||||||
|
|
||||||
private int contentLength = 0;
|
private long contentLength = 0;
|
||||||
|
|
||||||
private String contentType;
|
private String contentType;
|
||||||
|
|
||||||
|
@ -193,6 +194,15 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getContentLength() {
|
public int getContentLength() {
|
||||||
|
return (int) this.contentLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContentLengthLong(long contentLength) {
|
||||||
|
this.contentLength = contentLength;
|
||||||
|
doAddHeaderValue(CONTENT_LENGTH_HEADER, contentLength, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getContentLengthLong() {
|
||||||
return this.contentLength;
|
return this.contentLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -21,6 +21,7 @@ import java.util.Enumeration;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
|
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
|
@ -50,7 +51,22 @@ public class MockMultipartHttpServletRequest extends MockHttpServletRequest impl
|
||||||
new LinkedMultiValueMap<String, MultipartFile>();
|
new LinkedMultiValueMap<String, MultipartFile>();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new {@code MockMultipartHttpServletRequest} with a default
|
||||||
|
* {@link MockServletContext}.
|
||||||
|
* @see #MockMultipartHttpServletRequest(ServletContext)
|
||||||
|
*/
|
||||||
public MockMultipartHttpServletRequest() {
|
public MockMultipartHttpServletRequest() {
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new {@code MockMultipartHttpServletRequest} with the supplied {@link ServletContext}.
|
||||||
|
* @param servletContext the ServletContext that the request runs in
|
||||||
|
* (may be {@code null} to use a default {@link MockServletContext})
|
||||||
|
*/
|
||||||
|
public MockMultipartHttpServletRequest(ServletContext servletContext) {
|
||||||
|
super(servletContext);
|
||||||
setMethod("POST");
|
setMethod("POST");
|
||||||
setContentType("multipart/form-data");
|
setContentType("multipart/form-data");
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@ import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
|
import java.util.EventListener;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
|
@ -30,9 +31,16 @@ import java.util.LinkedHashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import javax.activation.FileTypeMap;
|
import javax.activation.FileTypeMap;
|
||||||
|
import javax.servlet.Filter;
|
||||||
|
import javax.servlet.FilterRegistration;
|
||||||
import javax.servlet.RequestDispatcher;
|
import javax.servlet.RequestDispatcher;
|
||||||
import javax.servlet.Servlet;
|
import javax.servlet.Servlet;
|
||||||
import javax.servlet.ServletContext;
|
import javax.servlet.ServletContext;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.ServletRegistration;
|
||||||
|
import javax.servlet.SessionCookieConfig;
|
||||||
|
import javax.servlet.SessionTrackingMode;
|
||||||
|
import javax.servlet.descriptor.JspConfigDescriptor;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
@ -48,10 +56,10 @@ import org.springframework.web.util.WebUtils;
|
||||||
/**
|
/**
|
||||||
* Mock implementation of the {@link javax.servlet.ServletContext} interface.
|
* Mock implementation of the {@link javax.servlet.ServletContext} interface.
|
||||||
*
|
*
|
||||||
* <p>Compatible with Servlet 2.5 and partially with Servlet 3.0. Can be configured to
|
* <p>Compatible with Servlet 3.0. Can be configured to expose a specific version
|
||||||
* expose a specific version through {@link #setMajorVersion}/{@link #setMinorVersion};
|
* through {@link #setMajorVersion}/{@link #setMinorVersion}; default is 3.0.
|
||||||
* default is 2.5. Note that Servlet 3.0 support is limited: servlet, filter and listener
|
* Note that Servlet 3.0 support is limited: servlet, filter and listener
|
||||||
* registration methods are not supported; neither is cookie or JSP configuration.
|
* 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.
|
* WebApplicationInitializers which is where those registration methods would be used.
|
||||||
*
|
*
|
||||||
|
@ -96,35 +104,49 @@ public class MockServletContext implements ServletContext {
|
||||||
|
|
||||||
private static final String TEMP_DIR_SYSTEM_PROPERTY = "java.io.tmpdir";
|
private static final String TEMP_DIR_SYSTEM_PROPERTY = "java.io.tmpdir";
|
||||||
|
|
||||||
|
private static final Set<SessionTrackingMode> DEFAULT_SESSION_TRACKING_MODES =
|
||||||
|
new LinkedHashSet<SessionTrackingMode>(3);
|
||||||
|
|
||||||
|
static {
|
||||||
|
DEFAULT_SESSION_TRACKING_MODES.add(SessionTrackingMode.COOKIE);
|
||||||
|
DEFAULT_SESSION_TRACKING_MODES.add(SessionTrackingMode.URL);
|
||||||
|
DEFAULT_SESSION_TRACKING_MODES.add(SessionTrackingMode.SSL);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private final Log logger = LogFactory.getLog(getClass());
|
private final Log logger = LogFactory.getLog(getClass());
|
||||||
|
|
||||||
private final Map<String, ServletContext> contexts = new HashMap<String, ServletContext>();
|
|
||||||
|
|
||||||
private final Map<String, String> initParameters = new LinkedHashMap<String, String>();
|
|
||||||
|
|
||||||
private final Map<String, Object> attributes = new LinkedHashMap<String, Object>();
|
|
||||||
|
|
||||||
private final Set<String> declaredRoles = new HashSet<String>();
|
|
||||||
|
|
||||||
private final Map<String, RequestDispatcher> namedRequestDispatchers = new HashMap<String, RequestDispatcher>();
|
|
||||||
|
|
||||||
private final ResourceLoader resourceLoader;
|
private final ResourceLoader resourceLoader;
|
||||||
|
|
||||||
private final String resourceBasePath;
|
private final String resourceBasePath;
|
||||||
|
|
||||||
private String contextPath = "";
|
private String contextPath = "";
|
||||||
|
|
||||||
private int majorVersion = 2;
|
private final Map<String, ServletContext> contexts = new HashMap<String, ServletContext>();
|
||||||
|
|
||||||
private int minorVersion = 5;
|
private int majorVersion = 3;
|
||||||
|
|
||||||
private int effectiveMajorVersion = 2;
|
private int minorVersion = 0;
|
||||||
|
|
||||||
private int effectiveMinorVersion = 5;
|
private int effectiveMajorVersion = 3;
|
||||||
|
|
||||||
|
private int effectiveMinorVersion = 0;
|
||||||
|
|
||||||
|
private final Map<String, RequestDispatcher> namedRequestDispatchers = new HashMap<String, RequestDispatcher>();
|
||||||
|
|
||||||
|
private String defaultServletName = COMMON_DEFAULT_SERVLET_NAME;
|
||||||
|
|
||||||
|
private final Map<String, String> initParameters = new LinkedHashMap<String, String>();
|
||||||
|
|
||||||
|
private final Map<String, Object> attributes = new LinkedHashMap<String, Object>();
|
||||||
|
|
||||||
private String servletContextName = "MockServletContext";
|
private String servletContextName = "MockServletContext";
|
||||||
|
|
||||||
private String defaultServletName = COMMON_DEFAULT_SERVLET_NAME;
|
private final Set<String> declaredRoles = new HashSet<String>();
|
||||||
|
|
||||||
|
private Set<SessionTrackingMode> sessionTrackingModes;
|
||||||
|
|
||||||
|
private final SessionCookieConfig sessionCookieConfig = new MockSessionCookieConfig();
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -158,12 +180,11 @@ public class MockServletContext implements ServletContext {
|
||||||
* Create a new MockServletContext using the supplied resource base path and
|
* Create a new MockServletContext using the supplied resource base path and
|
||||||
* resource loader.
|
* resource loader.
|
||||||
* <p>Registers a {@link MockRequestDispatcher} for the Servlet named
|
* <p>Registers a {@link MockRequestDispatcher} for the Servlet named
|
||||||
* {@linkplain #COMMON_DEFAULT_SERVLET_NAME "default"}.
|
* {@value #COMMON_DEFAULT_SERVLET_NAME}.
|
||||||
* @param resourceBasePath the root directory of the WAR (should not end with a slash)
|
* @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)
|
* @param resourceLoader the ResourceLoader to use (or null for the default)
|
||||||
* @see #registerNamedDispatcher
|
* @see #registerNamedDispatcher
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("javadoc")
|
|
||||||
public MockServletContext(String resourceBasePath, ResourceLoader resourceLoader) {
|
public MockServletContext(String resourceBasePath, ResourceLoader resourceLoader) {
|
||||||
this.resourceLoader = (resourceLoader != null ? resourceLoader : new DefaultResourceLoader());
|
this.resourceLoader = (resourceLoader != null ? resourceLoader : new DefaultResourceLoader());
|
||||||
this.resourceBasePath = (resourceBasePath != null ? resourceBasePath : "");
|
this.resourceBasePath = (resourceBasePath != null ? resourceBasePath : "");
|
||||||
|
@ -194,7 +215,6 @@ public class MockServletContext implements ServletContext {
|
||||||
this.contextPath = (contextPath != null ? contextPath : "");
|
this.contextPath = (contextPath != null ? contextPath : "");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* This is a Servlet API 2.5 method. */
|
|
||||||
public String getContextPath() {
|
public String getContextPath() {
|
||||||
return this.contextPath;
|
return this.contextPath;
|
||||||
}
|
}
|
||||||
|
@ -251,7 +271,7 @@ public class MockServletContext implements ServletContext {
|
||||||
*/
|
*/
|
||||||
public String getMimeType(String filePath) {
|
public String getMimeType(String filePath) {
|
||||||
String mimeType = MimeTypeResolver.getMimeType(filePath);
|
String mimeType = MimeTypeResolver.getMimeType(filePath);
|
||||||
return ("application/octet-stream".equals(mimeType)) ? null : mimeType;
|
return ("application/octet-stream".equals(mimeType) ? null : mimeType);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<String> getResourcePaths(String path) {
|
public Set<String> getResourcePaths(String path) {
|
||||||
|
@ -350,10 +370,9 @@ public class MockServletContext implements ServletContext {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the name of the <em>default</em> {@code Servlet}.
|
* Get the name of the <em>default</em> {@code Servlet}.
|
||||||
* <p>Defaults to {@linkplain #COMMON_DEFAULT_SERVLET_NAME "default"}.
|
* <p>Defaults to {@value #COMMON_DEFAULT_SERVLET_NAME}.
|
||||||
* @see #setDefaultServletName
|
* @see #setDefaultServletName
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("javadoc")
|
|
||||||
public String getDefaultServletName() {
|
public String getDefaultServletName() {
|
||||||
return this.defaultServletName;
|
return this.defaultServletName;
|
||||||
}
|
}
|
||||||
|
@ -485,6 +504,97 @@ public class MockServletContext implements ServletContext {
|
||||||
return Collections.unmodifiableSet(this.declaredRoles);
|
return Collections.unmodifiableSet(this.declaredRoles);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes)
|
||||||
|
throws IllegalStateException, IllegalArgumentException {
|
||||||
|
this.sessionTrackingModes = sessionTrackingModes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<SessionTrackingMode> getDefaultSessionTrackingModes() {
|
||||||
|
return DEFAULT_SESSION_TRACKING_MODES;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<SessionTrackingMode> getEffectiveSessionTrackingModes() {
|
||||||
|
return (this.sessionTrackingModes != null ?
|
||||||
|
Collections.unmodifiableSet(this.sessionTrackingModes) : DEFAULT_SESSION_TRACKING_MODES);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SessionCookieConfig getSessionCookieConfig() {
|
||||||
|
return this.sessionCookieConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
|
// Unsupported Servlet 3.0 registration methods
|
||||||
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
|
public JspConfigDescriptor getJspConfigDescriptor() {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServletRegistration.Dynamic addServlet(String servletName, String className) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T extends Servlet> T createServlet(Class<T> c) throws ServletException {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServletRegistration getServletRegistration(String servletName) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, ? extends ServletRegistration> getServletRegistrations() {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public FilterRegistration.Dynamic addFilter(String filterName, String className) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public FilterRegistration.Dynamic addFilter(String filterName, Filter filter) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public FilterRegistration.Dynamic addFilter(String filterName, Class<? extends Filter> filterClass) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T extends Filter> T createFilter(Class<T> c) throws ServletException {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public FilterRegistration getFilterRegistration(String filterName) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, ? extends FilterRegistration> getFilterRegistrations() {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addListener(Class<? extends EventListener> listenerClass) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addListener(String className) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T extends EventListener> void addListener(T t) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T extends EventListener> T createListener(Class<T> c) throws ServletException {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inner factory class used to introduce a Java Activation Framework
|
* Inner factory class used to introduce a Java Activation Framework
|
||||||
|
|
|
@ -0,0 +1,101 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.mock.web;
|
||||||
|
|
||||||
|
import javax.servlet.SessionCookieConfig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mock implementation of the {@link javax.servlet.SessionCookieConfig} interface.
|
||||||
|
*
|
||||||
|
* @author Juergen Hoeller
|
||||||
|
* @since 4.0
|
||||||
|
* @see javax.servlet.ServletContext#getSessionCookieConfig()
|
||||||
|
*/
|
||||||
|
public class MockSessionCookieConfig implements SessionCookieConfig {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private String domain;
|
||||||
|
|
||||||
|
private String path;
|
||||||
|
|
||||||
|
private String comment;
|
||||||
|
|
||||||
|
private boolean httpOnly;
|
||||||
|
|
||||||
|
private boolean secure;
|
||||||
|
|
||||||
|
private int maxAge;
|
||||||
|
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return this.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDomain(String domain) {
|
||||||
|
this.domain = domain;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDomain() {
|
||||||
|
return this.domain;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPath(String path) {
|
||||||
|
this.path = path;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPath() {
|
||||||
|
return this.path;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setComment(String comment) {
|
||||||
|
this.comment = comment;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getComment() {
|
||||||
|
return this.comment;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHttpOnly(boolean httpOnly) {
|
||||||
|
this.httpOnly = httpOnly;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isHttpOnly() {
|
||||||
|
return this.httpOnly;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSecure(boolean secure) {
|
||||||
|
this.secure = secure;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isSecure() {
|
||||||
|
return this.secure;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMaxAge(int maxAge) {
|
||||||
|
this.maxAge = maxAge;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getMaxAge() {
|
||||||
|
return this.maxAge;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -16,14 +16,13 @@
|
||||||
|
|
||||||
package org.springframework.mock.web;
|
package org.springframework.mock.web;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.servlet.RequestDispatcher;
|
import javax.servlet.RequestDispatcher;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
|
@ -77,10 +76,11 @@ public class MockServletContextTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void minorVersion() {
|
public void servletVersion() {
|
||||||
assertEquals(5, sc.getMinorVersion());
|
assertEquals(3, sc.getMajorVersion());
|
||||||
sc.setMinorVersion(4);
|
assertEquals(0, sc.getMinorVersion());
|
||||||
assertEquals(4, sc.getMinorVersion());
|
sc.setMinorVersion(1);
|
||||||
|
assertEquals(1, sc.getMinorVersion());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -18,7 +18,6 @@ package org.springframework.mock.web.test;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.servlet.AsyncContext;
|
import javax.servlet.AsyncContext;
|
||||||
import javax.servlet.AsyncEvent;
|
import javax.servlet.AsyncEvent;
|
||||||
import javax.servlet.AsyncListener;
|
import javax.servlet.AsyncListener;
|
||||||
|
@ -56,6 +55,7 @@ public class MockAsyncContext implements AsyncContext {
|
||||||
this.response = (HttpServletResponse) response;
|
this.response = (HttpServletResponse) response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ServletRequest getRequest() {
|
public ServletRequest getRequest() {
|
||||||
return this.request;
|
return this.request;
|
||||||
|
@ -71,11 +71,6 @@ public class MockAsyncContext implements AsyncContext {
|
||||||
return (this.request instanceof MockHttpServletRequest) && (this.response instanceof MockHttpServletResponse);
|
return (this.request instanceof MockHttpServletRequest) && (this.response instanceof MockHttpServletResponse);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDispatchedPath() {
|
|
||||||
return this.dispatchedPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void dispatch() {
|
public void dispatch() {
|
||||||
dispatch(this.request.getRequestURI());
|
dispatch(this.request.getRequestURI());
|
||||||
}
|
}
|
||||||
|
@ -90,7 +85,10 @@ public class MockAsyncContext implements AsyncContext {
|
||||||
this.dispatchedPath = path;
|
this.dispatchedPath = path;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public String getDispatchedPath() {
|
||||||
|
return this.dispatchedPath;
|
||||||
|
}
|
||||||
|
|
||||||
public void complete() {
|
public void complete() {
|
||||||
MockHttpServletRequest mockRequest = WebUtils.getNativeRequest(request, MockHttpServletRequest.class);
|
MockHttpServletRequest mockRequest = WebUtils.getNativeRequest(request, MockHttpServletRequest.class);
|
||||||
if (mockRequest != null) {
|
if (mockRequest != null) {
|
||||||
|
@ -111,11 +109,6 @@ public class MockAsyncContext implements AsyncContext {
|
||||||
runnable.run();
|
runnable.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<AsyncListener> getListeners() {
|
|
||||||
return this.listeners;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addListener(AsyncListener listener) {
|
public void addListener(AsyncListener listener) {
|
||||||
this.listeners.add(listener);
|
this.listeners.add(listener);
|
||||||
}
|
}
|
||||||
|
@ -125,19 +118,20 @@ public class MockAsyncContext implements AsyncContext {
|
||||||
this.listeners.add(listener);
|
this.listeners.add(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public List<AsyncListener> getListeners() {
|
||||||
|
return this.listeners;
|
||||||
|
}
|
||||||
|
|
||||||
public <T extends AsyncListener> T createListener(Class<T> clazz) throws ServletException {
|
public <T extends AsyncListener> T createListener(Class<T> clazz) throws ServletException {
|
||||||
return BeanUtils.instantiateClass(clazz);
|
return BeanUtils.instantiateClass(clazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public long getTimeout() {
|
|
||||||
return this.timeout;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setTimeout(long timeout) {
|
public void setTimeout(long timeout) {
|
||||||
this.timeout = timeout;
|
this.timeout = timeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public long getTimeout() {
|
||||||
|
return this.timeout;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,9 +28,9 @@ import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
@ -54,12 +54,9 @@ import org.springframework.util.Assert;
|
||||||
import org.springframework.util.LinkedCaseInsensitiveMap;
|
import org.springframework.util.LinkedCaseInsensitiveMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mock implementation of the {@link javax.servlet.http.HttpServletRequest}
|
* Mock implementation of the {@link javax.servlet.http.HttpServletRequest} interface.
|
||||||
* interface. Supports the Servlet 2.5 API level; throws
|
|
||||||
* {@link UnsupportedOperationException} for some methods introduced in Servlet 3.0.
|
|
||||||
*
|
*
|
||||||
* <p>Used for testing the web framework; also useful for testing
|
* <p>As of Spring 4.0, this set of mocks is entirely based on Servlet 3.0.
|
||||||
* application controllers.
|
|
||||||
*
|
*
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Rod Johnson
|
* @author Rod Johnson
|
||||||
|
@ -150,7 +147,13 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||||
|
|
||||||
private int localPort = DEFAULT_SERVER_PORT;
|
private int localPort = DEFAULT_SERVER_PORT;
|
||||||
|
|
||||||
private final Map<String, Part> parts = new HashMap<String, Part>();
|
private boolean asyncStarted = false;
|
||||||
|
|
||||||
|
private boolean asyncSupported = false;
|
||||||
|
|
||||||
|
private MockAsyncContext asyncContext;
|
||||||
|
|
||||||
|
private DispatcherType dispatcherType = DispatcherType.REQUEST;
|
||||||
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
|
@ -191,13 +194,7 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||||
|
|
||||||
private boolean requestedSessionIdFromURL = false;
|
private boolean requestedSessionIdFromURL = false;
|
||||||
|
|
||||||
private boolean asyncSupported = false;
|
private final Map<String, Part> parts = new LinkedHashMap<String, Part>();
|
||||||
|
|
||||||
private boolean asyncStarted = false;
|
|
||||||
|
|
||||||
private MockAsyncContext asyncContext;
|
|
||||||
|
|
||||||
private DispatcherType dispatcherType = DispatcherType.REQUEST;
|
|
||||||
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
|
@ -228,8 +225,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new {@code MockHttpServletRequest} with the supplied {@link ServletContext}.
|
* Create a new {@code MockHttpServletRequest} with the supplied {@link ServletContext}.
|
||||||
* @param servletContext the ServletContext that the request runs in (may be
|
* @param servletContext the ServletContext that the request runs in
|
||||||
* {@code null} to use a default {@link MockServletContext})
|
* (may be {@code null} to use a default {@link MockServletContext})
|
||||||
* @see #MockHttpServletRequest(ServletContext, String, String)
|
* @see #MockHttpServletRequest(ServletContext, String, String)
|
||||||
*/
|
*/
|
||||||
public MockHttpServletRequest(ServletContext servletContext) {
|
public MockHttpServletRequest(ServletContext servletContext) {
|
||||||
|
@ -688,10 +685,61 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||||
return this.localPort;
|
return this.localPort;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AsyncContext startAsync() {
|
||||||
|
return startAsync(this, null);
|
||||||
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------
|
@Override
|
||||||
|
public AsyncContext startAsync(ServletRequest request, ServletResponse response) {
|
||||||
|
if (!this.asyncSupported) {
|
||||||
|
throw new IllegalStateException("Async not supported");
|
||||||
|
}
|
||||||
|
this.asyncStarted = true;
|
||||||
|
this.asyncContext = new MockAsyncContext(request, response);
|
||||||
|
return this.asyncContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAsyncStarted(boolean asyncStarted) {
|
||||||
|
this.asyncStarted = asyncStarted;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isAsyncStarted() {
|
||||||
|
return this.asyncStarted;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAsyncSupported(boolean asyncSupported) {
|
||||||
|
this.asyncSupported = asyncSupported;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isAsyncSupported() {
|
||||||
|
return this.asyncSupported;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAsyncContext(MockAsyncContext asyncContext) {
|
||||||
|
this.asyncContext = asyncContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AsyncContext getAsyncContext() {
|
||||||
|
return this.asyncContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDispatcherType(DispatcherType dispatcherType) {
|
||||||
|
this.dispatcherType = dispatcherType;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DispatcherType getDispatcherType() {
|
||||||
|
return this.dispatcherType;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
// HttpServletRequest interface
|
// HttpServletRequest interface
|
||||||
//---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
public void setAuthType(String authType) {
|
public void setAuthType(String authType) {
|
||||||
this.authType = authType;
|
this.authType = authType;
|
||||||
|
@ -713,15 +761,15 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a header entry for the given name.
|
* Add a header entry for the given name.
|
||||||
* <p>If there was no entry for that header name before,
|
* <p>If there was no entry for that header name before, the value will be used
|
||||||
* the value will be used as-is. In case of an existing entry,
|
* as-is. In case of an existing entry, a String array will be created,
|
||||||
* a String array will be created, adding the given value (more
|
* adding the given value (more specifically, its toString representation)
|
||||||
* specifically, its toString representation) as further element.
|
* as further element.
|
||||||
* <p>Multiple values can only be stored as list of Strings,
|
* <p>Multiple values can only be stored as list of Strings, following the
|
||||||
* following the Servlet spec (see {@code getHeaders} accessor).
|
* Servlet spec (see {@code getHeaders} accessor). As alternative to
|
||||||
* As alternative to repeated {@code addHeader} calls for
|
* repeated {@code addHeader} calls for individual elements, you can
|
||||||
* individual elements, you can use a single call with an entire
|
* use a single call with an entire array or Collection of values as
|
||||||
* array or Collection of values as parameter.
|
* parameter.
|
||||||
* @see #getHeaderNames
|
* @see #getHeaderNames
|
||||||
* @see #getHeader
|
* @see #getHeader
|
||||||
* @see #getHeaders
|
* @see #getHeaders
|
||||||
|
@ -972,89 +1020,35 @@ public class MockHttpServletRequest implements HttpServletRequest {
|
||||||
return isRequestedSessionIdFromURL();
|
return isRequestedSessionIdFromURL();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------
|
|
||||||
// Methods introduced in Servlet 3.0
|
|
||||||
//---------------------------------------------------------------------
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AsyncContext getAsyncContext() {
|
public boolean authenticate(HttpServletResponse response) throws IOException, ServletException {
|
||||||
return this.asyncContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAsyncContext(MockAsyncContext asyncContext) {
|
|
||||||
this.asyncContext = asyncContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public DispatcherType getDispatcherType() {
|
|
||||||
return this.dispatcherType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDispatcherType(DispatcherType dispatcherType) {
|
|
||||||
this.dispatcherType = dispatcherType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAsyncSupported(boolean asyncSupported) {
|
|
||||||
this.asyncSupported = asyncSupported;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isAsyncSupported() {
|
|
||||||
return this.asyncSupported;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public AsyncContext startAsync() {
|
|
||||||
return startAsync(this, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public AsyncContext startAsync(ServletRequest request, ServletResponse response) {
|
|
||||||
if (!this.asyncSupported) {
|
|
||||||
throw new IllegalStateException("Async not supported");
|
|
||||||
}
|
|
||||||
this.asyncStarted = true;
|
|
||||||
this.asyncContext = new MockAsyncContext(request, response);
|
|
||||||
return this.asyncContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAsyncStarted(boolean asyncStarted) {
|
|
||||||
this.asyncStarted = asyncStarted;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isAsyncStarted() {
|
|
||||||
return this.asyncStarted;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean authenticate(HttpServletResponse arg0) throws IOException, ServletException {
|
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addPart(Part part) {
|
|
||||||
parts.put(part.getName(), part);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Part getPart(String key) throws IOException, IllegalStateException, ServletException {
|
public void login(String username, String password) throws ServletException {
|
||||||
return parts.get(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Collection<Part> getParts() throws IOException, IllegalStateException, ServletException {
|
|
||||||
return parts.values();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void login(String arg0, String arg1) throws ServletException {
|
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void logout() throws ServletException {
|
public void logout() throws ServletException {
|
||||||
throw new UnsupportedOperationException();
|
this.userPrincipal = null;
|
||||||
|
this.remoteUser = null;
|
||||||
|
this.authType = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addPart(Part part) {
|
||||||
|
this.parts.put(part.getName(), part);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Part getPart(String name) throws IOException, IllegalStateException, ServletException {
|
||||||
|
return this.parts.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<Part> getParts() throws IOException, IllegalStateException, ServletException {
|
||||||
|
return this.parts.values();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,8 @@ import org.springframework.web.util.WebUtils;
|
||||||
/**
|
/**
|
||||||
* Mock implementation of the {@link javax.servlet.http.HttpServletResponse} interface.
|
* Mock implementation of the {@link javax.servlet.http.HttpServletResponse} interface.
|
||||||
*
|
*
|
||||||
* <p>Compatible with Servlet 2.5 as well as Servlet 3.0.
|
* <p>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.
|
||||||
*
|
*
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Rod Johnson
|
* @author Rod Johnson
|
||||||
|
@ -75,7 +76,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||||
|
|
||||||
private PrintWriter writer;
|
private PrintWriter writer;
|
||||||
|
|
||||||
private int contentLength = 0;
|
private long contentLength = 0;
|
||||||
|
|
||||||
private String contentType;
|
private String contentType;
|
||||||
|
|
||||||
|
@ -198,6 +199,15 @@ public class MockHttpServletResponse implements HttpServletResponse {
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getContentLength() {
|
public int getContentLength() {
|
||||||
|
return (int) this.contentLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContentLengthLong(long contentLength) {
|
||||||
|
this.contentLength = contentLength;
|
||||||
|
doAddHeaderValue(CONTENT_LENGTH_HEADER, contentLength, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getContentLengthLong() {
|
||||||
return this.contentLength;
|
return this.contentLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -21,6 +21,7 @@ import java.util.Enumeration;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
|
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
|
@ -50,7 +51,22 @@ public class MockMultipartHttpServletRequest extends MockHttpServletRequest impl
|
||||||
new LinkedMultiValueMap<String, MultipartFile>();
|
new LinkedMultiValueMap<String, MultipartFile>();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new {@code MockMultipartHttpServletRequest} with a default
|
||||||
|
* {@link MockServletContext}.
|
||||||
|
* @see #MockMultipartHttpServletRequest(ServletContext)
|
||||||
|
*/
|
||||||
public MockMultipartHttpServletRequest() {
|
public MockMultipartHttpServletRequest() {
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new {@code MockMultipartHttpServletRequest} with the supplied {@link ServletContext}.
|
||||||
|
* @param servletContext the ServletContext that the request runs in
|
||||||
|
* (may be {@code null} to use a default {@link MockServletContext})
|
||||||
|
*/
|
||||||
|
public MockMultipartHttpServletRequest(ServletContext servletContext) {
|
||||||
|
super(servletContext);
|
||||||
setMethod("POST");
|
setMethod("POST");
|
||||||
setContentType("multipart/form-data");
|
setContentType("multipart/form-data");
|
||||||
}
|
}
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -30,7 +30,6 @@ import java.util.LinkedHashMap;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.activation.FileTypeMap;
|
import javax.activation.FileTypeMap;
|
||||||
import javax.servlet.Filter;
|
import javax.servlet.Filter;
|
||||||
import javax.servlet.FilterRegistration;
|
import javax.servlet.FilterRegistration;
|
||||||
|
@ -45,6 +44,7 @@ import javax.servlet.descriptor.JspConfigDescriptor;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
import org.springframework.core.io.DefaultResourceLoader;
|
import org.springframework.core.io.DefaultResourceLoader;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.core.io.ResourceLoader;
|
import org.springframework.core.io.ResourceLoader;
|
||||||
|
@ -56,13 +56,11 @@ import org.springframework.web.util.WebUtils;
|
||||||
/**
|
/**
|
||||||
* Mock implementation of the {@link javax.servlet.ServletContext} interface.
|
* Mock implementation of the {@link javax.servlet.ServletContext} interface.
|
||||||
*
|
*
|
||||||
* <p>Compatible with Servlet 2.5 and partially with Servlet 3.0 but throws
|
* <p>Compatible with Servlet 3.0. Can be configured to expose a specific version
|
||||||
* {@link UnsupportedOperationException} for most methods introduced in Servlet
|
* through {@link #setMajorVersion}/{@link #setMinorVersion}; default is 3.0.
|
||||||
* 3.0. Can be configured to expose a specific version through
|
* Note that Servlet 3.0 support is limited: servlet, filter and listener
|
||||||
* {@link #setMajorVersion}/{@link #setMinorVersion}; default is 2.5. Note that
|
* registration methods are not supported; neither is JSP configuration.
|
||||||
* Servlet 3.0 support is limited: servlet, filter and listener registration
|
* We generally do not recommend to unit-test your ServletContainerInitializers and
|
||||||
* methods are not supported; neither is cookie or JSP configuration. We generally
|
|
||||||
* do not recommend to unit-test your ServletContainerInitializers and
|
|
||||||
* WebApplicationInitializers which is where those registration methods would be used.
|
* WebApplicationInitializers which is where those registration methods would be used.
|
||||||
*
|
*
|
||||||
* <p>Used for testing the Spring web framework; only rarely necessary for testing
|
* <p>Used for testing the Spring web framework; only rarely necessary for testing
|
||||||
|
@ -106,35 +104,49 @@ public class MockServletContext implements ServletContext {
|
||||||
|
|
||||||
private static final String TEMP_DIR_SYSTEM_PROPERTY = "java.io.tmpdir";
|
private static final String TEMP_DIR_SYSTEM_PROPERTY = "java.io.tmpdir";
|
||||||
|
|
||||||
|
private static final Set<SessionTrackingMode> DEFAULT_SESSION_TRACKING_MODES =
|
||||||
|
new LinkedHashSet<SessionTrackingMode>(3);
|
||||||
|
|
||||||
|
static {
|
||||||
|
DEFAULT_SESSION_TRACKING_MODES.add(SessionTrackingMode.COOKIE);
|
||||||
|
DEFAULT_SESSION_TRACKING_MODES.add(SessionTrackingMode.URL);
|
||||||
|
DEFAULT_SESSION_TRACKING_MODES.add(SessionTrackingMode.SSL);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private final Log logger = LogFactory.getLog(getClass());
|
private final Log logger = LogFactory.getLog(getClass());
|
||||||
|
|
||||||
private final Map<String, ServletContext> contexts = new HashMap<String, ServletContext>();
|
|
||||||
|
|
||||||
private final Map<String, String> initParameters = new LinkedHashMap<String, String>();
|
|
||||||
|
|
||||||
private final Map<String, Object> attributes = new LinkedHashMap<String, Object>();
|
|
||||||
|
|
||||||
private final Set<String> declaredRoles = new HashSet<String>();
|
|
||||||
|
|
||||||
private final Map<String, RequestDispatcher> namedRequestDispatchers = new HashMap<String, RequestDispatcher>();
|
|
||||||
|
|
||||||
private final ResourceLoader resourceLoader;
|
private final ResourceLoader resourceLoader;
|
||||||
|
|
||||||
private final String resourceBasePath;
|
private final String resourceBasePath;
|
||||||
|
|
||||||
private String contextPath = "";
|
private String contextPath = "";
|
||||||
|
|
||||||
private int majorVersion = 2;
|
private final Map<String, ServletContext> contexts = new HashMap<String, ServletContext>();
|
||||||
|
|
||||||
private int minorVersion = 5;
|
private int majorVersion = 3;
|
||||||
|
|
||||||
private int effectiveMajorVersion = 2;
|
private int minorVersion = 0;
|
||||||
|
|
||||||
private int effectiveMinorVersion = 5;
|
private int effectiveMajorVersion = 3;
|
||||||
|
|
||||||
|
private int effectiveMinorVersion = 0;
|
||||||
|
|
||||||
|
private final Map<String, RequestDispatcher> namedRequestDispatchers = new HashMap<String, RequestDispatcher>();
|
||||||
|
|
||||||
|
private String defaultServletName = COMMON_DEFAULT_SERVLET_NAME;
|
||||||
|
|
||||||
|
private final Map<String, String> initParameters = new LinkedHashMap<String, String>();
|
||||||
|
|
||||||
|
private final Map<String, Object> attributes = new LinkedHashMap<String, Object>();
|
||||||
|
|
||||||
private String servletContextName = "MockServletContext";
|
private String servletContextName = "MockServletContext";
|
||||||
|
|
||||||
private String defaultServletName = COMMON_DEFAULT_SERVLET_NAME;
|
private final Set<String> declaredRoles = new HashSet<String>();
|
||||||
|
|
||||||
|
private Set<SessionTrackingMode> sessionTrackingModes;
|
||||||
|
|
||||||
|
private final SessionCookieConfig sessionCookieConfig = new MockSessionCookieConfig();
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -203,7 +215,6 @@ public class MockServletContext implements ServletContext {
|
||||||
this.contextPath = (contextPath != null ? contextPath : "");
|
this.contextPath = (contextPath != null ? contextPath : "");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* This is a Servlet API 2.5 method. */
|
|
||||||
@Override
|
@Override
|
||||||
public String getContextPath() {
|
public String getContextPath() {
|
||||||
return this.contextPath;
|
return this.contextPath;
|
||||||
|
@ -476,7 +487,7 @@ public class MockServletContext implements ServletContext {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Enumeration<String> getAttributeNames() {
|
public Enumeration<String> getAttributeNames() {
|
||||||
return Collections.enumeration(this.attributes.keySet());
|
return Collections.enumeration(new LinkedHashSet<String>(this.attributes.keySet()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -523,23 +534,68 @@ public class MockServletContext implements ServletContext {
|
||||||
return Collections.unmodifiableSet(this.declaredRoles);
|
return Collections.unmodifiableSet(this.declaredRoles);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes)
|
||||||
|
throws IllegalStateException, IllegalArgumentException {
|
||||||
|
this.sessionTrackingModes = sessionTrackingModes;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* Inner factory class used to introduce a Java Activation Framework
|
public Set<SessionTrackingMode> getDefaultSessionTrackingModes() {
|
||||||
* dependency when actually asked to resolve a MIME type.
|
return DEFAULT_SESSION_TRACKING_MODES;
|
||||||
*/
|
}
|
||||||
private static class MimeTypeResolver {
|
|
||||||
|
|
||||||
public static String getMimeType(String filePath) {
|
@Override
|
||||||
return FileTypeMap.getDefaultFileTypeMap().getContentType(filePath);
|
public Set<SessionTrackingMode> getEffectiveSessionTrackingModes() {
|
||||||
}
|
return (this.sessionTrackingModes != null ?
|
||||||
|
Collections.unmodifiableSet(this.sessionTrackingModes) : DEFAULT_SESSION_TRACKING_MODES);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SessionCookieConfig getSessionCookieConfig() {
|
||||||
|
return this.sessionCookieConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
// Methods introduced in Servlet 3.0
|
// Unsupported Servlet 3.0 registration methods
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JspConfigDescriptor getJspConfigDescriptor() {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ServletRegistration.Dynamic addServlet(String servletName, String className) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T extends Servlet> T createServlet(Class<T> c) throws ServletException {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ServletRegistration getServletRegistration(String servletName) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, ? extends ServletRegistration> getServletRegistrations() {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FilterRegistration.Dynamic addFilter(String filterName, String className) {
|
public FilterRegistration.Dynamic addFilter(String filterName, String className) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
|
@ -555,6 +611,21 @@ public class MockServletContext implements ServletContext {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T extends Filter> T createFilter(Class<T> c) throws ServletException {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FilterRegistration getFilterRegistration(String filterName) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, ? extends FilterRegistration> getFilterRegistrations() {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addListener(Class<? extends EventListener> listenerClass) {
|
public void addListener(Class<? extends EventListener> listenerClass) {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
|
@ -571,83 +642,20 @@ public class MockServletContext implements ServletContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ServletRegistration.Dynamic addServlet(String servletName, String className) {
|
public <T extends EventListener> T createListener(Class<T> c) throws ServletException {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet) {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public ServletRegistration.Dynamic addServlet(String servletName,
|
* Inner factory class used to introduce a Java Activation Framework
|
||||||
Class<? extends Servlet> servletClass) {
|
* dependency when actually asked to resolve a MIME type.
|
||||||
throw new UnsupportedOperationException();
|
*/
|
||||||
}
|
private static class MimeTypeResolver {
|
||||||
|
|
||||||
@Override
|
public static String getMimeType(String filePath) {
|
||||||
public <T extends Filter> T createFilter(Class<T> c)
|
return FileTypeMap.getDefaultFileTypeMap().getContentType(filePath);
|
||||||
throws ServletException {
|
}
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T extends EventListener> T createListener(Class<T> c)
|
|
||||||
throws ServletException {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T extends Servlet> T createServlet(Class<T> c)
|
|
||||||
throws ServletException {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Set<SessionTrackingMode> getDefaultSessionTrackingModes() {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Set<SessionTrackingMode> getEffectiveSessionTrackingModes() {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public FilterRegistration getFilterRegistration(String filterName) {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Map<String, ? extends FilterRegistration> getFilterRegistrations() {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public JspConfigDescriptor getJspConfigDescriptor() {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public ServletRegistration getServletRegistration(String servletName) {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Map<String, ? extends ServletRegistration> getServletRegistrations() {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public SessionCookieConfig getSessionCookieConfig() {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes)
|
|
||||||
throws IllegalStateException, IllegalArgumentException {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,101 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.mock.web.test;
|
||||||
|
|
||||||
|
import javax.servlet.SessionCookieConfig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mock implementation of the {@link javax.servlet.SessionCookieConfig} interface.
|
||||||
|
*
|
||||||
|
* @author Juergen Hoeller
|
||||||
|
* @since 4.0
|
||||||
|
* @see javax.servlet.ServletContext#getSessionCookieConfig()
|
||||||
|
*/
|
||||||
|
public class MockSessionCookieConfig implements SessionCookieConfig {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private String domain;
|
||||||
|
|
||||||
|
private String path;
|
||||||
|
|
||||||
|
private String comment;
|
||||||
|
|
||||||
|
private boolean httpOnly;
|
||||||
|
|
||||||
|
private boolean secure;
|
||||||
|
|
||||||
|
private int maxAge;
|
||||||
|
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return this.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDomain(String domain) {
|
||||||
|
this.domain = domain;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDomain() {
|
||||||
|
return this.domain;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPath(String path) {
|
||||||
|
this.path = path;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPath() {
|
||||||
|
return this.path;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setComment(String comment) {
|
||||||
|
this.comment = comment;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getComment() {
|
||||||
|
return this.comment;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHttpOnly(boolean httpOnly) {
|
||||||
|
this.httpOnly = httpOnly;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isHttpOnly() {
|
||||||
|
return this.httpOnly;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSecure(boolean secure) {
|
||||||
|
this.secure = secure;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isSecure() {
|
||||||
|
return this.secure;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMaxAge(int maxAge) {
|
||||||
|
this.maxAge = maxAge;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getMaxAge() {
|
||||||
|
return this.maxAge;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue