Skip to content

Commit d03afeb

Browse files
committed
Consistent use of Charset.forName over JDK 7 StandardCharsets in 4.x line
(cherry picked from commit 2a82b8f)
1 parent f4037bf commit d03afeb

File tree

5 files changed

+93
-91
lines changed

5 files changed

+93
-91
lines changed

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

Lines changed: 43 additions & 40 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
import java.util.LinkedHashMap;
2423
import java.util.Map;
@@ -42,6 +41,7 @@
4241
import org.springframework.messaging.converter.StringMessageConverter;
4342
import org.springframework.messaging.handler.DestinationPatternsMessageCondition;
4443
import org.springframework.messaging.handler.annotation.SendTo;
44+
import org.springframework.messaging.handler.annotation.support.DestinationVariableMethodArgumentResolver;
4545
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
4646
import org.springframework.messaging.simp.SimpMessageSendingOperations;
4747
import org.springframework.messaging.simp.SimpMessagingTemplate;
@@ -53,8 +53,6 @@
5353

5454
import static org.junit.Assert.*;
5555
import static org.mockito.BDDMockito.*;
56-
import static org.springframework.messaging.handler.annotation.support.DestinationVariableMethodArgumentResolver.*;
57-
import static org.springframework.messaging.support.MessageHeaderAccessor.*;
5856

5957
/**
6058
* Test fixture for {@link SendToMethodReturnValueHandlerTests}.
@@ -103,31 +101,31 @@ public void setup() throws Exception {
103101
jsonMessagingTemplate.setMessageConverter(new MappingJackson2MessageConverter());
104102
this.jsonHandler = new SendToMethodReturnValueHandler(jsonMessagingTemplate, true);
105103

106-
Method method = this.getClass().getDeclaredMethod("handleNoAnnotations");
104+
Method method = getClass().getDeclaredMethod("handleNoAnnotations");
107105
this.noAnnotationsReturnType = new SynthesizingMethodParameter(method, -1);
108106

109-
method = this.getClass().getDeclaredMethod("handleAndSendToDefaultDestination");
107+
method = getClass().getDeclaredMethod("handleAndSendToDefaultDestination");
110108
this.sendToDefaultDestReturnType = new SynthesizingMethodParameter(method, -1);
111109

112-
method = this.getClass().getDeclaredMethod("handleAndSendTo");
110+
method = getClass().getDeclaredMethod("handleAndSendTo");
113111
this.sendToReturnType = new SynthesizingMethodParameter(method, -1);
114112

115-
method = this.getClass().getDeclaredMethod("handleAndSendToWithPlaceholders");
113+
method = getClass().getDeclaredMethod("handleAndSendToWithPlaceholders");
116114
this.sendToWithPlaceholdersReturnType = new SynthesizingMethodParameter(method, -1);
117115

118-
method = this.getClass().getDeclaredMethod("handleAndSendToUser");
116+
method = getClass().getDeclaredMethod("handleAndSendToUser");
119117
this.sendToUserReturnType = new SynthesizingMethodParameter(method, -1);
120118

121-
method = this.getClass().getDeclaredMethod("handleAndSendToUserSingleSession");
119+
method = getClass().getDeclaredMethod("handleAndSendToUserSingleSession");
122120
this.sendToUserSingleSessionReturnType = new SynthesizingMethodParameter(method, -1);
123121

124-
method = this.getClass().getDeclaredMethod("handleAndSendToUserDefaultDestination");
122+
method = getClass().getDeclaredMethod("handleAndSendToUserDefaultDestination");
125123
this.sendToUserDefaultDestReturnType = new SynthesizingMethodParameter(method, -1);
126124

127-
method = this.getClass().getDeclaredMethod("handleAndSendToUserDefaultDestinationSingleSession");
125+
method = getClass().getDeclaredMethod("handleAndSendToUserDefaultDestinationSingleSession");
128126
this.sendToUserSingleSessionDefaultDestReturnType = new SynthesizingMethodParameter(method, -1);
129127

130-
method = this.getClass().getDeclaredMethod("handleAndSendToJsonView");
128+
method = getClass().getDeclaredMethod("handleAndSendToJsonView");
131129
this.jsonViewReturnType = new SynthesizingMethodParameter(method, -1);
132130
}
133131

@@ -226,7 +224,8 @@ public void testHeadersToSend() throws Exception {
226224
verify(messagingTemplate).convertAndSend(eq("/topic/dest"), eq(PAYLOAD), captor.capture());
227225

228226
MessageHeaders messageHeaders = captor.getValue();
229-
SimpMessageHeaderAccessor accessor = getAccessor(messageHeaders, SimpMessageHeaderAccessor.class);
227+
SimpMessageHeaderAccessor accessor =
228+
MessageHeaderAccessor.getAccessor(messageHeaders, SimpMessageHeaderAccessor.class);
230229
assertNotNull(accessor);
231230
assertTrue(accessor.isMutable());
232231
assertEquals("sess1", accessor.getSessionId());
@@ -267,7 +266,7 @@ public void sendToWithDestinationPlaceholders() throws Exception {
267266
SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
268267
accessor.setSessionId(sessionId);
269268
accessor.setSubscriptionId("sub1");
270-
accessor.setHeader(DESTINATION_TEMPLATE_VARIABLES_HEADER, vars);
269+
accessor.setHeader(DestinationVariableMethodArgumentResolver.DESTINATION_TEMPLATE_VARIABLES_HEADER, vars);
271270
Message<?> message = MessageBuilder.createMessage(PAYLOAD, accessor.getMessageHeaders());
272271
this.handler.handleReturnValue(PAYLOAD, this.sendToWithPlaceholdersReturnType, message);
273272

@@ -403,7 +402,7 @@ public void jsonView() throws Exception {
403402
Message<?> message = this.messageCaptor.getValue();
404403
assertNotNull(message);
405404

406-
assertEquals("{\"withView1\":\"with\"}", new String((byte[]) message.getPayload(), StandardCharsets.UTF_8));
405+
assertEquals("{\"withView1\":\"with\"}", new String((byte[]) message.getPayload(), Charset.forName("UTF-8")));
407406
}
408407

409408

@@ -429,25 +428,6 @@ private SimpMessageHeaderAccessor getCapturedAccessor(int index) {
429428
}
430429

431430

432-
private static class TestUser implements Principal {
433-
434-
public String getName() {
435-
return "joe";
436-
}
437-
438-
public boolean implies(Subject subject) {
439-
return false;
440-
}
441-
}
442-
443-
private static class UniqueUser extends TestUser implements DestinationUserNameProvider {
444-
445-
@Override
446-
public String getDestinationUserName() {
447-
return "Me myself and I";
448-
}
449-
}
450-
451431
public String handleNoAnnotations() {
452432
return PAYLOAD;
453433
}
@@ -498,9 +478,32 @@ public JacksonViewBean handleAndSendToJsonView() {
498478
}
499479

500480

481+
private static class TestUser implements Principal {
482+
483+
public String getName() {
484+
return "joe";
485+
}
486+
487+
public boolean implies(Subject subject) {
488+
return false;
489+
}
490+
}
491+
492+
493+
private static class UniqueUser extends TestUser implements DestinationUserNameProvider {
494+
495+
@Override
496+
public String getDestinationUserName() {
497+
return "Me myself and I";
498+
}
499+
}
500+
501+
501502
private interface MyJacksonView1 {}
503+
502504
private interface MyJacksonView2 {}
503505

506+
504507
@SuppressWarnings("unused")
505508
private static class JacksonViewBean {
506509

@@ -516,23 +519,23 @@ public String getWithView1() {
516519
return withView1;
517520
}
518521

519-
public void setWithView1(String withView1) {
522+
void setWithView1(String withView1) {
520523
this.withView1 = withView1;
521524
}
522525

523-
public String getWithView2() {
526+
String getWithView2() {
524527
return withView2;
525528
}
526529

527-
public void setWithView2(String withView2) {
530+
void setWithView2(String withView2) {
528531
this.withView2 = withView2;
529532
}
530533

531-
public String getWithoutView() {
534+
String getWithoutView() {
532535
return withoutView;
533536
}
534537

535-
public void setWithoutView(String withoutView) {
538+
void setWithoutView(String withoutView) {
536539
this.withoutView = withoutView;
537540
}
538541
}

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: 12 additions & 24 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.
@@ -98,10 +98,7 @@ public void deferredResultWithImmediateValue() throws Exception {
9898
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
9999
}
100100

101-
/**
102-
* SPR-13079
103-
*/
104-
@Test
101+
@Test // SPR-13079
105102
public void deferredResultWithDelayedError() throws Exception {
106103
MvcResult mvcResult = this.mockMvc.perform(get("/1").param("deferredResultWithDelayedError", "true"))
107104
.andExpect(request().asyncStarted())
@@ -126,10 +123,7 @@ public void listenableFuture() throws Exception {
126123
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
127124
}
128125

129-
/**
130-
* SPR-12597
131-
*/
132-
@Test
126+
@Test // SPR-12597
133127
public void completableFutureWithImmediateValue() throws Exception {
134128
MvcResult mvcResult = this.mockMvc.perform(get("/1").param("completableFutureWithImmediateValue", "true"))
135129
.andExpect(request().asyncStarted())
@@ -141,10 +135,7 @@ public void completableFutureWithImmediateValue() throws Exception {
141135
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
142136
}
143137

144-
/**
145-
* SPR-12735
146-
*/
147-
@Test
138+
@Test // SPR-12735
148139
public void printAsyncResult() throws Exception {
149140
StringWriter writer = new StringWriter();
150141

@@ -172,12 +163,9 @@ public void printAsyncResult() throws Exception {
172163
@RequestMapping(path = "/{id}", produces = "application/json")
173164
private static class AsyncController {
174165

175-
private final Collection<DeferredResult<Person>> deferredResults =
176-
new CopyOnWriteArrayList<DeferredResult<Person>>();
177-
178-
private final Collection<ListenableFutureTask<Person>> futureTasks =
179-
new CopyOnWriteArrayList<ListenableFutureTask<Person>>();
166+
private final Collection<DeferredResult<Person>> deferredResults = new CopyOnWriteArrayList<>();
180167

168+
private final Collection<ListenableFutureTask<Person>> futureTasks = new CopyOnWriteArrayList<>();
181169

182170
@RequestMapping(params = "callable")
183171
public Callable<Person> getCallable() {
@@ -186,21 +174,21 @@ public Callable<Person> getCallable() {
186174

187175
@RequestMapping(params = "deferredResult")
188176
public DeferredResult<Person> getDeferredResult() {
189-
DeferredResult<Person> deferredResult = new DeferredResult<Person>();
177+
DeferredResult<Person> deferredResult = new DeferredResult<>();
190178
this.deferredResults.add(deferredResult);
191179
return deferredResult;
192180
}
193181

194182
@RequestMapping(params = "deferredResultWithImmediateValue")
195183
public DeferredResult<Person> getDeferredResultWithImmediateValue() {
196-
DeferredResult<Person> deferredResult = new DeferredResult<Person>();
184+
DeferredResult<Person> deferredResult = new DeferredResult<>();
197185
deferredResult.setResult(new Person("Joe"));
198186
return deferredResult;
199187
}
200188

201189
@RequestMapping(params = "deferredResultWithDelayedError")
202190
public DeferredResult<Person> getDeferredResultWithDelayedError() {
203-
final DeferredResult<Person> deferredResult = new DeferredResult<Person>();
191+
final DeferredResult<Person> deferredResult = new DeferredResult<>();
204192
new Thread() {
205193
public void run() {
206194
try {
@@ -217,14 +205,14 @@ public void run() {
217205

218206
@RequestMapping(params = "listenableFuture")
219207
public ListenableFuture<Person> getListenableFuture() {
220-
ListenableFutureTask<Person> futureTask = new ListenableFutureTask<Person>(() -> new Person("Joe"));
208+
ListenableFutureTask<Person> futureTask = new ListenableFutureTask<>(() -> new Person("Joe"));
221209
this.futureTasks.add(futureTask);
222210
return futureTask;
223211
}
224212

225213
@RequestMapping(params = "completableFutureWithImmediateValue")
226214
public CompletableFuture<Person> getCompletableFutureWithImmediateValue() {
227-
CompletableFuture<Person> future = new CompletableFuture<Person>();
215+
CompletableFuture<Person> future = new CompletableFuture<>();
228216
future.complete(new Person("Joe"));
229217
return future;
230218
}
@@ -235,7 +223,7 @@ public String errorHandler(Exception e) {
235223
return e.getMessage();
236224
}
237225

238-
public void onMessage(String name) {
226+
void onMessage(String name) {
239227
for (DeferredResult<Person> deferredResult : this.deferredResults) {
240228
deferredResult.setResult(new Person(name));
241229
this.deferredResults.remove(deferredResult);

0 commit comments

Comments
 (0)