1
1
/*
2
- * Copyright 2002-2017 the original author or authors.
2
+ * Copyright 2002-2018 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.
84
84
*/
85
85
public class MessageBrokerConfigurationTests {
86
86
87
- private ApplicationContext defaultContext = new AnnotationConfigApplicationContext (DefaultConfig .class );
88
-
89
- private ApplicationContext simpleBrokerContext = new AnnotationConfigApplicationContext (SimpleBrokerConfig .class );
90
-
91
- private ApplicationContext brokerRelayContext = new AnnotationConfigApplicationContext (BrokerRelayConfig .class );
92
-
93
- private ApplicationContext customContext = new AnnotationConfigApplicationContext (CustomConfig .class );
94
-
95
-
96
87
@ Test
97
88
public void clientInboundChannel () {
98
- TestChannel channel = this .simpleBrokerContext .getBean ("clientInboundChannel" , TestChannel .class );
89
+ ApplicationContext context = loadConfig (SimpleBrokerConfig .class );
90
+
91
+ TestChannel channel = context .getBean ("clientInboundChannel" , TestChannel .class );
99
92
Set <MessageHandler > handlers = channel .getSubscribers ();
100
93
101
94
assertEquals (3 , handlers .size ());
102
- assertTrue (handlers .contains (simpleBrokerContext .getBean (SimpAnnotationMethodMessageHandler .class )));
103
- assertTrue (handlers .contains (simpleBrokerContext .getBean (UserDestinationMessageHandler .class )));
104
- assertTrue (handlers .contains (simpleBrokerContext .getBean (SimpleBrokerMessageHandler .class )));
95
+ assertTrue (handlers .contains (context .getBean (SimpAnnotationMethodMessageHandler .class )));
96
+ assertTrue (handlers .contains (context .getBean (UserDestinationMessageHandler .class )));
97
+ assertTrue (handlers .contains (context .getBean (SimpleBrokerMessageHandler .class )));
105
98
}
106
99
107
100
@ Test
108
101
public void clientInboundChannelWithBrokerRelay () {
109
- TestChannel channel = this .brokerRelayContext .getBean ("clientInboundChannel" , TestChannel .class );
102
+ ApplicationContext context = loadConfig (BrokerRelayConfig .class );
103
+
104
+ TestChannel channel = context .getBean ("clientInboundChannel" , TestChannel .class );
110
105
Set <MessageHandler > handlers = channel .getSubscribers ();
111
106
112
107
assertEquals (3 , handlers .size ());
113
- assertTrue (handlers .contains (brokerRelayContext .getBean (SimpAnnotationMethodMessageHandler .class )));
114
- assertTrue (handlers .contains (brokerRelayContext .getBean (UserDestinationMessageHandler .class )));
115
- assertTrue (handlers .contains (brokerRelayContext .getBean (StompBrokerRelayMessageHandler .class )));
108
+ assertTrue (handlers .contains (context .getBean (SimpAnnotationMethodMessageHandler .class )));
109
+ assertTrue (handlers .contains (context .getBean (UserDestinationMessageHandler .class )));
110
+ assertTrue (handlers .contains (context .getBean (StompBrokerRelayMessageHandler .class )));
116
111
}
117
112
118
113
@ Test
119
114
public void clientInboundChannelCustomized () {
120
- AbstractSubscribableChannel channel = this .customContext .getBean (
115
+ ApplicationContext context = loadConfig (CustomConfig .class );
116
+
117
+ AbstractSubscribableChannel channel = context .getBean (
121
118
"clientInboundChannel" , AbstractSubscribableChannel .class );
122
119
123
120
assertEquals (3 , channel .getInterceptors ().size ());
124
121
125
- CustomThreadPoolTaskExecutor taskExecutor = this . customContext .getBean (
122
+ CustomThreadPoolTaskExecutor taskExecutor = context .getBean (
126
123
"clientInboundChannelExecutor" , CustomThreadPoolTaskExecutor .class );
127
124
128
125
assertEquals (11 , taskExecutor .getCorePoolSize ());
@@ -132,9 +129,11 @@ public void clientInboundChannelCustomized() {
132
129
133
130
@ Test
134
131
public void clientOutboundChannelUsedByAnnotatedMethod () {
135
- TestChannel channel = this .simpleBrokerContext .getBean ("clientOutboundChannel" , TestChannel .class );
132
+ ApplicationContext context = loadConfig (SimpleBrokerConfig .class );
133
+
134
+ TestChannel channel = context .getBean ("clientOutboundChannel" , TestChannel .class );
136
135
SimpAnnotationMethodMessageHandler messageHandler =
137
- this . simpleBrokerContext .getBean (SimpAnnotationMethodMessageHandler .class );
136
+ context .getBean (SimpAnnotationMethodMessageHandler .class );
138
137
139
138
StompHeaderAccessor headers = StompHeaderAccessor .create (StompCommand .SUBSCRIBE );
140
139
headers .setSessionId ("sess1" );
@@ -155,8 +154,10 @@ public void clientOutboundChannelUsedByAnnotatedMethod() {
155
154
156
155
@ Test
157
156
public void clientOutboundChannelUsedBySimpleBroker () {
158
- TestChannel channel = this .simpleBrokerContext .getBean ("clientOutboundChannel" , TestChannel .class );
159
- SimpleBrokerMessageHandler broker = this .simpleBrokerContext .getBean (SimpleBrokerMessageHandler .class );
157
+ ApplicationContext context = loadConfig (SimpleBrokerConfig .class );
158
+
159
+ TestChannel channel = context .getBean ("clientOutboundChannel" , TestChannel .class );
160
+ SimpleBrokerMessageHandler broker = context .getBean (SimpleBrokerMessageHandler .class );
160
161
161
162
StompHeaderAccessor headers = StompHeaderAccessor .create (StompCommand .SUBSCRIBE );
162
163
headers .setSessionId ("sess1" );
@@ -185,12 +186,14 @@ public void clientOutboundChannelUsedBySimpleBroker() {
185
186
186
187
@ Test
187
188
public void clientOutboundChannelCustomized () {
188
- AbstractSubscribableChannel channel = this .customContext .getBean (
189
+ ApplicationContext context = loadConfig (CustomConfig .class );
190
+
191
+ AbstractSubscribableChannel channel = context .getBean (
189
192
"clientOutboundChannel" , AbstractSubscribableChannel .class );
190
193
191
194
assertEquals (3 , channel .getInterceptors ().size ());
192
195
193
- ThreadPoolTaskExecutor taskExecutor = this . customContext .getBean (
196
+ ThreadPoolTaskExecutor taskExecutor = context .getBean (
194
197
"clientOutboundChannelExecutor" , ThreadPoolTaskExecutor .class );
195
198
196
199
assertEquals (21 , taskExecutor .getCorePoolSize ());
@@ -200,31 +203,37 @@ public void clientOutboundChannelCustomized() {
200
203
201
204
@ Test
202
205
public void brokerChannel () {
203
- TestChannel channel = this .simpleBrokerContext .getBean ("brokerChannel" , TestChannel .class );
206
+ ApplicationContext context = loadConfig (SimpleBrokerConfig .class );
207
+
208
+ TestChannel channel = context .getBean ("brokerChannel" , TestChannel .class );
204
209
Set <MessageHandler > handlers = channel .getSubscribers ();
205
210
206
211
assertEquals (2 , handlers .size ());
207
- assertTrue (handlers .contains (simpleBrokerContext .getBean (UserDestinationMessageHandler .class )));
208
- assertTrue (handlers .contains (simpleBrokerContext .getBean (SimpleBrokerMessageHandler .class )));
212
+ assertTrue (handlers .contains (context .getBean (UserDestinationMessageHandler .class )));
213
+ assertTrue (handlers .contains (context .getBean (SimpleBrokerMessageHandler .class )));
209
214
210
215
assertNull (channel .getExecutor ());
211
216
}
212
217
213
218
@ Test
214
219
public void brokerChannelWithBrokerRelay () {
215
- TestChannel channel = this .brokerRelayContext .getBean ("brokerChannel" , TestChannel .class );
220
+ ApplicationContext context = loadConfig (BrokerRelayConfig .class );
221
+
222
+ TestChannel channel = context .getBean ("brokerChannel" , TestChannel .class );
216
223
Set <MessageHandler > handlers = channel .getSubscribers ();
217
224
218
225
assertEquals (2 , handlers .size ());
219
- assertTrue (handlers .contains (brokerRelayContext .getBean (UserDestinationMessageHandler .class )));
220
- assertTrue (handlers .contains (brokerRelayContext .getBean (StompBrokerRelayMessageHandler .class )));
226
+ assertTrue (handlers .contains (context .getBean (UserDestinationMessageHandler .class )));
227
+ assertTrue (handlers .contains (context .getBean (StompBrokerRelayMessageHandler .class )));
221
228
}
222
229
223
230
@ Test
224
231
public void brokerChannelUsedByAnnotatedMethod () {
225
- TestChannel channel = this .simpleBrokerContext .getBean ("brokerChannel" , TestChannel .class );
232
+ ApplicationContext context = loadConfig (SimpleBrokerConfig .class );
233
+
234
+ TestChannel channel = context .getBean ("brokerChannel" , TestChannel .class );
226
235
SimpAnnotationMethodMessageHandler messageHandler =
227
- this . simpleBrokerContext .getBean (SimpAnnotationMethodMessageHandler .class );
236
+ context .getBean (SimpAnnotationMethodMessageHandler .class );
228
237
229
238
StompHeaderAccessor headers = StompHeaderAccessor .create (StompCommand .SEND );
230
239
headers .setSessionId ("sess1" );
@@ -244,12 +253,14 @@ public void brokerChannelUsedByAnnotatedMethod() {
244
253
245
254
@ Test
246
255
public void brokerChannelCustomized () {
247
- AbstractSubscribableChannel channel = this .customContext .getBean (
256
+ ApplicationContext context = loadConfig (CustomConfig .class );
257
+
258
+ AbstractSubscribableChannel channel = context .getBean (
248
259
"brokerChannel" , AbstractSubscribableChannel .class );
249
260
250
261
assertEquals (4 , channel .getInterceptors ().size ());
251
262
252
- ThreadPoolTaskExecutor taskExecutor = this . customContext .getBean (
263
+ ThreadPoolTaskExecutor taskExecutor = context .getBean (
253
264
"brokerChannelExecutor" , ThreadPoolTaskExecutor .class );
254
265
255
266
assertEquals (31 , taskExecutor .getCorePoolSize ());
@@ -274,17 +285,19 @@ public void configureMessageConvertersDefault() {
274
285
275
286
@ Test
276
287
public void threadPoolSizeDefault () {
288
+ ApplicationContext context = loadConfig (DefaultConfig .class );
289
+
277
290
String name = "clientInboundChannelExecutor" ;
278
- ThreadPoolTaskExecutor executor = this . defaultContext .getBean (name , ThreadPoolTaskExecutor .class );
291
+ ThreadPoolTaskExecutor executor = context .getBean (name , ThreadPoolTaskExecutor .class );
279
292
assertEquals (Runtime .getRuntime ().availableProcessors () * 2 , executor .getCorePoolSize ());
280
293
// No way to verify queue capacity
281
294
282
295
name = "clientOutboundChannelExecutor" ;
283
- executor = this . defaultContext .getBean (name , ThreadPoolTaskExecutor .class );
296
+ executor = context .getBean (name , ThreadPoolTaskExecutor .class );
284
297
assertEquals (Runtime .getRuntime ().availableProcessors () * 2 , executor .getCorePoolSize ());
285
298
286
299
name = "brokerChannelExecutor" ;
287
- executor = this . defaultContext .getBean (name , ThreadPoolTaskExecutor .class );
300
+ executor = context .getBean (name , ThreadPoolTaskExecutor .class );
288
301
assertEquals (0 , executor .getCorePoolSize ());
289
302
assertEquals (1 , executor .getMaxPoolSize ());
290
303
}
@@ -328,9 +341,11 @@ protected boolean configureMessageConverters(List<MessageConverter> messageConve
328
341
}
329
342
330
343
@ Test
331
- public void customArgumentAndReturnValueTypes () throws Exception {
344
+ public void customArgumentAndReturnValueTypes () {
345
+ ApplicationContext context = loadConfig (CustomConfig .class );
346
+
332
347
SimpAnnotationMethodMessageHandler handler =
333
- this . customContext .getBean (SimpAnnotationMethodMessageHandler .class );
348
+ context .getBean (SimpAnnotationMethodMessageHandler .class );
334
349
335
350
List <HandlerMethodArgumentResolver > customResolvers = handler .getCustomArgumentResolvers ();
336
351
assertEquals (1 , customResolvers .size ());
@@ -376,67 +391,81 @@ public void simpValidatorMvc() {
376
391
377
392
@ Test
378
393
public void simpValidatorInjected () {
394
+ ApplicationContext context = loadConfig (SimpleBrokerConfig .class );
395
+
379
396
SimpAnnotationMethodMessageHandler messageHandler =
380
- this . simpleBrokerContext .getBean (SimpAnnotationMethodMessageHandler .class );
397
+ context .getBean (SimpAnnotationMethodMessageHandler .class );
381
398
382
399
assertThat (messageHandler .getValidator (), Matchers .notNullValue (Validator .class ));
383
400
}
384
401
385
402
@ Test
386
403
public void customPathMatcher () {
387
- SimpleBrokerMessageHandler broker = this .customContext .getBean (SimpleBrokerMessageHandler .class );
404
+ ApplicationContext context = loadConfig (CustomConfig .class );
405
+
406
+ SimpleBrokerMessageHandler broker = context .getBean (SimpleBrokerMessageHandler .class );
388
407
DefaultSubscriptionRegistry registry = (DefaultSubscriptionRegistry ) broker .getSubscriptionRegistry ();
389
408
assertEquals ("a.a" , registry .getPathMatcher ().combine ("a" , "a" ));
390
409
391
410
PathMatcher pathMatcher =
392
- this . customContext .getBean (SimpAnnotationMethodMessageHandler .class ).getPathMatcher ();
411
+ context .getBean (SimpAnnotationMethodMessageHandler .class ).getPathMatcher ();
393
412
394
413
assertEquals ("a.a" , pathMatcher .combine ("a" , "a" ));
395
414
396
- DefaultUserDestinationResolver resolver = this . customContext .getBean (DefaultUserDestinationResolver .class );
415
+ DefaultUserDestinationResolver resolver = context .getBean (DefaultUserDestinationResolver .class );
397
416
assertNotNull (resolver );
398
417
assertEquals (false , new DirectFieldAccessor (resolver ).getPropertyValue ("keepLeadingSlash" ));
399
418
}
400
419
401
420
@ Test
402
421
public void customCacheLimit () {
403
- SimpleBrokerMessageHandler broker = this .customContext .getBean (SimpleBrokerMessageHandler .class );
422
+ ApplicationContext context = loadConfig (CustomConfig .class );
423
+
424
+ SimpleBrokerMessageHandler broker = context .getBean (SimpleBrokerMessageHandler .class );
404
425
DefaultSubscriptionRegistry registry = (DefaultSubscriptionRegistry ) broker .getSubscriptionRegistry ();
405
426
assertEquals (8192 , registry .getCacheLimit ());
406
427
}
407
428
408
429
@ Test
409
- public void userBroadcasts () throws Exception {
410
- SimpUserRegistry userRegistry = this .brokerRelayContext .getBean (SimpUserRegistry .class );
430
+ public void userBroadcasts () {
431
+ ApplicationContext context = loadConfig (BrokerRelayConfig .class );
432
+
433
+ SimpUserRegistry userRegistry = context .getBean (SimpUserRegistry .class );
411
434
assertEquals (MultiServerUserRegistry .class , userRegistry .getClass ());
412
435
413
- UserDestinationMessageHandler handler1 = this . brokerRelayContext .getBean (UserDestinationMessageHandler .class );
436
+ UserDestinationMessageHandler handler1 = context .getBean (UserDestinationMessageHandler .class );
414
437
assertEquals ("/topic/unresolved-user-destination" , handler1 .getBroadcastDestination ());
415
438
416
- UserRegistryMessageHandler handler2 = this . brokerRelayContext .getBean (UserRegistryMessageHandler .class );
439
+ UserRegistryMessageHandler handler2 = context .getBean (UserRegistryMessageHandler .class );
417
440
assertEquals ("/topic/simp-user-registry" , handler2 .getBroadcastDestination ());
418
441
419
- StompBrokerRelayMessageHandler relay = this . brokerRelayContext .getBean (StompBrokerRelayMessageHandler .class );
442
+ StompBrokerRelayMessageHandler relay = context .getBean (StompBrokerRelayMessageHandler .class );
420
443
assertNotNull (relay .getSystemSubscriptions ());
421
444
assertEquals (2 , relay .getSystemSubscriptions ().size ());
422
445
assertSame (handler1 , relay .getSystemSubscriptions ().get ("/topic/unresolved-user-destination" ));
423
446
assertSame (handler2 , relay .getSystemSubscriptions ().get ("/topic/simp-user-registry" ));
424
447
}
425
448
426
449
@ Test
427
- public void userBroadcastsDisabledWithSimpleBroker () throws Exception {
428
- SimpUserRegistry registry = this .simpleBrokerContext .getBean (SimpUserRegistry .class );
450
+ public void userBroadcastsDisabledWithSimpleBroker () {
451
+ ApplicationContext context = loadConfig (SimpleBrokerConfig .class );
452
+
453
+ SimpUserRegistry registry = context .getBean (SimpUserRegistry .class );
429
454
assertNotNull (registry );
430
455
assertNotEquals (MultiServerUserRegistry .class , registry .getClass ());
431
456
432
- UserDestinationMessageHandler handler = this . simpleBrokerContext .getBean (UserDestinationMessageHandler .class );
457
+ UserDestinationMessageHandler handler = context .getBean (UserDestinationMessageHandler .class );
433
458
assertNull (handler .getBroadcastDestination ());
434
459
435
460
String name = "userRegistryMessageHandler" ;
436
- MessageHandler messageHandler = this . simpleBrokerContext .getBean (name , MessageHandler .class );
461
+ MessageHandler messageHandler = context .getBean (name , MessageHandler .class );
437
462
assertNotEquals (UserRegistryMessageHandler .class , messageHandler .getClass ());
438
463
}
439
464
465
+ private AnnotationConfigApplicationContext loadConfig (Class <?> configClass ) {
466
+ return new AnnotationConfigApplicationContext (configClass );
467
+ }
468
+
440
469
441
470
@ SuppressWarnings ("unused" )
442
471
@ Controller
0 commit comments