This commit is contained in:
Rossen Stoyanchev 2015-07-30 10:45:32 -04:00
parent 80767ff6e9
commit 24285956a5
1 changed files with 21 additions and 18 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * 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.
@ -26,13 +26,19 @@ import org.springframework.mock.web.test.MockAsyncContext;
import org.springframework.mock.web.test.MockHttpServletRequest; import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse; import org.springframework.mock.web.test.MockHttpServletResponse;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.*; import static org.junit.Assert.assertEquals;
import static org.mockito.BDDMockito.*; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.mock;
import static org.mockito.BDDMockito.verify;
/** /**
* A test fixture with a {@link StandardServletAsyncWebRequest}. * A test fixture with a {@link StandardServletAsyncWebRequest}.
*
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
*/ */
public class StandardServletAsyncWebRequestTests { public class StandardServletAsyncWebRequestTests {
@ -43,6 +49,7 @@ public class StandardServletAsyncWebRequestTests {
private MockHttpServletResponse response; private MockHttpServletResponse response;
@Before @Before
public void setup() { public void setup() {
this.request = new MockHttpServletRequest(); this.request = new MockHttpServletRequest();
@ -52,10 +59,10 @@ public class StandardServletAsyncWebRequestTests {
this.asyncRequest.setTimeout(44*1000L); this.asyncRequest.setTimeout(44*1000L);
} }
@Test @Test
public void isAsyncStarted() throws Exception { public void isAsyncStarted() throws Exception {
assertFalse(this.asyncRequest.isAsyncStarted()); assertFalse(this.asyncRequest.isAsyncStarted());
this.asyncRequest.startAsync(); this.asyncRequest.startAsync();
assertTrue(this.asyncRequest.isAsyncStarted()); assertTrue(this.asyncRequest.isAsyncStarted());
} }
@ -64,12 +71,11 @@ public class StandardServletAsyncWebRequestTests {
public void startAsync() throws Exception { public void startAsync() throws Exception {
this.asyncRequest.startAsync(); this.asyncRequest.startAsync();
MockAsyncContext asyncContext = (MockAsyncContext) this.request.getAsyncContext(); MockAsyncContext context = (MockAsyncContext) this.request.getAsyncContext();
assertNotNull(context);
assertNotNull(asyncContext); assertEquals("Timeout value not set", 44 * 1000, context.getTimeout());
assertEquals("Timeout value not set", 44 * 1000, asyncContext.getTimeout()); assertEquals(1, context.getListeners().size());
assertEquals(1, asyncContext.getListeners().size()); assertSame(this.asyncRequest, context.getListeners().get(0));
assertSame(this.asyncRequest, asyncContext.getListeners().get(0));
} }
@Test @Test
@ -79,10 +85,9 @@ public class StandardServletAsyncWebRequestTests {
this.asyncRequest.startAsync(); this.asyncRequest.startAsync();
this.asyncRequest.startAsync(); // idempotent this.asyncRequest.startAsync(); // idempotent
MockAsyncContext asyncContext = (MockAsyncContext) this.request.getAsyncContext(); MockAsyncContext context = (MockAsyncContext) this.request.getAsyncContext();
assertNotNull(context);
assertNotNull(asyncContext); assertEquals(1, context.getListeners().size());
assertEquals(1, asyncContext.getListeners().size());
} }
@Test @Test
@ -118,10 +123,8 @@ public class StandardServletAsyncWebRequestTests {
@Test @Test
public void onTimeoutTimeoutHandler() throws Exception { public void onTimeoutTimeoutHandler() throws Exception {
Runnable timeoutHandler = mock(Runnable.class); Runnable timeoutHandler = mock(Runnable.class);
this.asyncRequest.addTimeoutHandler(timeoutHandler); this.asyncRequest.addTimeoutHandler(timeoutHandler);
this.asyncRequest.onTimeout(new AsyncEvent(null)); this.asyncRequest.onTimeout(new AsyncEvent(null));
verify(timeoutHandler).run(); verify(timeoutHandler).run();
} }