From 92ca8b32fb8ab5747378de2aa504b325d6d42a12 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 16 Nov 2012 09:47:31 -0500 Subject: [PATCH] Add DefaultMvcResultTests --- build.gradle | 1 + .../test/web/servlet/DefaultMvcResult.java | 11 +- .../web/servlet/DefaultMvcResultTests.java | 132 ++++++++++++++++++ 3 files changed, 139 insertions(+), 5 deletions(-) create mode 100644 spring-test-mvc/src/test/java/org/springframework/test/web/servlet/DefaultMvcResultTests.java diff --git a/build.gradle b/build.gradle index 4eff504129..cb43d3a927 100644 --- a/build.gradle +++ b/build.gradle @@ -579,6 +579,7 @@ project('spring-test-mvc') { testCompile "cglib:cglib-nodep:2.2" testCompile "rome:rome:1.0" testCompile "javax.xml.bind:jaxb-api:2.2.6" + testCompile "org.easymock:easymockclassextension:2.3" testCompile("org.springframework.security:spring-security-core:3.1.2.RELEASE") { exclude group: 'org.springframework' } diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/DefaultMvcResult.java b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/DefaultMvcResult.java index 275287c733..117a95c2bf 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/DefaultMvcResult.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/DefaultMvcResult.java @@ -114,9 +114,13 @@ class DefaultMvcResult implements MvcResult { } public Object getAsyncResult(long timeout) { + // MockHttpServletRequest type doesn't have async methods HttpServletRequest request = this.mockRequest; if ((timeout != 0) && request.isAsyncStarted()) { - if (!awaitAsyncResult(request, timeout)) { + if (timeout == -1) { + timeout = request.getAsyncContext().getTimeout(); + } + if (!awaitAsyncResult(timeout)) { throw new IllegalStateException( "Gave up waiting on async result from handler [" + this.handler + "] to complete"); } @@ -124,10 +128,7 @@ class DefaultMvcResult implements MvcResult { return this.asyncResult; } - private boolean awaitAsyncResult(HttpServletRequest request, long timeout) { - if (timeout != -1) { - timeout = request.getAsyncContext().getTimeout(); - } + private boolean awaitAsyncResult(long timeout) { if (this.asyncResultLatch != null) { try { return this.asyncResultLatch.await(timeout, TimeUnit.MILLISECONDS); diff --git a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/DefaultMvcResultTests.java b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/DefaultMvcResultTests.java new file mode 100644 index 0000000000..66e5e064ef --- /dev/null +++ b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/DefaultMvcResultTests.java @@ -0,0 +1,132 @@ +/* + * 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; + +import static org.easymock.EasyMock.expect; +import static org.easymock.classextension.EasyMock.replay; +import static org.easymock.classextension.EasyMock.verify; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import javax.servlet.AsyncContext; + +import org.easymock.classextension.EasyMock; +import org.junit.Before; +import org.junit.Test; +import org.springframework.mock.web.MockHttpServletRequest; + +/** + * Test fixture for {@link DefaultMvcResult}. + * + * @author Rossen Stoyanchev + */ +public class DefaultMvcResultTests { + + private static final long DEFAULT_TIMEOUT = 10000L; + + private DefaultMvcResult mvcResult; + + private CountDownLatch countDownLatch; + + + @Before + public void setup() { + ExtendedMockHttpServletRequest request = new ExtendedMockHttpServletRequest(); + request.setAsyncStarted(true); + + this.countDownLatch = EasyMock.createMock(CountDownLatch.class); + + this.mvcResult = new DefaultMvcResult(request, null); + this.mvcResult.setAsyncResultLatch(this.countDownLatch); + } + + @Test + public void getAsyncResultWithTimeout() throws Exception { + long timeout = 1234L; + + expect(this.countDownLatch.await(timeout, TimeUnit.MILLISECONDS)).andReturn(true); + replay(this.countDownLatch); + + this.mvcResult.getAsyncResult(timeout); + + verify(this.countDownLatch); + } + + @Test + public void getAsyncResultWithTimeoutNegativeOne() throws Exception { + expect(this.countDownLatch.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS)).andReturn(true); + replay(this.countDownLatch); + + this.mvcResult.getAsyncResult(-1); + + verify(this.countDownLatch); + } + + @Test + public void getAsyncResultWithoutTimeout() throws Exception { + expect(this.countDownLatch.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS)).andReturn(true); + replay(this.countDownLatch); + + this.mvcResult.getAsyncResult(); + + verify(this.countDownLatch); + } + + @Test + public void getAsyncResultWithTimeoutZero() throws Exception { + replay(this.countDownLatch); + + this.mvcResult.getAsyncResult(0); + + verify(this.countDownLatch); + } + + @Test(expected=IllegalStateException.class) + public void getAsyncResultAndTimeOut() throws Exception { + expect(this.countDownLatch.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS)).andReturn(false); + replay(this.countDownLatch); + + this.mvcResult.getAsyncResult(-1); + } + + + private static class ExtendedMockHttpServletRequest extends MockHttpServletRequest { + + private boolean asyncStarted; + private AsyncContext asyncContext; + + public ExtendedMockHttpServletRequest() { + super(); + this.asyncContext = EasyMock.createMock(AsyncContext.class); + expect(this.asyncContext.getTimeout()).andReturn(new Long(DEFAULT_TIMEOUT)); + replay(this.asyncContext); + } + + public void setAsyncStarted(boolean asyncStarted) { + this.asyncStarted = asyncStarted; + } + + public boolean isAsyncStarted() { + return this.asyncStarted; + } + + public AsyncContext getAsyncContext() { + return asyncContext; + } + } + +}