From 1409757f46f9c414c2b3f8a31030b1e9338f9336 Mon Sep 17 00:00:00 2001 From: Silvio Giebl Date: Mon, 4 Mar 2024 16:45:04 +0100 Subject: [PATCH] Fix tests for Java 21 > avoid System.exit() in tests remove dependency com.ginsberg:junit5-system-exit --- build.gradle.kts | 2 -- gradle/libs.versions.toml | 2 -- .../commands/cli/TestBrokerCommandDefaultIT.java | 16 +++++++--------- .../commands/cli/TestBrokerCommandQos0IT.java | 9 ++++----- .../commands/cli/TestBrokerCommandQos1IT.java | 10 +++++----- .../cli/TestBrokerCommandRestrictedIT.java | 10 +++++----- src/main/java/com/hivemq/cli/MqttCLIMain.java | 13 +++++++------ .../java/com/hivemq/cli/MqttCLIMainTest.java | 15 ++++++--------- 8 files changed, 34 insertions(+), 43 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 235fbe8fc..254053b71 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -232,7 +232,6 @@ testing { val test by getting(JvmTestSuite::class) { dependencies { - implementation(libs.junit.systemExit) implementation(libs.mockito) implementation(libs.okhttp.mockWebserver) } @@ -244,7 +243,6 @@ testing { dependencies { implementation(libs.awaitility) implementation(libs.hivemq.testcontainer.junit5) - implementation(libs.junit.systemExit) implementation(libs.mockito) implementation(libs.testcontainers) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 9abb4a005..777f936b1 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -21,7 +21,6 @@ jsr305 = "3.0.2" junit-jupiter = "5.10.2" junit-pioneer = "2.2.0" junit-platformLauncher = "1.10.2" -junit-systemExit = "1.1.2" mockito = "5.10.0" netty = "4.1.107.Final" okhttp = "4.12.0" @@ -57,7 +56,6 @@ jsr305 = { module = "com.google.code.findbugs:jsr305", version.ref = "jsr305" } junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" } junit-pioneer = { module = "org.junit-pioneer:junit-pioneer", version.ref = "junit-pioneer" } junit-platformLauncher = { module = "org.junit.platform:junit-platform-launcher", version.ref = "junit-platformLauncher" } -junit-systemExit = { module = "com.ginsberg:junit5-system-exit", version.ref = "junit-systemExit" } mockito = { module = "org.mockito:mockito-core", version.ref = "mockito" } netty-codec-http = { module = "io.netty:netty-codec-http", version.ref = "netty" } netty-handler = { module = "io.netty:netty-handler", version.ref = "netty" } diff --git a/src/integrationTest/java/com/hivemq/cli/commands/cli/TestBrokerCommandDefaultIT.java b/src/integrationTest/java/com/hivemq/cli/commands/cli/TestBrokerCommandDefaultIT.java index 1a0006c9d..c8ad4472a 100644 --- a/src/integrationTest/java/com/hivemq/cli/commands/cli/TestBrokerCommandDefaultIT.java +++ b/src/integrationTest/java/com/hivemq/cli/commands/cli/TestBrokerCommandDefaultIT.java @@ -16,7 +16,6 @@ package com.hivemq.cli.commands.cli; -import com.ginsberg.junit.exit.ExpectSystemExitWithStatus; import com.hivemq.cli.MqttCLIMain; import com.hivemq.cli.utils.TestLoggerUtils; import com.hivemq.testcontainer.junit5.HiveMQTestContainerExtension; @@ -28,6 +27,8 @@ import org.junit.jupiter.api.Test; import org.testcontainers.utility.DockerImageName; +import static org.junit.jupiter.api.Assertions.assertEquals; + @Disabled("Tests are only used to check output") class TestBrokerCommandDefaultIT { @@ -50,26 +51,23 @@ static void afterAll() { } @Test - @ExpectSystemExitWithStatus(0) void mqtt3_failed_connect() { - MqttCLIMain.main("test", "-V", "3"); + assertEquals(0, MqttCLIMain.mainWithExitCode("test", "-V", "3")); } @Test - @ExpectSystemExitWithStatus(0) void mqtt5_failed_connect() { - MqttCLIMain.main("test", "-V", "5"); + assertEquals(0, MqttCLIMain.mainWithExitCode("test", "-V", "5")); } @Test - @ExpectSystemExitWithStatus(0) void mqtt3_features() { - MqttCLIMain.main("test", "-V", "3", "-p", String.valueOf(hivemq.getMqttPort())); + assertEquals(0, MqttCLIMain.mainWithExitCode("test", "-V", "3", "-p", String.valueOf(hivemq.getMqttPort()))); } @Test - @ExpectSystemExitWithStatus(0) void mqtt5_features() { - MqttCLIMain.main("test", "-V", "5", "-a", "-p", String.valueOf(hivemq.getMqttPort())); + assertEquals(0, + MqttCLIMain.mainWithExitCode("test", "-V", "5", "-a", "-p", String.valueOf(hivemq.getMqttPort()))); } } diff --git a/src/integrationTest/java/com/hivemq/cli/commands/cli/TestBrokerCommandQos0IT.java b/src/integrationTest/java/com/hivemq/cli/commands/cli/TestBrokerCommandQos0IT.java index 5535dbba6..adefa697f 100644 --- a/src/integrationTest/java/com/hivemq/cli/commands/cli/TestBrokerCommandQos0IT.java +++ b/src/integrationTest/java/com/hivemq/cli/commands/cli/TestBrokerCommandQos0IT.java @@ -16,7 +16,6 @@ package com.hivemq.cli.commands.cli; -import com.ginsberg.junit.exit.ExpectSystemExitWithStatus; import com.hivemq.cli.MqttCLIMain; import com.hivemq.cli.utils.TestLoggerUtils; import com.hivemq.testcontainer.junit5.HiveMQTestContainerExtension; @@ -29,6 +28,8 @@ import org.testcontainers.utility.DockerImageName; import org.testcontainers.utility.MountableFile; +import static org.junit.jupiter.api.Assertions.assertEquals; + @Disabled("Tests are only used to check output") class TestBrokerCommandQos0IT { @@ -52,14 +53,12 @@ static void afterAll() { } @Test - @ExpectSystemExitWithStatus(0) void qos0_restricted_mqtt3_features() { - MqttCLIMain.main("test", "-V", "3", "-p", String.valueOf(hivemq.getMqttPort())); + assertEquals(0, MqttCLIMain.mainWithExitCode("test", "-V", "3", "-p", String.valueOf(hivemq.getMqttPort()))); } @Test - @ExpectSystemExitWithStatus(0) void qos0_restricted_mqtt5_features() { - MqttCLIMain.main("test", "-V", "3", "-p", String.valueOf(hivemq.getMqttPort())); + assertEquals(0, MqttCLIMain.mainWithExitCode("test", "-V", "3", "-p", String.valueOf(hivemq.getMqttPort()))); } } diff --git a/src/integrationTest/java/com/hivemq/cli/commands/cli/TestBrokerCommandQos1IT.java b/src/integrationTest/java/com/hivemq/cli/commands/cli/TestBrokerCommandQos1IT.java index e7980e1da..1dd80842f 100644 --- a/src/integrationTest/java/com/hivemq/cli/commands/cli/TestBrokerCommandQos1IT.java +++ b/src/integrationTest/java/com/hivemq/cli/commands/cli/TestBrokerCommandQos1IT.java @@ -16,7 +16,6 @@ package com.hivemq.cli.commands.cli; -import com.ginsberg.junit.exit.ExpectSystemExitWithStatus; import com.hivemq.cli.MqttCLIMain; import com.hivemq.cli.utils.TestLoggerUtils; import com.hivemq.testcontainer.junit5.HiveMQTestContainerExtension; @@ -29,6 +28,8 @@ import org.testcontainers.utility.DockerImageName; import org.testcontainers.utility.MountableFile; +import static org.junit.jupiter.api.Assertions.assertEquals; + @Disabled("Tests are only used to check output") class TestBrokerCommandQos1IT { @@ -52,14 +53,13 @@ static void afterAll() { } @Test - @ExpectSystemExitWithStatus(0) void qos1_restricted_mqtt3_features() { - MqttCLIMain.main("test", "-V", "3", "-p", String.valueOf(hivemq.getMqttPort())); + assertEquals(0, MqttCLIMain.mainWithExitCode("test", "-V", "3", "-p", String.valueOf(hivemq.getMqttPort()))); } @Test - @ExpectSystemExitWithStatus(0) void qos1_restricted_mqtt5_features() { - MqttCLIMain.main("test", "-V", "5", "-a", "-p", String.valueOf(hivemq.getMqttPort())); + assertEquals(0, + MqttCLIMain.mainWithExitCode("test", "-V", "5", "-a", "-p", String.valueOf(hivemq.getMqttPort()))); } } diff --git a/src/integrationTest/java/com/hivemq/cli/commands/cli/TestBrokerCommandRestrictedIT.java b/src/integrationTest/java/com/hivemq/cli/commands/cli/TestBrokerCommandRestrictedIT.java index cbf8ed0d5..e31c5e83a 100644 --- a/src/integrationTest/java/com/hivemq/cli/commands/cli/TestBrokerCommandRestrictedIT.java +++ b/src/integrationTest/java/com/hivemq/cli/commands/cli/TestBrokerCommandRestrictedIT.java @@ -16,7 +16,6 @@ package com.hivemq.cli.commands.cli; -import com.ginsberg.junit.exit.ExpectSystemExitWithStatus; import com.hivemq.cli.MqttCLIMain; import com.hivemq.cli.utils.TestLoggerUtils; import com.hivemq.testcontainer.junit5.HiveMQTestContainerExtension; @@ -29,6 +28,8 @@ import org.testcontainers.utility.DockerImageName; import org.testcontainers.utility.MountableFile; +import static org.junit.jupiter.api.Assertions.assertEquals; + @Disabled("Tests are only used to check output") class TestBrokerCommandRestrictedIT { @@ -52,14 +53,13 @@ static void afterAll() { } @Test - @ExpectSystemExitWithStatus(0) void restricted_mqtt3_features() { - MqttCLIMain.main("test", "-V", "3", "-p", String.valueOf(hivemq.getMqttPort())); + assertEquals(0, MqttCLIMain.mainWithExitCode("test", "-V", "3", "-p", String.valueOf(hivemq.getMqttPort()))); } @Test - @ExpectSystemExitWithStatus(0) void restricted_mqtt5_features() { - MqttCLIMain.main("test", "-V", "5", "-a", "-p", String.valueOf(hivemq.getMqttPort())); + assertEquals(0, + MqttCLIMain.mainWithExitCode("test", "-V", "5", "-a", "-p", String.valueOf(hivemq.getMqttPort()))); } } diff --git a/src/main/java/com/hivemq/cli/MqttCLIMain.java b/src/main/java/com/hivemq/cli/MqttCLIMain.java index 9c572f747..3a63a15d4 100644 --- a/src/main/java/com/hivemq/cli/MqttCLIMain.java +++ b/src/main/java/com/hivemq/cli/MqttCLIMain.java @@ -39,6 +39,10 @@ public class MqttCLIMain { public static @Nullable MqttCLI MQTT_CLI = null; public static void main(final @NotNull String... args) { + System.exit(mainWithExitCode(args)); + } + + public static int mainWithExitCode(final @NotNull String... args) { Security.setProperty("crypto.policy", "unlimited"); @@ -50,20 +54,17 @@ public static void main(final @NotNull String... args) { defaultCLIProperties.init(); } catch (final Exception e) { System.err.println(e.getMessage()); - System.exit(1); + return 1; } if (args.length == 0) { System.out.println(commandLine.getUsageMessage()); - System.exit(0); + return 0; } Runtime.getRuntime().addShutdownHook(new DisconnectAllClientsTask()); - final int exitCode = commandLine.execute(args); - - System.exit(exitCode); - + return commandLine.execute(args); } private static class DisconnectAllClientsTask extends Thread { diff --git a/src/test/java/com/hivemq/cli/MqttCLIMainTest.java b/src/test/java/com/hivemq/cli/MqttCLIMainTest.java index de925f000..f13a696d0 100644 --- a/src/test/java/com/hivemq/cli/MqttCLIMainTest.java +++ b/src/test/java/com/hivemq/cli/MqttCLIMainTest.java @@ -15,12 +15,13 @@ */ package com.hivemq.cli; -import com.ginsberg.junit.exit.ExpectSystemExitWithStatus; import com.hivemq.cli.utils.TestLoggerUtils; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; + class MqttCLIMainTest { @BeforeEach @@ -34,26 +35,22 @@ void tearDown() { } @Test - @ExpectSystemExitWithStatus(0) void mqtt_command() { - MqttCLIMain.main(); + assertEquals(0, MqttCLIMain.mainWithExitCode()); } @Test - @ExpectSystemExitWithStatus(0) void hivemq_command() { - MqttCLIMain.main("hivemq"); + assertEquals(0, MqttCLIMain.mainWithExitCode("hivemq")); } @Test - @ExpectSystemExitWithStatus(0) void hivemq_export_command() { - MqttCLIMain.main("hivemq", "export"); + assertEquals(0, MqttCLIMain.mainWithExitCode("hivemq", "export")); } @Test - @ExpectSystemExitWithStatus(0) void hivemq_export_clients_help_command() { - MqttCLIMain.main("hivemq", "export", "clients", "-h"); + assertEquals(0, MqttCLIMain.mainWithExitCode("hivemq", "export", "clients", "-h")); } }