GH-3462: Upgrade to Smack 4.4.5

Fixes https://github.com/spring-projects/spring-integration/issues/3462
This commit is contained in:
Florian Schmaus
2021-04-04 15:36:28 +02:00
committed by Artem Bilan
parent 4484c4da75
commit 330dfd03f2
14 changed files with 165 additions and 110 deletions

View File

@@ -12,7 +12,7 @@
<beans:bean id="testConnection" class="org.mockito.Mockito" factory-method="spy">
<beans:constructor-arg>
<beans:bean class="org.jivesoftware.smack.tcp.XMPPTCPConnection">
<beans:constructor-arg value="guest"/>
<beans:constructor-arg value="guest@example.org"/>
<beans:constructor-arg value="guest"/>
</beans:bean>
</beans:constructor-arg>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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.
@@ -23,7 +23,8 @@ import java.util.Map;
import org.jivesoftware.smack.StanzaListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.MessageBuilder;
import org.jivesoftware.smack.packet.StanzaBuilder;
import org.jivesoftware.smackx.jiveproperties.JivePropertiesManager;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -102,12 +103,12 @@ public class ChatMessageInboundChannelAdapterParserTests {
StanzaListener stanzaListener = TestUtils.getPropertyValue(adapter, "stanzaListener", StanzaListener.class);
Message message = new Message();
MessageBuilder message = StanzaBuilder.buildMessage();
message.setBody("hello");
message.setTo(JidCreate.from("oleg"));
message.to(JidCreate.from("oleg"));
JivePropertiesManager.addProperty(message, "foo", "foo");
JivePropertiesManager.addProperty(message, "bar", "bar");
stanzaListener.processStanza(message);
stanzaListener.processStanza(message.build());
org.springframework.messaging.Message<?> siMessage = xmppInbound.receive(0);
assertThat(siMessage.getHeaders().get("foo")).isEqualTo("foo");
assertThat(siMessage.getHeaders().get("xmpp_to")).isEqualTo("oleg");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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,6 +17,7 @@
package org.springframework.integration.xmpp.ignore;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.StanzaBuilder;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -33,6 +34,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
* Tests {@link PresenceSendingMessageHandler} to ensure that we are able to publish status.
*
* @author Josh Long
* @author Florian Schmaus
*
* @since 2.0
*/
@ContextConfiguration
@@ -46,8 +49,9 @@ public class OutboundPresenceTests {
@Test
@Ignore
public void testOutbound() throws Throwable {
Presence presence = new Presence(Presence.Type.available);
input.send(new GenericMessage<Presence>(presence));
Presence presence = StanzaBuilder.buildPresence().build();
input.send(new GenericMessage<>(presence));
Thread.sleep(60 * 1000);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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,9 +16,9 @@
package org.springframework.integration.xmpp.ignore;
import org.jivesoftware.smack.packet.StanzaBuilder;
import org.junit.Ignore;
import org.junit.Test;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -30,6 +30,7 @@ import org.springframework.messaging.support.GenericMessage;
* @author Oleg Zhurakousky
* @author Gary Russell
* @author Artem Bilan
* @author Florian Schmaus
*/
public class SmackMessageSampleTests {
@@ -41,9 +42,10 @@ public class SmackMessageSampleTests {
MessageChannel xmppInput = ac.getBean("xmppInput", MessageChannel.class);
org.jivesoftware.smack.packet.Message smackMessage =
new org.jivesoftware.smack.packet.Message(JidCreate.from("springintegration@gmail.com"));
smackMessage.setBody("Message sent as Smack Message");
org.jivesoftware.smack.packet.Message smackMessage = StanzaBuilder.buildMessage()
.to("springintegration@gmail.com")
.setBody("Message sent as Smack Message")
.build();
Message<org.jivesoftware.smack.packet.Message> message = new GenericMessage<>(smackMessage);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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.
@@ -26,7 +26,7 @@ import static org.mockito.BDDMockito.willAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import java.io.StringReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
@@ -35,16 +35,20 @@ import java.util.concurrent.TimeUnit;
import org.jivesoftware.smack.StanzaListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.MessageBuilder;
import org.jivesoftware.smack.packet.StanzaBuilder;
import org.jivesoftware.smack.packet.StreamOpen;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jivesoftware.smack.util.PacketParserUtils;
import org.jivesoftware.smack.xml.XmlPullParser;
import org.jivesoftware.smack.xml.XmlPullParserException;
import org.jivesoftware.smackx.gcm.packet.GcmPacketExtension;
import org.junit.jupiter.api.Test;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.xmlpull.v1.XmlPullParser;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.BeanFactory;
@@ -143,10 +147,11 @@ public class ChatMessageListeningEndpointTests {
endpoint.setErrorChannel(errorChannel);
endpoint.afterPropertiesSet();
StanzaListener listener = (StanzaListener) TestUtils.getPropertyValue(endpoint, "stanzaListener");
Message smackMessage = new Message(JidCreate.from("kermit@frog.com"));
MessageBuilder smackMessage = StanzaBuilder.buildMessage();
smackMessage.to(JidCreate.from("kermit@frog.com"));
smackMessage.setBody("hello");
smackMessage.setThread("1234");
listener.processStanza(smackMessage);
listener.processStanza(smackMessage.build());
ErrorMessage msg =
(ErrorMessage) errorChannel.receive();
@@ -167,12 +172,11 @@ public class ChatMessageListeningEndpointTests {
endpoint.afterPropertiesSet();
endpoint.start();
Message smackMessage = new Message();
MessageBuilder smackMessage = StanzaBuilder.buildMessage();
smackMessage.setBody("foo");
XmlPullParser xmlPullParser =
PacketParserUtils.newXmppParser(new StringReader(smackMessage.toXML(null).toString()));
xmlPullParser.next();
PacketParserUtils.getParserFor(smackMessage.build().toXML().toString());
testXMPPConnection.parseAndProcessStanza(xmlPullParser);
org.springframework.messaging.Message<?> receive = inputChannel.receive(10000);
@@ -196,9 +200,8 @@ public class ChatMessageListeningEndpointTests {
endpoint.setPayloadExpression(null);
smackMessage = new Message();
xmlPullParser = PacketParserUtils.newXmppParser(new StringReader(smackMessage.toXML(null).toString()));
xmlPullParser.next();
Message message = StanzaBuilder.buildMessage().build();
xmlPullParser = PacketParserUtils.getParserFor(message.toXML().toString());
testXMPPConnection.parseAndProcessStanza(xmlPullParser);
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
@@ -208,7 +211,7 @@ public class ChatMessageListeningEndpointTests {
verify(logger).info(argumentCaptor.capture());
assertThat(argumentCaptor.getValue())
.isEqualTo("The XMPP Message [" + smackMessage + "] with empty body is ignored.");
.isEqualTo("The XMPP Message [" + message + "] with empty body is ignored.");
endpoint.stop();
}
@@ -225,7 +228,7 @@ public class ChatMessageListeningEndpointTests {
" }\n" +
"}";
GcmPacketExtension packetExtension = new GcmPacketExtension(data);
Message smackMessage = new Message();
MessageBuilder smackMessage = StanzaBuilder.buildMessage();
smackMessage.addExtension(packetExtension);
TestXMPPConnection testXMPPConnection = new TestXMPPConnection();
@@ -241,8 +244,7 @@ public class ChatMessageListeningEndpointTests {
endpoint.start();
XmlPullParser xmlPullParser =
PacketParserUtils.newXmppParser(new StringReader(smackMessage.toXML(null).toString()));
xmlPullParser.next();
PacketParserUtils.getParserFor(smackMessage.build().toXML().toString());
testXMPPConnection.parseAndProcessStanza(xmlPullParser);
org.springframework.messaging.Message<?> receive = inputChannel.receive(10000);
@@ -255,12 +257,24 @@ public class ChatMessageListeningEndpointTests {
private static class TestXMPPConnection extends XMPPTCPConnection {
TestXMPPConnection() throws XmppStringprepException {
super(XMPPTCPConnectionConfiguration.builder().setXmppDomain("/foo").build());
super(XMPPTCPConnectionConfiguration.builder().setXmppDomain("example.org").build());
StreamOpen streamOpen = new StreamOpen("example.org");
XmlPullParser parser;
try {
parser = PacketParserUtils.getParserFor(streamOpen.toXML().toString());
}
catch (XmlPullParserException | IOException e) {
throw new AssertionError(e);
}
onStreamOpen(parser);
}
@Override
protected void parseAndProcessStanza(XmlPullParser parser) throws Exception {
protected void parseAndProcessStanza(XmlPullParser parser)
throws XmlPullParserException, IOException, InterruptedException {
super.parseAndProcessStanza(parser);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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.
@@ -23,8 +23,7 @@ import java.util.Set;
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.packet.StanzaBuilder;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.roster.RosterListener;
import org.junit.Test;
@@ -45,6 +44,7 @@ import org.springframework.messaging.support.ErrorMessage;
* @author Gunnar Hillert
* @author Gary Russell
* @author Artem Bilan
* @author Florian Schmaus
*/
public class PresenceListeningEndpointTests {
@@ -74,7 +74,6 @@ public class PresenceListeningEndpointTests {
}
@Test
@SuppressWarnings("unchecked")
public void testRosterPresenceChangeEvent() {
XMPPConnection connection = mock(XMPPConnection.class);
PresenceListeningEndpoint rosterEndpoint = new PresenceListeningEndpoint(connection);
@@ -84,7 +83,11 @@ public class PresenceListeningEndpointTests {
rosterEndpoint.afterPropertiesSet();
rosterEndpoint.start();
RosterListener rosterListener = (RosterListener) TestUtils.getPropertyValue(rosterEndpoint, "rosterListener");
Presence presence = new Presence(Type.available, "Hello", 1, Mode.chat);
Presence presence = StanzaBuilder.buildPresence()
.setStatus("Hello")
.setPriority(1)
.setMode(Presence.Mode.chat)
.build();
rosterListener.presenceChanged(presence);
Message<?> message = channel.receive(10);
assertThat(message.getPayload()).isEqualTo(presence);
@@ -126,7 +129,7 @@ public class PresenceListeningEndpointTests {
endpoint.setErrorChannel(errorChannel);
endpoint.afterPropertiesSet();
RosterListener listener = (RosterListener) TestUtils.getPropertyValue(endpoint, "rosterListener");
Presence presence = new Presence(Type.available);
Presence presence = StanzaBuilder.buildPresence().build();
listener.presenceChanged(presence);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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,10 +25,10 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.StanzaBuilder;
import org.jivesoftware.smackx.gcm.packet.GcmPacketExtension;
import org.jivesoftware.smackx.gcm.provider.GcmExtensionProvider;
import org.junit.Test;
import org.jxmpp.jid.impl.JidCreate;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
@@ -46,6 +46,7 @@ import org.springframework.messaging.support.GenericMessage;
* @author Oleg Zhurakousky
* @author Gunnar Hillert
* @author Artem Bilan
* @author Florian Schmaus
*/
public class ChatMessageSendingMessageHandlerTests {
@@ -111,28 +112,43 @@ public class ChatMessageSendingMessageHandlerTests {
handler.setBeanFactory(mock(BeanFactory.class));
handler.afterPropertiesSet();
org.jivesoftware.smack.packet.Message smackMessage =
new org.jivesoftware.smack.packet.Message(JidCreate.from("kermit@frog.com"));
smackMessage.setBody("Test Message");
org.jivesoftware.smack.packet.Message smackMessage = StanzaBuilder.buildMessage()
.to("kermit@frog.com")
.setBody("Test Message")
.build();
Message<?> message = MessageBuilder.withPayload(smackMessage).build();
// first Message new
handler.handleMessage(message);
verify(connection, times(1)).sendStanza(smackMessage);
verify(connection).isConnected();
verify(connection).sendStanza(Mockito.argThat((org.jivesoftware.smack.packet.Message m) -> {
boolean bodyMatches = "Test Message".equals(m.getBody());
boolean toMatches = m.getTo().toString().equals("kermit@frog.com");
return bodyMatches && toMatches;
}));
// assuming we know thread ID although currently we do not provide this capability
smackMessage = new org.jivesoftware.smack.packet.Message(JidCreate.from("kermit@frog.com"));
smackMessage.setBody("Hello Kitty");
smackMessage.setThread("123");
smackMessage = StanzaBuilder.buildMessage()
.ofType(org.jivesoftware.smack.packet.Message.Type.normal)
.to("kermit@frog.com")
.setBody("Hello Kitty")
.setThread("123")
.build();
message = MessageBuilder.withPayload(smackMessage).build();
reset(connection);
handler.handleMessage(message);
// in threaded conversation we need to look for existing chat
verify(connection, times(1)).sendStanza(smackMessage);
verify(connection).isConnected();
verify(connection).sendStanza(Mockito.argThat((org.jivesoftware.smack.packet.Message m) -> {
boolean bodyMatches = "Hello Kitty".equals(m.getBody());
boolean toMatches = "kermit@frog.com".equals(m.getTo().toString());
boolean threadMatches = "123".equals(m.getThread());
return bodyMatches && toMatches && threadMatches;
}));
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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,7 +20,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.StanzaBuilder;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
@@ -34,19 +34,19 @@ import org.springframework.messaging.support.GenericMessage;
* @author Oleg Zhurakousky
* @author Gunnar Hillert
* @author Artem Bilan
* @author Florian Schmaus
*/
public class PresenceSendingMessageHandlerTests {
@SuppressWarnings({"unchecked", "rawtypes"})
@Test
public void testPresencePayload() {
PresenceSendingMessageHandler handler = new PresenceSendingMessageHandler(mock(XMPPConnection.class));
handler.setBeanFactory(mock(BeanFactory.class));
handler.afterPropertiesSet();
handler.handleMessage(new GenericMessage<Presence>(new Presence(Presence.Type.subscribe)));
handler.handleMessage(new GenericMessage<>(StanzaBuilder.buildPresence().build()));
}
@SuppressWarnings({"unchecked", "rawtypes"})
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test(expected = MessageHandlingException.class)
public void testWrongPayload() {
PresenceSendingMessageHandler handler = new PresenceSendingMessageHandler(mock(XMPPConnection.class));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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,9 +22,10 @@ import java.util.HashMap;
import java.util.Map;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.MessageBuilder;
import org.jivesoftware.smack.packet.StanzaBuilder;
import org.jivesoftware.smackx.jiveproperties.JivePropertiesManager;
import org.junit.Test;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
import org.springframework.integration.xmpp.XmppHeaders;
@@ -51,7 +52,7 @@ public class DefaultXmppHeaderMapperTests {
headerMap.put(XmppHeaders.SUBJECT, "test.subject");
headerMap.put(XmppHeaders.TYPE, "headline");
MessageHeaders headers = new MessageHeaders(headerMap);
Message target = new Message();
MessageBuilder target = StanzaBuilder.buildMessage();
mapper.fromHeadersToRequest(headers, target);
// "standard" XMPP headers
@@ -84,7 +85,7 @@ public class DefaultXmppHeaderMapperTests {
headerMap.put(XmppHeaders.SUBJECT, "test.subject");
headerMap.put(XmppHeaders.TYPE, "headline");
MessageHeaders headers = new MessageHeaders(headerMap);
Message target = new Message();
MessageBuilder target = StanzaBuilder.buildMessage();
mapper.fromHeadersToRequest(headers, target);
// "standard" XMPP headers not included
@@ -94,7 +95,7 @@ public class DefaultXmppHeaderMapperTests {
Object from = target.getFrom();
assertThat(from).isNull();
assertThat(target.getSubject()).isNull();
assertThat(target.getType()).isEqualTo(Message.Type.normal);
assertThat(target.getType()).isNull();
// user-defined headers are included if in the list
assertThat(JivePropertiesManager.getProperty(target, "userDefined1")).isEqualTo("foo");
@@ -111,10 +112,12 @@ public class DefaultXmppHeaderMapperTests {
@Test
public void toHeadersStandardOnly() throws XmppStringprepException {
DefaultXmppHeaderMapper mapper = new DefaultXmppHeaderMapper();
Message source = new Message(JidCreate.from("test.to"), Message.Type.headline);
source.setFrom(JidCreate.from("test.from"));
source.setSubject("test.subject");
source.setThread("test.thread");
MessageBuilder source = StanzaBuilder.buildMessage()
.ofType(Message.Type.headline)
.to("test.to")
.from("test.from")
.setSubject("test.subject")
.setThread("test.thread");
JivePropertiesManager.addProperty(source, "userDefined1", "foo");
JivePropertiesManager.addProperty(source, "userDefined2", "bar");
Map<String, Object> headers = mapper.toHeadersFromRequest(source);
@@ -131,10 +134,12 @@ public class DefaultXmppHeaderMapperTests {
public void toHeadersUserDefinedOnly() throws XmppStringprepException {
DefaultXmppHeaderMapper mapper = new DefaultXmppHeaderMapper();
mapper.setReplyHeaderNames("userDefined*");
Message source = new Message(JidCreate.from("test.to"), Message.Type.headline);
source.setFrom(JidCreate.from("test.from"));
source.setSubject("test.subject");
source.setThread("test.thread");
MessageBuilder source = StanzaBuilder.buildMessage()
.ofType(Message.Type.headline)
.to("test.to")
.from("test.from")
.setSubject("test.subject")
.setThread("test.thread");
JivePropertiesManager.addProperty(source, "userDefined1", "foo");
JivePropertiesManager.addProperty(source, "userDefined2", "bar");
Map<String, Object> headers = mapper.toHeadersFromReply(source);