Rewrite test control and configuration code
All checks were successful
build-and-test / test (push) Successful in 2m20s
build-and-test / generate-javadoc (push) Successful in 2m24s
build-and-test / build (push) Successful in 2m26s

This commit is contained in:
JeremyStar™ 2024-09-01 21:20:47 +02:00
parent d4c8494b34
commit 25c9f45817
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
12 changed files with 104 additions and 44 deletions

View file

@ -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<>();

View file

@ -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.
*/

View file

@ -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 {

View file

@ -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 {

View file

@ -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 {

View file

@ -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 {

View file

@ -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 {

View file

@ -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 {

View file

@ -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();

View file

@ -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<String, String> 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;

View file

@ -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<>();

View file

@ -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();