INT-3349 BeanFactory Propagation
JIRA: https://jira.spring.io/browse/INT-3349 Several FactoryBeans did not propagate the BeanFactory to their created object(s). Beans that create messages must have access to a bean factory to get the message builder factory. Fix the FactoryBeans and add a mock FB to all tests that need one. Add a runtime environment variable to make any infractions fatal. This should be set to `true` on CI builds and on framework developer environments.
This commit is contained in:
@@ -35,15 +35,16 @@ import org.mockito.Mockito;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.integration.channel.DirectChannel;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.messaging.MessageHandler;
|
||||
import org.springframework.messaging.PollableChannel;
|
||||
import org.springframework.messaging.support.ErrorMessage;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.integration.xmpp.core.XmppContextUtils;
|
||||
import org.springframework.messaging.MessageHandler;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.messaging.PollableChannel;
|
||||
import org.springframework.messaging.support.ErrorMessage;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
@@ -63,6 +64,7 @@ public class ChatMessageListeningEndpointTests {
|
||||
ChatMessageListeningEndpoint endpoint = new ChatMessageListeningEndpoint(connection);
|
||||
|
||||
doAnswer(new Answer<Object>() {
|
||||
@Override
|
||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||
packetListSet.add((PacketListener) invocation.getArguments()[0]);
|
||||
return null;
|
||||
@@ -70,6 +72,7 @@ public class ChatMessageListeningEndpointTests {
|
||||
}).when(connection).addPacketListener(Mockito.any(PacketListener.class), (PacketFilter) Mockito.any());
|
||||
|
||||
doAnswer(new Answer<Object>() {
|
||||
@Override
|
||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||
packetListSet.remove(invocation.getArguments()[0]);
|
||||
return null;
|
||||
@@ -78,6 +81,7 @@ public class ChatMessageListeningEndpointTests {
|
||||
|
||||
assertEquals(0, packetListSet.size());
|
||||
endpoint.setOutputChannel(new QueueChannel());
|
||||
endpoint.setBeanFactory(mock(BeanFactory.class));
|
||||
endpoint.afterPropertiesSet();
|
||||
endpoint.start();
|
||||
assertEquals(1, packetListSet.size());
|
||||
@@ -105,6 +109,7 @@ public class ChatMessageListeningEndpointTests {
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testNoXmppConnection(){
|
||||
ChatMessageListeningEndpoint endpoint = new ChatMessageListeningEndpoint();
|
||||
endpoint.setBeanFactory(mock(BeanFactory.class));
|
||||
endpoint.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@@ -123,6 +128,7 @@ public class ChatMessageListeningEndpointTests {
|
||||
|
||||
DirectChannel outChannel = new DirectChannel();
|
||||
outChannel.subscribe(new MessageHandler() {
|
||||
@Override
|
||||
public void handleMessage(org.springframework.messaging.Message<?> message)
|
||||
throws MessagingException {
|
||||
throw new RuntimeException("ooops");
|
||||
|
||||
@@ -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.
|
||||
@@ -38,20 +38,22 @@ import org.mockito.Mockito;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.integration.channel.DirectChannel;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.messaging.MessageHandler;
|
||||
import org.springframework.messaging.PollableChannel;
|
||||
import org.springframework.messaging.support.ErrorMessage;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.integration.xmpp.core.XmppContextUtils;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHandler;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.messaging.PollableChannel;
|
||||
import org.springframework.messaging.support.ErrorMessage;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
* @author Gunnar Hillert
|
||||
* @author Gary Russell
|
||||
*/
|
||||
public class PresenceListeningEndpointTests {
|
||||
|
||||
@@ -63,6 +65,7 @@ public class PresenceListeningEndpointTests {
|
||||
when(connection.getRoster()).thenReturn(roster);
|
||||
|
||||
doAnswer(new Answer<Object>() {
|
||||
@Override
|
||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||
rosterSet.add((RosterListener) invocation.getArguments()[0]);
|
||||
return null;
|
||||
@@ -70,6 +73,7 @@ public class PresenceListeningEndpointTests {
|
||||
}).when(roster).addRosterListener(Mockito.any(RosterListener.class));
|
||||
|
||||
doAnswer(new Answer<Object>() {
|
||||
@Override
|
||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||
rosterSet.remove(invocation.getArguments()[0]);
|
||||
return null;
|
||||
@@ -77,6 +81,7 @@ public class PresenceListeningEndpointTests {
|
||||
}).when(roster).removeRosterListener(Mockito.any(RosterListener.class));
|
||||
PresenceListeningEndpoint rosterEndpoint = new PresenceListeningEndpoint(connection);
|
||||
rosterEndpoint.setOutputChannel(new QueueChannel());
|
||||
rosterEndpoint.setBeanFactory(mock(BeanFactory.class));
|
||||
rosterEndpoint.afterPropertiesSet();
|
||||
assertEquals(0, rosterSet.size());
|
||||
rosterEndpoint.start();
|
||||
@@ -99,6 +104,7 @@ public class PresenceListeningEndpointTests {
|
||||
PresenceListeningEndpoint rosterEndpoint = new PresenceListeningEndpoint(connection);
|
||||
QueueChannel channel = new QueueChannel();
|
||||
rosterEndpoint.setOutputChannel(channel);
|
||||
rosterEndpoint.setBeanFactory(mock(BeanFactory.class));
|
||||
rosterEndpoint.afterPropertiesSet();
|
||||
rosterEndpoint.start();
|
||||
RosterListener rosterListener = (RosterListener) TestUtils.getPropertyValue(rosterEndpoint, "rosterListener");
|
||||
@@ -122,6 +128,7 @@ public class PresenceListeningEndpointTests {
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testNoXmppConnection() {
|
||||
PresenceListeningEndpoint handler = new PresenceListeningEndpoint();
|
||||
handler.setBeanFactory(mock(BeanFactory.class));
|
||||
handler.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@@ -140,6 +147,7 @@ public class PresenceListeningEndpointTests {
|
||||
|
||||
DirectChannel outChannel = new DirectChannel();
|
||||
outChannel.subscribe(new MessageHandler() {
|
||||
@Override
|
||||
public void handleMessage(org.springframework.messaging.Message<?> message)
|
||||
throws MessagingException {
|
||||
throw new RuntimeException("ooops");
|
||||
|
||||
@@ -27,14 +27,15 @@ import org.junit.Test;
|
||||
import org.mockito.ArgumentMatcher;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHandlingException;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.integration.xmpp.XmppHeaders;
|
||||
import org.springframework.integration.xmpp.core.XmppContextUtils;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHandlingException;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
@@ -48,6 +49,7 @@ public class ChatMessageSendingMessageHandlerTests {
|
||||
public void validateMessagePostAsString() throws Exception{
|
||||
XMPPConnection connection = mock(XMPPConnection.class);
|
||||
ChatMessageSendingMessageHandler handler = new ChatMessageSendingMessageHandler(connection);
|
||||
handler.setBeanFactory(mock(BeanFactory.class));
|
||||
handler.afterPropertiesSet();
|
||||
Message<?> message = MessageBuilder.withPayload("Test Message").
|
||||
setHeader(XmppHeaders.TO, "kermit@frog.com").
|
||||
@@ -56,7 +58,8 @@ public class ChatMessageSendingMessageHandlerTests {
|
||||
handler.handleMessage(message);
|
||||
|
||||
class EqualSmackMessage extends ArgumentMatcher<org.jivesoftware.smack.packet.Message> {
|
||||
public boolean matches(Object msg) {
|
||||
@Override
|
||||
public boolean matches(Object msg) {
|
||||
org.jivesoftware.smack.packet.Message smackMessage = (org.jivesoftware.smack.packet.Message) msg;
|
||||
boolean bodyMatches = smackMessage.getBody().equals("Test Message");
|
||||
boolean toMatches = smackMessage.getTo().equals("kermit@frog.com");
|
||||
@@ -73,7 +76,8 @@ public class ChatMessageSendingMessageHandlerTests {
|
||||
build();
|
||||
|
||||
class EqualSmackMessageWithThreadId extends ArgumentMatcher<org.jivesoftware.smack.packet.Message> {
|
||||
public boolean matches(Object msg) {
|
||||
@Override
|
||||
public boolean matches(Object msg) {
|
||||
org.jivesoftware.smack.packet.Message smackMessage = (org.jivesoftware.smack.packet.Message) msg;
|
||||
boolean bodyMatches = smackMessage.getBody().equals("Hello Kitty");
|
||||
boolean toMatches = smackMessage.getTo().equals("kermit@frog.com");
|
||||
@@ -92,6 +96,7 @@ public class ChatMessageSendingMessageHandlerTests {
|
||||
public void validateMessagePostAsSmackMessage() throws Exception{
|
||||
XMPPConnection connection = mock(XMPPConnection.class);
|
||||
ChatMessageSendingMessageHandler handler = new ChatMessageSendingMessageHandler(connection);
|
||||
handler.setBeanFactory(mock(BeanFactory.class));
|
||||
handler.afterPropertiesSet();
|
||||
|
||||
org.jivesoftware.smack.packet.Message smackMessage = new org.jivesoftware.smack.packet.Message("kermit@frog.com");
|
||||
@@ -141,6 +146,7 @@ public class ChatMessageSendingMessageHandlerTests {
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testNoXmppConnection(){
|
||||
ChatMessageSendingMessageHandler handler = new ChatMessageSendingMessageHandler();
|
||||
handler.setBeanFactory(mock(BeanFactory.class));
|
||||
handler.afterPropertiesSet();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,12 +22,13 @@ import static org.mockito.Mockito.mock;
|
||||
import org.jivesoftware.smack.XMPPConnection;
|
||||
import org.jivesoftware.smack.packet.Presence;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.messaging.MessageHandlingException;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.integration.xmpp.core.XmppContextUtils;
|
||||
import org.springframework.integration.xmpp.outbound.PresenceSendingMessageHandler;
|
||||
import org.springframework.messaging.MessageHandlingException;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
@@ -40,6 +41,7 @@ public class PresenceSendingMessageHandlerTests {
|
||||
@Test
|
||||
public void testPresencePayload(){
|
||||
PresenceSendingMessageHandler handler = new PresenceSendingMessageHandler(mock(XMPPConnection.class));
|
||||
handler.setBeanFactory(mock(BeanFactory.class));
|
||||
handler.afterPropertiesSet();
|
||||
handler.handleMessage(new GenericMessage(mock(Presence.class)));
|
||||
}
|
||||
@@ -48,6 +50,7 @@ public class PresenceSendingMessageHandlerTests {
|
||||
@Test(expected=MessageHandlingException.class)
|
||||
public void testWrongPayload(){
|
||||
PresenceSendingMessageHandler handler = new PresenceSendingMessageHandler(mock(XMPPConnection.class));
|
||||
handler.setBeanFactory(mock(BeanFactory.class));
|
||||
handler.afterPropertiesSet();
|
||||
handler.handleMessage(new GenericMessage(new Object()));
|
||||
}
|
||||
@@ -65,6 +68,7 @@ public class PresenceSendingMessageHandlerTests {
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testNoXmppConnection(){
|
||||
PresenceSendingMessageHandler handler = new PresenceSendingMessageHandler();
|
||||
handler.setBeanFactory(mock(BeanFactory.class));
|
||||
handler.afterPropertiesSet();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user