Skip to content

Commit

Permalink
INT-3470: Fix SF 4.1 Compatibility
Browse files Browse the repository at this point in the history
JIRA: https://jira.spring.io/browse/INT-3470

According to the commit spring-projects/spring-framework@c06ac06,
the `MessagingException` is now `NestedRuntimeException` including nested StackTrace.
Hence test-cases have to be changed to the `Mathers.containsString` instead of `equals` for the `e.getMessage()`

**Cherry-pick to the 4.0.x**
  • Loading branch information
artembilan authored and garyrussell committed Aug 12, 2014
1 parent 938d61f commit edbbcef
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@

package org.springframework.integration.amqp.channel;

import static org.junit.Assert.*;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.rabbitmq.client.AMQP.Queue.DeclareOk;
import com.rabbitmq.client.Channel;
import org.apache.commons.logging.Log;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
Expand All @@ -46,6 +50,9 @@
import org.springframework.integration.test.util.TestUtils;
import org.springframework.messaging.MessageDeliveryException;

import com.rabbitmq.client.AMQP.Queue.DeclareOk;
import com.rabbitmq.client.Channel;


/**
* @author Gary Russell
Expand All @@ -67,7 +74,8 @@ public void testPtP() throws Exception {
@Override
public Channel answer(InvocationOnMock invocation) throws Throwable {
return channel;
}}).when(connection).createChannel(anyBoolean());
}
}).when(connection).createChannel(anyBoolean());
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
when(connectionFactory.createConnection()).thenReturn(connection);
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
Expand All @@ -86,7 +94,8 @@ public Channel answer(InvocationOnMock invocation) throws Throwable {
fail("Exception expected");
}
catch (MessageDeliveryException e) {
assertEquals("Dispatcher has no subscribers for amqp-channel 'noSubscribersChannel'.", e.getMessage());
assertThat(e.getMessage(),
containsString("Dispatcher has no subscribers for amqp-channel 'noSubscribersChannel'."));
}
}

Expand All @@ -98,7 +107,8 @@ public void testPubSub() {
@Override
public Channel answer(InvocationOnMock invocation) throws Throwable {
return channel;
}}).when(connection).createChannel(anyBoolean());
}
}).when(connection).createChannel(anyBoolean());
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
when(connectionFactory.createConnection()).thenReturn(connection);
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
Expand All @@ -107,11 +117,12 @@ public Channel answer(InvocationOnMock invocation) throws Throwable {
final Queue queue = new Queue("noSubscribersQueue");
PublishSubscribeAmqpChannel amqpChannel = new PublishSubscribeAmqpChannel("noSubscribersChannel",
container, amqpTemplate) {
@Override
protected String obtainQueueName(AmqpAdmin admin,
String channelName) {
return queue.getName();
}};
@Override
protected String obtainQueueName(AmqpAdmin admin,
String channelName) {
return queue.getName();
}
};
amqpChannel.setBeanName("noSubscribersChannel");
amqpChannel.setBeanFactory(mock(BeanFactory.class));
amqpChannel.afterPropertiesSet();
Expand All @@ -137,7 +148,8 @@ public Object answer(InvocationOnMock invocation)
logList.add(message);
}
return null;
}}).when(logger).warn(anyString(), any(Exception.class));
}
}).when(logger).warn(anyString(), any(Exception.class));
when(logger.isWarnEnabled()).thenReturn(true);
Object listener = container.getMessageListener();
DirectFieldAccessor dfa = new DirectFieldAccessor(listener);
Expand All @@ -153,7 +165,8 @@ private void verifyLogReceived(final List<String> logList) {
assertNotNull("Failed to get expected exception", message);
if (message.startsWith("Dispatcher has no subscribers")) {
expectedExceptionFound = true;
assertEquals("Dispatcher has no subscribers for amqp-channel 'noSubscribersChannel'.", message);
assertThat(message,
containsString("Dispatcher has no subscribers for amqp-channel 'noSubscribersChannel'."));
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,14 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.integration.channel;

import static org.junit.Assert.assertEquals;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.messaging.MessageChannel;
Expand All @@ -31,6 +34,7 @@

/**
* @author Gary Russell
* @author Artem Bilan
* @since 2.1
*
*/
Expand All @@ -57,8 +61,10 @@ public void oneChannel() {
try {
noSubscribersChannel.send(new GenericMessage<String>("Hello, world!"));
fail("Exception expected");
} catch (MessagingException e) {
assertEquals("Dispatcher has no subscribers for channel 'foo.noSubscribersChannel'.", e.getMessage());
}
catch (MessagingException e) {
assertThat(e.getMessage(),
containsString("Dispatcher has no subscribers for channel 'foo.noSubscribersChannel'."));
}
}

Expand All @@ -67,8 +73,10 @@ public void stackedChannels() {
try {
subscribedChannel.send(new GenericMessage<String>("Hello, world!"));
fail("Exception expected");
} catch (MessagingException e) {
assertEquals("Dispatcher has no subscribers for channel 'foo.noSubscribersChannel'.", e.getMessage());
}
catch (MessagingException e) {
assertThat(e.getMessage(),
containsString("Dispatcher has no subscribers for channel 'foo.noSubscribersChannel'."));
}
}

Expand All @@ -79,8 +87,10 @@ public void withNoContext() {
try {
channel.send(new GenericMessage<String>("Hello, world!"));
fail("Exception expected");
} catch (MessagingException e) {
assertEquals("Dispatcher has no subscribers for channel 'bar'.", e.getMessage());
}
catch (MessagingException e) {
assertThat(e.getMessage(),
containsString("Dispatcher has no subscribers for channel 'bar'."));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.integration.channel.registry;

import static org.hamcrest.Matchers.allOf;
Expand Down Expand Up @@ -240,7 +241,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
}
catch (DestinationResolutionException e){
assertThat(e.getMessage(),
Matchers.equalTo("failed to look up MessageChannel with name 'foo' in the BeanFactory."));
Matchers.containsString("failed to look up MessageChannel with name 'foo' in the BeanFactory."));
}
}

Expand All @@ -262,7 +263,8 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
}
catch (DestinationResolutionException e){
assertThat(e.getMessage(),
Matchers.equalTo("failed to look up MessageChannel with name 'foo' in the BeanFactory (and there is no HeaderChannelRegistry present)."));
Matchers.containsString("failed to look up MessageChannel with name 'foo' in the BeanFactory " +
"(and there is no HeaderChannelRegistry present)."));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.integration.handler.advice;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -113,7 +115,7 @@ public void circuitBreakerExceptionText() {
fail("expected exception");
}
catch (RuntimeException e) {
assertThat(e.getMessage(), endsWith("(myService)]"));
assertThat(e.getMessage(), containsString("(myService)]"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

package org.springframework.integration.transformer;

import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -414,7 +416,7 @@ protected Object handleRequestMessage(Message<?> requestMessage) {
try {
enricher.handleMessage(requestMessage);
} catch (MessageHandlingException e) {
assertEquals("Failed to clone payload object", e.getMessage());
assertThat(e.getMessage(), containsString("Failed to clone payload object"));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

package org.springframework.integration.jms;

import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
Expand Down Expand Up @@ -307,7 +309,8 @@ public void dispatcherHasNoSubscribersQueue() throws Exception {
fail("Exception expected");
}
catch (MessageDeliveryException e) {
assertEquals("Dispatcher has no subscribers for jms-channel 'noSubscribersChannel'.", e.getMessage());
assertThat(e.getMessage(),
containsString("Dispatcher has no subscribers for jms-channel 'noSubscribersChannel'."));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
*/
package org.springframework.integration.mongodb.store;

import static org.junit.Assert.*;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.io.Serializable;
import java.util.Properties;
import java.util.UUID;

import com.mongodb.MongoClient;
import org.hamcrest.Matchers;
import org.junit.Test;

Expand All @@ -39,6 +43,8 @@
import org.springframework.messaging.support.ErrorMessage;
import org.springframework.messaging.support.GenericMessage;

import com.mongodb.MongoClient;

/**
*
* @author Mark Fisher
Expand Down Expand Up @@ -249,7 +255,8 @@ public void testInt3076ErrorMessage() throws Exception{
assertNotNull(retrievedMessage);
assertTrue(retrievedMessage instanceof ErrorMessage);
assertThat(retrievedMessage.getPayload(), Matchers.instanceOf(MessagingException.class));
assertEquals("intentional MessagingException", ((MessagingException) retrievedMessage.getPayload()).getMessage());
assertThat(((MessagingException) retrievedMessage.getPayload()).getMessage(),
containsString("intentional MessagingException"));
assertEquals(failedMessage, ((MessagingException) retrievedMessage.getPayload()).getFailedMessage());
assertEquals(messageToStore.getHeaders(), retrievedMessage.getHeaders());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
*/
package org.springframework.integration.redis.channel;

import static org.junit.Assert.assertEquals;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -104,8 +105,8 @@ public void dispatcherHasNoSubscribersTest() throws Exception{
catch (InvocationTargetException e) {
Throwable cause = e.getCause();
assertNotNull(cause);
assertEquals("Dispatcher has no subscribers for redis-channel 'si.test.channel.no.subs' (dhnsChannel).",
cause.getMessage());
assertThat(cause.getMessage(),
containsString("Dispatcher has no subscribers for redis-channel 'si.test.channel.no.subs' (dhnsChannel)."));
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,10 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.integration.rmi;

import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

import org.junit.Test;
Expand Down Expand Up @@ -78,7 +81,8 @@ public void testUgly() {
fail("Expected exception");
}
catch (Exception e) {
assertEquals("Dispatcher has no subscribers for channel 'context.baz'.", e.getCause().getMessage());
assertThat(e.getCause().getMessage(),
containsString("Dispatcher has no subscribers for channel 'context.baz'."));
}
}

Expand Down

0 comments on commit edbbcef

Please sign in to comment.