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