Skip to content

Commit 5781493

Browse files
committed
Improve JMS tests
https://build.spring.io/browse/INT-AT42SIO-530/ * Add `@DirtiesContext` to some active tests * Close manually started `ApplicationContext`s * Increase some reply waiting timeouts
1 parent 7ab3656 commit 5781493

12 files changed

+101
-53
lines changed

spring-integration-jms/src/test/java/org/springframework/integration/jms/OutboundGatewayFunctionTests.java

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -88,7 +88,8 @@ public void testContainerWithDest() throws Exception {
8888
.thenReturn(scheduler);
8989
final JmsOutboundGateway gateway = new JmsOutboundGateway();
9090
gateway.setBeanFactory(beanFactory);
91-
gateway.setConnectionFactory(getGatewayConnectionFactory());
91+
ConnectionFactory connectionFactory = getConnectionFactory();
92+
gateway.setConnectionFactory(connectionFactory);
9293
gateway.setRequestDestination(requestQueue1);
9394
gateway.setReplyDestination(replyQueue1);
9495
gateway.setCorrelationKey("JMSCorrelationID");
@@ -112,8 +113,8 @@ public void run() {
112113
});
113114
assertTrue(latch1.await(10, TimeUnit.SECONDS));
114115
JmsTemplate template = new JmsTemplate();
115-
template.setConnectionFactory(getTemplateConnectionFactory());
116-
template.setReceiveTimeout(5000);
116+
template.setConnectionFactory(connectionFactory);
117+
template.setReceiveTimeout(10000);
117118
javax.jms.Message request = template.receive(requestQueue1);
118119
assertNotNull(request);
119120
final javax.jms.Message jmsReply = request;
@@ -128,6 +129,7 @@ public Message createMessage(Session session) throws JMSException {
128129
assertNotNull(reply.get());
129130

130131
gateway.stop();
132+
scheduler.destroy();
131133
}
132134

133135
@Test
@@ -140,7 +142,7 @@ public void testContainerWithDestNoCorrelation() throws Exception {
140142
.thenReturn(scheduler);
141143
final JmsOutboundGateway gateway = new JmsOutboundGateway();
142144
gateway.setBeanFactory(beanFactory);
143-
gateway.setConnectionFactory(getGatewayConnectionFactory());
145+
gateway.setConnectionFactory(getConnectionFactory());
144146
gateway.setRequestDestination(requestQueue2);
145147
gateway.setReplyDestination(replyQueue2);
146148
gateway.setUseReplyContainer(true);
@@ -163,8 +165,8 @@ public void run() {
163165
});
164166
assertTrue(latch1.await(10, TimeUnit.SECONDS));
165167
JmsTemplate template = new JmsTemplate();
166-
template.setConnectionFactory(getTemplateConnectionFactory());
167-
template.setReceiveTimeout(5000);
168+
template.setConnectionFactory(getConnectionFactory());
169+
template.setReceiveTimeout(10000);
168170
javax.jms.Message request = template.receive(requestQueue2);
169171
assertNotNull(request);
170172
final javax.jms.Message jmsReply = request;
@@ -180,6 +182,7 @@ public Message createMessage(Session session) throws JMSException {
180182
assertNotNull(reply.get());
181183

182184
gateway.stop();
185+
scheduler.destroy();
183186
}
184187

185188
@Test
@@ -192,7 +195,7 @@ public void testContainerWithDestName() throws Exception {
192195
.thenReturn(scheduler);
193196
final JmsOutboundGateway gateway = new JmsOutboundGateway();
194197
gateway.setBeanFactory(beanFactory);
195-
gateway.setConnectionFactory(getGatewayConnectionFactory());
198+
gateway.setConnectionFactory(getConnectionFactory());
196199
gateway.setRequestDestination(requestQueue3);
197200
gateway.setReplyDestinationName("reply3");
198201
gateway.setCorrelationKey("JMSCorrelationID");
@@ -216,8 +219,8 @@ public void run() {
216219
});
217220
assertTrue(latch1.await(10, TimeUnit.SECONDS));
218221
JmsTemplate template = new JmsTemplate();
219-
template.setConnectionFactory(getTemplateConnectionFactory());
220-
template.setReceiveTimeout(5000);
222+
template.setConnectionFactory(getConnectionFactory());
223+
template.setReceiveTimeout(10000);
221224
javax.jms.Message request = template.receive(requestQueue3);
222225
assertNotNull(request);
223226
final javax.jms.Message jmsReply = request;
@@ -232,6 +235,7 @@ public Message createMessage(Session session) throws JMSException {
232235
assertNotNull(reply.get());
233236

234237
gateway.stop();
238+
scheduler.destroy();
235239
}
236240

237241
@Test
@@ -244,7 +248,7 @@ public void testContainerWithDestNameNoCorrelation() throws Exception {
244248
.thenReturn(scheduler);
245249
final JmsOutboundGateway gateway = new JmsOutboundGateway();
246250
gateway.setBeanFactory(beanFactory);
247-
gateway.setConnectionFactory(getGatewayConnectionFactory());
251+
gateway.setConnectionFactory(getConnectionFactory());
248252
gateway.setRequestDestination(requestQueue4);
249253
gateway.setReplyDestinationName("reply4");
250254
gateway.setUseReplyContainer(true);
@@ -267,8 +271,8 @@ public void run() {
267271
});
268272
assertTrue(latch1.await(10, TimeUnit.SECONDS));
269273
JmsTemplate template = new JmsTemplate();
270-
template.setConnectionFactory(getTemplateConnectionFactory());
271-
template.setReceiveTimeout(5000);
274+
template.setConnectionFactory(getConnectionFactory());
275+
template.setReceiveTimeout(10000);
272276
javax.jms.Message request = template.receive(requestQueue4);
273277
assertNotNull(request);
274278
final javax.jms.Message jmsReply = request;
@@ -284,6 +288,7 @@ public Message createMessage(Session session) throws JMSException {
284288
assertNotNull(reply.get());
285289

286290
gateway.stop();
291+
scheduler.destroy();
287292
}
288293

289294
@Test
@@ -296,10 +301,11 @@ public void testContainerWithTemporary() throws Exception {
296301
.thenReturn(scheduler);
297302
final JmsOutboundGateway gateway = new JmsOutboundGateway();
298303
gateway.setBeanFactory(beanFactory);
299-
gateway.setConnectionFactory(getGatewayConnectionFactory());
304+
gateway.setConnectionFactory(getConnectionFactory());
300305
gateway.setRequestDestination(requestQueue5);
301306
gateway.setCorrelationKey("JMSCorrelationID");
302307
gateway.setUseReplyContainer(true);
308+
gateway.setComponentName("testContainerWithTemporary.gateway");
303309
gateway.afterPropertiesSet();
304310
gateway.start();
305311
final AtomicReference<Object> reply = new AtomicReference<Object>();
@@ -319,8 +325,8 @@ public void run() {
319325
});
320326
assertTrue(latch1.await(10, TimeUnit.SECONDS));
321327
JmsTemplate template = new JmsTemplate();
322-
template.setConnectionFactory(getTemplateConnectionFactory());
323-
template.setReceiveTimeout(5000);
328+
template.setConnectionFactory(getConnectionFactory());
329+
template.setReceiveTimeout(10000);
324330
javax.jms.Message request = template.receive(requestQueue5);
325331
assertNotNull(request);
326332
final javax.jms.Message jmsReply = request;
@@ -335,6 +341,7 @@ public Message createMessage(Session session) throws JMSException {
335341
assertNotNull(reply.get());
336342

337343
gateway.stop();
344+
scheduler.destroy();
338345
}
339346

340347
@Test
@@ -348,7 +355,7 @@ public void testContainerWithTemporaryNoCorrelation() throws Exception {
348355
.thenReturn(scheduler);
349356
final JmsOutboundGateway gateway = new JmsOutboundGateway();
350357
gateway.setBeanFactory(beanFactory);
351-
gateway.setConnectionFactory(getGatewayConnectionFactory());
358+
gateway.setConnectionFactory(getConnectionFactory());
352359
gateway.setRequestDestination(requestQueue6);
353360
gateway.setUseReplyContainer(true);
354361
gateway.afterPropertiesSet();
@@ -370,8 +377,8 @@ public void run() {
370377
});
371378
assertTrue(latch1.await(10, TimeUnit.SECONDS));
372379
JmsTemplate template = new JmsTemplate();
373-
template.setConnectionFactory(getTemplateConnectionFactory());
374-
template.setReceiveTimeout(5000);
380+
template.setConnectionFactory(getConnectionFactory());
381+
template.setReceiveTimeout(10000);
375382
javax.jms.Message request = template.receive(requestQueue6);
376383
assertNotNull(request);
377384
final javax.jms.Message jmsReply = request;
@@ -387,6 +394,7 @@ public Message createMessage(Session session) throws JMSException {
387394
assertNotNull(reply.get());
388395

389396
gateway.stop();
397+
scheduler.destroy();
390398
}
391399

392400
@Test
@@ -399,7 +407,7 @@ public void testLazyContainerWithDest() throws Exception {
399407
.thenReturn(scheduler);
400408
final JmsOutboundGateway gateway = new JmsOutboundGateway();
401409
gateway.setBeanFactory(beanFactory);
402-
gateway.setConnectionFactory(getGatewayConnectionFactory());
410+
gateway.setConnectionFactory(getConnectionFactory());
403411
gateway.setRequestDestination(requestQueue7);
404412
gateway.setReplyDestination(replyQueue7);
405413
gateway.setCorrelationKey("JMSCorrelationID");
@@ -413,7 +421,7 @@ public void testLazyContainerWithDest() throws Exception {
413421
@Override
414422
public void run() {
415423
JmsTemplate template = new JmsTemplate();
416-
template.setConnectionFactory(getTemplateConnectionFactory());
424+
template.setConnectionFactory(getConnectionFactory());
417425
template.setReceiveTimeout(20000);
418426
receiveAndSend(template);
419427
receiveAndSend(template);
@@ -451,16 +459,15 @@ public Message createMessage(Session session) throws JMSException {
451459

452460
gateway.stop();
453461
assertFalse(container.isRunning());
462+
scheduler.destroy();
454463
}
455464

456-
private ConnectionFactory getTemplateConnectionFactory() {
457-
ConnectionFactory amqConnectionFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
458-
return amqConnectionFactory;
459-
}
460-
461-
private ConnectionFactory getGatewayConnectionFactory() {
462-
ConnectionFactory amqConnectionFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
463-
return new CachingConnectionFactory(amqConnectionFactory);
465+
private ConnectionFactory getConnectionFactory() {
466+
ActiveMQConnectionFactory activeMQConnectionFactory =
467+
new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
468+
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(activeMQConnectionFactory);
469+
cachingConnectionFactory.setCacheConsumers(false);
470+
return cachingConnectionFactory;
464471
}
465472

466473
}

spring-integration-jms/src/test/java/org/springframework/integration/jms/SubscribableJmsChannelTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ public void dispatcherHasNoSubscribersTopic() throws Exception {
336336
List<String> logList = insertMockLoggerInListener(channel);
337337
listener.onMessage(new StubTextMessage("Hello, world!"));
338338
verifyLogReceived(logList);
339+
channel.destroy();
339340
}
340341

341342
private List<String> insertMockLoggerInListener(

spring-integration-jms/src/test/java/org/springframework/integration/jms/config/GlobalChannelInterceptorTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,7 +24,7 @@
2424

2525
import org.junit.Test;
2626

27-
import org.springframework.context.ApplicationContext;
27+
import org.springframework.context.ConfigurableApplicationContext;
2828
import org.springframework.context.support.ClassPathXmlApplicationContext;
2929
import org.springframework.integration.channel.AbstractMessageChannel;
3030
import org.springframework.integration.channel.ChannelInterceptorAware;
@@ -43,13 +43,14 @@ public class GlobalChannelInterceptorTests {
4343
@Test
4444
public void testJmsChannel() {
4545
ActiveMqTestUtils.prepare();
46-
ApplicationContext context = new ClassPathXmlApplicationContext(
46+
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
4747
"GlobalChannelInterceptorTests-context.xml", GlobalChannelInterceptorTests.class);
4848
ChannelInterceptorAware jmsChannel = context.getBean("jmsChannel", AbstractMessageChannel.class);
4949
List<ChannelInterceptor> interceptors = jmsChannel.getChannelInterceptors();
5050
assertNotNull(interceptors);
5151
assertEquals(1, interceptors.size());
5252
assertTrue(interceptors.get(0) instanceof SampleInterceptor);
53+
context.close();
5354
}
5455

5556

spring-integration-jms/src/test/java/org/springframework/integration/jms/config/JmsChannelHistoryTests.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.integration.jms.config;
1718

1819
import static org.junit.Assert.assertTrue;
@@ -26,7 +27,7 @@
2627
import org.mockito.invocation.InvocationOnMock;
2728
import org.mockito.stubbing.Answer;
2829

29-
import org.springframework.context.ApplicationContext;
30+
import org.springframework.context.ConfigurableApplicationContext;
3031
import org.springframework.context.support.ClassPathXmlApplicationContext;
3132
import org.springframework.integration.history.MessageHistory;
3233
import org.springframework.integration.jms.SubscribableJmsChannel;
@@ -40,6 +41,7 @@
4041
/**
4142
* @author Oleg Zhurakousky
4243
* @author Gunnar Hillert
44+
* @author Artem Bilan
4345
*
4446
*/
4547
public class JmsChannelHistoryTests {
@@ -70,12 +72,15 @@ public Object answer(InvocationOnMock invocation) {
7072
@Test
7173
public void testFullConfig() throws Exception{
7274
ActiveMqTestUtils.prepare();
73-
ApplicationContext ac = new ClassPathXmlApplicationContext("JmsChannelHistoryTests-context.xml", this.getClass());
75+
ConfigurableApplicationContext ac =
76+
new ClassPathXmlApplicationContext("JmsChannelHistoryTests-context.xml", this.getClass());
7477
SubscribableChannel channel = ac.getBean("jmsChannel", SubscribableChannel.class);
7578
PollableChannel resultChannel = ac.getBean("resultChannel", PollableChannel.class);
7679
channel.send(new GenericMessage<String>("hello"));
7780
Message<?> resultMessage = resultChannel.receive(5000);
7881
MessageHistory history = MessageHistory.read(resultMessage);
7982
assertTrue(history.get(0).contains("jmsChannel"));
83+
ac.close();
8084
}
85+
8186
}

spring-integration-jms/src/test/java/org/springframework/integration/jms/config/JmsDynamicDestinationTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@
3030
import org.springframework.messaging.MessageChannel;
3131
import org.springframework.messaging.PollableChannel;
3232
import org.springframework.integration.core.MessagingTemplate;
33+
import org.springframework.test.annotation.DirtiesContext;
3334
import org.springframework.test.context.ContextConfiguration;
3435
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3536

@@ -39,6 +40,7 @@
3940
*/
4041
@ContextConfiguration
4142
@RunWith(SpringJUnit4ClassRunner.class)
43+
@DirtiesContext
4244
public class JmsDynamicDestinationTests {
4345

4446
@Autowired

0 commit comments

Comments
 (0)