From 16ea0999405ef242b34dd6bf707645dafa9200ed Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Tue, 5 Nov 2024 11:20:40 +0100 Subject: [PATCH] [integration-test] Check that connections are connected for normal integration tests --- .../inttest/SmackIntegrationTestFramework.java | 18 +++++++++++++++++- .../DummySmackIntegrationTestFramework.java | 7 ++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/SmackIntegrationTestFramework.java b/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/SmackIntegrationTestFramework.java index 0d6cf1c971..71961aec9c 100644 --- a/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/SmackIntegrationTestFramework.java +++ b/smack-integration-test/src/main/java/org/igniterealtime/smack/inttest/SmackIntegrationTestFramework.java @@ -48,6 +48,7 @@ import java.util.TreeSet; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.stream.Collectors; import org.jivesoftware.smack.AbstractXMPPConnection; import org.jivesoftware.smack.ConnectionConfiguration; @@ -57,6 +58,7 @@ import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException.NoResponseException; import org.jivesoftware.smack.SmackException.NotConnectedException; +import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException.XMPPErrorException; import org.jivesoftware.smack.util.StringUtils; @@ -483,7 +485,13 @@ private void runTests(Set> classes) for (Method testMethod : smackIntegrationTestMethods) { switch (testType) { case Normal: { - ConcreteTest.Executor concreteTestExecutor = () -> testMethod.invoke(test); + ConcreteTest.Executor concreteTestExecutor = () -> { + AbstractSmackIntegrationTest abstractTest = (AbstractSmackIntegrationTest) test; + + throwIfDisconnectedConnections(abstractTest, testMethod, "Cannot execute test of"); + testMethod.invoke(test); + throwIfDisconnectedConnections(abstractTest, testMethod, "There where disconnected connections after executing"); + }; ConcreteTest concreteTest = new ConcreteTest(testType, testMethod, concreteTestExecutor); concreteTests.add(concreteTest); } @@ -1111,4 +1119,12 @@ static TestMethodParameterType determineTestMethodParameterType(Method testMetho return null; } + private static void throwIfDisconnectedConnections(AbstractSmackIntegrationTest abstractTest, Method testMethod, String message) throws IOException { + List disconnectedConnections = abstractTest.connections.stream().filter(c -> !c.isConnected()).collect(Collectors.toList()); + if (disconnectedConnections.isEmpty()) return; + + throw new IOException(message + " " + testMethod.getDeclaringClass().getSimpleName() + "." + + testMethod.getName() + ", as not all connections are connected. Disconnected connections: " + + disconnectedConnections); + } } diff --git a/smack-integration-test/src/test/java/org/igniterealtime/smack/inttest/DummySmackIntegrationTestFramework.java b/smack-integration-test/src/test/java/org/igniterealtime/smack/inttest/DummySmackIntegrationTestFramework.java index f16e2ba547..92c086a806 100644 --- a/smack-integration-test/src/test/java/org/igniterealtime/smack/inttest/DummySmackIntegrationTestFramework.java +++ b/smack-integration-test/src/test/java/org/igniterealtime/smack/inttest/DummySmackIntegrationTestFramework.java @@ -1,6 +1,6 @@ /** * - * Copyright 2015-2020 Florian Schmaus + * Copyright 2015-2024 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -53,6 +53,11 @@ public DummySmackIntegrationTestFramework(Configuration configuration) throws Ke @Override protected SmackIntegrationTestEnvironment prepareEnvironment() { DummyConnection dummyConnection = new DummyConnection(); + try { + dummyConnection.connect(); + } catch (SmackException | IOException | XMPPException | InterruptedException e) { + throw new AssertionError(e); + } connectionManager.conOne = connectionManager.conTwo = connectionManager.conThree = dummyConnection; return new SmackIntegrationTestEnvironment(dummyConnection, dummyConnection, dummyConnection, testRunResult.getTestRunId(), config, null);