1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-08 08:39:39 +02:00
Smack/smack-integration-test/src/main/java/org/jivesoftware/smack/ChatTest.java
Florian Schmaus 1e5d34eacf Bump to Gradle 8.10.2, require Java 11
Bump Gradle from 6.8.3 to 8.10.2 and increase the minimum required
Java version from 8 to 11 (SMACK-953).

The switch from Java 8 to 11 caused some Bytecode portability issues
regarding NIO Buffers. Java changed with version 9 the return type of
some subclasses of Buffer to return the specific Buffer type instead
of the Buffer superclass [JDK-4774077]. For example, ByteBuffer.filp()
previously returned Buffer, while it does return ByteBuffer now.

This sensible change was not reflected by the Android API [1], which
means that AnimalSniffer rightfully started to complain that there is
no method "ByteBuffer ByteBuffer.flip()" in Android, there is only
"Buffer ByteBuffer.flip()", and those are incompatible methods on
Java's Bytecode layer.

As workaround, this changes

    return charBuffer.flip().toString();

to

    ((java.nio.Buffer) charBuffer).flip();
    return charBuffer.toString();

to restore the Bytecode portability between Android and Java.

Errorprone also got new checks, of which JavaUtilDate and JdkObsolete
are wroth mentioning.

JavaUtilData basically strongly recommends to use Java's newer time
API over java.util.Date. But since Smack was Java 8 until now,
j.u.Date is widely used.

Similar JdkObsolete mentions obsolete JDK APIs, like data structures
like Vector and Stack. But mostly LinkedList, which should usually be
replaced by ArrayList. And this is what this commit largely does.

JDK-4774077: https://bugs.openjdk.org/browse/JDK-4774077
1: https://issuetracker.google.com/issues/369219141
2024-09-25 12:08:50 +02:00

140 lines
5.1 KiB
Java

/**
*
* Copyright 2015-2020 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack;
import static org.jivesoftware.smackx.jiveproperties.JivePropertiesManager.addProperty;
import static org.jivesoftware.smackx.jiveproperties.JivePropertiesManager.getProperty;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Date;
import org.jivesoftware.smack.chat.ChatManagerListener;
import org.jivesoftware.smack.filter.ThreadFilter;
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.igniterealtime.smack.inttest.AbstractSmackIntegrationTest;
import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment;
import org.igniterealtime.smack.inttest.annotations.AfterClass;
import org.igniterealtime.smack.inttest.annotations.BeforeClass;
import org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest;
/**
* Tests for Chat Manager and for Chat Manager Listener.
*
* @author Stawicki Adam
*/
public class ChatTest extends AbstractSmackIntegrationTest {
@SuppressWarnings("deprecation")
private final org.jivesoftware.smack.chat.ChatManager chatManagerOne;
private boolean invoked;
@SuppressWarnings("deprecation")
public ChatTest(SmackIntegrationTestEnvironment environment) {
super(environment);
chatManagerOne = org.jivesoftware.smack.chat.ChatManager.getInstanceFor(conOne);
}
@BeforeClass
public void setUp() {
JivePropertiesManager.setJavaObjectEnabled(true);
}
@AfterClass
public void tearDown() {
JivePropertiesManager.setJavaObjectEnabled(false);
}
@SmackIntegrationTest
@SuppressWarnings({"deprecation", "JavaUtilDate"})
public void testProperties() throws Exception {
org.jivesoftware.smack.chat.Chat newChat = chatManagerOne.createChat(conTwo.getUser());
StanzaCollector collector = conTwo.createStanzaCollector(new ThreadFilter(newChat.getThreadID()));
MessageBuilder messageBuilder = StanzaBuilder.buildMessage();
messageBuilder.setSubject("Subject of the chat");
messageBuilder.setBody("Body of the chat");
addProperty(messageBuilder, "favoriteColor", "red");
addProperty(messageBuilder, "age", 30);
addProperty(messageBuilder, "distance", 30f);
addProperty(messageBuilder, "weight", 30d);
addProperty(messageBuilder, "male", true);
addProperty(messageBuilder, "birthdate", new Date());
Message msg = messageBuilder.build();
newChat.sendMessage(msg);
Message msg2 = collector.nextResult(2000);
assertNotNull(msg2, "No message was received");
assertEquals(msg.getSubject(), msg2.getSubject(), "Subjects are different");
assertEquals(msg.getBody(), msg2.getBody(), "Bodies are different");
assertEquals(
getProperty(msg, "favoriteColor"),
getProperty(msg2, "favoriteColor"),
"favoriteColors are different");
assertEquals(
getProperty(msg, "age"),
getProperty(msg2, "age"),
"ages are different");
assertEquals(
getProperty(msg, "distance"),
getProperty(msg2, "distance"),
"distances are different");
assertEquals(
getProperty(msg, "weight"),
getProperty(msg2, "weight"),
"weights are different");
assertEquals(
getProperty(msg, "male"),
getProperty(msg2, "male"),
"males are different");
assertEquals(
getProperty(msg, "birthdate"),
getProperty(msg2, "birthdate"),
"birthdates are different");
}
@SuppressWarnings("deprecation")
@SmackIntegrationTest
public void chatManagerTest() {
ChatManagerListener listener = new ChatManagerListener() {
@Override
public void chatCreated(org.jivesoftware.smack.chat.Chat chat, boolean createdLocally) {
invoked = true;
}
};
try {
chatManagerOne.addChatListener(listener);
chatManagerOne.createChat(conTwo.getUser());
assertTrue(invoked, "TestChatManagerListener wasn't invoked");
}
finally {
chatManagerOne.removeChatListener(listener);
}
}
}