Skip to content

Commit 2a82b8f

Browse files
committed
Consistent use of Charset.forName over JDK 7 StandardCharsets in 4.x line
1 parent 696f687 commit 2a82b8f

File tree

5 files changed

+50
-51
lines changed

5 files changed

+50
-51
lines changed

spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SendToMethodReturnValueHandlerTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.lang.annotation.RetentionPolicy;
2121
import java.lang.reflect.Method;
2222
import java.nio.charset.Charset;
23-
import java.nio.charset.StandardCharsets;
2423
import java.security.Principal;
2524
import java.util.LinkedHashMap;
2625
import java.util.Map;
@@ -539,7 +538,7 @@ public void jsonView() throws Exception {
539538
Message<?> message = this.messageCaptor.getValue();
540539
assertNotNull(message);
541540

542-
String bytes = new String((byte[]) message.getPayload(), StandardCharsets.UTF_8);
541+
String bytes = new String((byte[]) message.getPayload(), Charset.forName("UTF-8"));
543542
assertEquals("{\"withView1\":\"with\"}", bytes);
544543
}
545544

spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SubscriptionMethodReturnValueHandlerTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@
1818

1919
import java.lang.reflect.Method;
2020
import java.nio.charset.Charset;
21-
import java.nio.charset.StandardCharsets;
2221
import java.security.Principal;
2322

2423
import com.fasterxml.jackson.annotation.JsonView;
@@ -179,7 +178,7 @@ public void testJsonView() throws Exception {
179178
Message<?> message = this.messageCaptor.getValue();
180179
assertNotNull(message);
181180

182-
assertEquals("{\"withView1\":\"with\"}", new String((byte[]) message.getPayload(), StandardCharsets.UTF_8));
181+
assertEquals("{\"withView1\":\"with\"}", new String((byte[]) message.getPayload(), Charset.forName("UTF-8")));
183182
}
184183

185184

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

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.springframework.test.web.servlet.samples.standalone;
1818

1919
import java.io.StringWriter;
20-
import java.nio.charset.StandardCharsets;
20+
import java.nio.charset.Charset;
2121
import java.util.Collection;
2222
import java.util.concurrent.Callable;
2323
import java.util.concurrent.CompletableFuture;
@@ -78,7 +78,7 @@ public void callable() throws Exception {
7878
public void streaming() throws Exception {
7979
this.mockMvc.perform(get("/1").param("streaming", "true"))
8080
.andExpect(request().asyncStarted())
81-
.andDo(r -> r.getAsyncResult()) // fetch async result similar to "asyncDispatch" builder
81+
.andDo(MvcResult::getAsyncResult) // fetch async result similar to "asyncDispatch" builder
8282
.andExpect(status().isOk())
8383
.andExpect(content().string("name=Joe"));
8484
}
@@ -87,7 +87,7 @@ public void streaming() throws Exception {
8787
public void streamingSlow() throws Exception {
8888
this.mockMvc.perform(get("/1").param("streamingSlow", "true"))
8989
.andExpect(request().asyncStarted())
90-
.andDo(r -> r.getAsyncResult())
90+
.andDo(MvcResult::getAsyncResult)
9191
.andExpect(status().isOk())
9292
.andExpect(content().string("name=Joe&someBoolean=true"));
9393
}
@@ -96,7 +96,7 @@ public void streamingSlow() throws Exception {
9696
public void streamingJson() throws Exception {
9797
this.mockMvc.perform(get("/1").param("streamingJson", "true"))
9898
.andExpect(request().asyncStarted())
99-
.andDo(r -> r.getAsyncResult())
99+
.andDo(MvcResult::getAsyncResult)
100100
.andExpect(status().isOk())
101101
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
102102
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.5}"));
@@ -129,10 +129,7 @@ public void deferredResultWithImmediateValue() throws Exception {
129129
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
130130
}
131131

132-
/**
133-
* SPR-13079
134-
*/
135-
@Test
132+
@Test // SPR-13079
136133
public void deferredResultWithDelayedError() throws Exception {
137134
MvcResult mvcResult = this.mockMvc.perform(get("/1").param("deferredResultWithDelayedError", "true"))
138135
.andExpect(request().asyncStarted())
@@ -157,10 +154,7 @@ public void listenableFuture() throws Exception {
157154
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
158155
}
159156

160-
/**
161-
* SPR-12597
162-
*/
163-
@Test
157+
@Test // SPR-12597
164158
public void completableFutureWithImmediateValue() throws Exception {
165159
MvcResult mvcResult = this.mockMvc.perform(get("/1").param("completableFutureWithImmediateValue", "true"))
166160
.andExpect(request().asyncStarted())
@@ -172,10 +166,7 @@ public void completableFutureWithImmediateValue() throws Exception {
172166
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
173167
}
174168

175-
/**
176-
* SPR-12735
177-
*/
178-
@Test
169+
@Test // SPR-12735
179170
public void printAsyncResult() throws Exception {
180171
StringWriter writer = new StringWriter();
181172

@@ -203,12 +194,9 @@ public void printAsyncResult() throws Exception {
203194
@RequestMapping(path = "/{id}", produces = "application/json")
204195
private static class AsyncController {
205196

206-
private final Collection<DeferredResult<Person>> deferredResults =
207-
new CopyOnWriteArrayList<DeferredResult<Person>>();
208-
209-
private final Collection<ListenableFutureTask<Person>> futureTasks =
210-
new CopyOnWriteArrayList<ListenableFutureTask<Person>>();
197+
private final Collection<DeferredResult<Person>> deferredResults = new CopyOnWriteArrayList<>();
211198

199+
private final Collection<ListenableFutureTask<Person>> futureTasks = new CopyOnWriteArrayList<>();
212200

213201
@RequestMapping(params = "callable")
214202
public Callable<Person> getCallable() {
@@ -217,7 +205,7 @@ public Callable<Person> getCallable() {
217205

218206
@RequestMapping(params = "streaming")
219207
public StreamingResponseBody getStreaming() {
220-
return os -> os.write("name=Joe".getBytes());
208+
return os -> os.write("name=Joe".getBytes(Charset.forName("UTF-8")));
221209
}
222210

223211
@RequestMapping(params = "streamingSlow")
@@ -226,7 +214,7 @@ public StreamingResponseBody getStreamingSlow() {
226214
os.write("name=Joe".getBytes());
227215
try {
228216
Thread.sleep(200);
229-
os.write("&someBoolean=true".getBytes());
217+
os.write("&someBoolean=true".getBytes(Charset.forName("UTF-8")));
230218
}
231219
catch (InterruptedException e) {
232220
/* no-op */
@@ -237,26 +225,26 @@ public StreamingResponseBody getStreamingSlow() {
237225
@RequestMapping(params = "streamingJson")
238226
public ResponseEntity<StreamingResponseBody> getStreamingJson() {
239227
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON_UTF8)
240-
.body(os -> os.write("{\"name\":\"Joe\",\"someDouble\":0.5}".getBytes(StandardCharsets.UTF_8)));
228+
.body(os -> os.write("{\"name\":\"Joe\",\"someDouble\":0.5}".getBytes(Charset.forName("UTF-8"))));
241229
}
242230

243231
@RequestMapping(params = "deferredResult")
244232
public DeferredResult<Person> getDeferredResult() {
245-
DeferredResult<Person> deferredResult = new DeferredResult<Person>();
233+
DeferredResult<Person> deferredResult = new DeferredResult<>();
246234
this.deferredResults.add(deferredResult);
247235
return deferredResult;
248236
}
249237

250238
@RequestMapping(params = "deferredResultWithImmediateValue")
251239
public DeferredResult<Person> getDeferredResultWithImmediateValue() {
252-
DeferredResult<Person> deferredResult = new DeferredResult<Person>();
240+
DeferredResult<Person> deferredResult = new DeferredResult<>();
253241
deferredResult.setResult(new Person("Joe"));
254242
return deferredResult;
255243
}
256244

257245
@RequestMapping(params = "deferredResultWithDelayedError")
258246
public DeferredResult<Person> getDeferredResultWithDelayedError() {
259-
final DeferredResult<Person> deferredResult = new DeferredResult<Person>();
247+
final DeferredResult<Person> deferredResult = new DeferredResult<>();
260248
new Thread() {
261249
public void run() {
262250
try {
@@ -273,14 +261,14 @@ public void run() {
273261

274262
@RequestMapping(params = "listenableFuture")
275263
public ListenableFuture<Person> getListenableFuture() {
276-
ListenableFutureTask<Person> futureTask = new ListenableFutureTask<Person>(() -> new Person("Joe"));
264+
ListenableFutureTask<Person> futureTask = new ListenableFutureTask<>(() -> new Person("Joe"));
277265
this.futureTasks.add(futureTask);
278266
return futureTask;
279267
}
280268

281269
@RequestMapping(params = "completableFutureWithImmediateValue")
282270
public CompletableFuture<Person> getCompletableFutureWithImmediateValue() {
283-
CompletableFuture<Person> future = new CompletableFuture<Person>();
271+
CompletableFuture<Person> future = new CompletableFuture<>();
284272
future.complete(new Person("Joe"));
285273
return future;
286274
}
@@ -291,7 +279,7 @@ public String errorHandler(Exception e) {
291279
return e.getMessage();
292280
}
293281

294-
public void onMessage(String name) {
282+
void onMessage(String name) {
295283
for (DeferredResult<Person> deferredResult : this.deferredResults) {
296284
deferredResult.setResult(new Person(name));
297285
this.deferredResults.remove(deferredResult);

spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,7 +21,6 @@
2121
import java.lang.annotation.RetentionPolicy;
2222
import java.lang.annotation.Target;
2323
import java.lang.reflect.Method;
24-
import java.nio.charset.StandardCharsets;
2524
import java.util.Arrays;
2625
import java.util.Collection;
2726
import java.util.Collections;
@@ -145,8 +144,8 @@
145144
import org.springframework.web.servlet.view.velocity.VelocityViewResolver;
146145
import org.springframework.web.util.UrlPathHelper;
147146

148-
import static org.hamcrest.CoreMatchers.*;
149-
import static org.hamcrest.Matchers.containsInAnyOrder;
147+
import static org.hamcrest.CoreMatchers.instanceOf;
148+
import static org.hamcrest.Matchers.*;
150149
import static org.junit.Assert.*;
151150

152151
/**
@@ -823,7 +822,7 @@ public void testViewResolution() throws Exception {
823822
assertNotNull(scriptTemplateConfigurer);
824823
assertEquals("render", scriptTemplateConfigurer.getRenderFunction());
825824
assertEquals(MediaType.TEXT_PLAIN_VALUE, scriptTemplateConfigurer.getContentType());
826-
assertEquals(StandardCharsets.ISO_8859_1, scriptTemplateConfigurer.getCharset());
825+
assertEquals("ISO-8859-1", scriptTemplateConfigurer.getCharset().name());
827826
assertEquals("classpath:", scriptTemplateConfigurer.getResourceLoaderPath());
828827
assertFalse(scriptTemplateConfigurer.isSharedEngine());
829828
String[] scripts = { "org/springframework/web/servlet/view/script/nashorn/render.js" };
@@ -956,18 +955,21 @@ private void loadBeanDefinitions(String fileName, int expectedBeanCount) {
956955
public @interface IsoDate {
957956
}
958957

958+
959959
@NumberFormat(style = NumberFormat.Style.PERCENT)
960960
@Target({ElementType.PARAMETER})
961961
@Retention(RetentionPolicy.RUNTIME)
962962
public @interface PercentNumber {
963963
}
964964

965+
965966
@Validated(MyGroup.class)
966967
@Target({ElementType.PARAMETER})
967968
@Retention(RetentionPolicy.RUNTIME)
968969
public @interface MyValid {
969970
}
970971

972+
971973
@Controller
972974
public static class TestController {
973975

@@ -978,13 +980,16 @@ public static class TestController {
978980
private boolean recordedValidationError;
979981

980982
@RequestMapping
981-
public void testBind(@RequestParam @IsoDate Date date, @RequestParam(required = false) @PercentNumber Double percent, @MyValid TestBean bean, BindingResult result) {
983+
public void testBind(@RequestParam @IsoDate Date date,
984+
@RequestParam(required = false) @PercentNumber Double percent,
985+
@MyValid TestBean bean, BindingResult result) {
982986
this.date = date;
983987
this.percent = percent;
984988
this.recordedValidationError = (result.getErrorCount() == 1);
985989
}
986990
}
987991

992+
988993
public static class TestValidator implements Validator {
989994

990995
boolean validatorInvoked;
@@ -1000,10 +1005,12 @@ public void validate(Object target, Errors errors) {
10001005
}
10011006
}
10021007

1008+
10031009
@Retention(RetentionPolicy.RUNTIME)
10041010
public @interface MyGroup {
10051011
}
10061012

1013+
10071014
private static class TestBean {
10081015

10091016
@NotNull(groups = MyGroup.class)
@@ -1020,6 +1027,7 @@ public void setField(String field) {
10201027
}
10211028
}
10221029

1030+
10231031
private static class TestMockServletContext extends MockServletContext {
10241032

10251033
@Override
@@ -1033,12 +1041,15 @@ public RequestDispatcher getNamedDispatcher(String path) {
10331041
}
10341042
}
10351043

1044+
10361045
public static class TestCallableProcessingInterceptor extends CallableProcessingInterceptorAdapter {
10371046
}
10381047

1048+
10391049
public static class TestDeferredResultProcessingInterceptor extends DeferredResultProcessingInterceptorAdapter {
10401050
}
10411051

1052+
10421053
public static class TestPathMatcher implements PathMatcher {
10431054

10441055
@Override
@@ -1077,9 +1088,11 @@ public String combine(String pattern1, String pattern2) {
10771088
}
10781089
}
10791090

1091+
10801092
public static class TestPathHelper extends UrlPathHelper {
10811093
}
10821094

1095+
10831096
public static class TestCacheManager implements CacheManager {
10841097

10851098
@Override

0 commit comments

Comments
 (0)