StreamManagementTest.java

  1. /**
  2.  *
  3.  * Copyright 2015-2020 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smack;

  18. import static org.junit.jupiter.api.Assertions.assertEquals;
  19. import static org.junit.jupiter.api.Assertions.assertNotNull;

  20. import java.io.IOException;

  21. import org.jivesoftware.smack.SmackException.NotConnectedException;
  22. import org.jivesoftware.smack.filter.AndFilter;
  23. import org.jivesoftware.smack.filter.FromMatchesFilter;
  24. import org.jivesoftware.smack.filter.MessageWithBodiesFilter;
  25. import org.jivesoftware.smack.packet.Message;
  26. import org.jivesoftware.smack.tcp.XMPPTCPConnection;

  27. import org.igniterealtime.smack.inttest.AbstractSmackSpecificLowLevelIntegrationTest;
  28. import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment;
  29. import org.igniterealtime.smack.inttest.TestNotPossibleException;
  30. import org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest;
  31. import org.jxmpp.jid.EntityFullJid;

  32. public class StreamManagementTest extends AbstractSmackSpecificLowLevelIntegrationTest<XMPPTCPConnection> {

  33.     public StreamManagementTest(SmackIntegrationTestEnvironment environment) throws Exception {
  34.         super(environment, XMPPTCPConnection.class);
  35.         XMPPTCPConnection connection = getSpecificUnconnectedConnection();
  36.         connection.connect().login();
  37.         if (!connection.isSmAvailable()) {
  38.             throw new TestNotPossibleException("XEP-198: Stream Mangement not supported by service");
  39.         }
  40.     }

  41.     @SmackIntegrationTest
  42.     public void testStreamManagement(XMPPTCPConnection conOne, XMPPTCPConnection conTwo) throws InterruptedException,
  43.                     SmackException, IOException, XMPPException {
  44.         final String body1 = "Hi, what's up? " + testRunId;
  45.         final String body2 = "Hi, what's up? I've been just instantly shutdown" + testRunId;
  46.         final String body3 = "Hi, what's up? I've been just resumed" + testRunId;

  47.         final StanzaCollector collector = conTwo.createStanzaCollector(new AndFilter(
  48.                         MessageWithBodiesFilter.INSTANCE,
  49.                         FromMatchesFilter.createFull(conOne.getUser())));

  50.         try {
  51.             send(body1, conOne, conTwo);
  52.             assertMessageWithBodyReceived(body1, collector, conTwo.getUser());

  53.             conOne.instantShutdown();

  54.             send(body2, conOne, conTwo);

  55.             // Reconnect with xep198
  56.             conOne.connect().login();
  57.             assertMessageWithBodyReceived(body2, collector, conTwo.getUser());

  58.             send(body3, conOne, conTwo);
  59.             assertMessageWithBodyReceived(body3, collector, conTwo.getUser());
  60.         }
  61.         finally {
  62.             collector.cancel();
  63.         }
  64.     }

  65.     private static void send(String messageString, XMPPConnection from, XMPPConnection to)
  66.                     throws NotConnectedException, InterruptedException {
  67.         Message message = from.getStanzaFactory().buildMessageStanza()
  68.                         .to(to.getUser())
  69.                         .setBody(messageString)
  70.                         .build();
  71.         from.sendStanza(message);
  72.     }

  73.     private static void assertMessageWithBodyReceived(String body, StanzaCollector collector, EntityFullJid recipient) throws InterruptedException {
  74.         Message message = collector.nextResult();
  75.         assertNotNull(message, "Expected '" + recipient + "' to receive a message stanza with body '" + body + "', but it didn't receive the message stanza at all.");
  76.         assertEquals(body, message.getBody(), "Expected '" + recipient + "'to receive a message stanza with a specific body, but it received a message stanza with a different body.");
  77.     }
  78. }