29
29
import reactor .test .StepVerifier ;
30
30
31
31
import org .springframework .beans .DirectFieldAccessor ;
32
- import org .springframework .beans .factory .BeanFactory ;
33
32
import org .springframework .integration .MessageDispatchingException ;
34
33
import org .springframework .integration .annotation .Gateway ;
35
34
import org .springframework .integration .annotation .GatewayHeader ;
42
41
import org .springframework .messaging .support .ChannelInterceptor ;
43
42
import org .springframework .messaging .support .GenericMessage ;
44
43
import org .springframework .messaging .support .MessageBuilder ;
45
- import org .springframework .scheduling .TaskScheduler ;
44
+ import org .springframework .scheduling .concurrent . SimpleAsyncTaskScheduler ;
46
45
import org .springframework .util .ReflectionUtils ;
47
46
48
47
import static org .assertj .core .api .Assertions .assertThat ;
49
48
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
50
- import static org .mockito .ArgumentMatchers .any ;
51
- import static org .mockito .ArgumentMatchers .eq ;
52
49
import static org .mockito .Mockito .mock ;
53
- import static org .mockito .Mockito .when ;
54
50
55
51
/**
56
52
* @author Mark Fisher
@@ -67,9 +63,7 @@ public void futureWithMessageReturned() throws Exception {
67
63
QueueChannel requestChannel = new QueueChannel ();
68
64
startResponder (requestChannel );
69
65
GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
70
- proxyFactory .setDefaultRequestChannel (requestChannel );
71
- proxyFactory .setBeanName ("testGateway" );
72
- proxyFactory .setBeanFactory (getBeanFactory ());
66
+ setupProxyFactory (requestChannel , proxyFactory );
73
67
proxyFactory .afterPropertiesSet ();
74
68
TestEchoService service = proxyFactory .getObject ();
75
69
Future <Message <?>> f = service .returnMessage ("foo" );
@@ -90,9 +84,7 @@ protected boolean doSend(Message<?> message, long timeout) {
90
84
91
85
};
92
86
GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
93
- proxyFactory .setDefaultRequestChannel (channel );
94
- proxyFactory .setBeanName ("testGateway" );
95
- proxyFactory .setBeanFactory (getBeanFactory ());
87
+ setupProxyFactory (channel , proxyFactory );
96
88
proxyFactory .afterPropertiesSet ();
97
89
TestEchoService service = proxyFactory .getObject ();
98
90
Future <Message <?>> f = service .returnMessage ("foo" );
@@ -108,9 +100,7 @@ public void listenableFutureWithMessageReturned() throws Exception {
108
100
addThreadEnricher (requestChannel );
109
101
startResponder (requestChannel );
110
102
GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
111
- proxyFactory .setDefaultRequestChannel (requestChannel );
112
- proxyFactory .setBeanName ("testGateway" );
113
- proxyFactory .setBeanFactory (getBeanFactory ());
103
+ setupProxyFactory (requestChannel , proxyFactory );
114
104
proxyFactory .afterPropertiesSet ();
115
105
TestEchoService service = proxyFactory .getObject ();
116
106
CompletableFuture <Message <?>> f = service .returnMessageListenable ("foo" );
@@ -134,9 +124,7 @@ public void customFutureReturned() {
134
124
addThreadEnricher (requestChannel );
135
125
startResponder (requestChannel );
136
126
GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
137
- proxyFactory .setDefaultRequestChannel (requestChannel );
138
- proxyFactory .setBeanName ("testGateway" );
139
- proxyFactory .setBeanFactory (getBeanFactory ());
127
+ setupProxyFactory (requestChannel , proxyFactory );
140
128
proxyFactory .afterPropertiesSet ();
141
129
TestEchoService service = proxyFactory .getObject ();
142
130
CustomFuture f = service .returnCustomFuture ("foo" );
@@ -151,10 +139,7 @@ public void nonAsyncFutureReturned() {
151
139
addThreadEnricher (requestChannel );
152
140
startResponder (requestChannel );
153
141
GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
154
- proxyFactory .setDefaultRequestChannel (requestChannel );
155
- proxyFactory .setBeanName ("testGateway" );
156
- proxyFactory .setBeanFactory (getBeanFactory ());
157
-
142
+ setupProxyFactory (requestChannel , proxyFactory );
158
143
proxyFactory .setAsyncExecutor (null ); // Not async - user flow returns Future<?>
159
144
160
145
proxyFactory .afterPropertiesSet ();
@@ -183,9 +168,7 @@ public void futureWithPayloadReturned() throws Exception {
183
168
QueueChannel requestChannel = new QueueChannel ();
184
169
startResponder (requestChannel );
185
170
GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
186
- proxyFactory .setDefaultRequestChannel (requestChannel );
187
- proxyFactory .setBeanName ("testGateway" );
188
- proxyFactory .setBeanFactory (getBeanFactory ());
171
+ setupProxyFactory (requestChannel , proxyFactory );
189
172
proxyFactory .afterPropertiesSet ();
190
173
TestEchoService service = proxyFactory .getObject ();
191
174
Future <String > f = service .returnString ("foo" );
@@ -199,9 +182,7 @@ public void futureWithWildcardReturned() throws Exception {
199
182
QueueChannel requestChannel = new QueueChannel ();
200
183
startResponder (requestChannel );
201
184
GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
202
- proxyFactory .setDefaultRequestChannel (requestChannel );
203
- proxyFactory .setBeanName ("testGateway" );
204
- proxyFactory .setBeanFactory (getBeanFactory ());
185
+ setupProxyFactory (requestChannel , proxyFactory );
205
186
proxyFactory .afterPropertiesSet ();
206
187
TestEchoService service = proxyFactory .getObject ();
207
188
Future <?> f = service .returnSomething ("foo" );
@@ -213,9 +194,7 @@ public void futureWithWildcardReturned() throws Exception {
213
194
@ Test
214
195
public void futureVoid () throws Exception {
215
196
GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
216
- proxyFactory .setDefaultRequestChannel (new NullChannel ());
217
- proxyFactory .setBeanName ("testGateway" );
218
- proxyFactory .setBeanFactory (getBeanFactory ());
197
+ setupProxyFactory (new NullChannel (), proxyFactory );
219
198
proxyFactory .afterPropertiesSet ();
220
199
TestEchoService service = proxyFactory .getObject ();
221
200
Future <Void > f = service .asyncSendAndForget ("test1" );
@@ -253,9 +232,7 @@ public void futureVoidReply() throws Exception {
253
232
}
254
233
}).start ();
255
234
GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
256
- proxyFactory .setDefaultRequestChannel (requestChannel );
257
- proxyFactory .setBeanName ("testGateway" );
258
- proxyFactory .setBeanFactory (getBeanFactory ());
235
+ setupProxyFactory (requestChannel , proxyFactory );
259
236
proxyFactory .setAsyncExecutor (null );
260
237
proxyFactory .afterPropertiesSet ();
261
238
TestEchoService service = proxyFactory .getObject ();
@@ -270,9 +247,7 @@ public void monoWithMessageReturned() {
270
247
QueueChannel requestChannel = new QueueChannel ();
271
248
startResponder (requestChannel );
272
249
GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
273
- proxyFactory .setDefaultRequestChannel (requestChannel );
274
- proxyFactory .setBeanFactory (getBeanFactory ());
275
- proxyFactory .setBeanName ("testGateway" );
250
+ setupProxyFactory (requestChannel , proxyFactory );
276
251
proxyFactory .afterPropertiesSet ();
277
252
TestEchoService service = proxyFactory .getObject ();
278
253
Mono <Message <?>> mono = service .returnMessagePromise ("foo" );
@@ -285,9 +260,7 @@ public void monoWithPayloadReturned() {
285
260
QueueChannel requestChannel = new QueueChannel ();
286
261
startResponder (requestChannel );
287
262
GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
288
- proxyFactory .setDefaultRequestChannel (requestChannel );
289
- proxyFactory .setBeanFactory (getBeanFactory ());
290
- proxyFactory .setBeanName ("testGateway" );
263
+ setupProxyFactory (requestChannel , proxyFactory );
291
264
proxyFactory .afterPropertiesSet ();
292
265
TestEchoService service = proxyFactory .getObject ();
293
266
Mono <String > mono = service .returnStringPromise ("foo" );
@@ -300,9 +273,7 @@ public void monoWithWildcardReturned() {
300
273
QueueChannel requestChannel = new QueueChannel ();
301
274
startResponder (requestChannel );
302
275
GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
303
- proxyFactory .setDefaultRequestChannel (requestChannel );
304
- proxyFactory .setBeanFactory (getBeanFactory ());
305
- proxyFactory .setBeanName ("testGateway" );
276
+ setupProxyFactory (requestChannel , proxyFactory );
306
277
proxyFactory .afterPropertiesSet ();
307
278
TestEchoService service = proxyFactory .getObject ();
308
279
Mono <?> mono = service .returnSomethingPromise ("foo" );
@@ -316,9 +287,7 @@ public void monoWithConsumer() {
316
287
QueueChannel requestChannel = new QueueChannel ();
317
288
startResponder (requestChannel );
318
289
GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
319
- proxyFactory .setDefaultRequestChannel (requestChannel );
320
- proxyFactory .setBeanFactory (getBeanFactory ());
321
- proxyFactory .setBeanName ("testGateway" );
290
+ setupProxyFactory (requestChannel , proxyFactory );
322
291
proxyFactory .afterPropertiesSet ();
323
292
TestEchoService service = proxyFactory .getObject ();
324
293
Mono <String > mono = service .returnStringPromise ("foo" );
@@ -331,9 +300,7 @@ public void monoWithConsumer() {
331
300
@ Test
332
301
public void monoVoid () throws InterruptedException {
333
302
GatewayProxyFactoryBean <TestEchoService > proxyFactory = new GatewayProxyFactoryBean <>(TestEchoService .class );
334
- proxyFactory .setDefaultRequestChannel (new NullChannel ());
335
- proxyFactory .setBeanName ("testGateway" );
336
- proxyFactory .setBeanFactory (getBeanFactory ());
303
+ setupProxyFactory (new NullChannel (), proxyFactory );
337
304
proxyFactory .afterPropertiesSet ();
338
305
TestEchoService service = proxyFactory .getObject ();
339
306
Mono <Void > mono = service .monoVoid ("test1" );
@@ -376,15 +343,6 @@ private static void startResponder(final PollableChannel requestChannel) {
376
343
}).start ();
377
344
}
378
345
379
- private BeanFactory getBeanFactory () {
380
- BeanFactory beanFactory = mock (BeanFactory .class );
381
- TaskScheduler taskScheduler = mock (TaskScheduler .class );
382
- when (beanFactory .getBean (eq ("taskScheduler" ), any (Class .class )))
383
- .thenReturn (taskScheduler );
384
- when (beanFactory .containsBean ("taskScheduler" )).thenReturn (true );
385
- return beanFactory ;
386
- }
387
-
388
346
private interface TestEchoService {
389
347
390
348
Future <String > returnString (String s );
@@ -415,6 +373,13 @@ private interface TestEchoService {
415
373
416
374
}
417
375
376
+ private static void setupProxyFactory (MessageChannel messageChannel , GatewayProxyFactoryBean proxyFactory ) {
377
+ proxyFactory .setDefaultRequestChannel (messageChannel );
378
+ proxyFactory .setBeanName ("testGateway" );
379
+ proxyFactory .setTaskScheduler (new SimpleAsyncTaskScheduler ());
380
+ proxyFactory .setBeanFactory (mock ());
381
+ }
382
+
418
383
private record CustomFuture (String result , Thread thread ) implements Future <String > {
419
384
420
385
@ Override
0 commit comments