From 2a15b5a89585c24203e32537b0306cff45076638 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 18 Jul 2013 17:06:50 -0400 Subject: [PATCH] Enable asyncSupported flag in Spring Test MVC Also remove Servlet 3 bridge classes no longer needed since Spring Framework 4 compiles with the Servlet 3 API. That means applications may need to run tests with the Servlet 3 API jar although technically they don't have to run with that in production. --- .../web/servlet/request/MockAsyncContext.java | 145 ------------------ .../MockHttpServletRequestBuilder.java | 2 + .../Servlet3MockHttpServletRequest.java | 114 -------------- ...rvlet3MockMultipartHttpServletRequest.java | 109 ------------- .../setup/StandaloneMockMvcBuilder.java | 16 ++ 5 files changed, 18 insertions(+), 368 deletions(-) delete mode 100644 spring-test-mvc/src/main/java/org/springframework/test/web/servlet/request/MockAsyncContext.java delete mode 100644 spring-test-mvc/src/main/java/org/springframework/test/web/servlet/request/Servlet3MockHttpServletRequest.java delete mode 100644 spring-test-mvc/src/main/java/org/springframework/test/web/servlet/request/Servlet3MockMultipartHttpServletRequest.java diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/request/MockAsyncContext.java b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/request/MockAsyncContext.java deleted file mode 100644 index 753d63db5b..0000000000 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/request/MockAsyncContext.java +++ /dev/null @@ -1,145 +0,0 @@ -/* - * Copyright 2002-2012 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.test.web.servlet.request; - -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.mock.web.MockHttpServletRequest; -import org.springframework.mock.web.MockHttpServletResponse; -import org.springframework.web.util.WebUtils; - -/** - * Mock implementation of the {@link AsyncContext} interface. - * - * @author Rossen Stoyanchev - * @since 3.2 - */ -class MockAsyncContext implements AsyncContext { - - private final HttpServletRequest request; - - private final HttpServletResponse response; - - private final List listeners = new ArrayList(); - - 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; - } - - @Override - public ServletRequest getRequest() { - return this.request; - } - - @Override - public ServletResponse getResponse() { - return this.response; - } - - @Override - public boolean hasOriginalRequestAndResponse() { - return (this.request instanceof MockHttpServletRequest) && (this.response instanceof MockHttpServletResponse); - } - - public String getDispatchedPath() { - return this.dispatchedPath; - } - - @Override - public void dispatch() { - dispatch(this.request.getRequestURI()); - } - - @Override - public void dispatch(String path) { - dispatch(null, path); - } - - @Override - public void dispatch(ServletContext context, String path) { - this.dispatchedPath = path; - } - - @Override - public void complete() { - Servlet3MockHttpServletRequest mockRequest = WebUtils.getNativeRequest(request, Servlet3MockHttpServletRequest.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); - } - } - } - - @Override - public void start(Runnable runnable) { - runnable.run(); - } - - public List getListeners() { - return this.listeners; - } - - @Override - public void addListener(AsyncListener listener) { - this.listeners.add(listener); - } - - @Override - public void addListener(AsyncListener listener, ServletRequest request, ServletResponse response) { - this.listeners.add(listener); - } - - @Override - public T createListener(Class clazz) throws ServletException { - return BeanUtils.instantiateClass(clazz); - } - - @Override - public long getTimeout() { - return this.timeout; - } - - @Override - public void setTimeout(long timeout) { - this.timeout = timeout; - } - -} diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java index 142131fcf5..37c19f2e75 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java @@ -653,6 +653,8 @@ public class MockHttpServletRequestBuilder implements RequestBuilder, Mergeable Assert.notNull(request, "Post-processor [" + postProcessor.getClass().getName() + "] returned null"); } + request.setAsyncSupported(true); + return request; } diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/request/Servlet3MockHttpServletRequest.java b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/request/Servlet3MockHttpServletRequest.java deleted file mode 100644 index 40ee42250f..0000000000 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/request/Servlet3MockHttpServletRequest.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright 2002-2012 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.test.web.servlet.request; - -import java.io.IOException; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import javax.servlet.AsyncContext; -import javax.servlet.DispatcherType; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.Part; - -import org.springframework.mock.web.MockHttpServletRequest; - -/** - * A Servlet 3 sub-class of MockHttpServletRequest. - * - * @author Rossen Stoyanchev - * @since 3.2 - */ -class Servlet3MockHttpServletRequest extends MockHttpServletRequest { - - private boolean asyncStarted; - - private MockAsyncContext asyncContext; - - private Map parts = new HashMap(); - - - public Servlet3MockHttpServletRequest(ServletContext servletContext) { - super(servletContext); - } - - @Override - public boolean isAsyncSupported() { - return true; - } - - @Override - public AsyncContext startAsync() { - return startAsync(this, null); - } - - @Override - public AsyncContext startAsync(ServletRequest request, ServletResponse response) { - this.asyncStarted = true; - this.asyncContext = new MockAsyncContext(request, response); - return this.asyncContext; - } - - @Override - public AsyncContext getAsyncContext() { - return this.asyncContext; - } - - public void setAsyncContext(MockAsyncContext asyncContext) { - this.asyncContext = asyncContext; - } - - @Override - public DispatcherType getDispatcherType() { - return DispatcherType.REQUEST; - } - - @Override - public boolean isAsyncStarted() { - return this.asyncStarted; - } - - @Override - public void setAsyncStarted(boolean asyncStarted) { - this.asyncStarted = asyncStarted; - } - - @Override - public void addPart(Part part) { - this.parts.put(part.getName(), part); - } - - @Override - public Part getPart(String key) throws IOException, IllegalStateException, ServletException { - return this.parts.get(key); - } - - @Override - public Collection getParts() throws IOException, IllegalStateException, ServletException { - return this.parts.values(); - } - - @Override - public boolean authenticate(HttpServletResponse response) throws IOException, ServletException { - throw new UnsupportedOperationException(); - } - -} diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/request/Servlet3MockMultipartHttpServletRequest.java b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/request/Servlet3MockMultipartHttpServletRequest.java deleted file mode 100644 index 2133831d2b..0000000000 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/request/Servlet3MockMultipartHttpServletRequest.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright 2002-2012 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.test.web.servlet.request; - -import java.io.IOException; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import javax.servlet.AsyncContext; -import javax.servlet.DispatcherType; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.Part; - -import org.springframework.mock.web.MockMultipartHttpServletRequest; - -/** - * A Servlet 3 sub-class of MockMultipartHttpServletRequest. - * - * @author Rossen Stoyanchev - * @since 3.2 - */ -class Servlet3MockMultipartHttpServletRequest extends MockMultipartHttpServletRequest { - - private boolean asyncStarted; - - private MockAsyncContext asyncContext; - - private Map parts = new HashMap(); - - - @Override - public boolean isAsyncSupported() { - return true; - } - - @Override - public AsyncContext startAsync() { - return startAsync(this, null); - } - - @Override - public AsyncContext startAsync(ServletRequest request, ServletResponse response) { - this.asyncStarted = true; - this.asyncContext = new MockAsyncContext(request, response); - return this.asyncContext; - } - - @Override - public AsyncContext getAsyncContext() { - return this.asyncContext; - } - - public void setAsyncContext(MockAsyncContext asyncContext) { - this.asyncContext = asyncContext; - } - - @Override - public DispatcherType getDispatcherType() { - return DispatcherType.REQUEST; - } - - @Override - public boolean isAsyncStarted() { - return this.asyncStarted; - } - - @Override - public void setAsyncStarted(boolean asyncStarted) { - this.asyncStarted = asyncStarted; - } - - @Override - public void addPart(Part part) { - this.parts.put(part.getName(), part); - } - - @Override - public Part getPart(String key) throws IOException, IllegalStateException, ServletException { - return this.parts.get(key); - } - - @Override - public Collection getParts() throws IOException, IllegalStateException, ServletException { - return this.parts.values(); - } - - @Override - public boolean authenticate(HttpServletResponse response) throws IOException, ServletException { - throw new UnsupportedOperationException(); - } - -} diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilder.java b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilder.java index 9fa7e1b26f..dd098bd4ee 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilder.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/setup/StandaloneMockMvcBuilder.java @@ -47,6 +47,7 @@ import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer; import org.springframework.web.servlet.config.annotation.InterceptorRegistration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; +import org.springframework.web.servlet.handler.AbstractHandlerMapping; import org.springframework.web.servlet.handler.MappedInterceptor; import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; @@ -107,6 +108,8 @@ public class StandaloneMockMvcBuilder extends DefaultMockMvcBuilder