Skip to content

Commit ede9528

Browse files
committed
Fix some tests for not closed application context
1 parent aeab030 commit ede9528

File tree

4 files changed

+106
-72
lines changed

4 files changed

+106
-72
lines changed

spring-integration-core/src/test/java/org/springframework/integration/config/xml/InnerDefinitionHandlerAwareEndpointParserTests.java

Lines changed: 86 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.integration.config.xml;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2021

2122
import java.io.ByteArrayInputStream;
2223
import java.util.Collection;
@@ -25,13 +26,14 @@
2526
import java.util.Map;
2627
import java.util.Properties;
2728

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;
3031

3132
import org.springframework.beans.factory.BeanDefinitionStoreException;
3233
import org.springframework.beans.factory.annotation.Autowired;
34+
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
3335
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
34-
import org.springframework.context.ApplicationContext;
36+
import org.springframework.context.ConfigurableApplicationContext;
3537
import org.springframework.context.support.GenericApplicationContext;
3638
import org.springframework.core.io.InputStreamResource;
3739
import org.springframework.integration.IntegrationMessageHeaderAccessor;
@@ -42,27 +44,35 @@
4244
import org.springframework.messaging.MessageChannel;
4345
import org.springframework.messaging.PollableChannel;
4446
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;
4748
import org.springframework.util.CollectionUtils;
4849
import org.springframework.util.StringUtils;
4950

5051
/**
5152
* @author Oleg Zhurakousky
5253
* @author Gunnar Hillert
5354
* @author Gary Russell
55+
* @author Artem Bilan
5456
*/
55-
@RunWith(SpringJUnit4ClassRunner.class)
56-
@ContextConfiguration
57+
@SpringJUnitConfig
5758
public class InnerDefinitionHandlerAwareEndpointParserTests {
5859

5960
@Autowired
6061
private Properties testConfigurations;
6162

63+
private ConfigurableApplicationContext applicationContext;
64+
65+
@AfterEach
66+
public void tearDown() {
67+
if (this.applicationContext != null) {
68+
this.applicationContext.close();
69+
}
70+
}
71+
6272
@Test
6373
public void testInnerSplitterDefinitionSuccess() {
6474
String configProperty = testConfigurations.getProperty("splitter-inner-success");
65-
this.testSplitterDefinitionSuccess(configProperty);
75+
testSplitterDefinitionSuccess(configProperty);
6676
}
6777

6878
@Test
@@ -83,10 +93,11 @@ public void testRefSplitterDefinitionSuccess() {
8393
this.testSplitterDefinitionSuccess(configProperty);
8494
}
8595

86-
@Test(expected = BeanDefinitionStoreException.class)
96+
@Test
8797
public void testInnerSplitterDefinitionFailureRefAndInner() {
8898
String xmlConfig = testConfigurations.getProperty("splitter-failure-refAndBean");
89-
this.bootStrap(xmlConfig);
99+
assertThatExceptionOfType(BeanDefinitionStoreException.class)
100+
.isThrownBy(() -> bootStrap(xmlConfig));
90101
}
91102

92103
@Test
@@ -98,31 +109,33 @@ public void testInnerTransformerDefinitionSuccess() {
98109
@Test
99110
public void testRefTransformerDefinitionSuccess() {
100111
String configProperty = testConfigurations.getProperty("transformer-ref-success");
101-
this.testTransformerDefinitionSuccess(configProperty);
112+
testTransformerDefinitionSuccess(configProperty);
102113
}
103114

104-
@Test(expected = BeanDefinitionStoreException.class)
115+
@Test
105116
public void testInnerTransformerDefinitionFailureRefAndInner() {
106117
String xmlConfig = testConfigurations.getProperty("transformer-failure-refAndBean");
107-
this.bootStrap(xmlConfig);
118+
assertThatExceptionOfType(BeanDefinitionStoreException.class)
119+
.isThrownBy(() -> bootStrap(xmlConfig));
108120
}
109121

110122
@Test
111123
public void testInnerRouterDefinitionSuccess() {
112124
String configProperty = testConfigurations.getProperty("router-inner-success");
113-
this.testRouterDefinitionSuccess(configProperty);
125+
testRouterDefinitionSuccess(configProperty);
114126
}
115127

116128
@Test
117129
public void testRefRouterDefinitionSuccess() {
118130
String configProperty = testConfigurations.getProperty("router-ref-success");
119-
this.testRouterDefinitionSuccess(configProperty);
131+
testRouterDefinitionSuccess(configProperty);
120132
}
121133

122-
@Test(expected = BeanDefinitionStoreException.class)
134+
@Test
123135
public void testInnerRouterDefinitionFailureRefAndInner() {
124136
String xmlConfig = testConfigurations.getProperty("router-failure-refAndBean");
125-
this.bootStrap(xmlConfig);
137+
assertThatExceptionOfType(BeanDefinitionStoreException.class)
138+
.isThrownBy(() -> bootStrap(xmlConfig));
126139
}
127140

128141
@Test
@@ -137,10 +150,11 @@ public void testRefSADefinitionSuccess() {
137150
this.testSADefinitionSuccess(configProperty);
138151
}
139152

140-
@Test(expected = BeanDefinitionStoreException.class)
153+
@Test
141154
public void testInnerSADefinitionFailureRefAndInner() {
142155
String xmlConfig = testConfigurations.getProperty("sa-failure-refAndBean");
143-
this.bootStrap(xmlConfig);
156+
assertThatExceptionOfType(BeanDefinitionStoreException.class)
157+
.isThrownBy(() -> bootStrap(xmlConfig));
144158
}
145159

146160
@Test
@@ -157,7 +171,8 @@ public void testInnerConcurrentAggregatorDefinitionSuccess() {
157171

158172
@Test
159173
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");
161176
this.testAggregatorDefinitionSuccess(configProperty);
162177
}
163178

@@ -167,10 +182,11 @@ public void testRefAggregatorDefinitionSuccess() {
167182
this.testAggregatorDefinitionSuccess(configProperty);
168183
}
169184

170-
@Test(expected = BeanDefinitionStoreException.class)
185+
@Test
171186
public void testInnerAggregatorDefinitionFailureRefAndInner() {
172187
String xmlConfig = testConfigurations.getProperty("aggregator-failure-refAndBean");
173-
this.bootStrap(xmlConfig);
188+
assertThatExceptionOfType(BeanDefinitionStoreException.class)
189+
.isThrownBy(() -> bootStrap(xmlConfig));
174190
}
175191

176192
@Test
@@ -185,103 +201,104 @@ public void testRefFilterDefinitionSuccess() {
185201
this.testFilterDefinitionSuccess(configProperty);
186202
}
187203

188-
@Test(expected = BeanDefinitionStoreException.class)
204+
@Test
189205
public void testInnerFilterDefinitionFailureRefAndInner() {
190206
String xmlConfig = testConfigurations.getProperty("filter-failure-refAndBean");
191-
this.bootStrap(xmlConfig);
207+
assertThatExceptionOfType(BeanDefinitionStoreException.class)
208+
.isThrownBy(() -> bootStrap(xmlConfig));
192209
}
193210

194211
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);
197214
assertThat(splitter).isNotNull();
198-
MessageBuilder<String[]> inChannelMessageBuilder = MessageBuilder.withPayload(new String[]{"One", "Two"});
215+
MessageBuilder<String[]> inChannelMessageBuilder = MessageBuilder.withPayload(new String[] { "One", "Two" });
199216
Message<String[]> inMessage = inChannelMessageBuilder.build();
200-
MessageChannel inChannel = (MessageChannel) ac.getBean("inChannel");
217+
MessageChannel inChannel = this.applicationContext.getBean("inChannel", MessageChannel.class);
201218
inChannel.send(inMessage);
202-
PollableChannel outChannel = (PollableChannel) ac.getBean("outChannel");
219+
PollableChannel outChannel = this.applicationContext.getBean("outChannel", PollableChannel.class);
203220
assertThat(outChannel.receive().getPayload() instanceof String).isTrue();
204-
outChannel = (PollableChannel) ac.getBean("outChannel");
221+
outChannel = this.applicationContext.getBean("outChannel", PollableChannel.class);
205222
assertThat(outChannel.receive().getPayload() instanceof String).isTrue();
206223
}
207224

208225
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);
211228
assertThat(transformer).isNotNull();
212-
MessageBuilder<String[]> inChannelMessageBuilder = MessageBuilder.withPayload(new String[]{"One", "Two"});
229+
MessageBuilder<String[]> inChannelMessageBuilder = MessageBuilder.withPayload(new String[] { "One", "Two" });
213230
Message<String[]> inMessage = inChannelMessageBuilder.build();
214-
DirectChannel inChannel = (DirectChannel) ac.getBean("inChannel");
231+
DirectChannel inChannel = this.applicationContext.getBean("inChannel", DirectChannel.class);
215232
inChannel.send(inMessage);
216-
PollableChannel outChannel = (PollableChannel) ac.getBean("outChannel");
233+
PollableChannel outChannel = this.applicationContext.getBean("outChannel", PollableChannel.class);
217234
String payload = (String) outChannel.receive().getPayload();
218235
assertThat(payload.equals("One,Two")).isTrue();
219236
}
220237

221238
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);
224241
assertThat(splitter).isNotNull();
225242
MessageBuilder<String> inChannelMessageBuilder = MessageBuilder.withPayload("1");
226243
Message<String> inMessage = inChannelMessageBuilder.build();
227-
DirectChannel inChannel = (DirectChannel) ac.getBean("inChannel");
244+
DirectChannel inChannel = this.applicationContext.getBean("inChannel", DirectChannel.class);
228245
inChannel.send(inMessage);
229-
PollableChannel channel1 = (PollableChannel) ac.getBean("channel1");
246+
PollableChannel channel1 = this.applicationContext.getBean("channel1", PollableChannel.class);
230247
assertThat(channel1.receive().getPayload().equals("1")).isTrue();
231248
inChannelMessageBuilder = MessageBuilder.withPayload("2");
232249
inMessage = inChannelMessageBuilder.build();
233250
inChannel.send(inMessage);
234-
PollableChannel channel2 = (PollableChannel) ac.getBean("channel2");
251+
PollableChannel channel2 = this.applicationContext.getBean("channel2", PollableChannel.class);
235252
assertThat(channel2.receive().getPayload().equals("2")).isTrue();
236253
}
237254

238255
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);
241259
assertThat(splitter).isNotNull();
242260
MessageBuilder<String> inChannelMessageBuilder = MessageBuilder.withPayload("1");
243261
Message<String> inMessage = inChannelMessageBuilder.build();
244-
DirectChannel inChannel = (DirectChannel) ac.getBean("inChannel");
262+
DirectChannel inChannel = this.applicationContext.getBean("inChannel", DirectChannel.class);
245263
inChannel.send(inMessage);
246-
PollableChannel channel1 = (PollableChannel) ac.getBean("outChannel");
264+
PollableChannel channel1 = this.applicationContext.getBean("outChannel", PollableChannel.class);
247265
assertThat(channel1.receive().getPayload().equals("1")).isTrue();
248266
}
249267

250268
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);
253271
for (int i = 0; i < 5; i++) {
254272
Map<String, Object> headers = stubHeaders(i, 5, 1);
255273
Message<Integer> message = MessageBuilder.withPayload(i).copyHeaders(headers).build();
256274
inChannel.send(message);
257275
}
258-
PollableChannel output = (PollableChannel) ac.getBean("outChannel");
276+
PollableChannel output = this.applicationContext.getBean("outChannel", PollableChannel.class);
259277
Message<?> receivedMessage = output.receive(10000);
260278
assertThat(receivedMessage).isNotNull();
261279
assertThat(receivedMessage.getPayload()).isEqualTo(0 + 1 + 2 + 3 + 4);
262280
}
263281

264282
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"));
269287
Message<?> reply = output.receive(0);
270288
assertThat(reply.getPayload()).isEqualTo("foo");
271289
}
272290

273-
private ApplicationContext bootStrap(String configProperty) {
291+
private void bootStrap(String configProperty) {
274292
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);
277295
reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
278296
reader.loadBeanDefinitions(new InputStreamResource(stream));
279-
ac.refresh();
280-
return ac;
297+
this.applicationContext.refresh();
281298
}
282299

283300
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<>();
285302
headers.put(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER, sequenceNumber);
286303
headers.put(IntegrationMessageHeaderAccessor.SEQUENCE_SIZE, sequenceSize);
287304
headers.put(IntegrationMessageHeaderAccessor.CORRELATION_ID, correllationId);
@@ -290,43 +307,55 @@ private Map<String, Object> stubHeaders(int sequenceNumber, int sequenceSize, in
290307

291308
@SuppressWarnings("unchecked")
292309
public static class TestSplitter {
310+
293311
public Collection<String> split(String[] payload) {
294312
return CollectionUtils.arrayToList(payload);
295313
}
314+
296315
}
297316

298317
public static class TestTransformer {
318+
299319
public String split(String[] payload) {
300320
return StringUtils.arrayToDelimitedString(payload, ",");
301321
}
322+
302323
}
303324

304325
public static class TestRouter {
326+
305327
public String route(String value) {
306328
return (value.equals("1")) ? "channel1" : "channel2";
307329
}
330+
308331
}
309332

310333
public static class TestServiceActivator {
334+
311335
public String foo(String value) {
312336
return value;
313337
}
338+
314339
}
315340

316341
public static class TestAggregator {
342+
317343
public Integer sum(List<Integer> numbers) {
318344
int result = 0;
319345
for (Integer number : numbers) {
320346
result += number;
321347
}
322348
return result;
323349
}
350+
324351
}
325352

326353
public static class TestMessageFilter {
354+
327355
public boolean filter(String value) {
328356
return value.equals("foo");
329357
}
358+
330359
}
331360

332361
}

0 commit comments

Comments
 (0)