17
17
package org .springframework .integration .config .xml ;
18
18
19
19
import static org .assertj .core .api .Assertions .assertThat ;
20
+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
20
21
21
22
import java .io .ByteArrayInputStream ;
22
23
import java .util .Collection ;
25
26
import java .util .Map ;
26
27
import java .util .Properties ;
27
28
28
- import org .junit .Test ;
29
- import org .junit .runner . RunWith ;
29
+ import org .junit .jupiter . api . AfterEach ;
30
+ import org .junit .jupiter . api . Test ;
30
31
31
32
import org .springframework .beans .factory .BeanDefinitionStoreException ;
32
33
import org .springframework .beans .factory .annotation .Autowired ;
34
+ import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
33
35
import org .springframework .beans .factory .xml .XmlBeanDefinitionReader ;
34
- import org .springframework .context .ApplicationContext ;
36
+ import org .springframework .context .ConfigurableApplicationContext ;
35
37
import org .springframework .context .support .GenericApplicationContext ;
36
38
import org .springframework .core .io .InputStreamResource ;
37
39
import org .springframework .integration .IntegrationMessageHeaderAccessor ;
42
44
import org .springframework .messaging .MessageChannel ;
43
45
import org .springframework .messaging .PollableChannel ;
44
46
import org .springframework .messaging .support .GenericMessage ;
45
- import org .springframework .test .context .ContextConfiguration ;
46
- import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
47
+ import org .springframework .test .context .junit .jupiter .SpringJUnitConfig ;
47
48
import org .springframework .util .CollectionUtils ;
48
49
import org .springframework .util .StringUtils ;
49
50
50
51
/**
51
52
* @author Oleg Zhurakousky
52
53
* @author Gunnar Hillert
53
54
* @author Gary Russell
55
+ * @author Artem Bilan
54
56
*/
55
- @ RunWith (SpringJUnit4ClassRunner .class )
56
- @ ContextConfiguration
57
+ @ SpringJUnitConfig
57
58
public class InnerDefinitionHandlerAwareEndpointParserTests {
58
59
59
60
@ Autowired
60
61
private Properties testConfigurations ;
61
62
63
+ private ConfigurableApplicationContext applicationContext ;
64
+
65
+ @ AfterEach
66
+ public void tearDown () {
67
+ if (this .applicationContext != null ) {
68
+ this .applicationContext .close ();
69
+ }
70
+ }
71
+
62
72
@ Test
63
73
public void testInnerSplitterDefinitionSuccess () {
64
74
String configProperty = testConfigurations .getProperty ("splitter-inner-success" );
65
- this . testSplitterDefinitionSuccess (configProperty );
75
+ testSplitterDefinitionSuccess (configProperty );
66
76
}
67
77
68
78
@ Test
@@ -83,10 +93,11 @@ public void testRefSplitterDefinitionSuccess() {
83
93
this .testSplitterDefinitionSuccess (configProperty );
84
94
}
85
95
86
- @ Test ( expected = BeanDefinitionStoreException . class )
96
+ @ Test
87
97
public void testInnerSplitterDefinitionFailureRefAndInner () {
88
98
String xmlConfig = testConfigurations .getProperty ("splitter-failure-refAndBean" );
89
- this .bootStrap (xmlConfig );
99
+ assertThatExceptionOfType (BeanDefinitionStoreException .class )
100
+ .isThrownBy (() -> bootStrap (xmlConfig ));
90
101
}
91
102
92
103
@ Test
@@ -98,31 +109,33 @@ public void testInnerTransformerDefinitionSuccess() {
98
109
@ Test
99
110
public void testRefTransformerDefinitionSuccess () {
100
111
String configProperty = testConfigurations .getProperty ("transformer-ref-success" );
101
- this . testTransformerDefinitionSuccess (configProperty );
112
+ testTransformerDefinitionSuccess (configProperty );
102
113
}
103
114
104
- @ Test ( expected = BeanDefinitionStoreException . class )
115
+ @ Test
105
116
public void testInnerTransformerDefinitionFailureRefAndInner () {
106
117
String xmlConfig = testConfigurations .getProperty ("transformer-failure-refAndBean" );
107
- this .bootStrap (xmlConfig );
118
+ assertThatExceptionOfType (BeanDefinitionStoreException .class )
119
+ .isThrownBy (() -> bootStrap (xmlConfig ));
108
120
}
109
121
110
122
@ Test
111
123
public void testInnerRouterDefinitionSuccess () {
112
124
String configProperty = testConfigurations .getProperty ("router-inner-success" );
113
- this . testRouterDefinitionSuccess (configProperty );
125
+ testRouterDefinitionSuccess (configProperty );
114
126
}
115
127
116
128
@ Test
117
129
public void testRefRouterDefinitionSuccess () {
118
130
String configProperty = testConfigurations .getProperty ("router-ref-success" );
119
- this . testRouterDefinitionSuccess (configProperty );
131
+ testRouterDefinitionSuccess (configProperty );
120
132
}
121
133
122
- @ Test ( expected = BeanDefinitionStoreException . class )
134
+ @ Test
123
135
public void testInnerRouterDefinitionFailureRefAndInner () {
124
136
String xmlConfig = testConfigurations .getProperty ("router-failure-refAndBean" );
125
- this .bootStrap (xmlConfig );
137
+ assertThatExceptionOfType (BeanDefinitionStoreException .class )
138
+ .isThrownBy (() -> bootStrap (xmlConfig ));
126
139
}
127
140
128
141
@ Test
@@ -137,10 +150,11 @@ public void testRefSADefinitionSuccess() {
137
150
this .testSADefinitionSuccess (configProperty );
138
151
}
139
152
140
- @ Test ( expected = BeanDefinitionStoreException . class )
153
+ @ Test
141
154
public void testInnerSADefinitionFailureRefAndInner () {
142
155
String xmlConfig = testConfigurations .getProperty ("sa-failure-refAndBean" );
143
- this .bootStrap (xmlConfig );
156
+ assertThatExceptionOfType (BeanDefinitionStoreException .class )
157
+ .isThrownBy (() -> bootStrap (xmlConfig ));
144
158
}
145
159
146
160
@ Test
@@ -157,7 +171,8 @@ public void testInnerConcurrentAggregatorDefinitionSuccess() {
157
171
158
172
@ Test
159
173
public void testInnerConcurrentAggregatorDefinitionSuccessReorderBeanPoller () {
160
- String configProperty = testConfigurations .getProperty ("aggregator-inner-concurrent-success-reorder-bean-poller" );
174
+ String configProperty = testConfigurations
175
+ .getProperty ("aggregator-inner-concurrent-success-reorder-bean-poller" );
161
176
this .testAggregatorDefinitionSuccess (configProperty );
162
177
}
163
178
@@ -167,10 +182,11 @@ public void testRefAggregatorDefinitionSuccess() {
167
182
this .testAggregatorDefinitionSuccess (configProperty );
168
183
}
169
184
170
- @ Test ( expected = BeanDefinitionStoreException . class )
185
+ @ Test
171
186
public void testInnerAggregatorDefinitionFailureRefAndInner () {
172
187
String xmlConfig = testConfigurations .getProperty ("aggregator-failure-refAndBean" );
173
- this .bootStrap (xmlConfig );
188
+ assertThatExceptionOfType (BeanDefinitionStoreException .class )
189
+ .isThrownBy (() -> bootStrap (xmlConfig ));
174
190
}
175
191
176
192
@ Test
@@ -185,103 +201,104 @@ public void testRefFilterDefinitionSuccess() {
185
201
this .testFilterDefinitionSuccess (configProperty );
186
202
}
187
203
188
- @ Test ( expected = BeanDefinitionStoreException . class )
204
+ @ Test
189
205
public void testInnerFilterDefinitionFailureRefAndInner () {
190
206
String xmlConfig = testConfigurations .getProperty ("filter-failure-refAndBean" );
191
- this .bootStrap (xmlConfig );
207
+ assertThatExceptionOfType (BeanDefinitionStoreException .class )
208
+ .isThrownBy (() -> bootStrap (xmlConfig ));
192
209
}
193
210
194
211
private void testSplitterDefinitionSuccess (String configProperty ) {
195
- ApplicationContext ac = this . bootStrap (configProperty );
196
- EventDrivenConsumer splitter = ( EventDrivenConsumer ) ac . getBean ("testSplitter" );
212
+ bootStrap (configProperty );
213
+ EventDrivenConsumer splitter = this . applicationContext . getBean ("testSplitter" , EventDrivenConsumer . class );
197
214
assertThat (splitter ).isNotNull ();
198
- MessageBuilder <String []> inChannelMessageBuilder = MessageBuilder .withPayload (new String []{ "One" , "Two" });
215
+ MessageBuilder <String []> inChannelMessageBuilder = MessageBuilder .withPayload (new String [] { "One" , "Two" });
199
216
Message <String []> inMessage = inChannelMessageBuilder .build ();
200
- MessageChannel inChannel = ( MessageChannel ) ac . getBean ("inChannel" );
217
+ MessageChannel inChannel = this . applicationContext . getBean ("inChannel" , MessageChannel . class );
201
218
inChannel .send (inMessage );
202
- PollableChannel outChannel = ( PollableChannel ) ac . getBean ("outChannel" );
219
+ PollableChannel outChannel = this . applicationContext . getBean ("outChannel" , PollableChannel . class );
203
220
assertThat (outChannel .receive ().getPayload () instanceof String ).isTrue ();
204
- outChannel = ( PollableChannel ) ac . getBean ("outChannel" );
221
+ outChannel = this . applicationContext . getBean ("outChannel" , PollableChannel . class );
205
222
assertThat (outChannel .receive ().getPayload () instanceof String ).isTrue ();
206
223
}
207
224
208
225
private void testTransformerDefinitionSuccess (String configProperty ) {
209
- ApplicationContext ac = this . bootStrap (configProperty );
210
- EventDrivenConsumer transformer = ( EventDrivenConsumer ) ac . getBean ("testTransformer" );
226
+ bootStrap (configProperty );
227
+ EventDrivenConsumer transformer = this . applicationContext . getBean ("testTransformer" , EventDrivenConsumer . class );
211
228
assertThat (transformer ).isNotNull ();
212
- MessageBuilder <String []> inChannelMessageBuilder = MessageBuilder .withPayload (new String []{ "One" , "Two" });
229
+ MessageBuilder <String []> inChannelMessageBuilder = MessageBuilder .withPayload (new String [] { "One" , "Two" });
213
230
Message <String []> inMessage = inChannelMessageBuilder .build ();
214
- DirectChannel inChannel = ( DirectChannel ) ac . getBean ("inChannel" );
231
+ DirectChannel inChannel = this . applicationContext . getBean ("inChannel" , DirectChannel . class );
215
232
inChannel .send (inMessage );
216
- PollableChannel outChannel = ( PollableChannel ) ac . getBean ("outChannel" );
233
+ PollableChannel outChannel = this . applicationContext . getBean ("outChannel" , PollableChannel . class );
217
234
String payload = (String ) outChannel .receive ().getPayload ();
218
235
assertThat (payload .equals ("One,Two" )).isTrue ();
219
236
}
220
237
221
238
private void testRouterDefinitionSuccess (String configProperty ) {
222
- ApplicationContext ac = this . bootStrap (configProperty );
223
- EventDrivenConsumer splitter = ( EventDrivenConsumer ) ac . getBean ("testRouter" );
239
+ bootStrap (configProperty );
240
+ EventDrivenConsumer splitter = this . applicationContext . getBean ("testRouter" , EventDrivenConsumer . class );
224
241
assertThat (splitter ).isNotNull ();
225
242
MessageBuilder <String > inChannelMessageBuilder = MessageBuilder .withPayload ("1" );
226
243
Message <String > inMessage = inChannelMessageBuilder .build ();
227
- DirectChannel inChannel = ( DirectChannel ) ac . getBean ("inChannel" );
244
+ DirectChannel inChannel = this . applicationContext . getBean ("inChannel" , DirectChannel . class );
228
245
inChannel .send (inMessage );
229
- PollableChannel channel1 = ( PollableChannel ) ac . getBean ("channel1" );
246
+ PollableChannel channel1 = this . applicationContext . getBean ("channel1" , PollableChannel . class );
230
247
assertThat (channel1 .receive ().getPayload ().equals ("1" )).isTrue ();
231
248
inChannelMessageBuilder = MessageBuilder .withPayload ("2" );
232
249
inMessage = inChannelMessageBuilder .build ();
233
250
inChannel .send (inMessage );
234
- PollableChannel channel2 = ( PollableChannel ) ac . getBean ("channel2" );
251
+ PollableChannel channel2 = this . applicationContext . getBean ("channel2" , PollableChannel . class );
235
252
assertThat (channel2 .receive ().getPayload ().equals ("2" )).isTrue ();
236
253
}
237
254
238
255
private void testSADefinitionSuccess (String configProperty ) {
239
- ApplicationContext ac = this .bootStrap (configProperty );
240
- EventDrivenConsumer splitter = (EventDrivenConsumer ) ac .getBean ("testServiceActivator" );
256
+ bootStrap (configProperty );
257
+ EventDrivenConsumer splitter = this .applicationContext
258
+ .getBean ("testServiceActivator" , EventDrivenConsumer .class );
241
259
assertThat (splitter ).isNotNull ();
242
260
MessageBuilder <String > inChannelMessageBuilder = MessageBuilder .withPayload ("1" );
243
261
Message <String > inMessage = inChannelMessageBuilder .build ();
244
- DirectChannel inChannel = ( DirectChannel ) ac . getBean ("inChannel" );
262
+ DirectChannel inChannel = this . applicationContext . getBean ("inChannel" , DirectChannel . class );
245
263
inChannel .send (inMessage );
246
- PollableChannel channel1 = ( PollableChannel ) ac . getBean ("outChannel" );
264
+ PollableChannel channel1 = this . applicationContext . getBean ("outChannel" , PollableChannel . class );
247
265
assertThat (channel1 .receive ().getPayload ().equals ("1" )).isTrue ();
248
266
}
249
267
250
268
private void testAggregatorDefinitionSuccess (String configProperty ) {
251
- ApplicationContext ac = this . bootStrap (configProperty );
252
- MessageChannel inChannel = ( MessageChannel ) ac . getBean ("inChannel" );
269
+ bootStrap (configProperty );
270
+ MessageChannel inChannel = this . applicationContext . getBean ("inChannel" , MessageChannel . class );
253
271
for (int i = 0 ; i < 5 ; i ++) {
254
272
Map <String , Object > headers = stubHeaders (i , 5 , 1 );
255
273
Message <Integer > message = MessageBuilder .withPayload (i ).copyHeaders (headers ).build ();
256
274
inChannel .send (message );
257
275
}
258
- PollableChannel output = ( PollableChannel ) ac . getBean ("outChannel" );
276
+ PollableChannel output = this . applicationContext . getBean ("outChannel" , PollableChannel . class );
259
277
Message <?> receivedMessage = output .receive (10000 );
260
278
assertThat (receivedMessage ).isNotNull ();
261
279
assertThat (receivedMessage .getPayload ()).isEqualTo (0 + 1 + 2 + 3 + 4 );
262
280
}
263
281
264
282
private void testFilterDefinitionSuccess (String configProperty ) {
265
- ApplicationContext ac = this . bootStrap (configProperty );
266
- MessageChannel input = ( MessageChannel ) ac . getBean ("inChannel" );
267
- PollableChannel output = ( PollableChannel ) ac . getBean ("outChannel" );
268
- input .send (new GenericMessage <String >("foo" ));
283
+ bootStrap (configProperty );
284
+ MessageChannel input = this . applicationContext . getBean ("inChannel" , MessageChannel . class );
285
+ PollableChannel output = this . applicationContext . getBean ("outChannel" , PollableChannel . class );
286
+ input .send (new GenericMessage <>("foo" ));
269
287
Message <?> reply = output .receive (0 );
270
288
assertThat (reply .getPayload ()).isEqualTo ("foo" );
271
289
}
272
290
273
- private ApplicationContext bootStrap (String configProperty ) {
291
+ private void bootStrap (String configProperty ) {
274
292
ByteArrayInputStream stream = new ByteArrayInputStream (configProperty .getBytes ());
275
- GenericApplicationContext ac = new GenericApplicationContext ();
276
- XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader (ac );
293
+ this . applicationContext = new GenericApplicationContext ();
294
+ XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader (( BeanDefinitionRegistry ) this . applicationContext );
277
295
reader .setValidationMode (XmlBeanDefinitionReader .VALIDATION_XSD );
278
296
reader .loadBeanDefinitions (new InputStreamResource (stream ));
279
- ac .refresh ();
280
- return ac ;
297
+ this .applicationContext .refresh ();
281
298
}
282
299
283
300
private Map <String , Object > stubHeaders (int sequenceNumber , int sequenceSize , int correllationId ) {
284
- Map <String , Object > headers = new HashMap <String , Object >();
301
+ Map <String , Object > headers = new HashMap <>();
285
302
headers .put (IntegrationMessageHeaderAccessor .SEQUENCE_NUMBER , sequenceNumber );
286
303
headers .put (IntegrationMessageHeaderAccessor .SEQUENCE_SIZE , sequenceSize );
287
304
headers .put (IntegrationMessageHeaderAccessor .CORRELATION_ID , correllationId );
@@ -290,43 +307,55 @@ private Map<String, Object> stubHeaders(int sequenceNumber, int sequenceSize, in
290
307
291
308
@ SuppressWarnings ("unchecked" )
292
309
public static class TestSplitter {
310
+
293
311
public Collection <String > split (String [] payload ) {
294
312
return CollectionUtils .arrayToList (payload );
295
313
}
314
+
296
315
}
297
316
298
317
public static class TestTransformer {
318
+
299
319
public String split (String [] payload ) {
300
320
return StringUtils .arrayToDelimitedString (payload , "," );
301
321
}
322
+
302
323
}
303
324
304
325
public static class TestRouter {
326
+
305
327
public String route (String value ) {
306
328
return (value .equals ("1" )) ? "channel1" : "channel2" ;
307
329
}
330
+
308
331
}
309
332
310
333
public static class TestServiceActivator {
334
+
311
335
public String foo (String value ) {
312
336
return value ;
313
337
}
338
+
314
339
}
315
340
316
341
public static class TestAggregator {
342
+
317
343
public Integer sum (List <Integer > numbers ) {
318
344
int result = 0 ;
319
345
for (Integer number : numbers ) {
320
346
result += number ;
321
347
}
322
348
return result ;
323
349
}
350
+
324
351
}
325
352
326
353
public static class TestMessageFilter {
354
+
327
355
public boolean filter (String value ) {
328
356
return value .equals ("foo" );
329
357
}
358
+
330
359
}
331
360
332
361
}
0 commit comments