Polishing

This commit is contained in:
Juergen Hoeller 2016-08-26 18:33:13 +02:00
parent e828be96f0
commit 430180aa96
5 changed files with 81 additions and 72 deletions

View File

@ -42,7 +42,9 @@ import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHeaders; import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.converter.MappingJackson2MessageConverter; import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.converter.StringMessageConverter; import org.springframework.messaging.converter.StringMessageConverter;
import org.springframework.messaging.handler.DestinationPatternsMessageCondition;
import org.springframework.messaging.handler.annotation.SendTo; import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.handler.annotation.support.DestinationVariableMethodArgumentResolver;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor; import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessageSendingOperations; import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.messaging.simp.SimpMessagingTemplate;
@ -54,9 +56,6 @@ import org.springframework.util.MimeType;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*; import static org.mockito.BDDMockito.*;
import static org.springframework.messaging.handler.DestinationPatternsMessageCondition.*;
import static org.springframework.messaging.handler.annotation.support.DestinationVariableMethodArgumentResolver.*;
import static org.springframework.messaging.support.MessageHeaderAccessor.*;
/** /**
* Test fixture for {@link SendToMethodReturnValueHandlerTests}. * Test fixture for {@link SendToMethodReturnValueHandlerTests}.
@ -357,7 +356,8 @@ public class SendToMethodReturnValueHandlerTests {
verify(messagingTemplate).convertAndSend(eq("/topic/dest"), eq(PAYLOAD), captor.capture()); verify(messagingTemplate).convertAndSend(eq("/topic/dest"), eq(PAYLOAD), captor.capture());
MessageHeaders headers = captor.getValue(); MessageHeaders headers = captor.getValue();
SimpMessageHeaderAccessor accessor = getAccessor(headers, SimpMessageHeaderAccessor.class); SimpMessageHeaderAccessor accessor =
MessageHeaderAccessor.getAccessor(headers, SimpMessageHeaderAccessor.class);
assertNotNull(accessor); assertNotNull(accessor);
assertTrue(accessor.isMutable()); assertTrue(accessor.isMutable());
assertEquals("sess1", accessor.getSessionId()); assertEquals("sess1", accessor.getSessionId());
@ -399,7 +399,7 @@ public class SendToMethodReturnValueHandlerTests {
SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(); SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
accessor.setSessionId(sessionId); accessor.setSessionId(sessionId);
accessor.setSubscriptionId("sub1"); accessor.setSubscriptionId("sub1");
accessor.setHeader(DESTINATION_TEMPLATE_VARIABLES_HEADER, vars); accessor.setHeader(DestinationVariableMethodArgumentResolver.DESTINATION_TEMPLATE_VARIABLES_HEADER, vars);
Message<?> message = MessageBuilder.createMessage(PAYLOAD, accessor.getMessageHeaders()); Message<?> message = MessageBuilder.createMessage(PAYLOAD, accessor.getMessageHeaders());
this.handler.handleReturnValue(PAYLOAD, this.sendToWithPlaceholdersReturnType, message); this.handler.handleReturnValue(PAYLOAD, this.sendToWithPlaceholdersReturnType, message);
@ -549,7 +549,7 @@ public class SendToMethodReturnValueHandlerTests {
headerAccessor.setSubscriptionId(subsId); headerAccessor.setSubscriptionId(subsId);
if (dest != null && destPrefix != null) { if (dest != null && destPrefix != null) {
headerAccessor.setDestination(destPrefix + dest); headerAccessor.setDestination(destPrefix + dest);
headerAccessor.setHeader(LOOKUP_DESTINATION_HEADER, dest); headerAccessor.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, dest);
} }
if (user != null) { if (user != null) {
headerAccessor.setUser(user); headerAccessor.setUser(user);
@ -563,6 +563,64 @@ public class SendToMethodReturnValueHandlerTests {
} }
@SuppressWarnings("unused")
String handleNoAnnotations() {
return PAYLOAD;
}
@SendTo
@SuppressWarnings("unused")
String handleAndSendToDefaultDestination() {
return PAYLOAD;
}
@SendTo({"/dest1", "/dest2"})
@SuppressWarnings("unused")
String handleAndSendTo() {
return PAYLOAD;
}
@SendTo("/topic/chat.message.filtered.{roomName}")
@SuppressWarnings("unused")
String handleAndSendToWithPlaceholders() {
return PAYLOAD;
}
@SendToUser
@SuppressWarnings("unused")
String handleAndSendToUserDefaultDestination() {
return PAYLOAD;
}
@SendToUser(broadcast = false)
@SuppressWarnings("unused")
String handleAndSendToUserDefaultDestinationSingleSession() {
return PAYLOAD;
}
@SendToUser({"/dest1", "/dest2"})
@SuppressWarnings("unused")
String handleAndSendToUser() {
return PAYLOAD;
}
@SendToUser(destinations = { "/dest1", "/dest2" }, broadcast = false)
@SuppressWarnings("unused")
String handleAndSendToUserSingleSession() {
return PAYLOAD;
}
@JsonView(MyJacksonView1.class)
@SuppressWarnings("unused")
JacksonViewBean handleAndSendToJsonView() {
JacksonViewBean payload = new JacksonViewBean();
payload.setWithView1("with");
payload.setWithView2("with");
payload.setWithoutView("without");
return payload;
}
private static class TestUser implements Principal { private static class TestUser implements Principal {
public String getName() { public String getName() {
@ -574,6 +632,7 @@ public class SendToMethodReturnValueHandlerTests {
} }
} }
private static class UniqueUser extends TestUser implements DestinationUserNameProvider { private static class UniqueUser extends TestUser implements DestinationUserNameProvider {
@Override @Override
@ -582,6 +641,7 @@ public class SendToMethodReturnValueHandlerTests {
} }
} }
@SendTo @SendTo
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@interface MySendTo { @interface MySendTo {
@ -590,6 +650,7 @@ public class SendToMethodReturnValueHandlerTests {
String[] dest(); String[] dest();
} }
@SendToUser @SendToUser
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@interface MySendToUser { @interface MySendToUser {
@ -599,56 +660,6 @@ public class SendToMethodReturnValueHandlerTests {
} }
@SuppressWarnings("unused")
String handleNoAnnotations() {
return PAYLOAD;
}
@SendTo @SuppressWarnings("unused")
String handleAndSendToDefaultDestination() {
return PAYLOAD;
}
@SendTo({"/dest1", "/dest2"}) @SuppressWarnings("unused")
String handleAndSendTo() {
return PAYLOAD;
}
@SendTo("/topic/chat.message.filtered.{roomName}") @SuppressWarnings("unused")
String handleAndSendToWithPlaceholders() {
return PAYLOAD;
}
@SendToUser @SuppressWarnings("unused")
String handleAndSendToUserDefaultDestination() {
return PAYLOAD;
}
@SendToUser(broadcast = false) @SuppressWarnings("unused")
String handleAndSendToUserDefaultDestinationSingleSession() {
return PAYLOAD;
}
@SendToUser({"/dest1", "/dest2"}) @SuppressWarnings("unused")
String handleAndSendToUser() {
return PAYLOAD;
}
@SendToUser(destinations = { "/dest1", "/dest2" }, broadcast = false) @SuppressWarnings("unused")
String handleAndSendToUserSingleSession() {
return PAYLOAD;
}
@JsonView(MyJacksonView1.class) @SuppressWarnings("unused")
JacksonViewBean handleAndSendToJsonView() {
JacksonViewBean payload = new JacksonViewBean();
payload.setWithView1("with");
payload.setWithView2("with");
payload.setWithoutView("without");
return payload;
}
@MySendTo(dest = "/dest-default") @SuppressWarnings("unused") @MySendTo(dest = "/dest-default") @SuppressWarnings("unused")
private static class SendToTestBean { private static class SendToTestBean {
@ -667,6 +678,7 @@ public class SendToMethodReturnValueHandlerTests {
} }
} }
@MySendToUser(dest = "/dest-default") @SuppressWarnings("unused") @MySendToUser(dest = "/dest-default") @SuppressWarnings("unused")
private static class SendToUserTestBean { private static class SendToUserTestBean {
@ -685,6 +697,7 @@ public class SendToMethodReturnValueHandlerTests {
} }
} }
@MySendToUser(dest = "/dest-default") @SuppressWarnings("unused") @MySendToUser(dest = "/dest-default") @SuppressWarnings("unused")
private static class SendToUserWithSendToOverrideTestBean { private static class SendToUserWithSendToOverrideTestBean {
@ -701,8 +714,10 @@ public class SendToMethodReturnValueHandlerTests {
private interface MyJacksonView1 {} private interface MyJacksonView1 {}
private interface MyJacksonView2 {} private interface MyJacksonView2 {}
@SuppressWarnings("unused") @SuppressWarnings("unused")
private static class JacksonViewBean { private static class JacksonViewBean {

View File

@ -70,7 +70,7 @@ public class AsyncTests {
this.mockMvc.perform(asyncDispatch(mvcResult)) this.mockMvc.perform(asyncDispatch(mvcResult))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
} }
@ -98,7 +98,7 @@ public class AsyncTests {
.andExpect(request().asyncStarted()) .andExpect(request().asyncStarted())
.andDo(MvcResult::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))
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.5}")); .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.5}"));
} }
@ -112,7 +112,7 @@ public class AsyncTests {
this.mockMvc.perform(asyncDispatch(mvcResult)) this.mockMvc.perform(asyncDispatch(mvcResult))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
} }
@ -125,7 +125,7 @@ public class AsyncTests {
this.mockMvc.perform(asyncDispatch(mvcResult)) this.mockMvc.perform(asyncDispatch(mvcResult))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
} }
@ -150,7 +150,7 @@ public class AsyncTests {
this.mockMvc.perform(asyncDispatch(mvcResult)) this.mockMvc.perform(asyncDispatch(mvcResult))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
} }
@ -162,7 +162,7 @@ public class AsyncTests {
this.mockMvc.perform(asyncDispatch(mvcResult)) this.mockMvc.perform(asyncDispatch(mvcResult))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
} }
@ -183,7 +183,7 @@ public class AsyncTests {
this.mockMvc.perform(asyncDispatch(mvcResult)) this.mockMvc.perform(asyncDispatch(mvcResult))
.andDo(print(writer)) .andDo(print(writer))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}")); .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
assertTrue(writer.toString().contains("Async started = false")); assertTrue(writer.toString().contains("Async started = false"));

View File

@ -630,7 +630,6 @@ public class AsyncRestTemplateIntegrationTests extends AbstractJettyServerTestCa
AsyncClientHttpRequestExecution execution) throws IOException { AsyncClientHttpRequestExecution execution) throws IOException {
request = new HttpRequestWrapper(request) { request = new HttpRequestWrapper(request) {
@Override @Override
public URI getURI() { public URI getURI() {
try { try {

View File

@ -240,9 +240,7 @@ public class RestTemplateIntegrationTests extends AbstractJettyServerTestCase {
assertFalse(s.contains("\"without\":\"without\"")); assertFalse(s.contains("\"without\":\"without\""));
} }
// SPR-12123 @Test // SPR-12123
@Test
public void serverPort() { public void serverPort() {
String s = template.getForObject("http://localhost:{port}/get", String.class, port); String s = template.getForObject("http://localhost:{port}/get", String.class, port);
assertEquals("Invalid content", helloWorld, s); assertEquals("Invalid content", helloWorld, s);

View File

@ -67,16 +67,16 @@ public class ScriptTemplateViewTests {
this.view = new ScriptTemplateView(); this.view = new ScriptTemplateView();
} }
@Test @Test
public void missingScriptTemplateConfig() throws Exception { public void missingScriptTemplateConfig() throws Exception {
try { try {
this.view.setApplicationContext(new StaticApplicationContext()); this.view.setApplicationContext(new StaticApplicationContext());
fail("Should have thrown ApplicationContextException");
} }
catch (ApplicationContextException ex) { catch (ApplicationContextException ex) {
assertTrue(ex.getMessage().contains("ScriptTemplateConfig")); assertTrue(ex.getMessage().contains("ScriptTemplateConfig"));
return;
} }
fail();
} }
@Test @Test
@ -158,7 +158,6 @@ public class ScriptTemplateViewTests {
} }
catch (IllegalArgumentException ex) { catch (IllegalArgumentException ex) {
assertThat(ex.getMessage(), containsString("instance")); assertThat(ex.getMessage(), containsString("instance"));
return;
} }
} }
@ -199,9 +198,7 @@ public class ScriptTemplateViewTests {
} }
catch (IllegalArgumentException ex) { catch (IllegalArgumentException ex) {
assertThat(ex.getMessage(), containsString("sharedEngine")); assertThat(ex.getMessage(), containsString("sharedEngine"));
return;
} }
fail();
} }
@Test // SPR-14210 @Test // SPR-14210