diff options
Diffstat (limited to 'spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SendToMethodReturnValueHandlerTests.java')
-rw-r--r-- | spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SendToMethodReturnValueHandlerTests.java | 379 |
1 files changed, 291 insertions, 88 deletions
diff --git a/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SendToMethodReturnValueHandlerTests.java b/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SendToMethodReturnValueHandlerTests.java index 277f7701..5d831dca 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SendToMethodReturnValueHandlerTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SendToMethodReturnValueHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,8 @@ package org.springframework.messaging.simp.annotation.support; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; @@ -34,13 +36,13 @@ import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.springframework.core.MethodParameter; +import org.springframework.core.annotation.AliasFor; import org.springframework.core.annotation.SynthesizingMethodParameter; import org.springframework.messaging.Message; import org.springframework.messaging.MessageChannel; import org.springframework.messaging.MessageHeaders; import org.springframework.messaging.converter.MappingJackson2MessageConverter; import org.springframework.messaging.converter.StringMessageConverter; -import org.springframework.messaging.handler.DestinationPatternsMessageCondition; import org.springframework.messaging.handler.annotation.SendTo; import org.springframework.messaging.simp.SimpMessageHeaderAccessor; import org.springframework.messaging.simp.SimpMessageSendingOperations; @@ -53,6 +55,7 @@ import org.springframework.util.MimeType; import static org.junit.Assert.*; 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.*; @@ -61,6 +64,7 @@ import static org.springframework.messaging.support.MessageHeaderAccessor.*; * * @author Rossen Stoyanchev * @author Sebastien Deleuze + * @author Stephane Nicoll */ public class SendToMethodReturnValueHandlerTests { @@ -88,6 +92,12 @@ public class SendToMethodReturnValueHandlerTests { private MethodParameter sendToUserDefaultDestReturnType; private MethodParameter sendToUserSingleSessionDefaultDestReturnType; private MethodParameter jsonViewReturnType; + private MethodParameter defaultNoAnnotation; + private MethodParameter defaultEmptyAnnotation; + private MethodParameter defaultOverrideAnnotation; + private MethodParameter userDefaultNoAnnotation; + private MethodParameter userDefaultEmptyAnnotation; + private MethodParameter userDefaultOverrideAnnotation; @Before @@ -103,32 +113,50 @@ public class SendToMethodReturnValueHandlerTests { jsonMessagingTemplate.setMessageConverter(new MappingJackson2MessageConverter()); this.jsonHandler = new SendToMethodReturnValueHandler(jsonMessagingTemplate, true); - Method method = this.getClass().getDeclaredMethod("handleNoAnnotations"); + Method method = getClass().getDeclaredMethod("handleNoAnnotations"); this.noAnnotationsReturnType = new SynthesizingMethodParameter(method, -1); - method = this.getClass().getDeclaredMethod("handleAndSendToDefaultDestination"); + method = getClass().getDeclaredMethod("handleAndSendToDefaultDestination"); this.sendToDefaultDestReturnType = new SynthesizingMethodParameter(method, -1); - method = this.getClass().getDeclaredMethod("handleAndSendTo"); + method = getClass().getDeclaredMethod("handleAndSendTo"); this.sendToReturnType = new SynthesizingMethodParameter(method, -1); - method = this.getClass().getDeclaredMethod("handleAndSendToWithPlaceholders"); + method = getClass().getDeclaredMethod("handleAndSendToWithPlaceholders"); this.sendToWithPlaceholdersReturnType = new SynthesizingMethodParameter(method, -1); - method = this.getClass().getDeclaredMethod("handleAndSendToUser"); + method = getClass().getDeclaredMethod("handleAndSendToUser"); this.sendToUserReturnType = new SynthesizingMethodParameter(method, -1); - method = this.getClass().getDeclaredMethod("handleAndSendToUserSingleSession"); + method = getClass().getDeclaredMethod("handleAndSendToUserSingleSession"); this.sendToUserSingleSessionReturnType = new SynthesizingMethodParameter(method, -1); - method = this.getClass().getDeclaredMethod("handleAndSendToUserDefaultDestination"); + method = getClass().getDeclaredMethod("handleAndSendToUserDefaultDestination"); this.sendToUserDefaultDestReturnType = new SynthesizingMethodParameter(method, -1); - method = this.getClass().getDeclaredMethod("handleAndSendToUserDefaultDestinationSingleSession"); + method = getClass().getDeclaredMethod("handleAndSendToUserDefaultDestinationSingleSession"); this.sendToUserSingleSessionDefaultDestReturnType = new SynthesizingMethodParameter(method, -1); - method = this.getClass().getDeclaredMethod("handleAndSendToJsonView"); + method = getClass().getDeclaredMethod("handleAndSendToJsonView"); this.jsonViewReturnType = new SynthesizingMethodParameter(method, -1); + + method = SendToTestBean.class.getDeclaredMethod("handleNoAnnotation"); + this.defaultNoAnnotation = new SynthesizingMethodParameter(method, -1); + + method = SendToTestBean.class.getDeclaredMethod("handleAndSendToDefaultDestination"); + this.defaultEmptyAnnotation = new SynthesizingMethodParameter(method, -1); + + method = SendToTestBean.class.getDeclaredMethod("handleAndSendToOverride"); + this.defaultOverrideAnnotation = new SynthesizingMethodParameter(method, -1); + + method = SendToUserTestBean.class.getDeclaredMethod("handleNoAnnotation"); + this.userDefaultNoAnnotation = new SynthesizingMethodParameter(method, -1); + + method = SendToUserTestBean.class.getDeclaredMethod("handleAndSendToDefaultDestination"); + this.userDefaultEmptyAnnotation = new SynthesizingMethodParameter(method, -1); + + method = SendToUserTestBean.class.getDeclaredMethod("handleAndSendToOverride"); + this.userDefaultOverrideAnnotation = new SynthesizingMethodParameter(method, -1); } @@ -138,23 +166,26 @@ public class SendToMethodReturnValueHandlerTests { assertTrue(this.handler.supportsReturnType(this.sendToUserReturnType)); assertFalse(this.handler.supportsReturnType(this.noAnnotationsReturnType)); assertTrue(this.handlerAnnotationNotRequired.supportsReturnType(this.noAnnotationsReturnType)); + + assertTrue(this.handler.supportsReturnType(this.defaultNoAnnotation)); + assertTrue(this.handler.supportsReturnType(this.defaultEmptyAnnotation)); + assertTrue(this.handler.supportsReturnType(this.defaultOverrideAnnotation)); + + assertTrue(this.handler.supportsReturnType(this.userDefaultNoAnnotation)); + assertTrue(this.handler.supportsReturnType(this.userDefaultEmptyAnnotation)); + assertTrue(this.handler.supportsReturnType(this.userDefaultOverrideAnnotation)); } @Test public void sendToNoAnnotations() throws Exception { given(this.messageChannel.send(any(Message.class))).willReturn(true); - Message<?> inputMessage = createInputMessage("sess1", "sub1", "/app", "/dest", null); + String sessionId = "sess1"; + Message<?> inputMessage = createMessage(sessionId, "sub1", "/app", "/dest", null); this.handler.handleReturnValue(PAYLOAD, this.noAnnotationsReturnType, inputMessage); verify(this.messageChannel, times(1)).send(this.messageCaptor.capture()); - - SimpMessageHeaderAccessor accessor = getCapturedAccessor(0); - assertEquals("sess1", accessor.getSessionId()); - assertEquals("/topic/dest", accessor.getDestination()); - assertEquals(MIME_TYPE, accessor.getContentType()); - assertNull("Subscription id should not be copied", accessor.getSubscriptionId()); - assertEquals(this.noAnnotationsReturnType, accessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER)); + assertResponse(this.noAnnotationsReturnType, sessionId, 0, "/topic/dest"); } @Test @@ -162,24 +193,12 @@ public class SendToMethodReturnValueHandlerTests { given(this.messageChannel.send(any(Message.class))).willReturn(true); String sessionId = "sess1"; - Message<?> inputMessage = createInputMessage(sessionId, "sub1", null, null, null); + Message<?> inputMessage = createMessage(sessionId, "sub1", null, null, null); this.handler.handleReturnValue(PAYLOAD, this.sendToReturnType, inputMessage); verify(this.messageChannel, times(2)).send(this.messageCaptor.capture()); - - SimpMessageHeaderAccessor accessor = getCapturedAccessor(0); - assertEquals(sessionId, accessor.getSessionId()); - assertEquals("/dest1", accessor.getDestination()); - assertEquals(MIME_TYPE, accessor.getContentType()); - assertNull("Subscription id should not be copied", accessor.getSubscriptionId()); - assertEquals(this.sendToReturnType, accessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER)); - - accessor = getCapturedAccessor(1); - assertEquals(sessionId, accessor.getSessionId()); - assertEquals("/dest2", accessor.getDestination()); - assertEquals(MIME_TYPE, accessor.getContentType()); - assertNull("Subscription id should not be copied", accessor.getSubscriptionId()); - assertEquals(this.sendToReturnType, accessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER)); + assertResponse(this.sendToReturnType, sessionId, 0, "/dest1"); + assertResponse(this.sendToReturnType, sessionId, 1, "/dest2"); } @Test @@ -187,24 +206,137 @@ public class SendToMethodReturnValueHandlerTests { given(this.messageChannel.send(any(Message.class))).willReturn(true); String sessionId = "sess1"; - Message<?> inputMessage = createInputMessage(sessionId, "sub1", "/app", "/dest", null); + Message<?> inputMessage = createMessage(sessionId, "sub1", "/app", "/dest", null); this.handler.handleReturnValue(PAYLOAD, this.sendToDefaultDestReturnType, inputMessage); verify(this.messageChannel, times(1)).send(this.messageCaptor.capture()); + assertResponse(this.sendToDefaultDestReturnType, sessionId, 0, "/topic/dest"); + } - SimpMessageHeaderAccessor accessor = getCapturedAccessor(0); + @Test + public void sendToClassDefaultNoAnnotation() throws Exception { + given(this.messageChannel.send(any(Message.class))).willReturn(true); + + String sessionId = "sess1"; + Message<?> inputMessage = createMessage(sessionId, "sub1", null, null, null); + this.handler.handleReturnValue(PAYLOAD, this.defaultNoAnnotation, inputMessage); + + verify(this.messageChannel, times(1)).send(this.messageCaptor.capture()); + assertResponse(this.defaultNoAnnotation, sessionId, 0, "/dest-default"); + } + + @Test + public void sendToClassDefaultEmptyAnnotation() throws Exception { + given(this.messageChannel.send(any(Message.class))).willReturn(true); + + String sessionId = "sess1"; + Message<?> inputMessage = createMessage(sessionId, "sub1", null, null, null); + this.handler.handleReturnValue(PAYLOAD, this.defaultEmptyAnnotation, inputMessage); + + verify(this.messageChannel, times(1)).send(this.messageCaptor.capture()); + assertResponse(this.defaultEmptyAnnotation, sessionId, 0, "/dest-default"); + } + + @Test + public void sendToClassDefaultOverride() throws Exception { + given(this.messageChannel.send(any(Message.class))).willReturn(true); + + String sessionId = "sess1"; + Message<?> inputMessage = createMessage(sessionId, "sub1", null, null, null); + this.handler.handleReturnValue(PAYLOAD, this.defaultOverrideAnnotation, inputMessage); + + verify(this.messageChannel, times(2)).send(this.messageCaptor.capture()); + assertResponse(this.defaultOverrideAnnotation, sessionId, 0, "/dest3"); + assertResponse(this.defaultOverrideAnnotation, sessionId, 1, "/dest4"); + } + + @Test + public void sendToUserClassDefaultNoAnnotation() throws Exception { + given(this.messageChannel.send(any(Message.class))).willReturn(true); + + String sessionId = "sess1"; + Message<?> inputMessage = createMessage(sessionId, "sub1", null, null, null); + this.handler.handleReturnValue(PAYLOAD, this.userDefaultNoAnnotation, inputMessage); + + verify(this.messageChannel, times(1)).send(this.messageCaptor.capture()); + assertResponse(this.userDefaultNoAnnotation, sessionId, 0, "/user/sess1/dest-default"); + } + + @Test + public void sendToUserClassDefaultEmptyAnnotation() throws Exception { + given(this.messageChannel.send(any(Message.class))).willReturn(true); + + String sessionId = "sess1"; + Message<?> inputMessage = createMessage(sessionId, "sub1", null, null, null); + this.handler.handleReturnValue(PAYLOAD, this.userDefaultEmptyAnnotation, inputMessage); + + verify(this.messageChannel, times(1)).send(this.messageCaptor.capture()); + assertResponse(this.userDefaultEmptyAnnotation, sessionId, 0, "/user/sess1/dest-default"); + } + + @Test + public void sendToUserClassDefaultOverride() throws Exception { + given(this.messageChannel.send(any(Message.class))).willReturn(true); + + String sessionId = "sess1"; + Message<?> inputMessage = createMessage(sessionId, "sub1", null, null, null); + this.handler.handleReturnValue(PAYLOAD, this.userDefaultOverrideAnnotation, inputMessage); + + verify(this.messageChannel, times(2)).send(this.messageCaptor.capture()); + assertResponse(this.userDefaultOverrideAnnotation, sessionId, 0, "/user/sess1/dest3"); + assertResponse(this.userDefaultOverrideAnnotation, sessionId, 1, "/user/sess1/dest4"); + } + + @Test // SPR-14238 + public void sendToUserWithSendToDefaultOverride() throws Exception { + given(this.messageChannel.send(any(Message.class))).willReturn(true); + + Class<?> clazz = SendToUserWithSendToOverrideTestBean.class; + Method method = clazz.getDeclaredMethod("handleAndSendToDefaultDestination"); + MethodParameter parameter = new SynthesizingMethodParameter(method, -1); + + String sessionId = "sess1"; + Message<?> inputMessage = createMessage(sessionId, "sub1", null, null, null); + this.handler.handleReturnValue(PAYLOAD, parameter, inputMessage); + + verify(this.messageChannel, times(1)).send(this.messageCaptor.capture()); + assertResponse(parameter, sessionId, 0, "/user/sess1/dest-default"); + } + + @Test // SPR-14238 + public void sendToUserWithSendToOverride() throws Exception { + given(this.messageChannel.send(any(Message.class))).willReturn(true); + + Class<?> clazz = SendToUserWithSendToOverrideTestBean.class; + Method method = clazz.getDeclaredMethod("handleAndSendToOverride"); + MethodParameter parameter = new SynthesizingMethodParameter(method, -1); + + String sessionId = "sess1"; + Message<?> inputMessage = createMessage(sessionId, "sub1", null, null, null); + this.handler.handleReturnValue(PAYLOAD, parameter, inputMessage); + + verify(this.messageChannel, times(2)).send(this.messageCaptor.capture()); + assertResponse(parameter, sessionId, 0, "/dest3"); + assertResponse(parameter, sessionId, 1, "/dest4"); + } + + + private void assertResponse(MethodParameter methodParameter, String sessionId, + int index, String destination) { + + SimpMessageHeaderAccessor accessor = getCapturedAccessor(index); assertEquals(sessionId, accessor.getSessionId()); - assertEquals("/topic/dest", accessor.getDestination()); + assertEquals(destination, accessor.getDestination()); assertEquals(MIME_TYPE, accessor.getContentType()); assertNull("Subscription id should not be copied", accessor.getSubscriptionId()); - assertEquals(this.sendToDefaultDestReturnType, accessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER)); + assertEquals(methodParameter, accessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER)); } @Test public void sendToDefaultDestinationWhenUsingDotPathSeparator() throws Exception { given(this.messageChannel.send(any(Message.class))).willReturn(true); - Message<?> inputMessage = createInputMessage("sess1", "sub1", "/app/", "dest.foo.bar", null); + Message<?> inputMessage = createMessage("sess1", "sub1", "/app/", "dest.foo.bar", null); this.handler.handleReturnValue(PAYLOAD, this.sendToDefaultDestReturnType, inputMessage); verify(this.messageChannel, times(1)).send(this.messageCaptor.capture()); @@ -215,23 +347,24 @@ public class SendToMethodReturnValueHandlerTests { @Test public void testHeadersToSend() throws Exception { - Message<?> inputMessage = createInputMessage("sess1", "sub1", "/app", "/dest", null); + Message<?> message = createMessage("sess1", "sub1", "/app", "/dest", null); SimpMessageSendingOperations messagingTemplate = Mockito.mock(SimpMessageSendingOperations.class); SendToMethodReturnValueHandler handler = new SendToMethodReturnValueHandler(messagingTemplate, false); - handler.handleReturnValue(PAYLOAD, this.noAnnotationsReturnType, inputMessage); + handler.handleReturnValue(PAYLOAD, this.noAnnotationsReturnType, message); ArgumentCaptor<MessageHeaders> captor = ArgumentCaptor.forClass(MessageHeaders.class); verify(messagingTemplate).convertAndSend(eq("/topic/dest"), eq(PAYLOAD), captor.capture()); - MessageHeaders messageHeaders = captor.getValue(); - SimpMessageHeaderAccessor accessor = getAccessor(messageHeaders, SimpMessageHeaderAccessor.class); + MessageHeaders headers = captor.getValue(); + SimpMessageHeaderAccessor accessor = getAccessor(headers, SimpMessageHeaderAccessor.class); assertNotNull(accessor); assertTrue(accessor.isMutable()); assertEquals("sess1", accessor.getSessionId()); assertNull("Subscription id should not be copied", accessor.getSubscriptionId()); - assertEquals(this.noAnnotationsReturnType, accessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER)); + assertEquals(this.noAnnotationsReturnType, + accessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER)); } @Test @@ -240,7 +373,7 @@ public class SendToMethodReturnValueHandlerTests { String sessionId = "sess1"; TestUser user = new TestUser(); - Message<?> inputMessage = createInputMessage(sessionId, "sub1", null, null, user); + Message<?> inputMessage = createMessage(sessionId, "sub1", null, null, user); this.handler.handleReturnValue(PAYLOAD, this.sendToUserReturnType, inputMessage); verify(this.messageChannel, times(2)).send(this.messageCaptor.capture()); @@ -284,7 +417,7 @@ public class SendToMethodReturnValueHandlerTests { String sessionId = "sess1"; TestUser user = new TestUser(); - Message<?> inputMessage = createInputMessage(sessionId, "sub1", null, null, user); + Message<?> inputMessage = createMessage(sessionId, "sub1", null, null, user); this.handler.handleReturnValue(PAYLOAD, this.sendToUserSingleSessionReturnType, inputMessage); verify(this.messageChannel, times(2)).send(this.messageCaptor.capture()); @@ -294,14 +427,16 @@ public class SendToMethodReturnValueHandlerTests { assertEquals(MIME_TYPE, accessor.getContentType()); assertEquals("/user/" + user.getName() + "/dest1", accessor.getDestination()); assertNull("Subscription id should not be copied", accessor.getSubscriptionId()); - assertEquals(this.sendToUserSingleSessionReturnType, accessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER)); + assertEquals(this.sendToUserSingleSessionReturnType, + accessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER)); accessor = getCapturedAccessor(1); assertEquals(sessionId, accessor.getSessionId()); assertEquals("/user/" + user.getName() + "/dest2", accessor.getDestination()); assertEquals(MIME_TYPE, accessor.getContentType()); assertNull("Subscription id should not be copied", accessor.getSubscriptionId()); - assertEquals(this.sendToUserSingleSessionReturnType, accessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER)); + assertEquals(this.sendToUserSingleSessionReturnType, + accessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER)); } @Test @@ -310,7 +445,7 @@ public class SendToMethodReturnValueHandlerTests { String sessionId = "sess1"; TestUser user = new UniqueUser(); - Message<?> inputMessage = createInputMessage(sessionId, "sub1", null, null, user); + Message<?> inputMessage = createMessage(sessionId, "sub1", null, null, user); this.handler.handleReturnValue(PAYLOAD, this.sendToUserReturnType, inputMessage); verify(this.messageChannel, times(2)).send(this.messageCaptor.capture()); @@ -328,7 +463,7 @@ public class SendToMethodReturnValueHandlerTests { String sessionId = "sess1"; TestUser user = new TestUser(); - Message<?> inputMessage = createInputMessage(sessionId, "sub1", "/app", "/dest", user); + Message<?> inputMessage = createMessage(sessionId, "sub1", "/app", "/dest", user); this.handler.handleReturnValue(PAYLOAD, this.sendToUserDefaultDestReturnType, inputMessage); verify(this.messageChannel, times(1)).send(this.messageCaptor.capture()); @@ -344,7 +479,7 @@ public class SendToMethodReturnValueHandlerTests { given(this.messageChannel.send(any(Message.class))).willReturn(true); TestUser user = new TestUser(); - Message<?> inputMessage = createInputMessage("sess1", "sub1", "/app/", "dest.foo.bar", user); + Message<?> inputMessage = createMessage("sess1", "sub1", "/app/", "dest.foo.bar", user); this.handler.handleReturnValue(PAYLOAD, this.sendToUserDefaultDestReturnType, inputMessage); verify(this.messageChannel, times(1)).send(this.messageCaptor.capture()); @@ -359,8 +494,8 @@ public class SendToMethodReturnValueHandlerTests { String sessionId = "sess1"; TestUser user = new TestUser(); - Message<?> inputMessage = createInputMessage(sessionId, "sub1", "/app", "/dest", user); - this.handler.handleReturnValue(PAYLOAD, this.sendToUserSingleSessionDefaultDestReturnType, inputMessage); + Message<?> message = createMessage(sessionId, "sub1", "/app", "/dest", user); + this.handler.handleReturnValue(PAYLOAD, this.sendToUserSingleSessionDefaultDestReturnType, message); verify(this.messageChannel, times(1)).send(this.messageCaptor.capture()); @@ -369,7 +504,8 @@ public class SendToMethodReturnValueHandlerTests { assertEquals("/user/" + user.getName() + "/queue/dest", accessor.getDestination()); assertEquals(MIME_TYPE, accessor.getContentType()); assertNull("Subscription id should not be copied", accessor.getSubscriptionId()); - assertEquals(this.sendToUserSingleSessionDefaultDestReturnType, accessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER)); + assertEquals(this.sendToUserSingleSessionDefaultDestReturnType, + accessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER)); } @Test @@ -377,7 +513,7 @@ public class SendToMethodReturnValueHandlerTests { given(this.messageChannel.send(any(Message.class))).willReturn(true); String sessionId = "sess1"; - Message<?> inputMessage = createInputMessage(sessionId, "sub1", null, null, null); + Message<?> inputMessage = createMessage(sessionId, "sub1", null, null, null); this.handler.handleReturnValue(PAYLOAD, this.sendToUserReturnType, inputMessage); verify(this.messageChannel, times(2)).send(this.messageCaptor.capture()); @@ -396,29 +532,28 @@ public class SendToMethodReturnValueHandlerTests { given(this.messageChannel.send(any(Message.class))).willReturn(true); String sessionId = "sess1"; - Message<?> inputMessage = createInputMessage(sessionId, "sub1", "/app", "/dest", null); + Message<?> inputMessage = createMessage(sessionId, "sub1", "/app", "/dest", null); this.jsonHandler.handleReturnValue(handleAndSendToJsonView(), this.jsonViewReturnType, inputMessage); verify(this.messageChannel).send(this.messageCaptor.capture()); Message<?> message = this.messageCaptor.getValue(); assertNotNull(message); - assertEquals("{\"withView1\":\"with\"}", new String((byte[]) message.getPayload(), StandardCharsets.UTF_8)); + String bytes = new String((byte[]) message.getPayload(), StandardCharsets.UTF_8); + assertEquals("{\"withView1\":\"with\"}", bytes); } - private Message<?> createInputMessage(String sessId, String subsId, String destinationPrefix, - String destination, Principal principal) { - + private Message<?> createMessage(String sessId, String subsId, String destPrefix, String dest, Principal user) { SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(); headerAccessor.setSessionId(sessId); headerAccessor.setSubscriptionId(subsId); - if (destination != null && destinationPrefix != null) { - headerAccessor.setDestination(destinationPrefix + destination); - headerAccessor.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, destination); + if (dest != null && destPrefix != null) { + headerAccessor.setDestination(destPrefix + dest); + headerAccessor.setHeader(LOOKUP_DESTINATION_HEADER, dest); } - if (principal != null) { - headerAccessor.setUser(principal); + if (user != null) { + headerAccessor.setUser(user); } return MessageBuilder.createMessage(new byte[0], headerAccessor.getMessageHeaders()); } @@ -448,48 +583,65 @@ public class SendToMethodReturnValueHandlerTests { } } - public String handleNoAnnotations() { + @SendTo + @Retention(RetentionPolicy.RUNTIME) + @interface MySendTo { + + @AliasFor(annotation = SendTo.class, attribute = "value") + String[] dest(); + } + + @SendToUser + @Retention(RetentionPolicy.RUNTIME) + @interface MySendToUser { + + @AliasFor(annotation = SendToUser.class, attribute = "destinations") + String[] dest(); + } + + + @SuppressWarnings("unused") + String handleNoAnnotations() { return PAYLOAD; } - @SendTo - public String handleAndSendToDefaultDestination() { + @SendTo @SuppressWarnings("unused") + String handleAndSendToDefaultDestination() { return PAYLOAD; } - @SendTo({"/dest1", "/dest2"}) - public String handleAndSendTo() { + @SendTo({"/dest1", "/dest2"}) @SuppressWarnings("unused") + String handleAndSendTo() { return PAYLOAD; } - @SendTo("/topic/chat.message.filtered.{roomName}") - public String handleAndSendToWithPlaceholders() { + @SendTo("/topic/chat.message.filtered.{roomName}") @SuppressWarnings("unused") + String handleAndSendToWithPlaceholders() { return PAYLOAD; } - @SendToUser - public String handleAndSendToUserDefaultDestination() { + @SendToUser @SuppressWarnings("unused") + String handleAndSendToUserDefaultDestination() { return PAYLOAD; } - @SendToUser(broadcast = false) - public String handleAndSendToUserDefaultDestinationSingleSession() { + @SendToUser(broadcast = false) @SuppressWarnings("unused") + String handleAndSendToUserDefaultDestinationSingleSession() { return PAYLOAD; } - @SendToUser({"/dest1", "/dest2"}) - public String handleAndSendToUser() { + @SendToUser({"/dest1", "/dest2"}) @SuppressWarnings("unused") + String handleAndSendToUser() { return PAYLOAD; } - @SendToUser(destinations = { "/dest1", "/dest2" }, broadcast = false) - public String handleAndSendToUserSingleSession() { + @SendToUser(destinations = { "/dest1", "/dest2" }, broadcast = false) @SuppressWarnings("unused") + String handleAndSendToUserSingleSession() { return PAYLOAD; } - @SendTo("/dest") - @JsonView(MyJacksonView1.class) - public JacksonViewBean handleAndSendToJsonView() { + @JsonView(MyJacksonView1.class) @SuppressWarnings("unused") + JacksonViewBean handleAndSendToJsonView() { JacksonViewBean payload = new JacksonViewBean(); payload.setWithView1("with"); payload.setWithView2("with"); @@ -498,6 +650,57 @@ public class SendToMethodReturnValueHandlerTests { } + @MySendTo(dest = "/dest-default") @SuppressWarnings("unused") + private static class SendToTestBean { + + String handleNoAnnotation() { + return PAYLOAD; + } + + @SendTo + String handleAndSendToDefaultDestination() { + return PAYLOAD; + } + + @MySendTo(dest = {"/dest3", "/dest4"}) + String handleAndSendToOverride() { + return PAYLOAD; + } + } + + @MySendToUser(dest = "/dest-default") @SuppressWarnings("unused") + private static class SendToUserTestBean { + + String handleNoAnnotation() { + return PAYLOAD; + } + + @SendToUser + String handleAndSendToDefaultDestination() { + return PAYLOAD; + } + + @MySendToUser(dest = {"/dest3", "/dest4"}) + String handleAndSendToOverride() { + return PAYLOAD; + } + } + + @MySendToUser(dest = "/dest-default") @SuppressWarnings("unused") + private static class SendToUserWithSendToOverrideTestBean { + + @SendTo + String handleAndSendToDefaultDestination() { + return PAYLOAD; + } + + @MySendTo(dest = {"/dest3", "/dest4"}) + String handleAndSendToOverride() { + return PAYLOAD; + } + } + + private interface MyJacksonView1 {} private interface MyJacksonView2 {} @@ -516,23 +719,23 @@ public class SendToMethodReturnValueHandlerTests { return withView1; } - public void setWithView1(String withView1) { + void setWithView1(String withView1) { this.withView1 = withView1; } - public String getWithView2() { + String getWithView2() { return withView2; } - public void setWithView2(String withView2) { + void setWithView2(String withView2) { this.withView2 = withView2; } - public String getWithoutView() { + String getWithoutView() { return withoutView; } - public void setWithoutView(String withoutView) { + void setWithoutView(String withoutView) { this.withoutView = withoutView; } } |