From ed9c94f17cf9714aacb1b202270967d99351bef1 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Mon, 19 Aug 2024 02:06:26 +0200 Subject: [PATCH] Added hacky way for controlling the execution of tests --- base/build.gradle | 6 ++++ .../sosengine/base/logging/Logger.java | 3 +- .../srctests/EngineConfigurationTest.java | 1 + .../sosengine/base/srctests/TestBase.java | 29 ++++++++++++++++++- .../FourNumberVersioningSystemTest.java | 1 + .../OneNumberVersioningSystemTest.java | 1 + .../SemanticVersioningSystemTest.java | 1 + .../StarOpenSourceVersioningSystemTest.java | 1 + .../ThreeNumberVersioningSystemTest.java | 1 + .../TwoNumberVersioningSystemTest.java | 1 + .../srctests/utility/MiscellaneousTest.java | 8 +++-- .../utility/PlaceholderEngineTest.java | 1 + .../utility/PropertiesReaderTest.java | 8 +++++ .../utility/StackTraceParserTest.java | 1 + 14 files changed, 58 insertions(+), 5 deletions(-) diff --git a/base/build.gradle b/base/build.gradle index 10e59f8f..0a8507a2 100644 --- a/base/build.gradle +++ b/base/build.gradle @@ -131,6 +131,12 @@ javadoc { test { useJUnitPlatform() + Map testConfiguration = new HashMap<>(); + for (String property : project.properties.keySet()) + if (property.startsWith("test.")) + testConfiguration.put(property, project.properties.get(property).toString()) + systemProperties(testConfiguration) + setMaxParallelForks(project.hasProperty("jobs") ? Integer.parseInt((String) project.property("jobs")) : 8) setForkEvery(1) setFailFast(true) diff --git a/base/src/main/java/de/staropensource/sosengine/base/logging/Logger.java b/base/src/main/java/de/staropensource/sosengine/base/logging/Logger.java index 5e8bcb9a..a3075e1b 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/logging/Logger.java +++ b/base/src/main/java/de/staropensource/sosengine/base/logging/Logger.java @@ -149,11 +149,10 @@ public final class Logger { .group(Engine.getThreadGroup()) .stackSize(10) .start(threadLogic); - } else { + } else // Restart logging thread if dead if (!loggingThread.isAlive()) loggingThread.start(); - } } /** diff --git a/base/src/test/java/de/staropensource/sosengine/base/srctests/EngineConfigurationTest.java b/base/src/test/java/de/staropensource/sosengine/base/srctests/EngineConfigurationTest.java index 9ca639f2..d74dbb21 100644 --- a/base/src/test/java/de/staropensource/sosengine/base/srctests/EngineConfigurationTest.java +++ b/base/src/test/java/de/staropensource/sosengine/base/srctests/EngineConfigurationTest.java @@ -43,6 +43,7 @@ class EngineConfigurationTest extends TestBase { @Test @DisplayName("Method loadConfiguration") void testLoadConfiguration() { + if (checkCondition()) return; getLogger().testCall("testLoadConfiguration"); Map<@NotNull String, @NotNull Object[]> settings = new HashMap<>(); diff --git a/base/src/test/java/de/staropensource/sosengine/base/srctests/TestBase.java b/base/src/test/java/de/staropensource/sosengine/base/srctests/TestBase.java index 944ab283..be33db1d 100644 --- a/base/src/test/java/de/staropensource/sosengine/base/srctests/TestBase.java +++ b/base/src/test/java/de/staropensource/sosengine/base/srctests/TestBase.java @@ -22,9 +22,11 @@ package de.staropensource.sosengine.base.srctests; import de.staropensource.sosengine.base.Engine; import de.staropensource.sosengine.unittests.UnitLogger; import lombok.Getter; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; +import java.util.Locale; +import java.util.Objects; + /** * A class implemented by all tests. */ @@ -61,6 +63,31 @@ public class TestBase { instance = this; } + /** + * Allows specifying that only some tests are executed. + */ + protected final boolean checkCondition() { + switch (System.getProperties().getProperty("test.controlmode")) { + case "force-disable" -> { + if (Objects.equals(System.getProperties().getProperty("test.control." + getClass().getName().replace(getClass().getPackageName() + ".", "").toLowerCase(Locale.ROOT)), "false")) { + if (!Objects.equals(System.getProperties().getProperty("test.silentdisable"), "true")) + logger.warn("The test " + getClass().getName() + " has been force-disabled and will be skipped"); + return true; + } + } + case "force-enable" -> { + if (!Objects.equals(System.getProperties().getProperty("test.control." + getClass().getName().replace(getClass().getPackageName() + ".", "").toLowerCase(Locale.ROOT)), "true")) { + if (!Objects.equals(System.getProperties().getProperty("test.silentdisable"), "true")) + logger.warn("The test suite is currently in force-enable mode and " + getClass().getName() + " does not have clearance to execute, meaning it will be skipped"); + return true; + } + } + case null, default -> {} + } + + return false; + } + /** * Initializes the engine before running tests. */ diff --git a/base/src/test/java/de/staropensource/sosengine/base/srctests/data/versioning/FourNumberVersioningSystemTest.java b/base/src/test/java/de/staropensource/sosengine/base/srctests/data/versioning/FourNumberVersioningSystemTest.java index 3eb994b1..2a56f551 100644 --- a/base/src/test/java/de/staropensource/sosengine/base/srctests/data/versioning/FourNumberVersioningSystemTest.java +++ b/base/src/test/java/de/staropensource/sosengine/base/srctests/data/versioning/FourNumberVersioningSystemTest.java @@ -50,6 +50,7 @@ public class FourNumberVersioningSystemTest extends TestBase { "1.1.0.0, 1.1.1.1, 2", }) void testCompare(String a, String b, int expected) throws Throwable { + if (checkCondition()) return; getLogger().testCall("testCompare", a, b, expected); try { diff --git a/base/src/test/java/de/staropensource/sosengine/base/srctests/data/versioning/OneNumberVersioningSystemTest.java b/base/src/test/java/de/staropensource/sosengine/base/srctests/data/versioning/OneNumberVersioningSystemTest.java index 16427266..6430e705 100644 --- a/base/src/test/java/de/staropensource/sosengine/base/srctests/data/versioning/OneNumberVersioningSystemTest.java +++ b/base/src/test/java/de/staropensource/sosengine/base/srctests/data/versioning/OneNumberVersioningSystemTest.java @@ -47,6 +47,7 @@ public class OneNumberVersioningSystemTest extends TestBase { "1, 2, 2", }) void testCompare(String a, String b, int expected) throws Throwable { + if (checkCondition()) return; getLogger().testCall("testCompare", a, b, expected); try { diff --git a/base/src/test/java/de/staropensource/sosengine/base/srctests/data/versioning/SemanticVersioningSystemTest.java b/base/src/test/java/de/staropensource/sosengine/base/srctests/data/versioning/SemanticVersioningSystemTest.java index e1e90c66..bc40cb06 100644 --- a/base/src/test/java/de/staropensource/sosengine/base/srctests/data/versioning/SemanticVersioningSystemTest.java +++ b/base/src/test/java/de/staropensource/sosengine/base/srctests/data/versioning/SemanticVersioningSystemTest.java @@ -55,6 +55,7 @@ public class SemanticVersioningSystemTest extends TestBase { "1.1.0+5, 1.1.0+6, 2", }) void testCompare(String a, String b, int expected) throws Throwable { + if (checkCondition()) return; getLogger().testCall("testCompare", a, b, expected); try { diff --git a/base/src/test/java/de/staropensource/sosengine/base/srctests/data/versioning/StarOpenSourceVersioningSystemTest.java b/base/src/test/java/de/staropensource/sosengine/base/srctests/data/versioning/StarOpenSourceVersioningSystemTest.java index 3d4c07e3..923d3e6d 100644 --- a/base/src/test/java/de/staropensource/sosengine/base/srctests/data/versioning/StarOpenSourceVersioningSystemTest.java +++ b/base/src/test/java/de/staropensource/sosengine/base/srctests/data/versioning/StarOpenSourceVersioningSystemTest.java @@ -54,6 +54,7 @@ public class StarOpenSourceVersioningSystemTest extends TestBase { "v9-beta5, v9-releasecandidate1, 2", }) void testCompare(String a, String b, int expected) throws Throwable { + if (checkCondition()) return; getLogger().testCall("testCompare", a, b, expected); try { diff --git a/base/src/test/java/de/staropensource/sosengine/base/srctests/data/versioning/ThreeNumberVersioningSystemTest.java b/base/src/test/java/de/staropensource/sosengine/base/srctests/data/versioning/ThreeNumberVersioningSystemTest.java index 33ac0371..1d3568da 100644 --- a/base/src/test/java/de/staropensource/sosengine/base/srctests/data/versioning/ThreeNumberVersioningSystemTest.java +++ b/base/src/test/java/de/staropensource/sosengine/base/srctests/data/versioning/ThreeNumberVersioningSystemTest.java @@ -46,6 +46,7 @@ public class ThreeNumberVersioningSystemTest extends TestBase { "1.1.0, 1.1.1, 2", }) void testCompare(String a, String b, int expected) throws Throwable { + if (checkCondition()) return; getLogger().testCall("testCompare", a, b, expected); try { diff --git a/base/src/test/java/de/staropensource/sosengine/base/srctests/data/versioning/TwoNumberVersioningSystemTest.java b/base/src/test/java/de/staropensource/sosengine/base/srctests/data/versioning/TwoNumberVersioningSystemTest.java index bdeafe71..537e629f 100644 --- a/base/src/test/java/de/staropensource/sosengine/base/srctests/data/versioning/TwoNumberVersioningSystemTest.java +++ b/base/src/test/java/de/staropensource/sosengine/base/srctests/data/versioning/TwoNumberVersioningSystemTest.java @@ -46,6 +46,7 @@ public class TwoNumberVersioningSystemTest extends TestBase { "1.5, 2.0, 2", }) void testCompare(String a, String b, int expected) throws Throwable { + if (checkCondition()) return; getLogger().testCall("testCompare", a, b, expected); try { diff --git a/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/MiscellaneousTest.java b/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/MiscellaneousTest.java index 6cf99136..18291cfc 100644 --- a/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/MiscellaneousTest.java +++ b/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/MiscellaneousTest.java @@ -63,6 +63,8 @@ public class MiscellaneousTest extends TestBase { "5819853, 10, 0005819853" }) void testPadNumbers(int number, int length, String expected) { + if (checkCondition()) return; + getLogger().testCall("testPadNumbers", number, length, expected); String result = Math.padNumbers(number, length); assertEquals(expected, result, "Result \"" + result + "\" does not match expected output \"" + expected + "\""); @@ -77,6 +79,7 @@ public class MiscellaneousTest extends TestBase { 100, 250, 500 }) void testMeasureExecutionTime(int sleepingDuration) { + if (checkCondition()) return; getLogger().testCall("testMeasureExecutionTime", sleepingDuration); long executionTime = Miscellaneous.measureExecutionTime(() -> { @@ -96,6 +99,7 @@ public class MiscellaneousTest extends TestBase { @Test @DisplayName("getMapValues") void testGetMapValues() { + if (checkCondition()) return; getLogger().testCall("testGetMapValues"); Map testMap = new HashMap<>(); @@ -118,10 +122,10 @@ public class MiscellaneousTest extends TestBase { @Test @DisplayName("executeSafely (test 0)") void testExecuteSafely0() { + if (checkCondition()) return; getLogger().testCall("testExecuteSafely0"); throwableCaught = false; - Miscellaneous.executeSafely(() -> System.out.println("You can safely ignore this message (this comes from MiscellaneousTest#testExecuteSafely0)"), "MiscellaneousTest#testExecuteSafely0"); assertFalse(throwableCaught, "Event was triggered"); @@ -133,8 +137,8 @@ public class MiscellaneousTest extends TestBase { @Test @DisplayName("executeSafely (test 1)") void testExecuteSafely1() { + if (checkCondition()) return; getLogger().testCall("testExecuteSafely1"); - throwableCaught = false; // Disable event optimization for instant results diff --git a/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/PlaceholderEngineTest.java b/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/PlaceholderEngineTest.java index 0c833604..b7bb0e6b 100644 --- a/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/PlaceholderEngineTest.java +++ b/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/PlaceholderEngineTest.java @@ -49,6 +49,7 @@ class PlaceholderEngineTest extends TestBase { "This %status%!, This works!", }) void testProcess(String text, String expected) { + if (checkCondition()) return; getLogger().testCall("testProcess", text, expected); List<@NotNull Placeholder> placeholders = new ArrayList<>(); diff --git a/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/PropertiesReaderTest.java b/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/PropertiesReaderTest.java index ce7feb21..e654682b 100644 --- a/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/PropertiesReaderTest.java +++ b/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/PropertiesReaderTest.java @@ -46,6 +46,7 @@ class PropertiesReaderTest extends TestBase { "a.property.mightLookLikeThis, some value" }) void testGetString(String propertyName, String propertyValue) { + if (checkCondition()) return; getLogger().testCall("testGetString", propertyName, propertyValue); Properties properties = new Properties(); @@ -70,6 +71,7 @@ class PropertiesReaderTest extends TestBase { "nah, no, false", }) void testGetBoolean(String propertyName, String propertyValue, boolean expected) { + if (checkCondition()) return; getLogger().testCall("testGetBoolean", propertyName, propertyValue, expected); Properties properties = new Properties(); @@ -94,6 +96,7 @@ class PropertiesReaderTest extends TestBase { "he's a 1 but he likes 0s, 1, 1", }) void testGetByte(String propertyName, String propertyValue, byte expected) { + if (checkCondition()) return; getLogger().testCall("testGetByte", propertyName, propertyValue, expected); Properties properties = new Properties(); @@ -118,6 +121,7 @@ class PropertiesReaderTest extends TestBase { "he's a 1 but he likes 0s, 1, 1", }) void testGetShort(String propertyName, String propertyValue, short expected) { + if (checkCondition()) return; getLogger().testCall("testGetShort", propertyName, propertyValue, expected); Properties properties = new Properties(); @@ -143,6 +147,7 @@ class PropertiesReaderTest extends TestBase { "he's a 10 but he likes 69, 10, 10", }) void testGetInteger(String propertyName, String propertyValue, int expected) { + if (checkCondition()) return; getLogger().testCall("testGetInteger", propertyName, propertyValue, expected); Properties properties = new Properties(); @@ -168,6 +173,7 @@ class PropertiesReaderTest extends TestBase { "he's a 10 but he likes 69, 10, 10", }) void testGetLong(String propertyName, String propertyValue, long expected) { + if (checkCondition()) return; getLogger().testCall("testGetLong", propertyName, propertyValue, expected); Properties properties = new Properties(); @@ -194,6 +200,7 @@ class PropertiesReaderTest extends TestBase { "he's a 10 but he likes 69, 10.69, 10.69", }) void testGetFloat(String propertyName, String propertyValue, float expected) { + if (checkCondition()) return; getLogger().testCall("testGetFloat", propertyName, propertyValue, expected); Properties properties = new Properties(); @@ -218,6 +225,7 @@ class PropertiesReaderTest extends TestBase { "he's a 10 but he likes 69, 10.69, 10.69", }) void testGetDouble(String propertyName, String propertyValue, double expected) { + if (checkCondition()) return; getLogger().testCall("testGetDouble", propertyName, propertyValue, expected); Properties properties = new Properties(); diff --git a/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/StackTraceParserTest.java b/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/StackTraceParserTest.java index 99e6e1a1..83f4bf0a 100644 --- a/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/StackTraceParserTest.java +++ b/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/StackTraceParserTest.java @@ -44,6 +44,7 @@ class StackTraceParserTest extends TestBase { "Some description here..." }) void testGetHeader(@NotNull String message) { + if (checkCondition()) return; getLogger().testCall("testGetHeader", message); String output;