ChatTest.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.jivesoftware.smackx.jiveproperties.JivePropertiesManager.addProperty;
  19. import static org.jivesoftware.smackx.jiveproperties.JivePropertiesManager.getProperty;
  20. import static org.junit.jupiter.api.Assertions.assertEquals;
  21. import static org.junit.jupiter.api.Assertions.assertNotNull;
  22. import static org.junit.jupiter.api.Assertions.assertTrue;

  23. import java.util.Date;

  24. import org.jivesoftware.smack.chat.ChatManagerListener;
  25. import org.jivesoftware.smack.filter.ThreadFilter;
  26. import org.jivesoftware.smack.packet.Message;
  27. import org.jivesoftware.smack.packet.MessageBuilder;
  28. import org.jivesoftware.smack.packet.StanzaBuilder;

  29. import org.jivesoftware.smackx.jiveproperties.JivePropertiesManager;

  30. import org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest;
  31. import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment;
  32. import org.igniterealtime.smack.inttest.annotations.AfterClass;
  33. import org.igniterealtime.smack.inttest.annotations.BeforeClass;
  34. import org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest;

  35. /**
  36.  * Tests for Chat Manager and for Chat Manager Listener.
  37.  *
  38.  * @author Stawicki Adam
  39.  */
  40. public class ChatTest extends AbstractSmackIntegrationTest {

  41.     @SuppressWarnings("deprecation")
  42.     private final org.jivesoftware.smack.chat.ChatManager chatManagerOne;
  43.     private boolean invoked;

  44.     @SuppressWarnings("deprecation")
  45.     public ChatTest(SmackIntegrationTestEnvironment environment) {
  46.         super(environment);
  47.         chatManagerOne = org.jivesoftware.smack.chat.ChatManager.getInstanceFor(conOne);
  48.     }

  49.     @BeforeClass
  50.     public void setUp() {
  51.         JivePropertiesManager.setJavaObjectEnabled(true);
  52.     }

  53.     @AfterClass
  54.     public void tearDown() {
  55.         JivePropertiesManager.setJavaObjectEnabled(false);
  56.     }

  57.     @SuppressWarnings("deprecation")
  58.     @SmackIntegrationTest
  59.     public void testProperties() throws Exception {
  60.         org.jivesoftware.smack.chat.Chat newChat = chatManagerOne.createChat(conTwo.getUser());
  61.         StanzaCollector collector = conTwo.createStanzaCollector(new ThreadFilter(newChat.getThreadID()));

  62.         MessageBuilder messageBuilder = StanzaBuilder.buildMessage();

  63.         messageBuilder.setSubject("Subject of the chat");
  64.         messageBuilder.setBody("Body of the chat");
  65.         addProperty(messageBuilder, "favoriteColor", "red");
  66.         addProperty(messageBuilder, "age", 30);
  67.         addProperty(messageBuilder, "distance", 30f);
  68.         addProperty(messageBuilder, "weight", 30d);
  69.         addProperty(messageBuilder, "male", true);
  70.         addProperty(messageBuilder, "birthdate", new Date());

  71.         Message msg = messageBuilder.build();
  72.         newChat.sendMessage(msg);

  73.         Message msg2 = collector.nextResult(2000);
  74.         assertNotNull(msg2, "No message was received");
  75.         assertEquals(msg.getSubject(), msg2.getSubject(), "Subjects are different");
  76.         assertEquals(msg.getBody(), msg2.getBody(), "Bodies are different");
  77.         assertEquals(
  78.                getProperty(msg, "favoriteColor"),
  79.                getProperty(msg2, "favoriteColor"),
  80.                "favoriteColors are different");
  81.         assertEquals(
  82.                getProperty(msg, "age"),
  83.                getProperty(msg2, "age"),
  84.                "ages are different");
  85.         assertEquals(
  86.                getProperty(msg, "distance"),
  87.                getProperty(msg2, "distance"),
  88.                "distances are different");
  89.         assertEquals(
  90.                getProperty(msg, "weight"),
  91.                getProperty(msg2, "weight"),
  92.                "weights are different");
  93.         assertEquals(
  94.                getProperty(msg, "male"),
  95.                getProperty(msg2, "male"),
  96.                "males are different");
  97.         assertEquals(
  98.                getProperty(msg, "birthdate"),
  99.                getProperty(msg2, "birthdate"),
  100.                "birthdates are different");
  101.     }

  102.     @SuppressWarnings("deprecation")
  103.     @SmackIntegrationTest
  104.     public void chatManagerTest() {
  105.         ChatManagerListener listener = new ChatManagerListener() {

  106.             @Override
  107.             public void chatCreated(org.jivesoftware.smack.chat.Chat chat, boolean createdLocally) {
  108.                 invoked = true;
  109.             }

  110.         };
  111.         try {
  112.             chatManagerOne.addChatListener(listener);
  113.             chatManagerOne.createChat(conTwo.getUser());

  114.             assertTrue(invoked, "TestChatManagerListener wasn't invoked");
  115.         }
  116.         finally {
  117.             chatManagerOne.removeChatListener(listener);
  118.         }
  119.     }
  120. }