Browse Source

Polish AsyncTests

pull/1109/head
Rossen Stoyanchev 10 years ago
parent
commit
01a63dd84d
  1. 22
      spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/AsyncTests.java

22
spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/AsyncTests.java

@ -17,6 +17,7 @@
package org.springframework.test.web.servlet.samples.standalone; package org.springframework.test.web.servlet.samples.standalone;
import java.io.StringWriter; import java.io.StringWriter;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Collection; import java.util.Collection;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
@ -40,6 +41,7 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult; import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody; import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
@ -78,7 +80,7 @@ public class AsyncTests {
public void streaming() throws Exception { public void streaming() throws Exception {
this.mockMvc.perform(get("/1").param("streaming", "true")) this.mockMvc.perform(get("/1").param("streaming", "true"))
.andExpect(request().asyncStarted()) .andExpect(request().asyncStarted())
.andDo(r -> r.getAsyncResult()) // fetch async result similar to "asyncDispatch" builder .andDo(MvcResult::getAsyncResult) // fetch async result similar to "asyncDispatch" builder
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().string("name=Joe")); .andExpect(content().string("name=Joe"));
} }
@ -87,7 +89,7 @@ public class AsyncTests {
public void streamingSlow() throws Exception { public void streamingSlow() throws Exception {
this.mockMvc.perform(get("/1").param("streamingSlow", "true")) this.mockMvc.perform(get("/1").param("streamingSlow", "true"))
.andExpect(request().asyncStarted()) .andExpect(request().asyncStarted())
.andDo(r -> r.getAsyncResult()) .andDo(MvcResult::getAsyncResult)
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().string("name=Joe&someBoolean=true")); .andExpect(content().string("name=Joe&someBoolean=true"));
} }
@ -96,7 +98,7 @@ public class AsyncTests {
public void streamingJson() throws Exception { public void streamingJson() throws Exception {
this.mockMvc.perform(get("/1").param("streamingJson", "true")) this.mockMvc.perform(get("/1").param("streamingJson", "true"))
.andExpect(request().asyncStarted()) .andExpect(request().asyncStarted())
.andDo(r -> r.getAsyncResult()) .andDo(MvcResult::getAsyncResult)
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.5}")); .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.5}"));
@ -129,10 +131,7 @@ public class AsyncTests {
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
} }
/** @Test // SPR-13079
* SPR-13079
*/
@Test
public void deferredResultWithDelayedError() throws Exception { public void deferredResultWithDelayedError() throws Exception {
MvcResult mvcResult = this.mockMvc.perform(get("/1").param("deferredResultWithDelayedError", "true")) MvcResult mvcResult = this.mockMvc.perform(get("/1").param("deferredResultWithDelayedError", "true"))
.andExpect(request().asyncStarted()) .andExpect(request().asyncStarted())
@ -201,6 +200,7 @@ public class AsyncTests {
@RestController @RestController
@RequestMapping(path = "/{id}", produces = "application/json") @RequestMapping(path = "/{id}", produces = "application/json")
@SuppressWarnings("unused")
private static class AsyncController { private static class AsyncController {
private final Collection<DeferredResult<Person>> deferredResults = private final Collection<DeferredResult<Person>> deferredResults =
@ -217,7 +217,7 @@ public class AsyncTests {
@RequestMapping(params = "streaming") @RequestMapping(params = "streaming")
public StreamingResponseBody getStreaming() { public StreamingResponseBody getStreaming() {
return os -> os.write("name=Joe".getBytes()); return os -> os.write("name=Joe".getBytes(UTF_8));
} }
@RequestMapping(params = "streamingSlow") @RequestMapping(params = "streamingSlow")
@ -226,7 +226,7 @@ public class AsyncTests {
os.write("name=Joe".getBytes()); os.write("name=Joe".getBytes());
try { try {
Thread.sleep(200); Thread.sleep(200);
os.write("&someBoolean=true".getBytes()); os.write("&someBoolean=true".getBytes(UTF_8));
} }
catch (InterruptedException e) { catch (InterruptedException e) {
/* no-op */ /* no-op */
@ -237,7 +237,7 @@ public class AsyncTests {
@RequestMapping(params = "streamingJson") @RequestMapping(params = "streamingJson")
public ResponseEntity<StreamingResponseBody> getStreamingJson() { public ResponseEntity<StreamingResponseBody> getStreamingJson() {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON_UTF8) return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON_UTF8)
.body(os -> os.write("{\"name\":\"Joe\",\"someDouble\":0.5}".getBytes(StandardCharsets.UTF_8))); .body(os -> os.write("{\"name\":\"Joe\",\"someDouble\":0.5}".getBytes(UTF_8)));
} }
@RequestMapping(params = "deferredResult") @RequestMapping(params = "deferredResult")
@ -291,7 +291,7 @@ public class AsyncTests {
return e.getMessage(); return e.getMessage();
} }
public void onMessage(String name) { void onMessage(String name) {
for (DeferredResult<Person> deferredResult : this.deferredResults) { for (DeferredResult<Person> deferredResult : this.deferredResults) {
deferredResult.setResult(new Person(name)); deferredResult.setResult(new Person(name));
this.deferredResults.remove(deferredResult); this.deferredResults.remove(deferredResult);

Loading…
Cancel
Save