1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2016 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -88,6 +88,9 @@ public class SendToMethodReturnValueHandlerTests {
88
88
private MethodParameter sendToUserDefaultDestReturnType ;
89
89
private MethodParameter sendToUserSingleSessionDefaultDestReturnType ;
90
90
private MethodParameter jsonViewReturnType ;
91
+ private MethodParameter defaultNoAnnotation ;
92
+ private MethodParameter defaultEmptyAnnotation ;
93
+ private MethodParameter defaultOverrideAnnotation ;
91
94
92
95
93
96
@ Before
@@ -129,6 +132,15 @@ public void setup() throws Exception {
129
132
130
133
method = this .getClass ().getDeclaredMethod ("handleAndSendToJsonView" );
131
134
this .jsonViewReturnType = new SynthesizingMethodParameter (method , -1 );
135
+
136
+ method = TestBean .class .getDeclaredMethod ("handleNoAnnotation" );
137
+ this .defaultNoAnnotation = new SynthesizingMethodParameter (method , -1 );
138
+
139
+ method = TestBean .class .getDeclaredMethod ("handleAndSendToDefaultDestination" );
140
+ this .defaultEmptyAnnotation = new SynthesizingMethodParameter (method , -1 );
141
+
142
+ method = TestBean .class .getDeclaredMethod ("handleAndSendToOverride" );
143
+ this .defaultOverrideAnnotation = new SynthesizingMethodParameter (method , -1 );
132
144
}
133
145
134
146
@@ -138,23 +150,22 @@ public void supportsReturnType() throws Exception {
138
150
assertTrue (this .handler .supportsReturnType (this .sendToUserReturnType ));
139
151
assertFalse (this .handler .supportsReturnType (this .noAnnotationsReturnType ));
140
152
assertTrue (this .handlerAnnotationNotRequired .supportsReturnType (this .noAnnotationsReturnType ));
153
+
154
+ assertTrue (this .handler .supportsReturnType (this .defaultNoAnnotation ));
155
+ assertTrue (this .handler .supportsReturnType (this .defaultEmptyAnnotation ));
156
+ assertTrue (this .handler .supportsReturnType (this .defaultOverrideAnnotation ));
141
157
}
142
158
143
159
@ Test
144
160
public void sendToNoAnnotations () throws Exception {
145
161
given (this .messageChannel .send (any (Message .class ))).willReturn (true );
146
162
147
- Message <?> inputMessage = createInputMessage ("sess1" , "sub1" , "/app" , "/dest" , null );
163
+ String sessionId = "sess1" ;
164
+ Message <?> inputMessage = createInputMessage (sessionId , "sub1" , "/app" , "/dest" , null );
148
165
this .handler .handleReturnValue (PAYLOAD , this .noAnnotationsReturnType , inputMessage );
149
166
150
167
verify (this .messageChannel , times (1 )).send (this .messageCaptor .capture ());
151
-
152
- SimpMessageHeaderAccessor accessor = getCapturedAccessor (0 );
153
- assertEquals ("sess1" , accessor .getSessionId ());
154
- assertEquals ("/topic/dest" , accessor .getDestination ());
155
- assertEquals (MIME_TYPE , accessor .getContentType ());
156
- assertNull ("Subscription id should not be copied" , accessor .getSubscriptionId ());
157
- assertEquals (this .noAnnotationsReturnType , accessor .getHeader (SimpMessagingTemplate .CONVERSION_HINT_HEADER ));
168
+ assertResponse (this .noAnnotationsReturnType , sessionId , 0 , "/topic/dest" );
158
169
}
159
170
160
171
@ Test
@@ -166,20 +177,8 @@ public void sendTo() throws Exception {
166
177
this .handler .handleReturnValue (PAYLOAD , this .sendToReturnType , inputMessage );
167
178
168
179
verify (this .messageChannel , times (2 )).send (this .messageCaptor .capture ());
169
-
170
- SimpMessageHeaderAccessor accessor = getCapturedAccessor (0 );
171
- assertEquals (sessionId , accessor .getSessionId ());
172
- assertEquals ("/dest1" , accessor .getDestination ());
173
- assertEquals (MIME_TYPE , accessor .getContentType ());
174
- assertNull ("Subscription id should not be copied" , accessor .getSubscriptionId ());
175
- assertEquals (this .sendToReturnType , accessor .getHeader (SimpMessagingTemplate .CONVERSION_HINT_HEADER ));
176
-
177
- accessor = getCapturedAccessor (1 );
178
- assertEquals (sessionId , accessor .getSessionId ());
179
- assertEquals ("/dest2" , accessor .getDestination ());
180
- assertEquals (MIME_TYPE , accessor .getContentType ());
181
- assertNull ("Subscription id should not be copied" , accessor .getSubscriptionId ());
182
- assertEquals (this .sendToReturnType , accessor .getHeader (SimpMessagingTemplate .CONVERSION_HINT_HEADER ));
180
+ assertResponse (this .sendToReturnType , sessionId , 0 , "/dest1" );
181
+ assertResponse (this .sendToReturnType , sessionId , 1 , "/dest2" );
183
182
}
184
183
185
184
@ Test
@@ -191,13 +190,54 @@ public void sendToDefaultDestination() throws Exception {
191
190
this .handler .handleReturnValue (PAYLOAD , this .sendToDefaultDestReturnType , inputMessage );
192
191
193
192
verify (this .messageChannel , times (1 )).send (this .messageCaptor .capture ());
193
+ assertResponse (this .sendToDefaultDestReturnType , sessionId , 0 , "/topic/dest" );
194
+ }
194
195
195
- SimpMessageHeaderAccessor accessor = getCapturedAccessor (0 );
196
+ @ Test
197
+ public void sendToClassDefaultNoAnnotation () throws Exception {
198
+ given (this .messageChannel .send (any (Message .class ))).willReturn (true );
199
+
200
+ String sessionId = "sess1" ;
201
+ Message <?> inputMessage = createInputMessage (sessionId , "sub1" , null , null , null );
202
+ this .handler .handleReturnValue (PAYLOAD , this .defaultNoAnnotation , inputMessage );
203
+
204
+ verify (this .messageChannel , times (1 )).send (this .messageCaptor .capture ());
205
+ assertResponse (this .defaultNoAnnotation , sessionId , 0 , "/dest-default" );
206
+ }
207
+
208
+ @ Test
209
+ public void sendToClassDefaultEmptyAnnotation () throws Exception {
210
+ given (this .messageChannel .send (any (Message .class ))).willReturn (true );
211
+
212
+ String sessionId = "sess1" ;
213
+ Message <?> inputMessage = createInputMessage (sessionId , "sub1" , null , null , null );
214
+ this .handler .handleReturnValue (PAYLOAD , this .defaultEmptyAnnotation , inputMessage );
215
+
216
+ verify (this .messageChannel , times (1 )).send (this .messageCaptor .capture ());
217
+ assertResponse (this .defaultEmptyAnnotation , sessionId , 0 , "/dest-default" );
218
+ }
219
+
220
+ @ Test
221
+ public void sendToClassDefaultOverride () throws Exception {
222
+ given (this .messageChannel .send (any (Message .class ))).willReturn (true );
223
+
224
+ String sessionId = "sess1" ;
225
+ Message <?> inputMessage = createInputMessage (sessionId , "sub1" , null , null , null );
226
+ this .handler .handleReturnValue (PAYLOAD , this .defaultOverrideAnnotation , inputMessage );
227
+
228
+ verify (this .messageChannel , times (2 )).send (this .messageCaptor .capture ());
229
+ assertResponse (this .defaultOverrideAnnotation , sessionId , 0 , "/dest3" );
230
+ assertResponse (this .defaultOverrideAnnotation , sessionId , 1 , "/dest4" );
231
+ }
232
+
233
+ private void assertResponse (MethodParameter methodParameter , String sessionId ,
234
+ int index , String destination ) {
235
+ SimpMessageHeaderAccessor accessor = getCapturedAccessor (index );
196
236
assertEquals (sessionId , accessor .getSessionId ());
197
- assertEquals ("/topic/dest" , accessor .getDestination ());
237
+ assertEquals (destination , accessor .getDestination ());
198
238
assertEquals (MIME_TYPE , accessor .getContentType ());
199
239
assertNull ("Subscription id should not be copied" , accessor .getSubscriptionId ());
200
- assertEquals (this . sendToDefaultDestReturnType , accessor .getHeader (SimpMessagingTemplate .CONVERSION_HINT_HEADER ));
240
+ assertEquals (methodParameter , accessor .getHeader (SimpMessagingTemplate .CONVERSION_HINT_HEADER ));
201
241
}
202
242
203
243
@ Test
@@ -497,6 +537,25 @@ public JacksonViewBean handleAndSendToJsonView() {
497
537
return payload ;
498
538
}
499
539
540
+ @ SendTo ("/dest-default" )
541
+ private static class TestBean {
542
+
543
+ public String handleNoAnnotation () {
544
+ return PAYLOAD ;
545
+ }
546
+
547
+ @ SendTo
548
+ public String handleAndSendToDefaultDestination () {
549
+ return PAYLOAD ;
550
+ }
551
+
552
+ @ SendTo ({"/dest3" , "/dest4" })
553
+ public String handleAndSendToOverride () {
554
+ return PAYLOAD ;
555
+ }
556
+
557
+ }
558
+
500
559
501
560
private interface MyJacksonView1 {}
502
561
private interface MyJacksonView2 {}
0 commit comments