1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2025-09-09 10:19:41 +02:00

Add smack-websocket-java11

This also lifts a bunch of logic from smack-websocket-okhttp into
smack-websocket. Furthermore, the following subprojects require now
Java 11:
- smack-integration-test
- smack-omemo-signal-integration-test
- smack-repl
- smack-websocket-java11

Related tracking issue: SMACK-835
This commit is contained in:
Florian Schmaus 2021-02-14 19:42:27 +01:00
parent cd40455b62
commit 4aacdc5154
24 changed files with 442 additions and 162 deletions

View file

@ -9,6 +9,7 @@ applicationDefaultJvmArgs = ["-enableassertions"]
dependencies {
api project(':smack-java8-full')
api project(':smack-resolver-dnsjava')
implementation project(':smack-websocket-java11')
implementation "com.google.guava:guava:${guavaVersion}"
compile 'org.reflections:reflections:0.9.12'
compile 'eu.geekplace.javapinning:java-pinning-java7:1.1.0-alpha1'

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2018-2020 Florian Schmaus
* Copyright 2018-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -30,15 +30,12 @@ public abstract class AbstractSmackSpecificLowLevelIntegrationTest<C extends Abs
private final SmackIntegrationTestEnvironment environment;
protected final Class<C> connectionClass;
private final XmppConnectionDescriptor<C, ? extends ConnectionConfiguration, ? extends ConnectionConfiguration.Builder<?, ?>> connectionDescriptor;
public AbstractSmackSpecificLowLevelIntegrationTest(SmackIntegrationTestEnvironment environment,
Class<C> connectionClass) {
super(environment);
this.environment = environment;
this.connectionClass = connectionClass;
connectionDescriptor = environment.connectionManager.getConnectionDescriptorFor(connectionClass);
if (connectionDescriptor == null) {
@ -46,8 +43,8 @@ public abstract class AbstractSmackSpecificLowLevelIntegrationTest<C extends Abs
}
}
public Class<C> getConnectionClass() {
return connectionClass;
public XmppConnectionDescriptor<C, ? extends ConnectionConfiguration, ? extends ConnectionConfiguration.Builder<?, ?>> getConnectionDescriptor() {
return connectionDescriptor;
}
protected C getSpecificUnconnectedConnection() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {

View file

@ -48,6 +48,7 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.Smack;
import org.jivesoftware.smack.SmackConfiguration;
@ -199,8 +200,8 @@ public class SmackIntegrationTestFramework {
Set<Class<? extends AbstractSmackIntegrationTest>> inttestClasses = reflections.getSubTypesOf(AbstractSmackIntegrationTest.class);
Set<Class<? extends AbstractSmackLowLevelIntegrationTest>> lowLevelInttestClasses = reflections.getSubTypesOf(AbstractSmackLowLevelIntegrationTest.class);
Set<Class<? extends AbstractSmackIntTest>> classes = new HashSet<>(inttestClasses.size()
+ lowLevelInttestClasses.size());
final int builtInTestCount = inttestClasses.size() + lowLevelInttestClasses.size();
Set<Class<? extends AbstractSmackIntTest>> classes = new HashSet<>(builtInTestCount);
classes.addAll(inttestClasses);
classes.addAll(lowLevelInttestClasses);
@ -333,11 +334,11 @@ public class SmackIntegrationTestFramework {
continue;
}
Class<? extends AbstractXMPPConnection> specificLowLevelConnectionClass = null;
XmppConnectionDescriptor<?, ?, ?> specificLowLevelConnectionDescriptor = null;
final TestType testType;
if (test instanceof AbstractSmackSpecificLowLevelIntegrationTest) {
AbstractSmackSpecificLowLevelIntegrationTest<?> specificLowLevelTest = (AbstractSmackSpecificLowLevelIntegrationTest<?>) test;
specificLowLevelConnectionClass = specificLowLevelTest.getConnectionClass();
specificLowLevelConnectionDescriptor = specificLowLevelTest.getConnectionDescriptor();
testType = TestType.SpecificLowLevel;
} else if (test instanceof AbstractSmackLowLevelIntegrationTest) {
testType = TestType.LowLevel;
@ -366,6 +367,7 @@ public class SmackIntegrationTestFramework {
verifyLowLevelTestMethod(method, AbstractXMPPConnection.class);
break;
case SpecificLowLevel:
Class<? extends AbstractXMPPConnection> specificLowLevelConnectionClass = specificLowLevelConnectionDescriptor.getConnectionClass();
verifyLowLevelTestMethod(method, specificLowLevelConnectionClass);
break;
}
@ -543,10 +545,8 @@ public class SmackIntegrationTestFramework {
continue;
}
Class<? extends AbstractXMPPConnection> connectionClass = connectionDescriptor.getConnectionClass();
ConcreteTest.Executor executor = () -> lowLevelTestMethod.invoke(test, connectionClass);
ConcreteTest concreteTest = new ConcreteTest(TestType.LowLevel, lowLevelTestMethod.testMethod, executor, connectionClass.getSimpleName());
ConcreteTest.Executor executor = () -> lowLevelTestMethod.invoke(test, connectionDescriptor);
ConcreteTest concreteTest = new ConcreteTest(TestType.LowLevel, lowLevelTestMethod.testMethod, executor, connectionDescriptor.getNickname());
resultingConcreteTests.add(concreteTest);
}
@ -560,8 +560,9 @@ public class SmackIntegrationTestFramework {
if (testMethod.smackIntegrationTestAnnotation.onlyDefaultConnectionType()) {
throw new IllegalArgumentException("SpecificLowLevelTests must not have set onlyDefaultConnectionType");
}
Class<C> connectionClass = test.getConnectionClass();
testMethod.invoke(test, connectionClass);
XmppConnectionDescriptor<C, ? extends ConnectionConfiguration, ? extends ConnectionConfiguration.Builder<?, ?>> connectionDescriptor = test.getConnectionDescriptor();
testMethod.invoke(test, connectionDescriptor);
}
protected SmackIntegrationTestEnvironment prepareEnvironment() throws SmackException,
@ -837,9 +838,8 @@ public class SmackIntegrationTestFramework {
parameterListOfConnections = testMethodParametersIsListOfConnections(testMethod);
}
// TODO: The second parameter should probably be a connection descriptor?
private void invoke(AbstractSmackLowLevelIntegrationTest test,
Class<? extends AbstractXMPPConnection> connectionClass)
XmppConnectionDescriptor<?, ?, ?> connectionDescriptor)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException,
InterruptedException, SmackException, IOException, XMPPException {
final int connectionCount;
@ -854,7 +854,7 @@ public class SmackIntegrationTestFramework {
}
List<? extends AbstractXMPPConnection> connections = connectionManager.constructConnectedConnections(
connectionClass, connectionCount);
connectionDescriptor, connectionCount);
if (parameterListOfConnections) {
testMethod.invoke(test, connections);

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2018-2020 Florian Schmaus
* Copyright 2018-2021 Florian Schmaus
*
* 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.igniterealtime.smack.inttest;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
@ -29,7 +30,12 @@ import java.util.List;
import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection;
import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration;
import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionModuleDescriptor;
import org.jivesoftware.smack.util.Consumer;
import org.jivesoftware.smack.websocket.XmppWebSocketTransportModuleDescriptor;
import org.jivesoftware.smack.websocket.impl.WebSocketFactory;
public final class XmppConnectionDescriptor<
C extends AbstractXMPPConnection,
@ -134,6 +140,32 @@ public final class XmppConnectionDescriptor<
return new Builder<>(connectionClass, connectionConfigurationClass, connectionConfigurationBuilderClass);
}
public static XmppConnectionDescriptor<ModularXmppClientToServerConnection, ModularXmppClientToServerConnectionConfiguration, ModularXmppClientToServerConnectionConfiguration.Builder> buildWebsocketDescriptor(
String nickname, Class<? extends WebSocketFactory> factoryClass)
throws InstantiationException, IllegalAccessException, IllegalArgumentException,
InvocationTargetException, NoSuchMethodException, SecurityException {
WebSocketFactory factory;
try {
Field instanceField = factoryClass.getField("INSTANCE");
factory = (WebSocketFactory) instanceField.get(null);
} catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
factory = factoryClass.getConstructor().newInstance();
}
WebSocketFactory finalFactory = factory;
return XmppConnectionDescriptor.buildWith(ModularXmppClientToServerConnection.class, ModularXmppClientToServerConnectionConfiguration.class, ModularXmppClientToServerConnectionConfiguration.Builder.class)
.withNickname("modular-websocket-java11")
.applyExtraConfguration(cb -> {
cb.removeAllModules();
ModularXmppClientToServerConnectionModuleDescriptor webSocketModuleDescriptor =
XmppWebSocketTransportModuleDescriptor.getBuilder(cb)
.setWebSocketFactory(finalFactory)
.build();
cb.addModule(webSocketModuleDescriptor);
})
.build();
}
public static final class Builder<C extends AbstractXMPPConnection, CC extends ConnectionConfiguration, CCB extends ConnectionConfiguration.Builder<?, CC>> {
private final Class<C> connectionClass;
private final Class<CC> connectionConfigurationClass;

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2018-2020 Florian Schmaus
* Copyright 2018-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -27,6 +27,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -44,8 +45,8 @@ import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jivesoftware.smack.util.MultiMap;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.websocket.XmppWebSocketTransportModuleDescriptor;
import org.jivesoftware.smack.websocket.java11.Java11WebSocketFactory;
import org.jivesoftware.smack.websocket.okhttp.OkHttpWebSocketFactory;
import org.jivesoftware.smackx.admin.ServiceAdministrationManager;
import org.jivesoftware.smackx.iqregister.AccountManager;
@ -88,15 +89,13 @@ public class XmppConnectionManager {
.build()
);
addConnectionDescriptor(
XmppConnectionDescriptor.buildWith(ModularXmppClientToServerConnection.class, ModularXmppClientToServerConnectionConfiguration.class, ModularXmppClientToServerConnectionConfiguration.Builder.class)
.withNickname("modular-websocket")
.applyExtraConfguration(cb -> {
cb.removeAllModules();
cb.addModule(XmppWebSocketTransportModuleDescriptor.class);
})
.build()
XmppConnectionDescriptor.buildWebsocketDescriptor("modular-websocket-okhttp", OkHttpWebSocketFactory.class)
);
} catch (NoSuchMethodException | SecurityException e) {
addConnectionDescriptor(
XmppConnectionDescriptor.buildWebsocketDescriptor("modular-websocket-java11", Java11WebSocketFactory.class)
);
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException e) {
throw new AssertionError(e);
}
}
@ -155,12 +154,12 @@ public class XmppConnectionManager {
/**
* A pool of authenticated and free to use connections.
*/
private final MultiMap<Class<? extends AbstractXMPPConnection>, AbstractXMPPConnection> connectionPool = new MultiMap<>();
private final MultiMap<XmppConnectionDescriptor<?, ?, ?>, AbstractXMPPConnection> connectionPool = new MultiMap<>();
/**
* A list of all ever created connections.
*/
private final List<AbstractXMPPConnection> connections = new ArrayList<>();
private final Map<AbstractXMPPConnection, XmppConnectionDescriptor<?, ?, ?>> connections = new ConcurrentHashMap<>();
XmppConnectionManager(SmackIntegrationTestFramework sinttestFramework)
throws SmackException, IOException, XMPPException, InterruptedException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
@ -249,7 +248,7 @@ public class XmppConnectionManager {
void disconnectAndCleanup() throws InterruptedException {
int successfullyDeletedAccountsCount = 0;
for (AbstractXMPPConnection connection : connections) {
for (AbstractXMPPConnection connection : connections.keySet()) {
if (sinttestConfiguration.accountRegistration == AccountRegistration.inBandRegistration) {
// Note that we use the account manager from the to-be-deleted connection.
AccountManager accountManager = AccountManager.getInstance(connection);
@ -345,7 +344,7 @@ public class XmppConnectionManager {
}
});
connections.add(mainConnection);
connections.put(mainConnection, defaultConnectionDescriptor);
mainConnection.connect();
mainConnection.login();
@ -384,13 +383,13 @@ public class XmppConnectionManager {
}
}
<C extends AbstractXMPPConnection> List<C> constructConnectedConnections(Class<C> connectionClass, int count)
<C extends AbstractXMPPConnection> List<C> constructConnectedConnections(XmppConnectionDescriptor<C, ? extends ConnectionConfiguration, ? extends ConnectionConfiguration.Builder<?, ?>> connectionDescriptor, int count)
throws InterruptedException, SmackException, IOException, XMPPException {
List<C> connections = new ArrayList<>(count);
synchronized (connectionPool) {
@SuppressWarnings("unchecked")
List<C> pooledConnections = (List<C>) connectionPool.getAll(connectionClass);
List<C> pooledConnections = (List<C>) connectionPool.getAll(connectionDescriptor);
while (count > 0 && !pooledConnections.isEmpty()) {
C connection = pooledConnections.remove(pooledConnections.size() - 1);
connections.add(connection);
@ -398,9 +397,6 @@ public class XmppConnectionManager {
}
}
@SuppressWarnings("unchecked")
XmppConnectionDescriptor<C, ? extends ConnectionConfiguration, ? extends ConnectionConfiguration.Builder<?, ?>> connectionDescriptor = (XmppConnectionDescriptor<C, ? extends ConnectionConfiguration, ? extends ConnectionConfiguration.Builder<?, ?>>) connectionDescriptors
.getFirst(connectionClass);
for (int i = 0; i < count; i++) {
C connection = constructConnectedConnection(connectionDescriptor);
connections.add(connection);
@ -469,7 +465,7 @@ public class XmppConnectionManager {
throw new IllegalStateException(e);
}
connections.add(connection);
connections.put(connection, connectionDescriptor);
return connection;
}
@ -487,8 +483,13 @@ public class XmppConnectionManager {
}
if (connection.isAuthenticated()) {
XmppConnectionDescriptor<?, ?, ?> connectionDescriptor = connections.get(connection);
if (connectionDescriptor == null) {
throw new IllegalStateException("Attempt to recycle unknown connection: " + connection);
}
synchronized (connectionPool) {
connectionPool.put(connectionClass, connection);
connectionPool.put(connectionDescriptor, connection);
}
} else {
connection.disconnect();

View file

@ -1,6 +1,6 @@
/**
*
* Copyright 2020 Aditya Borikar
* Copyright 2021 Florian Schmaus, 2020 Aditya Borikar
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,28 +16,31 @@
*/
package org.jivesoftware.smack.c2s;
import java.util.List;
import java.util.concurrent.TimeoutException;
import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.StanzaListener;
import org.jivesoftware.smack.filter.MessageWithBodiesFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Stanza;
import org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest;
import org.igniterealtime.smack.inttest.AbstractSmackLowLevelIntegrationTest;
import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment;
import org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest;
import org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint;
import org.jxmpp.jid.EntityFullJid;
public class SimpleXmppConnectionIntegrationTest extends AbstractSmackIntegrationTest {
public class SimpleXmppConnectionIntegrationTest extends AbstractSmackLowLevelIntegrationTest {
public SimpleXmppConnectionIntegrationTest(SmackIntegrationTestEnvironment environment) {
super(environment);
}
@SmackIntegrationTest
public void createConnectionTest() throws TimeoutException, Exception {
@SmackIntegrationTest(connectionCount = 2)
public void createConnectionTest(List<AbstractXMPPConnection> connections) throws TimeoutException, Exception {
final AbstractXMPPConnection conOne = connections.get(0), conTwo = connections.get(1);
EntityFullJid userTwo = conTwo.getUser();
final String messageBody = testRunId + ": Hello from the other side!";