|
| 1 | +package com.baeldung.smack; |
| 2 | + |
| 3 | +import org.jivesoftware.smack.AbstractXMPPConnection; |
| 4 | +import org.jivesoftware.smack.SmackException; |
| 5 | +import org.jivesoftware.smack.XMPPException; |
| 6 | +import org.jivesoftware.smack.chat2.ChatManager; |
| 7 | +import org.jivesoftware.smack.filter.StanzaTypeFilter; |
| 8 | +import org.jivesoftware.smack.packet.Message; |
| 9 | +import org.jivesoftware.smack.tcp.XMPPTCPConnection; |
| 10 | +import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration; |
| 11 | +import org.junit.Assert; |
| 12 | +import org.junit.BeforeClass; |
| 13 | +import org.junit.Test; |
| 14 | +import org.jxmpp.stringprep.XmppStringprepException; |
| 15 | +import org.slf4j.Logger; |
| 16 | +import org.slf4j.LoggerFactory; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.concurrent.CountDownLatch; |
| 20 | + |
| 21 | +public class SmackIntegrationTest { |
| 22 | + |
| 23 | + private static AbstractXMPPConnection connection; |
| 24 | + private Logger logger = LoggerFactory.getLogger(SmackIntegrationTest.class); |
| 25 | + |
| 26 | + @BeforeClass |
| 27 | + public static void setup() throws IOException, InterruptedException, XMPPException, SmackException { |
| 28 | + |
| 29 | + XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder() |
| 30 | + .setUsernameAndPassword("baeldung","baeldung") |
| 31 | + .setXmppDomain("jabb3r.org") |
| 32 | + .setHost("jabb3r.org") |
| 33 | + .build(); |
| 34 | + |
| 35 | + XMPPTCPConnectionConfiguration config2 = XMPPTCPConnectionConfiguration.builder() |
| 36 | + .setUsernameAndPassword("baeldung2","baeldung2") |
| 37 | + .setXmppDomain("jabb3r.org") |
| 38 | + .setHost("jabb3r.org") |
| 39 | + .build(); |
| 40 | + |
| 41 | + connection = new XMPPTCPConnection(config); |
| 42 | + connection.connect(); |
| 43 | + connection.login(); |
| 44 | + |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void whenSendMessageWithChat_thenReceiveMessage() throws XmppStringprepException, InterruptedException { |
| 49 | + |
| 50 | + CountDownLatch latch = new CountDownLatch(1); |
| 51 | + ChatManager chatManager = ChatManager.getInstanceFor(connection); |
| 52 | + final String[] expected = {null}; |
| 53 | + |
| 54 | + new StanzaThread().run(); |
| 55 | + |
| 56 | + chatManager.addIncomingListener((entityBareJid, message, chat) -> { |
| 57 | + logger.info("Message arrived: " + message.getBody()); |
| 58 | + expected[0] = message.getBody(); |
| 59 | + latch.countDown(); |
| 60 | + }); |
| 61 | + |
| 62 | + latch.await(); |
| 63 | + Assert.assertEquals("Hello!", expected[0]); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void whenSendMessage_thenReceiveMessageWithFilter() throws XmppStringprepException, InterruptedException { |
| 68 | + |
| 69 | + CountDownLatch latch = new CountDownLatch(1); |
| 70 | + final String[] expected = {null}; |
| 71 | + |
| 72 | + new StanzaThread().run(); |
| 73 | + |
| 74 | + connection.addAsyncStanzaListener(stanza -> { |
| 75 | + if (stanza instanceof Message) { |
| 76 | + Message message = (Message) stanza; |
| 77 | + expected[0] = message.getBody(); |
| 78 | + latch.countDown(); |
| 79 | + } |
| 80 | + }, StanzaTypeFilter.MESSAGE); |
| 81 | + |
| 82 | + latch.await(); |
| 83 | + Assert.assertEquals("Hello!", expected[0]); |
| 84 | + } |
| 85 | +} |
0 commit comments