Consistent use of Charset.forName over JDK 7 StandardCharsets in 4.x line

This commit is contained in:
Juergen Hoeller
2016-08-26 13:11:39 +02:00
parent 696f687419
commit 2a82b8fed9
5 changed files with 50 additions and 51 deletions

View File

@@ -17,7 +17,7 @@
package org.springframework.test.web.servlet.samples.standalone;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
@@ -78,7 +78,7 @@ public class AsyncTests {
public void streaming() throws Exception {
this.mockMvc.perform(get("/1").param("streaming", "true"))
.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(content().string("name=Joe"));
}
@@ -87,7 +87,7 @@ public class AsyncTests {
public void streamingSlow() throws Exception {
this.mockMvc.perform(get("/1").param("streamingSlow", "true"))
.andExpect(request().asyncStarted())
.andDo(r -> r.getAsyncResult())
.andDo(MvcResult::getAsyncResult)
.andExpect(status().isOk())
.andExpect(content().string("name=Joe&someBoolean=true"));
}
@@ -96,7 +96,7 @@ public class AsyncTests {
public void streamingJson() throws Exception {
this.mockMvc.perform(get("/1").param("streamingJson", "true"))
.andExpect(request().asyncStarted())
.andDo(r -> r.getAsyncResult())
.andDo(MvcResult::getAsyncResult)
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.5}"));
@@ -129,10 +129,7 @@ public class AsyncTests {
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
}
/**
* SPR-13079
*/
@Test
@Test // SPR-13079
public void deferredResultWithDelayedError() throws Exception {
MvcResult mvcResult = this.mockMvc.perform(get("/1").param("deferredResultWithDelayedError", "true"))
.andExpect(request().asyncStarted())
@@ -157,10 +154,7 @@ public class AsyncTests {
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
}
/**
* SPR-12597
*/
@Test
@Test // SPR-12597
public void completableFutureWithImmediateValue() throws Exception {
MvcResult mvcResult = this.mockMvc.perform(get("/1").param("completableFutureWithImmediateValue", "true"))
.andExpect(request().asyncStarted())
@@ -172,10 +166,7 @@ public class AsyncTests {
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
}
/**
* SPR-12735
*/
@Test
@Test // SPR-12735
public void printAsyncResult() throws Exception {
StringWriter writer = new StringWriter();
@@ -203,12 +194,9 @@ public class AsyncTests {
@RequestMapping(path = "/{id}", produces = "application/json")
private static class AsyncController {
private final Collection<DeferredResult<Person>> deferredResults =
new CopyOnWriteArrayList<DeferredResult<Person>>();
private final Collection<ListenableFutureTask<Person>> futureTasks =
new CopyOnWriteArrayList<ListenableFutureTask<Person>>();
private final Collection<DeferredResult<Person>> deferredResults = new CopyOnWriteArrayList<>();
private final Collection<ListenableFutureTask<Person>> futureTasks = new CopyOnWriteArrayList<>();
@RequestMapping(params = "callable")
public Callable<Person> getCallable() {
@@ -217,7 +205,7 @@ public class AsyncTests {
@RequestMapping(params = "streaming")
public StreamingResponseBody getStreaming() {
return os -> os.write("name=Joe".getBytes());
return os -> os.write("name=Joe".getBytes(Charset.forName("UTF-8")));
}
@RequestMapping(params = "streamingSlow")
@@ -226,7 +214,7 @@ public class AsyncTests {
os.write("name=Joe".getBytes());
try {
Thread.sleep(200);
os.write("&someBoolean=true".getBytes());
os.write("&someBoolean=true".getBytes(Charset.forName("UTF-8")));
}
catch (InterruptedException e) {
/* no-op */
@@ -237,26 +225,26 @@ public class AsyncTests {
@RequestMapping(params = "streamingJson")
public ResponseEntity<StreamingResponseBody> getStreamingJson() {
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(Charset.forName("UTF-8"))));
}
@RequestMapping(params = "deferredResult")
public DeferredResult<Person> getDeferredResult() {
DeferredResult<Person> deferredResult = new DeferredResult<Person>();
DeferredResult<Person> deferredResult = new DeferredResult<>();
this.deferredResults.add(deferredResult);
return deferredResult;
}
@RequestMapping(params = "deferredResultWithImmediateValue")
public DeferredResult<Person> getDeferredResultWithImmediateValue() {
DeferredResult<Person> deferredResult = new DeferredResult<Person>();
DeferredResult<Person> deferredResult = new DeferredResult<>();
deferredResult.setResult(new Person("Joe"));
return deferredResult;
}
@RequestMapping(params = "deferredResultWithDelayedError")
public DeferredResult<Person> getDeferredResultWithDelayedError() {
final DeferredResult<Person> deferredResult = new DeferredResult<Person>();
final DeferredResult<Person> deferredResult = new DeferredResult<>();
new Thread() {
public void run() {
try {
@@ -273,14 +261,14 @@ public class AsyncTests {
@RequestMapping(params = "listenableFuture")
public ListenableFuture<Person> getListenableFuture() {
ListenableFutureTask<Person> futureTask = new ListenableFutureTask<Person>(() -> new Person("Joe"));
ListenableFutureTask<Person> futureTask = new ListenableFutureTask<>(() -> new Person("Joe"));
this.futureTasks.add(futureTask);
return futureTask;
}
@RequestMapping(params = "completableFutureWithImmediateValue")
public CompletableFuture<Person> getCompletableFutureWithImmediateValue() {
CompletableFuture<Person> future = new CompletableFuture<Person>();
CompletableFuture<Person> future = new CompletableFuture<>();
future.complete(new Person("Joe"));
return future;
}
@@ -291,7 +279,7 @@ public class AsyncTests {
return e.getMessage();
}
public void onMessage(String name) {
void onMessage(String name) {
for (DeferredResult<Person> deferredResult : this.deferredResults) {
deferredResult.setResult(new Person(name));
this.deferredResults.remove(deferredResult);