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 8f1b9789e..467276f7f 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 @@ -42,7 +42,7 @@ class EngineConfigurationTest extends TestBase { @Test @DisplayName("Method loadConfiguration") void testLoadConfiguration() { - if (checkCondition()) return; + if (isRestricted()) 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 ac13f7b10..a9eb37acc 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 @@ -23,13 +23,16 @@ import de.staropensource.sosengine.base.Engine; import de.staropensource.sosengine.base.EngineConfiguration; import de.staropensource.sosengine.base.EngineInternals; import de.staropensource.sosengine.base.type.InternalAccessArea; +import de.staropensource.sosengine.base.type.Tristate; +import de.staropensource.sosengine.base.type.logging.LogLevel; +import de.staropensource.sosengine.unittest.UnitConfig; import de.staropensource.sosengine.unittest.UnitLogger; import lombok.Getter; +import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeEach; -import java.util.Locale; -import java.util.Objects; +import java.util.*; /** * A class implemented by all tests. @@ -52,10 +55,26 @@ public class TestBase { private final UnitLogger logger = new UnitLogger(getClass()); /** - * Contains whether the engine should be automatically initialized or not. + * Controls whether the engine should be automatically initialized or not. */ protected boolean doInitialization = true; + /** + * Contains the test control mode. + * Can be {@link Tristate#TRUE} for force-enable, {@link Tristate#FALSE} for force-disable or {@link Tristate#UNSET} for no test execution control. + */ + protected @NotNull Tristate controlMode = Tristate.UNSET; + + /** + * Contains all class names which should be enabled/disabled during testing. + */ + protected @NotNull Set<@NotNull String> controlClasses = new HashSet<>(); + + /** + * Determines whether to print a warning when a restricted class is instructed to run it's tests. + */ + protected boolean controlWarning; + /** * Constructs this class. */ @@ -64,33 +83,74 @@ public class TestBase { // Therefore we don't throw or complain here, instead we just override 'instance' and // hope nothing breaks. instance = this; + + configureEnvironment(); + } + + /** + * Reads the system properties and configures the environment appropriately. + */ + public void configureEnvironment() { + for (Object propertyObject : System.getProperties().keySet()) { + String property = (String) propertyObject; + + // Ignore non-testing-related properties + if (!property.startsWith("test.")) + continue; + + // Strip "test." prefix from 'property' + String prefix = "test."; + property = property.substring(5); + + switch (property) { + // Set control mode + case "control.mode" -> { + switch (System.getProperties().getProperty(prefix + property)) { + case "force-enable" -> controlMode = Tristate.TRUE; + case "force-disable" -> controlMode = Tristate.FALSE; + default -> controlMode = Tristate.UNSET; + } + } + + // Set control classes + case "control.classes" -> controlClasses.addAll(Arrays.stream(System.getProperties().getProperty(prefix + property).split(",")).toList()); + + // Set control warning flag + case "control.warning" -> controlWarning = Objects.equals(System.getProperties().getProperty(prefix + property), "true"); + + // Set logger level + case "loggerLevel" -> { + try { + UnitConfig.loggerLevel = LogLevel.valueOf(System.getProperties().getProperty(prefix + property).toUpperCase(Locale.ROOT)); + } catch (IllegalStateException exception) { + logger.warn("Invalid value of property '" + prefix + property + "': " + System.getProperties().getProperty(prefix + property)); + } + } + } + } } /** * 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 -> {} - } + protected final boolean isRestricted(boolean printWarning) { + String className = getClass().getName().replace(getClass().getPackage().getName() + ".", ""); + boolean allowed = controlMode == Tristate.TRUE && !controlClasses.contains(className) + || controlMode == Tristate.FALSE && controlClasses.contains(className); - return false; + if (allowed && printWarning && controlWarning) + logger.warn("Test class " + className + " is restricted and it's tests will not be executed"); + + return allowed; } + /** + * Allows specifying that only some tests are executed. + */ + protected final boolean isRestricted() { + return isRestricted(true); + } + /** * Initializes the engine before running tests. */ diff --git a/base/src/test/java/de/staropensource/sosengine/base/srctests/implementation/versioning/FourNumberVersioningSystemTest.java b/base/src/test/java/de/staropensource/sosengine/base/srctests/implementation/versioning/FourNumberVersioningSystemTest.java index aec2f792a..1b8bc7334 100644 --- a/base/src/test/java/de/staropensource/sosengine/base/srctests/implementation/versioning/FourNumberVersioningSystemTest.java +++ b/base/src/test/java/de/staropensource/sosengine/base/srctests/implementation/versioning/FourNumberVersioningSystemTest.java @@ -49,7 +49,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; + if (isRestricted()) return; getLogger().testCall("testCompare", a, b, expected); try { diff --git a/base/src/test/java/de/staropensource/sosengine/base/srctests/implementation/versioning/OneNumberVersioningSystemTest.java b/base/src/test/java/de/staropensource/sosengine/base/srctests/implementation/versioning/OneNumberVersioningSystemTest.java index 5299444d0..777cb54b1 100644 --- a/base/src/test/java/de/staropensource/sosengine/base/srctests/implementation/versioning/OneNumberVersioningSystemTest.java +++ b/base/src/test/java/de/staropensource/sosengine/base/srctests/implementation/versioning/OneNumberVersioningSystemTest.java @@ -46,7 +46,7 @@ public class OneNumberVersioningSystemTest extends TestBase { "1, 2, 2", }) void testCompare(String a, String b, int expected) throws Throwable { - if (checkCondition()) return; + if (isRestricted()) return; getLogger().testCall("testCompare", a, b, expected); try { diff --git a/base/src/test/java/de/staropensource/sosengine/base/srctests/implementation/versioning/SemanticVersioningSystemTest.java b/base/src/test/java/de/staropensource/sosengine/base/srctests/implementation/versioning/SemanticVersioningSystemTest.java index ead22f2e5..075ba0c01 100644 --- a/base/src/test/java/de/staropensource/sosengine/base/srctests/implementation/versioning/SemanticVersioningSystemTest.java +++ b/base/src/test/java/de/staropensource/sosengine/base/srctests/implementation/versioning/SemanticVersioningSystemTest.java @@ -54,7 +54,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; + if (isRestricted()) return; getLogger().testCall("testCompare", a, b, expected); try { diff --git a/base/src/test/java/de/staropensource/sosengine/base/srctests/implementation/versioning/StarOpenSourceVersioningSystemTest.java b/base/src/test/java/de/staropensource/sosengine/base/srctests/implementation/versioning/StarOpenSourceVersioningSystemTest.java index e95ca0a21..b89635900 100644 --- a/base/src/test/java/de/staropensource/sosengine/base/srctests/implementation/versioning/StarOpenSourceVersioningSystemTest.java +++ b/base/src/test/java/de/staropensource/sosengine/base/srctests/implementation/versioning/StarOpenSourceVersioningSystemTest.java @@ -53,7 +53,7 @@ public class StarOpenSourceVersioningSystemTest extends TestBase { "v9-beta5, v9-releasecandidate1, 2", }) void testCompare(String a, String b, int expected) throws Throwable { - if (checkCondition()) return; + if (isRestricted()) return; getLogger().testCall("testCompare", a, b, expected); try { diff --git a/base/src/test/java/de/staropensource/sosengine/base/srctests/implementation/versioning/ThreeNumberVersioningSystemTest.java b/base/src/test/java/de/staropensource/sosengine/base/srctests/implementation/versioning/ThreeNumberVersioningSystemTest.java index b16096772..47d01a3c1 100644 --- a/base/src/test/java/de/staropensource/sosengine/base/srctests/implementation/versioning/ThreeNumberVersioningSystemTest.java +++ b/base/src/test/java/de/staropensource/sosengine/base/srctests/implementation/versioning/ThreeNumberVersioningSystemTest.java @@ -45,7 +45,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; + if (isRestricted()) return; getLogger().testCall("testCompare", a, b, expected); try { diff --git a/base/src/test/java/de/staropensource/sosengine/base/srctests/implementation/versioning/TwoNumberVersioningSystemTest.java b/base/src/test/java/de/staropensource/sosengine/base/srctests/implementation/versioning/TwoNumberVersioningSystemTest.java index 53039ec3f..0d85a6292 100644 --- a/base/src/test/java/de/staropensource/sosengine/base/srctests/implementation/versioning/TwoNumberVersioningSystemTest.java +++ b/base/src/test/java/de/staropensource/sosengine/base/srctests/implementation/versioning/TwoNumberVersioningSystemTest.java @@ -45,7 +45,7 @@ public class TwoNumberVersioningSystemTest extends TestBase { "1.5, 2.0, 2", }) void testCompare(String a, String b, int expected) throws Throwable { - if (checkCondition()) return; + if (isRestricted()) return; getLogger().testCall("testCompare", a, b, expected); try { diff --git a/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/DependencyResolverTest.java b/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/DependencyResolverTest.java index b72b9935e..edac40e20 100644 --- a/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/DependencyResolverTest.java +++ b/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/DependencyResolverTest.java @@ -43,7 +43,7 @@ class DependencyResolverTest extends TestBase { 0, 1, 2, 3 }) void testResolve(int layers) throws UnmetDependenciesException { - if (checkCondition()) return; + if (isRestricted()) return; getLogger().testCall("testResolve", layers); DependencyResolver resolver = new DependencyResolver(); 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 95009c6b2..65e5c595a 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 @@ -62,7 +62,7 @@ public class MiscellaneousTest extends TestBase { "5819853, 10, 0005819853" }) void testPadNumbers(int number, int length, String expected) { - if (checkCondition()) return; + if (isRestricted()) return; getLogger().testCall("testPadNumbers", number, length, expected); String result = Math.padNumbers(number, length); @@ -78,7 +78,7 @@ public class MiscellaneousTest extends TestBase { 100, 250, 500 }) void testMeasureExecutionTime(int sleepingDuration) { - if (checkCondition()) return; + if (isRestricted()) return; getLogger().testCall("testMeasureExecutionTime", sleepingDuration); long executionTime = Miscellaneous.measureExecutionTime(() -> { @@ -98,7 +98,7 @@ public class MiscellaneousTest extends TestBase { @Test @DisplayName("getMapValues") void testGetMapValues() { - if (checkCondition()) return; + if (isRestricted()) return; getLogger().testCall("testGetMapValues"); Map testMap = new HashMap<>(); @@ -121,7 +121,7 @@ public class MiscellaneousTest extends TestBase { @Test @DisplayName("executeSafely (test 0)") void testExecuteSafely0() { - if (checkCondition()) return; + if (isRestricted()) return; getLogger().testCall("testExecuteSafely0"); throwableCaught = false; @@ -136,7 +136,7 @@ public class MiscellaneousTest extends TestBase { @Test @DisplayName("executeSafely (test 1)") void testExecuteSafely1() { - if (checkCondition()) return; + if (isRestricted()) return; getLogger().testCall("testExecuteSafely1"); throwableCaught = false; 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 571343aab..14925182d 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 @@ -48,7 +48,7 @@ class PlaceholderEngineTest extends TestBase { "This %status%!, This works!", }) void testProcess(String text, String expected) { - if (checkCondition()) return; + if (isRestricted()) 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 d2538719b..404580f4c 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,7 +46,7 @@ class PropertiesReaderTest extends TestBase { "a.property.mightLookLikeThis, some value" }) void testGetString(String propertyName, String propertyValue) { - if (checkCondition()) return; + if (isRestricted()) return; getLogger().testCall("testGetString", propertyName, propertyValue); Properties properties = new Properties(); @@ -71,7 +71,7 @@ class PropertiesReaderTest extends TestBase { "nah, no, false", }) void testGetBoolean(String propertyName, String propertyValue, boolean expected) { - if (checkCondition()) return; + if (isRestricted()) return; getLogger().testCall("testGetBoolean", propertyName, propertyValue, expected); Properties properties = new Properties(); @@ -96,7 +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; + if (isRestricted()) return; getLogger().testCall("testGetByte", propertyName, propertyValue, expected); Properties properties = new Properties(); @@ -121,7 +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; + if (isRestricted()) return; getLogger().testCall("testGetShort", propertyName, propertyValue, expected); Properties properties = new Properties(); @@ -147,7 +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; + if (isRestricted()) return; getLogger().testCall("testGetInteger", propertyName, propertyValue, expected); Properties properties = new Properties(); @@ -173,7 +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; + if (isRestricted()) return; getLogger().testCall("testGetLong", propertyName, propertyValue, expected); Properties properties = new Properties(); @@ -200,7 +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; + if (isRestricted()) return; getLogger().testCall("testGetFloat", propertyName, propertyValue, expected); Properties properties = new Properties(); @@ -225,7 +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; + if (isRestricted()) return; getLogger().testCall("testGetDouble", propertyName, propertyValue, expected); Properties properties = new Properties();