INT-3834: Upgrade to Smack-4.1.5

JIRA: https://jira.spring.io/browse/INT-3834
This commit is contained in:
Artem Bilan
2015-12-08 20:00:00 -05:00
committed by Gary Russell
parent 0fc1f555e7
commit 7da183f8ed
48 changed files with 430 additions and 383 deletions

View File

@@ -129,7 +129,7 @@ subprojects { subproject ->
slf4jVersion = "1.7.8"
tomcatVersion = "8.0.18"
smack3Version = '3.2.1'
smackVersion = '4.0.6'
smackVersion = '4.1.5'
springAmqpVersion = project.hasProperty('springAmqpVersion') ? project.springAmqpVersion : '1.5.2.RELEASE'
// springCloudClusterVersion = '1.0.0.BUILD-SNAPSHOT'
springDataJpaVersion = '1.8.2.RELEASE'
@@ -689,11 +689,10 @@ project('spring-integration-xmpp') {
description = 'Spring Integration XMPP Support'
dependencies {
compile project(":spring-integration-core")
compile("javax.activation:activation:$javaxActivationVersion", optional)
compile "org.igniterealtime.smack:smack-tcp:$smackVersion"
compile "org.igniterealtime.smack:smack-resolver-javax:$smackVersion"
compile "org.igniterealtime.smack:smack-java7:$smackVersion"
compile "org.igniterealtime.smack:smack-extensions:$smackVersion"
compile "org.springframework:spring-context-support:$springVersion"
testCompile project(":spring-integration-stream")
}
}

View File

@@ -48,6 +48,7 @@ public abstract class AbstractXmppInboundChannelAdapterParser extends AbstractCh
protected abstract String getBeanClassName(Element element);
@Override
protected AbstractBeanDefinition doParse(Element element, ParserContext parserContext, String channelName) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(getBeanClassName(element));

View File

@@ -16,6 +16,8 @@
package org.springframework.integration.xmpp.config;
import org.w3c.dom.Element;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
@@ -25,22 +27,20 @@ import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
import org.springframework.integration.xmpp.support.DefaultXmppHeaderMapper;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
/**
* Base class of XMPP outbound parsers
*
*
* @author Oleg Zhurakousky
* @since 2.0.1
*/
public abstract class AbstractXmppOutboundChannelAdapterParser extends AbstractOutboundChannelAdapterParser {
@Override
protected AbstractBeanDefinition parseConsumer(Element element, ParserContext parserContext) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(this.getHandlerClassName());
IntegrationNamespaceUtils.configureHeaderMapper(element, builder, parserContext, DefaultXmppHeaderMapper.class, null);
String connectionName = element.getAttribute("xmpp-connection");
if (StringUtils.hasText(connectionName)){
builder.addConstructorArgReference(connectionName);
@@ -53,9 +53,9 @@ public abstract class AbstractXmppOutboundChannelAdapterParser extends AbstractO
"'xmpp-connection' attribute or have default XMPP connection bean registered under the name 'xmppConnection'" +
"(e.g., <int-xmpp:xmpp-connection .../>). If 'id' is not provided the default will be 'xmppConnection'.");
}
return builder.getBeanDefinition();
}
protected abstract String getHandlerClassName();
}

View File

@@ -24,7 +24,7 @@ import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
/**
* Parser for the XMPP 'inbound-channel-adapter' element.
*
*
* @author Josh Long
* @author Oleg Zhurakousky
* @since 2.0

View File

@@ -19,7 +19,7 @@ package org.springframework.integration.xmpp.config;
/**
* Parser for the XMPP 'outbound-channel-adapter' element
*
*
* @author Oleg Zhurakousky
* @since 2.0
*/

View File

@@ -20,7 +20,7 @@ import org.w3c.dom.Element;
/**
* Parser for 'xmpp:presence-inbound-channel-adapter' element.
*
*
* @author Josh Long
* @author Oleg Zhurakousky
* @since 2.0
@@ -31,4 +31,5 @@ public class PresenceInboundChannelAdapterParser extends AbstractXmppInboundChan
protected String getBeanClassName(Element element) {
return "org.springframework.integration.xmpp.inbound.PresenceListeningEndpoint";
}
}

View File

@@ -19,7 +19,7 @@ package org.springframework.integration.xmpp.config;
/**
* Parser for 'xmpp:presence-outbound-channel-adapter' element
*
*
* @author Oleg Zhurakousky
* @since 2.0
*/

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
@@ -16,12 +16,13 @@
package org.springframework.integration.xmpp.config;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jxmpp.util.XmppStringUtils;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.context.SmartLifecycle;
@@ -29,46 +30,56 @@ import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* This class configures an {@link org.jivesoftware.smack.XMPPConnection} object.
* This class configures an {@link XMPPTCPConnection} object.
* This object is used for all scenarios to talk to a Smack server.
*
* @author Josh Long
* @author Mark Fisher
* @author Oleg Zhurakousky
* @author Florian Schmaus
* @author Artem Bilan
*
* @see org.jivesoftware.smack.XMPPConnection
* @see XMPPTCPConnection
* @since 2.0
*/
public class XmppConnectionFactoryBean extends AbstractFactoryBean<XMPPConnection> implements SmartLifecycle{
public class XmppConnectionFactoryBean extends AbstractFactoryBean<XMPPConnection> implements SmartLifecycle {
private final ConnectionConfiguration connectionConfiguration;
private final XMPPTCPConnectionConfiguration connectionConfiguration;
private volatile String resource = null; // server will generate resource if not provided
private final Object lifecycleMonitor = new Object();
private volatile String resource; // server will generate resource if not provided
private volatile String user;
private volatile String password;
private volatile String subscriptionMode = "accept_all";
private volatile String serviceName;
private volatile XMPPConnection connection;
private volatile String host;
private volatile int port = 5222;
private volatile Roster.SubscriptionMode subscriptionMode = Roster.getDefaultSubscriptionMode();
private volatile boolean autoStartup = true;
private volatile int phase = Integer.MIN_VALUE;
private final Object lifecycleMonitor = new Object();
private volatile boolean running;
private volatile XMPPTCPConnection connection;
public XmppConnectionFactoryBean(ConnectionConfiguration connectionConfiguration) {
public XmppConnectionFactoryBean() {
this.connectionConfiguration = null;
}
public XmppConnectionFactoryBean(XMPPTCPConnectionConfiguration connectionConfiguration) {
Assert.notNull(connectionConfiguration, "'connectionConfiguration' must not be null");
this.connectionConfiguration = connectionConfiguration;
}
public void setAutoStartup(boolean autoStartup) {
this.autoStartup = autoStartup;
}
@@ -77,10 +88,6 @@ public class XmppConnectionFactoryBean extends AbstractFactoryBean<XMPPConnectio
this.phase = phase;
}
public void setSubscriptionMode(String subscriptionMode) {
this.subscriptionMode = subscriptionMode;
}
public void setUser(String user) {
this.user = user;
}
@@ -93,6 +100,22 @@ public class XmppConnectionFactoryBean extends AbstractFactoryBean<XMPPConnectio
this.resource = resource;
}
public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}
public void setHost(String host) {
this.host = host;
}
public void setPort(int port) {
this.port = port;
}
public void setSubscriptionMode(Roster.SubscriptionMode subscriptionMode) {
this.subscriptionMode = subscriptionMode;
}
@Override
public Class<? extends XMPPConnection> getObjectType() {
return XMPPConnection.class;
@@ -100,7 +123,24 @@ public class XmppConnectionFactoryBean extends AbstractFactoryBean<XMPPConnectio
@Override
protected XMPPConnection createInstance() throws Exception {
this.connection = new XMPPTCPConnection(this.connectionConfiguration);
XMPPTCPConnectionConfiguration connectionConfiguration = this.connectionConfiguration;
if (this.connectionConfiguration == null) {
XMPPTCPConnectionConfiguration.Builder builder =
XMPPTCPConnectionConfiguration.builder()
.setHost(this.host)
.setPort(this.port)
.setResource(this.resource)
.setUsernameAndPassword(this.user, this.password)
.setServiceName(this.serviceName);
if (!StringUtils.hasText(this.serviceName) && StringUtils.hasText(this.user)) {
builder.setUsernameAndPassword(XmppStringUtils.parseLocalpart(this.user), this.password)
.setServiceName(XmppStringUtils.parseDomain(this.user));
}
connectionConfiguration = builder.build();
}
this.connection = new XMPPTCPConnection(connectionConfiguration);
return this.connection;
}
@@ -112,22 +152,15 @@ public class XmppConnectionFactoryBean extends AbstractFactoryBean<XMPPConnectio
try {
this.connection.connect();
this.connection.addConnectionListener(new LoggingConnectionListener());
if (StringUtils.hasText(this.user)) {
this.connection.login(this.user, this.password, this.resource);
Assert.isTrue(this.connection.isAuthenticated(), "Failed to authenticate user: " + this.user);
if (StringUtils.hasText(this.subscriptionMode)) {
Roster.SubscriptionMode subscriptionMode = Roster.SubscriptionMode.valueOf(this.subscriptionMode);
this.connection.getRoster().setSubscriptionMode(subscriptionMode);
}
}
else {
this.connection.loginAnonymously();
this.connection.login();
if (this.subscriptionMode != null) {
Roster.getInstanceFor(this.connection).setSubscriptionMode(this.subscriptionMode);
}
this.running = true;
}
catch (Exception e) {
throw new BeanInitializationException("failed to connect to XMPP service for "
+ this.connectionConfiguration.getServiceName(), e);
+ this.connection.getServiceName(), e);
}
}
}
@@ -135,18 +168,14 @@ public class XmppConnectionFactoryBean extends AbstractFactoryBean<XMPPConnectio
public void stop() {
synchronized (this.lifecycleMonitor) {
if (this.isRunning()) {
try {
this.connection.disconnect();
} catch (NotConnectedException e) {
// Ignore
}
this.connection.disconnect();
this.running = false;
}
}
}
public void stop(Runnable callback) {
this.stop();
stop();
callback.run();
}
@@ -178,7 +207,7 @@ public class XmppConnectionFactoryBean extends AbstractFactoryBean<XMPPConnectio
}
public void connectionClosedOnError(Exception e) {
logger.debug("Connection closed on error", e);
logger.debug("Connection closed on error", e);
}
public void connectionClosed() {
@@ -191,9 +220,10 @@ public class XmppConnectionFactoryBean extends AbstractFactoryBean<XMPPConnectio
}
@Override
public void authenticated(XMPPConnection connection) {
public void authenticated(XMPPConnection connection, boolean resumed) {
logger.debug("Connection authenticated");
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2015 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.
@@ -22,23 +22,19 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Parser for 'xmpp:xmpp-connection' element
*
* @author Oleg Zhurakousky
* @author Artem Bilan
* @since 2.0
*/
public class XmppConnectionParser extends AbstractSingleBeanDefinitionParser {
private static String[] connectionFactoryAttributes = new String[]{"user", "password", "resource","subscription-mode"};
@Override
protected String getBeanClassName(Element element) {
return "org.springframework.integration.xmpp.config.XmppConnectionFactoryBean";
protected Class<?> getBeanClass(Element element) {
return XmppConnectionFactoryBean.class;
}
@Override
@@ -49,27 +45,18 @@ public class XmppConnectionParser extends AbstractSingleBeanDefinitionParser {
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
String serviceName = element.getAttribute("service-name");
String host = element.getAttribute("host");
String port = element.getAttribute("port");
BeanDefinitionBuilder connectionConfigurationBuilder =
BeanDefinitionBuilder.genericBeanDefinition("org.jivesoftware.smack.ConnectionConfiguration");
if (StringUtils.hasText(host)) {
Assert.hasLength(port, "Port must be provided if 'host' is specified");
connectionConfigurationBuilder.addConstructorArgValue(host);
connectionConfigurationBuilder.addConstructorArgValue(port);
String user = element.getAttribute("user");
if (!StringUtils.hasText(serviceName) && !StringUtils.hasText(user)) {
parserContext.getReaderContext().error("One of 'service-name' or 'user' attributes is required", element);
}
else {
Assert.hasText(serviceName, "'serviceName' is requuired if 'host' is not provided");
}
if (StringUtils.hasText(serviceName)){
connectionConfigurationBuilder.addConstructorArgValue(serviceName);
}
for (String attribute : connectionFactoryAttributes) {
String[] attributes = {"user", "password", "resource", "subscription-mode", "host", "port", "service-name",
IntegrationNamespaceUtils.AUTO_STARTUP, IntegrationNamespaceUtils.PHASE};
for (String attribute : attributes) {
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, attribute);
}
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, IntegrationNamespaceUtils.AUTO_STARTUP);
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, IntegrationNamespaceUtils.PHASE);
builder.addConstructorArgValue(connectionConfigurationBuilder.getBeanDefinition());
}
}

View File

@@ -27,9 +27,10 @@ import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
* @since 2.0
*/
public class XmppNamespaceHandler extends NamespaceHandlerSupport {
public final static String XMPP_CONNECTION_BEAN_NAME = "xmppConnection";
@Override
public void init() {
// connection
registerBeanDefinitionParser("xmpp-connection", new XmppConnectionParser());
@@ -44,4 +45,5 @@ public class XmppNamespaceHandler extends NamespaceHandlerSupport {
registerBeanDefinitionParser("header-enricher", new XmppHeaderEnricherParser());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2015 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.
@@ -25,6 +25,7 @@ import org.springframework.util.Assert;
/**
* @author Oleg Zhurakousky
* @author Mark Fisher
* @author Artem Bilan
* @since 2.0
*/
public abstract class AbstractXmppConnectionAwareEndpoint extends MessageProducerSupport {
@@ -49,8 +50,9 @@ public abstract class AbstractXmppConnectionAwareEndpoint extends MessageProduce
if (this.xmppConnection == null && beanFactory != null) {
this.xmppConnection = beanFactory.getBean(XmppContextUtils.XMPP_CONNECTION_BEAN_NAME, XMPPConnection.class);
}
Assert.notNull(this.xmppConnection, "Failed to resolve XMPPConnection. XMPPConnection must either be set expicitly " +
"via the 'xmpp-connection' attribute or implicitly by registering a bean with the name 'xmppConnection' and of type " +
Assert.notNull(this.xmppConnection, "Failed to resolve XMPPConnection. " +
"XMPPConnection must either be set explicitly via constructor argument " +
"or implicitly by registering a bean with the name 'xmppConnection' and of type " +
"'org.jivesoftware.smack.XMPPConnection' in the Application Context.");
this.initialized = true;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2015 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.
@@ -17,12 +17,14 @@
package org.springframework.integration.xmpp.core;
import org.jivesoftware.smack.XMPPConnection;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.integration.handler.AbstractMessageHandler;
import org.springframework.util.Assert;
/**
* @author Oleg Zhurakousky
* @author Artem Bilan
* @since 2.0
*/
public abstract class AbstractXmppConnectionAwareMessageHandler extends AbstractMessageHandler {
@@ -44,10 +46,12 @@ public abstract class AbstractXmppConnectionAwareMessageHandler extends Abstract
protected void onInit() throws Exception {
BeanFactory beanFactory = this.getBeanFactory();
if (this.xmppConnection == null && beanFactory != null) {
this.xmppConnection = beanFactory.getBean(XmppContextUtils.XMPP_CONNECTION_BEAN_NAME, XMPPConnection.class);
this.xmppConnection =
beanFactory.getBean(XmppContextUtils.XMPP_CONNECTION_BEAN_NAME, XMPPConnection.class);
}
Assert.notNull(this.xmppConnection, "Failed to resolve XMPPConnection. XMPPConnection must either be set explicitly " +
"via 'xmpp-connection' attribute or implicitly by registering a bean with the name 'xmppConnection' and of type " +
Assert.notNull(this.xmppConnection, "Failed to resolve XMPPConnection. " +
"XMPPConnection must either be set explicitly via constructor argument " +
"or implicitly by registering a bean with the name 'xmppConnection' and of type " +
"'org.jivesoftware.smack.XMPPConnection' in the Application Context.");
this.initialized = true;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
@@ -18,9 +18,9 @@ package org.springframework.integration.xmpp.inbound;
import java.util.Map;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.StanzaListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Stanza;
import org.springframework.integration.support.AbstractIntegrationMessageBuilder;
import org.springframework.integration.xmpp.core.AbstractXmppConnectionAwareEndpoint;
@@ -36,13 +36,14 @@ import org.springframework.util.StringUtils;
* @author Josh Long
* @author Mark Fisher
* @author Oleg Zhurakousky
* @author Artem Bilan
* @since 2.0
*/
public class ChatMessageListeningEndpoint extends AbstractXmppConnectionAwareEndpoint {
private volatile boolean extractPayload = true;
private final PacketListener packetListener = new ChatMessagePublishingPacketListener();
private final StanzaListener stanzaListener = new ChatMessagePublishingStanzaListener();
private volatile XmppHeaderMapper headerMapper = new DefaultXmppHeaderMapper();
@@ -78,21 +79,21 @@ public class ChatMessageListeningEndpoint extends AbstractXmppConnectionAwareEnd
@Override
protected void doStart() {
Assert.isTrue(this.initialized, this.getComponentName() + " [" + this.getComponentType() + "] must be initialized");
this.xmppConnection.addPacketListener(this.packetListener, null);
this.xmppConnection.addAsyncStanzaListener(this.stanzaListener, null);
}
@Override
protected void doStop() {
if (this.xmppConnection != null) {
this.xmppConnection.removePacketListener(this.packetListener);
this.xmppConnection.removeAsyncStanzaListener(this.stanzaListener);
}
}
private class ChatMessagePublishingPacketListener implements PacketListener {
private class ChatMessagePublishingStanzaListener implements StanzaListener {
@Override
public void processPacket(final Packet packet) {
public void processPacket(final Stanza packet) {
if (packet instanceof org.jivesoftware.smack.packet.Message) {
org.jivesoftware.smack.packet.Message xmppMessage = (org.jivesoftware.smack.packet.Message) packet;
Map<String, ?> mappedHeaders = headerMapper.toHeadersFromRequest(xmppMessage);
@@ -117,6 +118,7 @@ public class ChatMessageListeningEndpoint extends AbstractXmppConnectionAwareEnd
}
}
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2015 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.
@@ -20,8 +20,8 @@ import java.util.Collection;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.roster.RosterListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Presence;
@@ -38,6 +38,7 @@ import org.springframework.util.StringUtils;
* @author Josh Long
* @author Oleg Zhurakousky
* @author Mark Fisher
* @author Artem Bilan
* @since 2.0
*/
public class PresenceListeningEndpoint extends AbstractXmppConnectionAwareEndpoint {
@@ -65,14 +66,14 @@ public class PresenceListeningEndpoint extends AbstractXmppConnectionAwareEndpoi
@Override
protected void doStart() {
Assert.isTrue(this.initialized, this.getComponentName() + " [" + this.getComponentType() + "] must be initialized");
Roster roster = this.xmppConnection.getRoster();
Roster roster = Roster.getInstanceFor(this.xmppConnection);
roster.addRosterListener(this.rosterListener);
}
@Override
protected void doStop() {
if (this.xmppConnection != null) {
this.xmppConnection.getRoster().removeRosterListener(this.rosterListener);
Roster.getInstanceFor(this.xmppConnection).removeRosterListener(this.rosterListener);
}
}

View File

@@ -1,6 +1,5 @@
/**
* Provides XMPP inbound Endpoint implementations that extend
* Provides XMPP inbound Endpoint implementations that extend
* {@link org.springframework.integration.xmpp.core.AbstractXmppConnectionAwareEndpoint}.
*/
package org.springframework.integration.xmpp.inbound;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
@@ -16,6 +16,7 @@
package org.springframework.integration.xmpp.outbound;
import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.XMPPConnection;
import org.springframework.integration.xmpp.XmppHeaders;
@@ -34,6 +35,7 @@ import org.springframework.util.StringUtils;
* @author Josh Long
* @author Mario Gray
* @author Oleg Zhurakousky
* @author Artem Bilan
* @since 2.0
*/
public class ChatMessageSendingMessageHandler extends AbstractXmppConnectionAwareMessageHandler {
@@ -61,7 +63,7 @@ public class ChatMessageSendingMessageHandler extends AbstractXmppConnectionAwar
@Override
protected void handleMessageInternal(Message<?> message) throws Exception {
Assert.isTrue(this.initialized, this.getComponentName() + "#" + this.getComponentType() + " must be initialized");
Assert.isTrue(this.initialized, getComponentName() + "#" + this.getComponentType() + " must be initialized");
Object messageBody = message.getPayload();
org.jivesoftware.smack.packet.Message xmppMessage = null;
if (messageBody instanceof org.jivesoftware.smack.packet.Message) {
@@ -77,14 +79,15 @@ public class ChatMessageSendingMessageHandler extends AbstractXmppConnectionAwar
xmppMessage.setBody((String) messageBody);
}
else {
throw new MessageHandlingException(message, "Only payloads of type java.lang.String or org.jivesoftware.smack.packet.Message " +
throw new MessageHandlingException(message,
"Only payloads of type java.lang.String or org.jivesoftware.smack.packet.Message " +
"are supported. Received [" + messageBody.getClass().getName() +
"]. Consider adding a Transformer prior to this adapter.");
}
if (!this.xmppConnection.isConnected()) {
this.xmppConnection.connect();
if (!this.xmppConnection.isConnected() && this.xmppConnection instanceof AbstractXMPPConnection) {
((AbstractXMPPConnection) this.xmppConnection).connect();
}
this.xmppConnection.sendPacket(xmppMessage);
this.xmppConnection.sendStanza(xmppMessage);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
@@ -16,6 +16,7 @@
package org.springframework.integration.xmpp.outbound;
import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Presence;
@@ -28,6 +29,7 @@ import org.springframework.util.Assert;
*
* @author Josh Long
* @author Oleg Zhurakousky
* @author Artem Bilan
* @since 2.0
*/
public class PresenceSendingMessageHandler extends AbstractXmppConnectionAwareMessageHandler {
@@ -47,14 +49,14 @@ public class PresenceSendingMessageHandler extends AbstractXmppConnectionAwareMe
@Override
protected void handleMessageInternal(Message<?> message) throws Exception {
Assert.isTrue(this.initialized, this.getComponentName() + " must be initialized");
Assert.state(this.initialized, this.getComponentName() + " must be initialized");
Object payload = message.getPayload();
Assert.isTrue(payload instanceof Presence,
Assert.state(payload instanceof Presence,
"Payload must be of type 'org.jivesoftware.smack.packet.Presence', was: " + payload.getClass().getName());
if (!this.xmppConnection.isConnected()) {
this.xmppConnection.connect();
if (!this.xmppConnection.isConnected() && this.xmppConnection instanceof AbstractXMPPConnection) {
((AbstractXMPPConnection) this.xmppConnection).connect();
}
this.xmppConnection.sendPacket((Presence) payload);
this.xmppConnection.sendStanza((Presence) payload);
}
}

View File

@@ -1,6 +1,6 @@
/**
* Provides XMPP outbound Messagehandler implementations.These components extend
* Provides XMPP outbound MessageHandler implementations.
* These components extend
* {@link org.springframework.integration.xmpp.core.AbstractXmppConnectionAwareMessageHandler}
*/
package org.springframework.integration.xmpp.outbound;

View File

@@ -1,5 +1,5 @@
/**
* Provides XMPP specific support classes. Right now you will find a
* Provides XMPP specific support classes. Right now you will find a
* {@link org.springframework.integration.xmpp.support.XmppHeaderMapper} implementation
* in this package.
*/

View File

@@ -21,21 +21,21 @@
</xsd:annotation>
<xsd:complexType>
<xsd:attribute name="id" type="xsd:string" default="xmppConnection"/>
<xsd:attribute name="user" type="xsd:string" use="required">
<xsd:attribute name="user" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
The user name (e.g., someuser@gmail.com) that will be used by this connection object
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="password" type="xsd:string" use="required">
<xsd:attribute name="password" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
The user's password
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="host" type="xsd:string" use="required">
<xsd:attribute name="host" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
The host name to connect TO

View File

@@ -0,0 +1,8 @@
log4j.rootCategory=WARN, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss.SSS} %-5p [%t][%c] %m%n
log4j.category.org.springframework.integration=WARN
log4j.category.org.springframework.integration.xmpp=INFO

View File

@@ -1,26 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
<logger name="org.springframework.integration">
<level value="trace" />
</logger>
<!-- <logger name="org.springframework.integration">-->
<!-- <level value="debug" />-->
<!-- </logger>-->
<!-- Root Logger -->
<root>
<priority value="warn" />
<appender-ref ref="console" />
</root>
</log4j:configuration>

View File

@@ -3,20 +3,11 @@
xmlns="http://www.springframework.org/schema/integration"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xmpp="http://www.springframework.org/schema/integration/xmpp"
xmlns:tool="http://www.springframework.org/schema/tool"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/integration/xmpp
http://www.springframework.org/schema/integration/xmpp/spring-integration-xmpp.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd">
http://www.springframework.org/schema/integration/xmpp/spring-integration-xmpp.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">
<beans:bean id="testConnection" class="org.mockito.Mockito" factory-method="mock">
<beans:constructor-arg value="org.jivesoftware.smack.XMPPConnection"/>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
@@ -22,14 +22,15 @@ import static org.junit.Assert.assertSame;
import java.lang.reflect.Field;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.StanzaListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smackx.jiveproperties.JivePropertiesManager;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
@@ -51,7 +52,7 @@ import org.springframework.util.ReflectionUtils;
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext(classMode=ClassMode.AFTER_EACH_TEST_METHOD)
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public class ChatMessageInboundChannelAdapterParserTests {
@Autowired
@@ -63,18 +64,19 @@ public class ChatMessageInboundChannelAdapterParserTests {
@Autowired
private MessageChannel autoChannel;
@Autowired @Qualifier("autoChannel.adapter")
@Autowired
@Qualifier("autoChannel.adapter")
private ChatMessageListeningEndpoint autoChannelAdapter;
@Test
public void testInboundAdapter(){
public void testInboundAdapter() {
ChatMessageListeningEndpoint adapter = context.getBean("xmppInboundAdapter", ChatMessageListeningEndpoint.class);
MessageChannel errorChannel = (MessageChannel) TestUtils.getPropertyValue(adapter, "errorChannel");
assertEquals(context.getBean("errorChannel"), errorChannel);
assertFalse(adapter.isAutoStartup());
QueueChannel channel = (QueueChannel) TestUtils.getPropertyValue(adapter, "outputChannel");
assertEquals("xmppInbound", channel.getComponentName());
XMPPConnection connection = (XMPPConnection)TestUtils.getPropertyValue(adapter, "xmppConnection");
XMPPConnection connection = (XMPPConnection) TestUtils.getPropertyValue(adapter, "xmppConnection");
assertEquals(connection, context.getBean("testConnection"));
}
@@ -88,14 +90,14 @@ public class ChatMessageInboundChannelAdapterParserTests {
xmppConnectionField.setAccessible(true);
ReflectionUtils.setField(xmppConnectionField, adapter, xmppConnection);
PacketListener packetListener = TestUtils.getPropertyValue(adapter, "packetListener", PacketListener.class);
StanzaListener stanzaListener = TestUtils.getPropertyValue(adapter, "stanzaListener", StanzaListener.class);
Message message = new Message();
message.setBody("hello");
message.setTo("oleg");
JivePropertiesManager.addProperty(message, "foo", "foo");
JivePropertiesManager.addProperty(message, "bar", "bar");
packetListener.processPacket(message);
stanzaListener.processPacket(message);
org.springframework.messaging.Message<?> siMessage = xmppInbound.receive(0);
assertEquals("foo", siMessage.getHeaders().get("foo"));
assertEquals("oleg", siMessage.getHeaders().get("xmpp_to"));
@@ -105,4 +107,5 @@ public class ChatMessageInboundChannelAdapterParserTests {
public void testAutoChannel() {
assertSame(autoChannel, TestUtils.getPropertyValue(autoChannelAdapter, "outputChannel"));
}
}

View File

@@ -6,40 +6,40 @@
http://www.springframework.org/schema/integration/xmpp http://www.springframework.org/schema/integration/xmpp/spring-integration-xmpp.xsd"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-xmpp="http://www.springframework.org/schema/integration/xmpp">
<bean id="testConnection" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.jivesoftware.smack.XMPPConnection"/>
</bean>
<int:channel id="outboundEventChannel"/>
<int-xmpp:outbound-channel-adapter id="outboundEventAdapter"
channel="outboundEventChannel"
<int-xmpp:outbound-channel-adapter id="outboundEventAdapter"
channel="outboundEventChannel"
xmpp-connection="testConnection"
mapped-request-headers="foo*, bar*"/>
<int:channel id="outboundPollingChannel">
<int:queue/>
</int:channel>
<int-xmpp:outbound-channel-adapter id="pollingConsumer"
channel="outboundPollingChannel"
<int-xmpp:outbound-channel-adapter id="pollingConsumer"
channel="outboundPollingChannel"
xmpp-connection="testConnection">
<int:poller fixed-rate="5000" max-messages-per-poll="1"/>
</int-xmpp:outbound-channel-adapter>
<int-xmpp:outbound-channel-adapter id="withHeaderMapper"
channel="outboundPollingChannel"
<int-xmpp:outbound-channel-adapter id="withHeaderMapper"
channel="outboundPollingChannel"
xmpp-connection="testConnection"
header-mapper="headerMapper">
<int:poller fixed-rate="5000" max-messages-per-poll="1"/>
</int-xmpp:outbound-channel-adapter>
<bean id="headerMapper" class="org.springframework.integration.xmpp.support.DefaultXmppHeaderMapper">
<property name="requestHeaderNames" value="foo*"/>
</bean>
<int-xmpp:outbound-channel-adapter id="outboundNoChannelAdapter"
<int-xmpp:outbound-channel-adapter id="outboundNoChannelAdapter"
xmpp-connection="testConnection">
</int-xmpp:outbound-channel-adapter>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
@@ -29,6 +29,7 @@ import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.integration.mapping.AbstractHeaderMapper;
@@ -110,7 +111,7 @@ public class ChatMessageOutboundChannelAdapterParserTests {
@SuppressWarnings("rawtypes")
@Test
public void withHeaderMapper() throws Exception{
public void withHeaderMapper() throws Exception {
Object pollingConsumer = context.getBean("withHeaderMapper");
assertTrue(pollingConsumer instanceof PollingConsumer);
assertEquals(headerMapper, TestUtils.getPropertyValue(pollingConsumer, "handler.headerMapper"));
@@ -120,40 +121,44 @@ public class ChatMessageOutboundChannelAdapterParserTests {
XMPPConnection connection = context.getBean("testConnection", XMPPConnection.class);
Mockito.doAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
org.jivesoftware.smack.packet.Message xmppMessage = (org.jivesoftware.smack.packet.Message) args[0];
assertEquals("oleg", xmppMessage.getTo());
assertEquals("foobar", JivePropertiesManager.getProperty(xmppMessage, "foobar"));
return null;
}})
.when(connection).sendPacket(Mockito.any(org.jivesoftware.smack.packet.Message.class));
public Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
org.jivesoftware.smack.packet.Message xmppMessage = (org.jivesoftware.smack.packet.Message) args[0];
assertEquals("oleg", xmppMessage.getTo());
assertEquals("foobar", JivePropertiesManager.getProperty(xmppMessage, "foobar"));
return null;
}
})
.when(connection).sendStanza(Mockito.any(org.jivesoftware.smack.packet.Message.class));
channel.send(message);
verify(connection, times(1)).sendPacket(Mockito.any(org.jivesoftware.smack.packet.Message.class));
verify(connection, times(1)).sendStanza(Mockito.any(org.jivesoftware.smack.packet.Message.class));
Mockito.reset(connection);
}
@SuppressWarnings("rawtypes")
@Test //INT-2275
public void testOutboundChannelAdapterInsideChain() throws Exception{
public void testOutboundChannelAdapterInsideChain() throws Exception {
MessageChannel channel = context.getBean("outboundChainChannel", MessageChannel.class);
Message<?> message = MessageBuilder.withPayload("hello").setHeader(XmppHeaders.TO, "artem").build();
XMPPConnection connection = context.getBean("testConnection", XMPPConnection.class);
Mockito.doAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
org.jivesoftware.smack.packet.Message xmppMessage = (org.jivesoftware.smack.packet.Message) args[0];
assertEquals("artem", xmppMessage.getTo());
assertEquals("hello", xmppMessage.getBody());
return null;
}})
.when(connection).sendPacket(Mockito.any(org.jivesoftware.smack.packet.Message.class));
}
}).when(connection).sendStanza(Mockito.any(org.jivesoftware.smack.packet.Message.class));
channel.send(message);
verify(connection, times(1)).sendPacket(Mockito.any(org.jivesoftware.smack.packet.Message.class));
verify(connection, times(1)).sendStanza(Mockito.any(org.jivesoftware.smack.packet.Message.class));
Mockito.reset(connection);
}
@@ -166,4 +171,5 @@ public class ChatMessageOutboundChannelAdapterParserTests {
}
}
}

View File

@@ -6,14 +6,14 @@
http://www.springframework.org/schema/integration/xmpp http://www.springframework.org/schema/integration/xmpp/spring-integration-xmpp.xsd"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-xmpp="http://www.springframework.org/schema/integration/xmpp">
<bean id="testConnection" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.jivesoftware.smack.XMPPConnection"/>
</bean>
<int-xmpp:presence-inbound-channel-adapter channel="outChannel"
<int-xmpp:presence-inbound-channel-adapter channel="outChannel"
xmpp-connection="testConnection" auto-startup="false"/>
<int:channel id="outChannel"/>
</beans>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2015 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.
@@ -28,8 +28,8 @@ public class PresenceInboundChannelAdapterParserTests {
@Test
public void testPresenceInboundChannelAdapterParser() {
new ClassPathXmlApplicationContext("PresenceInboundChannelAdapterParserTests-context.xml", this.getClass());
// no assertion needed. THe fact that no exception was thrown satisfies this test
new ClassPathXmlApplicationContext("PresenceInboundChannelAdapterParserTests-context.xml", this.getClass()).close();
// no assertion needed. The fact that no exception was thrown satisfies this test
}
}

View File

@@ -6,43 +6,43 @@
http://www.springframework.org/schema/integration/xmpp http://www.springframework.org/schema/integration/xmpp/spring-integration-xmpp.xsd"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-xmpp="http://www.springframework.org/schema/integration/xmpp">
<bean id="testConnection" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.jivesoftware.smack.XMPPConnection"/>
</bean>
<int:channel id="pollingChannel">
<int:queue/>
</int:channel>
<int-xmpp:presence-outbound-channel-adapter id="pollingOutboundRosterAdapter"
xmpp-connection="testConnection"
<int-xmpp:presence-outbound-channel-adapter id="pollingOutboundRosterAdapter"
xmpp-connection="testConnection"
channel="pollingChannel"
order="23">
<int:poller fixed-rate="1000" max-messages-per-poll="1"/>
</int-xmpp:presence-outbound-channel-adapter>
<int:channel id="eventChannel"/>
<int-xmpp:presence-outbound-channel-adapter id="eventOutboundRosterAdapter"
xmpp-connection="testConnection"
<int-xmpp:presence-outbound-channel-adapter id="eventOutboundRosterAdapter"
xmpp-connection="testConnection"
channel="eventChannel"
order="34"/>
<int-xmpp:presence-outbound-channel-adapter id="advised"
xmpp-connection="testConnection"
<int-xmpp:presence-outbound-channel-adapter id="advised"
xmpp-connection="testConnection"
channel="eventChannel"
order="34">
<int-xmpp:request-handler-advice-chain>
<bean class="org.springframework.integration.xmpp.config.PresenceOutboundChannelAdapterParserTests$FooAdvice" />
</int-xmpp:request-handler-advice-chain>
</int-xmpp:presence-outbound-channel-adapter>
<int-xmpp:presence-outbound-channel-adapter id="eventOutboundRosterChannel"
<int-xmpp:presence-outbound-channel-adapter id="eventOutboundRosterChannel"
xmpp-connection="testConnection"
order="45"/>
</beans>

View File

@@ -101,4 +101,5 @@ public class PresenceOutboundChannelAdapterParserTests {
}
}
}

View File

@@ -1,24 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/xmpp http://www.springframework.org/schema/integration/xmpp/spring-integration-xmpp.xsd"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-xmpp="http://www.springframework.org/schema/integration/xmpp">
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-xmpp="http://www.springframework.org/schema/integration/xmpp">
<bean id="xmppConnectionConfigurationBuilder" class="org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration"
factory-method="builder">
<property name="serviceName" value="myServiceName"/>
</bean>
<bean id="xmppConnection" class="org.springframework.integration.xmpp.config.XmppConnectionFactoryBean">
<constructor-arg>
<bean class="org.jivesoftware.smack.ConnectionConfiguration">
<constructor-arg value="myServiceName"/>
</bean>
<bean factory-bean="xmppConnectionConfigurationBuilder" factory-method="build"/>
</constructor-arg>
<property name="autoStartup" value="false"/>
</bean>
<int:channel id="outboundEventChannel"/>
<int-xmpp:outbound-channel-adapter id="outboundEventAdapter"
channel="outboundEventChannel"
xmpp-connection="xmppConnection"/>
<int-xmpp:outbound-channel-adapter id="outboundEventAdapter"
channel="outboundEventChannel"
xmpp-connection="xmppConnection"/>
</beans>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 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.
@@ -19,11 +19,11 @@ package org.springframework.integration.xmpp.config;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.xmpp.config.XmppConnectionFactoryBean;
/**
* @author Oleg Zhurakousky
@@ -33,14 +33,15 @@ public class XmppConnectionFactoryBeanTests {
@Test
public void testXmppConnectionFactoryBean() throws Exception {
XmppConnectionFactoryBean xmppConnectionFactoryBean = new XmppConnectionFactoryBean(mock(ConnectionConfiguration.class));
XmppConnectionFactoryBean xmppConnectionFactoryBean =
new XmppConnectionFactoryBean(mock(XMPPTCPConnectionConfiguration.class));
XMPPConnection connection = xmppConnectionFactoryBean.createInstance();
assertNotNull(connection);
}
@Test
public void testXmppConnectionFactoryBeanViaConfig() throws Exception {
new ClassPathXmlApplicationContext("XmppConnectionFactoryBeanTests-context.xml", this.getClass());
new ClassPathXmlApplicationContext("XmppConnectionFactoryBeanTests-context.xml", this.getClass()).close();
// the fact that no exception was thrown satisfies this test
}

View File

@@ -1,12 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-xmpp="http://www.springframework.org/schema/integration/xmpp"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/integration/xmpp http://www.springframework.org/schema/integration/xmpp/spring-integration-xmpp.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
@@ -17,10 +15,10 @@
<prop key="sub.mode">reject_all</prop>
</util:properties>
<int-xmpp:xmpp-connection id="connection"
user="happy.user"
password="blah"
host="localhost"
<int-xmpp:xmpp-connection id="connection"
user="happy.user"
password="blah"
host="localhost"
auto-startup="false"
resource="SpringSource"
port="6222"

View File

@@ -1,18 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/xmpp http://www.springframework.org/schema/integration/xmpp/spring-integration-xmpp.xsd"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-xmpp="http://www.springframework.org/schema/integration/xmpp">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int-xmpp="http://www.springframework.org/schema/integration/xmpp"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/xmpp http://www.springframework.org/schema/integration/xmpp/spring-integration-xmpp.xsd">
<int-xmpp:xmpp-connection id="connection"
user="happy.user" password="blah" host="localhost" auto-startup="false"/>
<int-xmpp:xmpp-connection user="happy.user" password="blah" host="localhost" auto-startup="false"/>
<int-xmpp:xmpp-connection id="connection"
user="happy.user@my.domain" password="blah" host="localhost" auto-startup="false"/>
<int-xmpp:xmpp-connection id="connectionWithResource" resource="Smack"
user="happy.user" password="blah" host="localhost" auto-startup="false"/>
<int-xmpp:xmpp-connection user="happy.user@my.domain" password="blah" host="localhost" auto-startup="false"/>
<int-xmpp:xmpp-connection id="connectionWithResource" resource="Smack" service-name="my.domain"
user="happy.user" password="blah" host="localhost" auto-startup="false"/>
</beans>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
@@ -18,12 +18,13 @@ package org.springframework.integration.xmpp.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.jivesoftware.smack.XMPPConnection;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.test.util.TestUtils;
@@ -32,36 +33,39 @@ import org.springframework.integration.test.util.TestUtils;
* @author Gary Russell
* @author Gunnar Hillert
* @author Florian Schmaus
* @author Artem Bilan
*/
public class XmppConnectionParserTests {
@Test
public void testSimpleConfiguration() {
ApplicationContext ac =
ConfigurableApplicationContext ac =
new ClassPathXmlApplicationContext("XmppConnectionParserTests-simple.xml", this.getClass());
XMPPConnection connection = ac.getBean("connection", XMPPConnection.class);
assertEquals("localhost", connection.getServiceName());
assertEquals("my.domain", connection.getServiceName());
assertFalse(connection.isConnected());
XmppConnectionFactoryBean xmppFb = ac.getBean("&connection", XmppConnectionFactoryBean.class);
assertEquals("happy.user", TestUtils.getPropertyValue(xmppFb, "user"));
assertEquals("happy.user@my.domain", TestUtils.getPropertyValue(xmppFb, "user"));
assertEquals("blah", TestUtils.getPropertyValue(xmppFb, "password"));
assertNull(TestUtils.getPropertyValue(xmppFb, "resource"));
assertEquals("accept_all", TestUtils.getPropertyValue(xmppFb, "subscriptionMode"));
assertEquals("accept_all", TestUtils.getPropertyValue(xmppFb, "subscriptionMode").toString());
xmppFb = ac.getBean("&connectionWithResource", XmppConnectionFactoryBean.class);
assertEquals("Smack", TestUtils.getPropertyValue(xmppFb, "resource"));
ac.close();
}
@Test
public void testDefaultConnectionName() {
ApplicationContext ac =
ConfigurableApplicationContext ac =
new ClassPathXmlApplicationContext("XmppConnectionParserTests-simple.xml", this.getClass());
assertTrue(ac.containsBean("xmppConnection"));
ac.close();
}
@Test
public void testCompleteConfiguration() {
ApplicationContext ac =
ConfigurableApplicationContext ac =
new ClassPathXmlApplicationContext("XmppConnectionParserTests-complete.xml", this.getClass());
XMPPConnection connection = ac.getBean("connection", XMPPConnection.class);
assertEquals("foogle.com", connection.getServiceName());
@@ -70,7 +74,8 @@ public class XmppConnectionParserTests {
assertEquals("happy.user", TestUtils.getPropertyValue(xmppFb, "user"));
assertEquals("blah", TestUtils.getPropertyValue(xmppFb, "password"));
assertEquals("SpringSource", TestUtils.getPropertyValue(xmppFb, "resource"));
assertEquals("reject_all", TestUtils.getPropertyValue(xmppFb, "subscriptionMode"));
assertEquals("reject_all", TestUtils.getPropertyValue(xmppFb, "subscriptionMode").toString());
ac.close();
}
}

View File

@@ -1,23 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
xmlns="http://www.springframework.org/schema/integration"
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xmpp="http://www.springframework.org/schema/integration/xmpp"
xsi:schemaLocation="
http://www.springframework.org/schema/integration/xmpp http://www.springframework.org/schema/integration/xmpp/spring-integration-xmpp.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/beans/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">
<channel id="input"/>
<channel id="output"/>
<xmpp:header-enricher input-channel="input" output-channel="output">
<xmpp:chat-to value="test1@example.org"/>
<xmpp:chat-to value="test1@example.org"/>
</xmpp:header-enricher>
</beans:beans>

View File

@@ -21,16 +21,14 @@
<xmpp:xmpp-connection
id="testConnection"
user="${user.1.login}"
password="${user.1.password}"
host="${user.1.host}"
service-name="${user.1.service}"/>
password="${user.1.password}"/>
<console:stdout-channel-adapter id="stdout" append-newline="true"/>
<console:stdin-channel-adapter id="stdin">
<poller fixed-rate="1000"/>
</console:stdin-channel-adapter>
<xmpp:header-enricher input-channel="stdin" output-channel="messagesToSend">
<xmpp:chat-to value="${user.2.login}"/>
</xmpp:header-enricher>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2015 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.
@@ -19,17 +19,24 @@ package org.springframework.integration.xmpp.ignore;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
* @author Artem Bilan
* @since 2.0
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class ConsoleChatTests {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("ConsoleChatTests-context.xml", ConsoleChatTests.class);
}
@Test
@Ignore
public void run() throws Exception {

View File

@@ -10,11 +10,11 @@
xmlns:tool="http://www.springframework.org/schema/tool"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/integration/xmpp
http://www.springframework.org/schema/integration/xmpp/spring-integration-xmpp.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/xmpp/spring-integration-xmpp.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd">

View File

@@ -26,14 +26,14 @@
service-name="${user.1.service}"/>
<xmpp:presence-inbound-channel-adapter channel="inboundPresenceChannel" xmpp-connection="testConnection"/>
<channel id="inboundPresenceChannel"/>
<service-activator input-channel="inboundPresenceChannel" output-channel="stdOutChannel"
<service-activator input-channel="inboundPresenceChannel" output-channel="stdOutChannel"
expression="'Presence event from: ' + payload.getFrom() + ' - ' + payload.getMode()"/>
<channel id="stdOutChannel"/>
<stream:stdout-channel-adapter channel="stdOutChannel" append-newline="true"/>
</beans:beans>

View File

@@ -5,10 +5,10 @@
xmlns:util="http://www.springframework.org/schema/util" xmlns:xmpp="http://www.springframework.org/schema/integration/xmpp"
xmlns:tool="http://www.springframework.org/schema/tool" xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/integration/xmpp http://www.springframework.org/schema/integration/xmpp/spring-integration-xmpp.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd">

View File

@@ -24,7 +24,7 @@
password="${user.1.password}"
host="${user.1.host}"
service-name="${user.1.service}"/>
<channel id="outboundPresenceChannel"/>
<xmpp:presence-outbound-channel-adapter xmpp-connection="testConnection" channel="outboundPresenceChannel"/>

View File

@@ -16,9 +16,9 @@
password="${user.1.password}"
host="${user.1.host}"
service-name="${user.1.service}"/>
<int:channel id="xmppInput"/>
<int-xmpp:outbound-channel-adapter channel="xmppInput"/>
</beans>

View File

@@ -35,14 +35,14 @@ public class SmackMessageSampleTests {
public void validateSmackMessageSent(){
ApplicationContext ac = new ClassPathXmlApplicationContext("SmackMessageSampleTest-context.xml", this.getClass());
MessageChannel xmppInput = ac.getBean("xmppInput", MessageChannel.class);
org.jivesoftware.smack.packet.Message smackMessage = new org.jivesoftware.smack.packet.Message("springintegration@gmail.com");
smackMessage.setBody("Message sent as Smack Message");
Message<org.jivesoftware.smack.packet.Message> message =
Message<org.jivesoftware.smack.packet.Message> message =
new GenericMessage<org.jivesoftware.smack.packet.Message>(smackMessage);
xmppInput.send(message);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
@@ -24,13 +24,12 @@ import static org.mockito.Mockito.mock;
import java.util.HashSet;
import java.util.Set;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.StanzaListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.StanzaFilter;
import org.jivesoftware.smack.packet.Message;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
@@ -50,35 +49,39 @@ import org.springframework.messaging.support.ErrorMessage;
* @author Oleg Zhurakousky
* @author Gunnar Hillert
* @author Florian Schmaus
*
* @author Artem Bilan
*/
public class ChatMessageListeningEndpointTests {
@Test
/**
* Should add/remove PacketListener when endpoint started/stopped
* Should add/remove StanzaListener when endpoint started/stopped
*/
public void testLifecycle(){
final Set<PacketListener> packetListSet = new HashSet<PacketListener>();
public void testLifecycle() {
final Set<StanzaListener> packetListSet = new HashSet<StanzaListener>();
XMPPConnection connection = mock(XMPPConnection.class);
ChatMessageListeningEndpoint endpoint = new ChatMessageListeningEndpoint(connection);
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
packetListSet.add((PacketListener) invocation.getArguments()[0]);
packetListSet.add((StanzaListener) invocation.getArguments()[0]);
return null;
}
}).when(connection).addPacketListener(Mockito.any(PacketListener.class), (PacketFilter) Mockito.any());
}).when(connection).addAsyncStanzaListener(Mockito.any(StanzaListener.class), Mockito.any(StanzaFilter.class));
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
packetListSet.remove(invocation.getArguments()[0]);
packetListSet.remove((StanzaListener) invocation.getArguments()[0]);
return null;
}
}).when(connection).removePacketListener(Mockito.any(PacketListener.class));
}).when(connection).removeAsyncStanzaListener(Mockito.any(StanzaListener.class));
assertEquals(0, packetListSet.size());
endpoint.setOutputChannel(new QueueChannel());
@@ -90,32 +93,32 @@ public class ChatMessageListeningEndpointTests {
assertEquals(0, packetListSet.size());
}
@Test(expected=IllegalArgumentException.class)
public void testNonInitializationFailure(){
@Test(expected = IllegalArgumentException.class)
public void testNonInitializationFailure() {
ChatMessageListeningEndpoint endpoint = new ChatMessageListeningEndpoint(mock(XMPPConnection.class));
endpoint.start();
}
@Test
public void testWithImplicitXmppConnection(){
public void testWithImplicitXmppConnection() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.registerSingleton(XmppContextUtils.XMPP_CONNECTION_BEAN_NAME, mock(XMPPConnection.class));
ChatMessageListeningEndpoint endpoint = new ChatMessageListeningEndpoint();
endpoint.setBeanFactory(bf);
endpoint.setOutputChannel(new QueueChannel());
endpoint.afterPropertiesSet();
assertNotNull(TestUtils.getPropertyValue(endpoint,"xmppConnection"));
assertNotNull(TestUtils.getPropertyValue(endpoint, "xmppConnection"));
}
@Test(expected=IllegalArgumentException.class)
public void testNoXmppConnection(){
@Test(expected = IllegalArgumentException.class)
public void testNoXmppConnection() {
ChatMessageListeningEndpoint endpoint = new ChatMessageListeningEndpoint();
endpoint.setBeanFactory(mock(BeanFactory.class));
endpoint.afterPropertiesSet();
}
@Test
public void testWithErrorChannel() throws NotConnectedException{
public void testWithErrorChannel() throws NotConnectedException {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
XMPPConnection connection = mock(XMPPConnection.class);
bf.registerSingleton(XmppContextUtils.XMPP_CONNECTION_BEAN_NAME, connection);
@@ -124,25 +127,28 @@ 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");
}
});
PollableChannel errorChannel = new QueueChannel();
endpoint.setBeanFactory(bf);
endpoint.setOutputChannel(outChannel);
endpoint.setErrorChannel(errorChannel);
endpoint.afterPropertiesSet();
PacketListener listener = (PacketListener) TestUtils.getPropertyValue(endpoint, "packetListener");
StanzaListener listener = (StanzaListener) TestUtils.getPropertyValue(endpoint, "stanzaListener");
Message smackMessage = new Message("kermit@frog.com");
smackMessage.setBody("hello");
smackMessage.setThread("1234");
listener.processPacket(smackMessage);
ErrorMessage msg =
(ErrorMessage) errorChannel.receive();
assertEquals("hello", ((MessagingException)msg.getPayload()).getFailedMessage().getPayload());
(ErrorMessage) errorChannel.receive();
assertEquals("hello", ((MessagingException) msg.getPayload()).getFailedMessage().getPayload());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
@@ -18,19 +18,20 @@ package org.springframework.integration.xmpp.inbound;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Mode;
import org.jivesoftware.smack.packet.Presence.Type;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.roster.RosterListener;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
@@ -52,30 +53,37 @@ import org.springframework.messaging.support.ErrorMessage;
* @author Oleg Zhurakousky
* @author Gunnar Hillert
* @author Gary Russell
* @author Artem Bilan
*/
public class PresenceListeningEndpointTests {
@Test
@SuppressWarnings("unchecked")
public void testEndpointLifecycle() {
final Set<RosterListener> rosterSet = new HashSet<RosterListener>();
XMPPConnection connection = mock(XMPPConnection.class);
Roster roster = mock(Roster.class);
when(connection.getRoster()).thenReturn(roster);
Map<XMPPConnection, Roster> instances = TestUtils.getPropertyValue(roster, "INSTANCES", Map.class);
instances.put(connection, roster);
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
rosterSet.add((RosterListener) invocation.getArguments()[0]);
return null;
}
}).when(roster).addRosterListener(Mockito.any(RosterListener.class));
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
rosterSet.remove(invocation.getArguments()[0]);
rosterSet.remove((RosterListener) invocation.getArguments()[0]);
return null;
}
}).when(roster).removeRosterListener(Mockito.any(RosterListener.class));
PresenceListeningEndpoint rosterEndpoint = new PresenceListeningEndpoint(connection);
rosterEndpoint.setOutputChannel(new QueueChannel());
@@ -88,17 +96,19 @@ public class PresenceListeningEndpointTests {
assertEquals(0, rosterSet.size());
}
@Test(expected=IllegalArgumentException.class)
@Test(expected = IllegalArgumentException.class)
public void testNonInitializedFailure() {
PresenceListeningEndpoint rosterEndpoint = new PresenceListeningEndpoint(mock(XMPPConnection.class));
rosterEndpoint.start();
}
@Test
@SuppressWarnings("unchecked")
public void testRosterPresenceChangeEvent() {
XMPPConnection connection = mock(XMPPConnection.class);
Roster roster = mock(Roster.class);
when(connection.getRoster()).thenReturn(roster);
Map<XMPPConnection, Roster> instances = TestUtils.getPropertyValue(roster, "INSTANCES", Map.class);
instances.put(connection, roster);
PresenceListeningEndpoint rosterEndpoint = new PresenceListeningEndpoint(connection);
QueueChannel channel = new QueueChannel();
rosterEndpoint.setOutputChannel(channel);
@@ -120,10 +130,10 @@ public class PresenceListeningEndpointTests {
endpoint.setBeanFactory(bf);
endpoint.setOutputChannel(new QueueChannel());
endpoint.afterPropertiesSet();
assertNotNull(TestUtils.getPropertyValue(endpoint,"xmppConnection"));
assertNotNull(TestUtils.getPropertyValue(endpoint, "xmppConnection"));
}
@Test(expected=IllegalArgumentException.class)
@Test(expected = IllegalArgumentException.class)
public void testNoXmppConnection() {
PresenceListeningEndpoint handler = new PresenceListeningEndpoint();
handler.setBeanFactory(mock(BeanFactory.class));
@@ -131,7 +141,7 @@ public class PresenceListeningEndpointTests {
}
@Test
public void testWithErrorChannel(){
public void testWithErrorChannel() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
XMPPConnection connection = mock(XMPPConnection.class);
bf.registerSingleton(XmppContextUtils.XMPP_CONNECTION_BEAN_NAME, connection);
@@ -140,11 +150,13 @@ 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");
}
});
PollableChannel errorChannel = new QueueChannel();
endpoint.setBeanFactory(bf);
@@ -157,7 +169,10 @@ public class PresenceListeningEndpointTests {
listener.presenceChanged(presence);
ErrorMessage msg =
(ErrorMessage) errorChannel.receive();
assertEquals(Type.available.toString(), ((MessagingException)msg.getPayload()).getFailedMessage().getPayload().toString());
(ErrorMessage) errorChannel.receive();
assertSame(presence, ((MessagingException) msg.getPayload())
.getFailedMessage()
.getPayload());
}
}

View File

@@ -40,60 +40,62 @@ import org.springframework.messaging.support.GenericMessage;
/**
* @author Oleg Zhurakousky
* @author Gunnar Hillert
*
* @author Artem Bilan
*/
public class ChatMessageSendingMessageHandlerTests {
@Test
public void validateMessagePostAsString() throws Exception{
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").
build();
setHeader(XmppHeaders.TO, "kermit@frog.com").
build();
// first Message new
handler.handleMessage(message);
class EqualSmackMessage extends ArgumentMatcher<org.jivesoftware.smack.packet.Message> {
@Override
@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");
return bodyMatches & toMatches;
}
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");
return bodyMatches & toMatches;
}
}
verify(connection, times(1)).sendPacket(Mockito.argThat(new EqualSmackMessage()));
verify(connection, times(1)).sendStanza(Mockito.argThat(new EqualSmackMessage()));
// assuming we know thread ID although currently we do not provide this capability
message = MessageBuilder.withPayload("Hello Kitty").
setHeader(XmppHeaders.TO, "kermit@frog.com").
setHeader(XmppHeaders.THREAD, "123").
build();
setHeader(XmppHeaders.TO, "kermit@frog.com").
setHeader(XmppHeaders.THREAD, "123").
build();
class EqualSmackMessageWithThreadId extends ArgumentMatcher<org.jivesoftware.smack.packet.Message> {
@Override
@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");
boolean threadIdMatches = smackMessage.getThread().equals("123");
return bodyMatches & toMatches & threadIdMatches;
}
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");
boolean threadIdMatches = smackMessage.getThread().equals("123");
return bodyMatches & toMatches & threadIdMatches;
}
}
reset(connection);
handler.handleMessage(message);
// in threaded conversation we need to look for existing chat
verify(connection, times(1)).sendPacket(Mockito.argThat(new EqualSmackMessageWithThreadId()));
verify(connection, times(1)).sendStanza(Mockito.argThat(new EqualSmackMessageWithThreadId()));
}
@Test
public void validateMessagePostAsSmackMessage() throws Exception{
public void validateMessagePostAsSmackMessage() throws Exception {
XMPPConnection connection = mock(XMPPConnection.class);
ChatMessageSendingMessageHandler handler = new ChatMessageSendingMessageHandler(connection);
handler.setBeanFactory(mock(BeanFactory.class));
@@ -107,7 +109,7 @@ public class ChatMessageSendingMessageHandlerTests {
// first Message new
handler.handleMessage(message);
verify(connection, times(1)).sendPacket(smackMessage);
verify(connection, times(1)).sendStanza(smackMessage);
// assuming we know thread ID although currently we do not provide this capability
smackMessage = new org.jivesoftware.smack.packet.Message("kermit@frog.com");
@@ -119,34 +121,36 @@ public class ChatMessageSendingMessageHandlerTests {
handler.handleMessage(message);
// in threaded conversation we need to look for existing chat
verify(connection, times(1)).sendPacket(smackMessage);
verify(connection, times(1)).sendStanza(smackMessage);
}
@Test(expected=MessageHandlingException.class)
public void validateFailureNoChatToUser() throws Exception{
@Test(expected = MessageHandlingException.class)
public void validateFailureNoChatToUser() throws Exception {
ChatMessageSendingMessageHandler handler = new ChatMessageSendingMessageHandler(mock(XMPPConnection.class));
handler.handleMessage(new GenericMessage<String>("hello"));
}
@Test(expected=MessageHandlingException.class)
public void validateMessageWithUnsupportedPayload() throws Exception{
@Test(expected = MessageHandlingException.class)
public void validateMessageWithUnsupportedPayload() throws Exception {
ChatMessageSendingMessageHandler handler = new ChatMessageSendingMessageHandler(mock(XMPPConnection.class));
handler.handleMessage(new GenericMessage<Integer>(123));
}
@Test
public void testWithImplicitXmppConnection(){
public void testWithImplicitXmppConnection() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.registerSingleton(XmppContextUtils.XMPP_CONNECTION_BEAN_NAME, mock(XMPPConnection.class));
ChatMessageSendingMessageHandler handler = new ChatMessageSendingMessageHandler();
handler.setBeanFactory(bf);
handler.afterPropertiesSet();
assertNotNull(TestUtils.getPropertyValue(handler,"xmppConnection"));
assertNotNull(TestUtils.getPropertyValue(handler, "xmppConnection"));
}
@Test(expected=IllegalArgumentException.class)
public void testNoXmppConnection(){
@Test(expected = IllegalArgumentException.class)
public void testNoXmppConnection() {
ChatMessageSendingMessageHandler handler = new ChatMessageSendingMessageHandler();
handler.setBeanFactory(mock(BeanFactory.class));
handler.afterPropertiesSet();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 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.
@@ -33,22 +33,22 @@ import org.springframework.messaging.support.GenericMessage;
/**
* @author Oleg Zhurakousky
* @author Gunnar Hillert
*
* @author Artem Bilan
*/
public class PresenceSendingMessageHandlerTests {
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({"unchecked", "rawtypes"})
@Test
public void testPresencePayload(){
public void testPresencePayload() {
PresenceSendingMessageHandler handler = new PresenceSendingMessageHandler(mock(XMPPConnection.class));
handler.setBeanFactory(mock(BeanFactory.class));
handler.afterPropertiesSet();
handler.handleMessage(new GenericMessage(mock(Presence.class)));
handler.handleMessage(new GenericMessage<Presence>(new Presence(Presence.Type.subscribe)));
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test(expected=MessageHandlingException.class)
public void testWrongPayload(){
@SuppressWarnings({"unchecked", "rawtypes"})
@Test(expected = MessageHandlingException.class)
public void testWrongPayload() {
PresenceSendingMessageHandler handler = new PresenceSendingMessageHandler(mock(XMPPConnection.class));
handler.setBeanFactory(mock(BeanFactory.class));
handler.afterPropertiesSet();
@@ -56,19 +56,20 @@ public class PresenceSendingMessageHandlerTests {
}
@Test
public void testWithImplicitXmppConnection(){
public void testWithImplicitXmppConnection() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.registerSingleton(XmppContextUtils.XMPP_CONNECTION_BEAN_NAME, mock(XMPPConnection.class));
PresenceSendingMessageHandler handler = new PresenceSendingMessageHandler();
handler.setBeanFactory(bf);
handler.afterPropertiesSet();
assertNotNull(TestUtils.getPropertyValue(handler,"xmppConnection"));
assertNotNull(TestUtils.getPropertyValue(handler, "xmppConnection"));
}
@Test(expected=IllegalArgumentException.class)
public void testNoXmppConnection(){
@Test(expected = IllegalArgumentException.class)
public void testNoXmppConnection() {
PresenceSendingMessageHandler handler = new PresenceSendingMessageHandler();
handler.setBeanFactory(mock(BeanFactory.class));
handler.afterPropertiesSet();
}
}