23
23
import static org .junit .Assert .assertFalse ;
24
24
import static org .junit .Assert .assertNotEquals ;
25
25
import static org .junit .Assert .assertNotNull ;
26
+ import static org .junit .Assert .assertNotSame ;
26
27
import static org .junit .Assert .assertNull ;
27
28
import static org .junit .Assert .assertSame ;
28
29
import static org .junit .Assert .assertThat ;
59
60
import org .springframework .context .annotation .ComponentScan ;
60
61
import org .springframework .context .annotation .Configuration ;
61
62
import org .springframework .context .annotation .FilterType ;
63
+ import org .springframework .context .annotation .Profile ;
62
64
import org .springframework .context .support .ClassPathXmlApplicationContext ;
63
65
import org .springframework .core .task .AsyncTaskExecutor ;
64
66
import org .springframework .core .task .SimpleAsyncTaskExecutor ;
77
79
import org .springframework .integration .context .IntegrationProperties ;
78
80
import org .springframework .integration .handler .AbstractReplyProducingMessageHandler ;
79
81
import org .springframework .integration .handler .BridgeHandler ;
82
+ import org .springframework .integration .handler .DelayHandler ;
80
83
import org .springframework .integration .support .MessageBuilder ;
81
84
import org .springframework .integration .test .util .TestUtils ;
82
85
import org .springframework .lang .Nullable ;
93
96
import org .springframework .scheduling .annotation .AsyncResult ;
94
97
import org .springframework .stereotype .Component ;
95
98
import org .springframework .test .annotation .DirtiesContext ;
99
+ import org .springframework .test .context .ActiveProfiles ;
96
100
import org .springframework .test .context .ContextConfiguration ;
97
101
import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
98
102
import org .springframework .util .concurrent .ListenableFuture ;
107
111
@ ContextConfiguration (classes = GatewayInterfaceTests .TestConfig .class )
108
112
@ RunWith (SpringJUnit4ClassRunner .class )
109
113
@ DirtiesContext
114
+ @ ActiveProfiles ("gatewayTest" )
110
115
public class GatewayInterfaceTests {
111
116
112
117
private static final String IGNORE_HEADER = "ignoreHeader" ;
@@ -153,20 +158,23 @@ public class GatewayInterfaceTests {
153
158
@ Autowired
154
159
private IgnoredHeaderGateway ignoredHeaderGateway ;
155
160
161
+ @ Autowired (required = false )
162
+ private NotActivatedByProfileGateway notActivatedByProfileGateway ;
163
+
156
164
@ Test
157
165
public void testWithServiceSuperclassAnnotatedMethod () throws Exception {
158
- ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext ( "GatewayInterfaceTests-context.xml" , this
159
- . getClass ());
166
+ ConfigurableApplicationContext ac =
167
+ new ClassPathXmlApplicationContext ( "GatewayInterfaceTests-context.xml" , getClass ());
160
168
DirectChannel channel = ac .getBean ("requestChannelFoo" , DirectChannel .class );
161
169
final Method fooMethod = Foo .class .getMethod ("foo" , String .class );
162
170
final AtomicBoolean called = new AtomicBoolean ();
163
171
MessageHandler handler = message -> {
164
- assertThat (( String ) message .getHeaders ().get ("name" ), equalTo ("foo" ));
165
- assertThat (
166
- ( String ) message . getHeaders (). get ( "string" ),
167
- equalTo ( "public abstract void org.springframework.integration.gateway. GatewayInterfaceTests$Foo.foo(java.lang.String)" ));
168
- assertThat (( Method ) message .getHeaders ().get ("object" ), equalTo (fooMethod ));
169
- assertThat (( String ) message .getPayload (), equalTo ("hello" ));
172
+ assertThat (message .getHeaders ().get ("name" ), equalTo ("foo" ));
173
+ assertThat (message . getHeaders (). get ( "string" ),
174
+ equalTo ( "public abstract void org.springframework.integration.gateway." +
175
+ " GatewayInterfaceTests$Foo.foo(java.lang.String)" ));
176
+ assertThat (message .getHeaders ().get ("object" ), equalTo (fooMethod ));
177
+ assertThat (message .getPayload (), equalTo ("hello" ));
170
178
assertThat (new MessageHeaderAccessor (message ).getErrorChannel (), equalTo ("errorChannel" ));
171
179
called .set (true );
172
180
};
@@ -181,18 +189,18 @@ public void testWithServiceSuperclassAnnotatedMethod() throws Exception {
181
189
182
190
@ Test
183
191
public void testWithServiceSuperclassAnnotatedMethodOverridePE () throws Exception {
184
- ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext ( "GatewayInterfaceTests2-context.xml" , this
185
- . getClass ());
192
+ ConfigurableApplicationContext ac =
193
+ new ClassPathXmlApplicationContext ( "GatewayInterfaceTests2-context.xml" , getClass ());
186
194
DirectChannel channel = ac .getBean ("requestChannelFoo" , DirectChannel .class );
187
195
final Method fooMethod = Foo .class .getMethod ("foo" , String .class );
188
196
final AtomicBoolean called = new AtomicBoolean ();
189
197
MessageHandler handler = message -> {
190
- assertThat (( String ) message .getHeaders ().get ("name" ), equalTo ("foo" ));
191
- assertThat (
192
- ( String ) message . getHeaders (). get ( "string" ),
193
- equalTo ( "public abstract void org.springframework.integration.gateway. GatewayInterfaceTests$Foo.foo(java.lang.String)" ));
194
- assertThat (( Method ) message .getHeaders ().get ("object" ), equalTo (fooMethod ));
195
- assertThat (( String ) message .getPayload (), equalTo ("foo" ));
198
+ assertThat (message .getHeaders ().get ("name" ), equalTo ("foo" ));
199
+ assertThat (message . getHeaders (). get ( "string" ),
200
+ equalTo ( "public abstract void org.springframework.integration.gateway." +
201
+ " GatewayInterfaceTests$Foo.foo(java.lang.String)" ));
202
+ assertThat (message .getHeaders ().get ("object" ), equalTo (fooMethod ));
203
+ assertThat (message .getPayload (), equalTo ("foo" ));
196
204
called .set (true );
197
205
};
198
206
channel .subscribe (handler );
@@ -204,8 +212,8 @@ public void testWithServiceSuperclassAnnotatedMethodOverridePE() throws Exceptio
204
212
205
213
@ Test
206
214
public void testWithServiceAnnotatedMethod () {
207
- ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext ( "GatewayInterfaceTests-context.xml" , this
208
- . getClass ());
215
+ ConfigurableApplicationContext ac =
216
+ new ClassPathXmlApplicationContext ( "GatewayInterfaceTests-context.xml" , getClass ());
209
217
DirectChannel channel = ac .getBean ("requestChannelBar" , DirectChannel .class );
210
218
MessageHandler handler = mock (MessageHandler .class );
211
219
channel .subscribe (handler );
@@ -217,15 +225,16 @@ public void testWithServiceAnnotatedMethod() {
217
225
218
226
@ Test
219
227
public void testWithServiceSuperclassUnAnnotatedMethod () throws Exception {
220
- ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext ( "GatewayInterfaceTests-context.xml" , this
221
- . getClass ());
228
+ ConfigurableApplicationContext ac =
229
+ new ClassPathXmlApplicationContext ( "GatewayInterfaceTests-context.xml" , getClass ());
222
230
DirectChannel channel = ac .getBean ("requestChannelBaz" , DirectChannel .class );
223
231
final Method bazMethod = Foo .class .getMethod ("baz" , String .class );
224
232
final AtomicBoolean called = new AtomicBoolean ();
225
233
MessageHandler handler = message -> {
226
234
assertThat (message .getHeaders ().get ("name" ), equalTo ("overrideGlobal" ));
227
235
assertThat (message .getHeaders ().get ("string" ),
228
- equalTo ("public abstract void org.springframework.integration.gateway.GatewayInterfaceTests$Foo.baz(java.lang.String)" ));
236
+ equalTo ("public abstract void org.springframework.integration.gateway." +
237
+ "GatewayInterfaceTests$Foo.baz(java.lang.String)" ));
229
238
assertThat (message .getHeaders ().get ("object" ), equalTo (bazMethod ));
230
239
assertThat (message .getPayload (), equalTo ("hello" ));
231
240
called .set (true );
@@ -239,15 +248,16 @@ public void testWithServiceSuperclassUnAnnotatedMethod() throws Exception {
239
248
240
249
@ Test
241
250
public void testWithServiceUnAnnotatedMethodGlobalHeaderDoesntOverride () throws Exception {
242
- ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext ( "GatewayInterfaceTests-context.xml" , this
243
- . getClass ());
251
+ ConfigurableApplicationContext ac =
252
+ new ClassPathXmlApplicationContext ( "GatewayInterfaceTests-context.xml" , getClass ());
244
253
DirectChannel channel = ac .getBean ("requestChannelBaz" , DirectChannel .class );
245
254
final Method quxMethod = Bar .class .getMethod ("qux" , String .class , String .class );
246
255
final AtomicBoolean called = new AtomicBoolean ();
247
256
MessageHandler handler = message -> {
248
257
assertThat (message .getHeaders ().get ("name" ), equalTo ("arg1" ));
249
258
assertThat (message .getHeaders ().get ("string" ),
250
- equalTo ("public abstract void org.springframework.integration.gateway.GatewayInterfaceTests$Bar.qux(java.lang.String,java.lang.String)" ));
259
+ equalTo ("public abstract void org.springframework.integration.gateway." +
260
+ "GatewayInterfaceTests$Bar.qux(java.lang.String,java.lang.String)" ));
251
261
assertThat (message .getHeaders ().get ("object" ), equalTo (quxMethod ));
252
262
assertThat (message .getPayload (), equalTo ("hello" ));
253
263
called .set (true );
@@ -261,8 +271,8 @@ public void testWithServiceUnAnnotatedMethodGlobalHeaderDoesntOverride() throws
261
271
262
272
@ Test
263
273
public void testWithServiceCastAsSuperclassAnnotatedMethod () {
264
- ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext ( "GatewayInterfaceTests-context.xml" , this
265
- . getClass ());
274
+ ConfigurableApplicationContext ac =
275
+ new ClassPathXmlApplicationContext ( "GatewayInterfaceTests-context.xml" , getClass ());
266
276
DirectChannel channel = ac .getBean ("requestChannelFoo" , DirectChannel .class );
267
277
MessageHandler handler = mock (MessageHandler .class );
268
278
channel .subscribe (handler );
@@ -274,8 +284,8 @@ public void testWithServiceCastAsSuperclassAnnotatedMethod() {
274
284
275
285
@ Test
276
286
public void testWithServiceCastAsSuperclassUnAnnotatedMethod () {
277
- ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext ( "GatewayInterfaceTests-context.xml" , this
278
- . getClass ());
287
+ ConfigurableApplicationContext ac =
288
+ new ClassPathXmlApplicationContext ( "GatewayInterfaceTests-context.xml" , getClass ());
279
289
DirectChannel channel = ac .getBean ("requestChannelBaz" , DirectChannel .class );
280
290
MessageHandler handler = mock (MessageHandler .class );
281
291
channel .subscribe (handler );
@@ -287,8 +297,8 @@ public void testWithServiceCastAsSuperclassUnAnnotatedMethod() {
287
297
288
298
@ Test
289
299
public void testWithServiceHashcode () {
290
- ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext ( "GatewayInterfaceTests-context.xml" , this
291
- . getClass ());
300
+ ConfigurableApplicationContext ac =
301
+ new ClassPathXmlApplicationContext ( "GatewayInterfaceTests-context.xml" , getClass ());
292
302
DirectChannel channel = ac .getBean ("requestChannelBaz" , DirectChannel .class );
293
303
MessageHandler handler = mock (MessageHandler .class );
294
304
channel .subscribe (handler );
@@ -300,8 +310,8 @@ public void testWithServiceHashcode() {
300
310
301
311
@ Test
302
312
public void testWithServiceToString () {
303
- ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext ( "GatewayInterfaceTests-context.xml" , this
304
- . getClass ());
313
+ ConfigurableApplicationContext ac =
314
+ new ClassPathXmlApplicationContext ( "GatewayInterfaceTests-context.xml" , getClass ());
305
315
DirectChannel channel = ac .getBean ("requestChannelBaz" , DirectChannel .class );
306
316
MessageHandler handler = mock (MessageHandler .class );
307
317
channel .subscribe (handler );
@@ -313,29 +323,29 @@ public void testWithServiceToString() {
313
323
314
324
@ Test
315
325
public void testWithServiceEquals () throws Exception {
316
- ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext ( "GatewayInterfaceTests-context.xml" , this
317
- . getClass ());
326
+ ConfigurableApplicationContext ac =
327
+ new ClassPathXmlApplicationContext ( "GatewayInterfaceTests-context.xml" , getClass ());
318
328
DirectChannel channel = ac .getBean ("requestChannelBaz" , DirectChannel .class );
319
329
MessageHandler handler = mock (MessageHandler .class );
320
330
channel .subscribe (handler );
321
331
Bar bar = ac .getBean (Bar .class );
322
- assertTrue (bar . equals ( ac .getBean (Bar .class ) ));
332
+ assertSame (bar , ac .getBean (Bar .class ));
323
333
GatewayProxyFactoryBean fb = new GatewayProxyFactoryBean (Bar .class );
324
334
DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
325
335
bf .registerSingleton ("requestChannelBar" , channel );
326
336
bf .registerSingleton ("requestChannelBaz" , channel );
327
337
bf .registerSingleton ("requestChannelFoo" , channel );
328
338
fb .setBeanFactory (bf );
329
339
fb .afterPropertiesSet ();
330
- assertFalse (bar . equals ( fb .getObject () ));
340
+ assertNotSame (bar , fb .getObject ());
331
341
verify (handler , times (0 )).handleMessage (Mockito .any (Message .class ));
332
342
ac .close ();
333
343
}
334
344
335
345
@ Test
336
346
public void testWithServiceGetClass () {
337
- ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext ( "GatewayInterfaceTests-context.xml" , this
338
- . getClass ());
347
+ ConfigurableApplicationContext ac =
348
+ new ClassPathXmlApplicationContext ( "GatewayInterfaceTests-context.xml" , getClass ());
339
349
DirectChannel channel = ac .getBean ("requestChannelBaz" , DirectChannel .class );
340
350
MessageHandler handler = mock (MessageHandler .class );
341
351
channel .subscribe (handler );
@@ -352,12 +362,12 @@ public void testWithServiceAsNotAnInterface() {
352
362
353
363
@ Test
354
364
public void testWithCustomMapper () {
355
- ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext ( "GatewayInterfaceTests-context.xml" , this
356
- . getClass ());
365
+ ConfigurableApplicationContext ac =
366
+ new ClassPathXmlApplicationContext ( "GatewayInterfaceTests-context.xml" , getClass ());
357
367
DirectChannel channel = ac .getBean ("requestChannelBaz" , DirectChannel .class );
358
368
final AtomicBoolean called = new AtomicBoolean ();
359
369
MessageHandler handler = message -> {
360
- assertThat (( String ) message .getPayload (), equalTo ("fizbuz" ));
370
+ assertThat (message .getPayload (), equalTo ("fizbuz" ));
361
371
called .set (true );
362
372
};
363
373
channel .subscribe (handler );
@@ -369,8 +379,13 @@ public void testWithCustomMapper() {
369
379
370
380
@ Test
371
381
public void testLateReply () {
372
- ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext ("GatewayInterfaceTests-context.xml" ,
373
- this .getClass ());
382
+ ConfigurableApplicationContext ac =
383
+ new ClassPathXmlApplicationContext ("GatewayInterfaceTests-context.xml" , getClass ());
384
+
385
+ DelayHandler delayHandler = ac .getBean (DelayHandler .class );
386
+ delayHandler .setMaxAttempts (2 );
387
+ delayHandler .setRetryDelay (10 );
388
+
374
389
Bar baz = ac .getBean (Bar .class );
375
390
String reply = baz .lateReply ("hello" , 1000 , 0 );
376
391
assertNull (reply );
@@ -448,14 +463,15 @@ public void testAutoCreateChannelGateway() {
448
463
@ SuppressWarnings ("rawtypes" )
449
464
public void testAnnotationGatewayProxyFactoryBean () {
450
465
assertNotNull (this .gatewayByAnnotationGPFB );
466
+ assertNull (this .notActivatedByProfileGateway );
451
467
452
468
assertSame (this .exec , this .annotationGatewayProxyFactoryBean .getAsyncExecutor ());
453
- assertEquals (1111L , TestUtils
454
- .getPropertyValue (this .annotationGatewayProxyFactoryBean , "defaultRequestTimeout" , Expression . class )
455
- .getValue ());
456
- assertEquals (222L , TestUtils
457
- .getPropertyValue (this .annotationGatewayProxyFactoryBean , "defaultReplyTimeout" , Expression . class )
458
- .getValue ());
469
+ assertEquals (1111L ,
470
+ TestUtils .getPropertyValue (this .annotationGatewayProxyFactoryBean ,
471
+ "defaultRequestTimeout" , Expression . class ) .getValue ());
472
+ assertEquals (222L ,
473
+ TestUtils .getPropertyValue (this .annotationGatewayProxyFactoryBean ,
474
+ "defaultReplyTimeout" , Expression . class ) .getValue ());
459
475
460
476
Collection <MessagingGatewaySupport > messagingGateways =
461
477
this .annotationGatewayProxyFactoryBean .getGateways ().values ();
@@ -563,7 +579,7 @@ public interface NoArgumentsGateway {
563
579
public static class BazMapper implements MethodArgsMessageMapper {
564
580
565
581
@ Override
566
- public Message <?> toMessage (MethodArgsHolder object , @ Nullable Map <String , Object > headers ) throws Exception {
582
+ public Message <?> toMessage (MethodArgsHolder object , @ Nullable Map <String , Object > headers ) {
567
583
return MessageBuilder .withPayload ("fizbuz" )
568
584
.copyHeadersIfAbsent (headers )
569
585
.build ();
@@ -652,6 +668,7 @@ public GatewayProxyFactoryBean annotationGatewayProxyFactoryBean() {
652
668
653
669
@ MessagingGateway
654
670
@ TestMessagingGateway
671
+ @ Profile ("gatewayTest" )
655
672
public interface Int2634Gateway {
656
673
657
674
@ Gateway (requestChannel = "gatewayChannel" , payloadExpression = "#args[0]" )
@@ -665,6 +682,15 @@ public interface Int2634Gateway {
665
682
666
683
}
667
684
685
+ @ MessagingGateway (defaultRequestChannel = "errorChannel" )
686
+ @ TestMessagingGateway
687
+ @ Profile ("notActiveProfile" )
688
+ public interface NotActivatedByProfileGateway {
689
+
690
+ void send (String payload );
691
+
692
+ }
693
+
668
694
@ MessagingGateway (asyncExecutor = "exec" )
669
695
@ TestMessagingGateway
670
696
public interface ExecGateway {
0 commit comments