Polish @SendTo test

This commit is contained in:
Rossen Stoyanchev 2018-06-09 04:06:36 -04:00
parent a31204938a
commit 7a70f7c5d8
1 changed files with 39 additions and 72 deletions

View File

@ -81,21 +81,34 @@ public class SendToMethodReturnValueHandlerTests {
@Captor private ArgumentCaptor<Message<?>> messageCaptor; @Captor private ArgumentCaptor<Message<?>> messageCaptor;
private MethodParameter noAnnotationsReturnType; private MethodParameter noAnnotationsReturnType = param("handleNoAnnotations");
private MethodParameter sendToReturnType; private MethodParameter sendToReturnType = param("handleAndSendTo");
private MethodParameter sendToDefaultDestReturnType; private MethodParameter sendToDefaultDestReturnType = param("handleAndSendToDefaultDest");
private MethodParameter sendToWithPlaceholdersReturnType; private MethodParameter sendToWithPlaceholdersReturnType = param("handleAndSendToWithPlaceholders");
private MethodParameter sendToUserReturnType; private MethodParameter sendToUserReturnType = param("handleAndSendToUser");
private MethodParameter sendToUserSingleSessionReturnType; private MethodParameter sendToUserInSessionReturnType = param("handleAndSendToUserInSession");
private MethodParameter sendToUserDefaultDestReturnType; private MethodParameter sendToUserDefaultDestReturnType = param("handleAndSendToUserDefaultDest");
private MethodParameter sendToUserSingleSessionDefaultDestReturnType; private MethodParameter sendToUserInSessionDefaultDestReturnType = param("handleAndSendToUserDefaultDestInSession");
private MethodParameter jsonViewReturnType; private MethodParameter jsonViewReturnType = param("handleAndSendToJsonView");
private MethodParameter defaultNoAnnotation; private MethodParameter defaultNoAnnotation = param(SendToTestBean.class, "handleNoAnnotation");
private MethodParameter defaultEmptyAnnotation; private MethodParameter defaultEmptyAnnotation = param(SendToTestBean.class, "handleAndSendToDefaultDest");
private MethodParameter defaultOverrideAnnotation; private MethodParameter defaultOverrideAnnotation = param(SendToTestBean.class, "handleAndSendToOverride");
private MethodParameter userDefaultNoAnnotation; private MethodParameter userDefaultNoAnnotation = param(SendToUserTestBean.class, "handleNoAnnotation");
private MethodParameter userDefaultEmptyAnnotation; private MethodParameter userDefaultEmptyAnnotation = param(SendToUserTestBean.class, "handleAndSendToDefaultDest");
private MethodParameter userDefaultOverrideAnnotation; private MethodParameter userDefaultOverrideAnnotation = param(SendToUserTestBean.class, "handleAndSendToOverride");
private MethodParameter param(String methodName) {
return param(getClass(), methodName);
}
private static MethodParameter param(Class<?> clazz, String methodName) {
try {
return new SynthesizingMethodParameter(clazz.getDeclaredMethod(methodName), -1);
}
catch (NoSuchMethodException ex) {
throw new IllegalArgumentException("No such method", ex);
}
}
@Before @Before
@ -110,54 +123,8 @@ public class SendToMethodReturnValueHandlerTests {
SimpMessagingTemplate jsonMessagingTemplate = new SimpMessagingTemplate(this.messageChannel); SimpMessagingTemplate jsonMessagingTemplate = new SimpMessagingTemplate(this.messageChannel);
jsonMessagingTemplate.setMessageConverter(new MappingJackson2MessageConverter()); jsonMessagingTemplate.setMessageConverter(new MappingJackson2MessageConverter());
this.jsonHandler = new SendToMethodReturnValueHandler(jsonMessagingTemplate, true); this.jsonHandler = new SendToMethodReturnValueHandler(jsonMessagingTemplate, true);
Method method = getClass().getDeclaredMethod("handleNoAnnotations");
this.noAnnotationsReturnType = new SynthesizingMethodParameter(method, -1);
method = getClass().getDeclaredMethod("handleAndSendToDefaultDestination");
this.sendToDefaultDestReturnType = new SynthesizingMethodParameter(method, -1);
method = getClass().getDeclaredMethod("handleAndSendTo");
this.sendToReturnType = new SynthesizingMethodParameter(method, -1);
method = getClass().getDeclaredMethod("handleAndSendToWithPlaceholders");
this.sendToWithPlaceholdersReturnType = new SynthesizingMethodParameter(method, -1);
method = getClass().getDeclaredMethod("handleAndSendToUser");
this.sendToUserReturnType = new SynthesizingMethodParameter(method, -1);
method = getClass().getDeclaredMethod("handleAndSendToUserSingleSession");
this.sendToUserSingleSessionReturnType = new SynthesizingMethodParameter(method, -1);
method = getClass().getDeclaredMethod("handleAndSendToUserDefaultDestination");
this.sendToUserDefaultDestReturnType = new SynthesizingMethodParameter(method, -1);
method = getClass().getDeclaredMethod("handleAndSendToUserDefaultDestinationSingleSession");
this.sendToUserSingleSessionDefaultDestReturnType = new SynthesizingMethodParameter(method, -1);
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);
} }
@Test @Test
public void supportsReturnType() throws Exception { public void supportsReturnType() throws Exception {
assertTrue(this.handler.supportsReturnType(this.sendToReturnType)); assertTrue(this.handler.supportsReturnType(this.sendToReturnType));
@ -417,7 +384,7 @@ public class SendToMethodReturnValueHandlerTests {
String sessionId = "sess1"; String sessionId = "sess1";
TestUser user = new TestUser(); TestUser user = new TestUser();
Message<?> inputMessage = createMessage(sessionId, "sub1", null, null, user); Message<?> inputMessage = createMessage(sessionId, "sub1", null, null, user);
this.handler.handleReturnValue(PAYLOAD, this.sendToUserSingleSessionReturnType, inputMessage); this.handler.handleReturnValue(PAYLOAD, this.sendToUserInSessionReturnType, inputMessage);
verify(this.messageChannel, times(2)).send(this.messageCaptor.capture()); verify(this.messageChannel, times(2)).send(this.messageCaptor.capture());
@ -426,7 +393,7 @@ public class SendToMethodReturnValueHandlerTests {
assertEquals(MIME_TYPE, accessor.getContentType()); assertEquals(MIME_TYPE, accessor.getContentType());
assertEquals("/user/" + user.getName() + "/dest1", accessor.getDestination()); assertEquals("/user/" + user.getName() + "/dest1", accessor.getDestination());
assertNull("Subscription id should not be copied", accessor.getSubscriptionId()); assertNull("Subscription id should not be copied", accessor.getSubscriptionId());
assertEquals(this.sendToUserSingleSessionReturnType, assertEquals(this.sendToUserInSessionReturnType,
accessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER)); accessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER));
accessor = getCapturedAccessor(1); accessor = getCapturedAccessor(1);
@ -434,7 +401,7 @@ public class SendToMethodReturnValueHandlerTests {
assertEquals("/user/" + user.getName() + "/dest2", accessor.getDestination()); assertEquals("/user/" + user.getName() + "/dest2", accessor.getDestination());
assertEquals(MIME_TYPE, accessor.getContentType()); assertEquals(MIME_TYPE, accessor.getContentType());
assertNull("Subscription id should not be copied", accessor.getSubscriptionId()); assertNull("Subscription id should not be copied", accessor.getSubscriptionId());
assertEquals(this.sendToUserSingleSessionReturnType, assertEquals(this.sendToUserInSessionReturnType,
accessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER)); accessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER));
} }
@ -494,7 +461,7 @@ public class SendToMethodReturnValueHandlerTests {
String sessionId = "sess1"; String sessionId = "sess1";
TestUser user = new TestUser(); TestUser user = new TestUser();
Message<?> message = createMessage(sessionId, "sub1", "/app", "/dest", user); Message<?> message = createMessage(sessionId, "sub1", "/app", "/dest", user);
this.handler.handleReturnValue(PAYLOAD, this.sendToUserSingleSessionDefaultDestReturnType, message); this.handler.handleReturnValue(PAYLOAD, this.sendToUserInSessionDefaultDestReturnType, message);
verify(this.messageChannel, times(1)).send(this.messageCaptor.capture()); verify(this.messageChannel, times(1)).send(this.messageCaptor.capture());
@ -503,7 +470,7 @@ public class SendToMethodReturnValueHandlerTests {
assertEquals("/user/" + user.getName() + "/queue/dest", accessor.getDestination()); assertEquals("/user/" + user.getName() + "/queue/dest", accessor.getDestination());
assertEquals(MIME_TYPE, accessor.getContentType()); assertEquals(MIME_TYPE, accessor.getContentType());
assertNull("Subscription id should not be copied", accessor.getSubscriptionId()); assertNull("Subscription id should not be copied", accessor.getSubscriptionId());
assertEquals(this.sendToUserSingleSessionDefaultDestReturnType, assertEquals(this.sendToUserInSessionDefaultDestReturnType,
accessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER)); accessor.getHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER));
} }
@ -570,7 +537,7 @@ public class SendToMethodReturnValueHandlerTests {
@SendTo @SendTo
@SuppressWarnings("unused") @SuppressWarnings("unused")
String handleAndSendToDefaultDestination() { String handleAndSendToDefaultDest() {
return PAYLOAD; return PAYLOAD;
} }
@ -588,13 +555,13 @@ public class SendToMethodReturnValueHandlerTests {
@SendToUser @SendToUser
@SuppressWarnings("unused") @SuppressWarnings("unused")
String handleAndSendToUserDefaultDestination() { String handleAndSendToUserDefaultDest() {
return PAYLOAD; return PAYLOAD;
} }
@SendToUser(broadcast = false) @SendToUser(broadcast = false)
@SuppressWarnings("unused") @SuppressWarnings("unused")
String handleAndSendToUserDefaultDestinationSingleSession() { String handleAndSendToUserDefaultDestInSession() {
return PAYLOAD; return PAYLOAD;
} }
@ -606,7 +573,7 @@ public class SendToMethodReturnValueHandlerTests {
@SendToUser(destinations = { "/dest1", "/dest2" }, broadcast = false) @SendToUser(destinations = { "/dest1", "/dest2" }, broadcast = false)
@SuppressWarnings("unused") @SuppressWarnings("unused")
String handleAndSendToUserSingleSession() { String handleAndSendToUserInSession() {
return PAYLOAD; return PAYLOAD;
} }
@ -668,7 +635,7 @@ public class SendToMethodReturnValueHandlerTests {
} }
@SendTo @SendTo
String handleAndSendToDefaultDestination() { String handleAndSendToDefaultDest() {
return PAYLOAD; return PAYLOAD;
} }
@ -687,7 +654,7 @@ public class SendToMethodReturnValueHandlerTests {
} }
@SendToUser @SendToUser
String handleAndSendToDefaultDestination() { String handleAndSendToDefaultDest() {
return PAYLOAD; return PAYLOAD;
} }