diff --git a/base/src/main/java/de/staropensource/sosengine/unittests/UnitConfig.java b/base/src/main/java/de/staropensource/sosengine/unittests/UnitConfig.java new file mode 100644 index 00000000..1f8bf13d --- /dev/null +++ b/base/src/main/java/de/staropensource/sosengine/unittests/UnitConfig.java @@ -0,0 +1,16 @@ +package de.staropensource.sosengine.unittests; + +import de.staropensource.sosengine.base.types.LogLevel; + +/** + * Static configuration for the sos!engine unit testing helpers. + */ +public class UnitConfig { + /** + * Configures the {@link LogLevel} for the {@link UnitLogger}.
+ * Note: The log level {@code SILENT_WARNING} is used for printing test calls. + * + * @see de.staropensource.sosengine.base.logging.Logger + */ + public static LogLevel loggerLevel = LogLevel.SILENT_WARNING; +} diff --git a/base/src/main/java/de/staropensource/sosengine/unittests/UnitLogger.java b/base/src/main/java/de/staropensource/sosengine/unittests/UnitLogger.java new file mode 100644 index 00000000..319a18a3 --- /dev/null +++ b/base/src/main/java/de/staropensource/sosengine/unittests/UnitLogger.java @@ -0,0 +1,119 @@ +package de.staropensource.sosengine.unittests; + +import de.staropensource.sosengine.base.types.LogLevel; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.List; + +/** + * Provides a extremely dead simple logging system for unit tests. + */ +@SuppressWarnings("unused") +public class UnitLogger { + /** + * The parent class. + */ + private final Class clazz; + + /** + * Constructor. + * + * @param clazz the parent class + */ + public UnitLogger(Class clazz) { + this.clazz = clazz; + } + + /** + * Prints a log message. + * + * @param level the log level + * @param message the log message (and arguments if called with level {@code SILENT_WARNING}) + */ + private void log(@NotNull LogLevel level, @NotNull String message, List<@Nullable Object> additionalStuff) { + String messageSingle = message; + String levelName; + + if (level.compareTo(UnitConfig.loggerLevel) < 0) + return; + + switch (level) { + case CRASH -> levelName = "CRSH"; + case ERROR -> levelName = "ERR!"; + case SILENT_WARNING -> { + levelName = "TEST"; + + StringBuilder args = new StringBuilder(); + for (Object arg : additionalStuff) { + if (!args.isEmpty()) args.append(" "); + args.append("'"); + if (arg instanceof String) + args.append("\"").append(arg).append("\""); + else + args.append(arg); + args.append("'"); + } + + messageSingle = "Executing test " + message + "(" + args + ")"; + } + default -> levelName = level.name(); + } + + System.out.println("[" + levelName + " " + clazz.getName() + "] " + messageSingle); + } + + /** + * Prints a diagnostic message. + * + * @param message the diagnostic log message + */ + public void diag(@NotNull String message) { + log(LogLevel.DIAGNOSTIC, message, null); + } + + /** + * Prints a verbose message. + * + * @param message the verbose log message + */ + public void verb(@NotNull String message) { + log(LogLevel.VERBOSE, message, null); + } + + /** + * Prints a test call message. + * + * @param methodName the name of the test method called + */ + public void testCall(@NotNull String methodName, @Nullable Object ... methodArgs) { + log(LogLevel.SILENT_WARNING, methodName, List.of(methodArgs)); + } + + /** + * Prints a informational message. + * + * @param message the informational log message + */ + public void info(@NotNull String message) { + log(LogLevel.INFORMATIONAL, message, null); + } + + /** + * Prints a warning message. + * + * @param message the warning log message + */ + public void warn(@NotNull String message) { + log(LogLevel.WARNING, message, null); + } + + /** + * Prints a error message. + * + * @param message the error log message + */ + public void error(@NotNull String message) { + log(LogLevel.ERROR, message, null); + } +} diff --git a/base/src/main/java/de/staropensource/sosengine/unittests/package-info.java b/base/src/main/java/de/staropensource/sosengine/unittests/package-info.java new file mode 100644 index 00000000..8ae2ea8d --- /dev/null +++ b/base/src/main/java/de/staropensource/sosengine/unittests/package-info.java @@ -0,0 +1,5 @@ +/** + * Classes in this package are designed to be used within unit tests. + * They aim at simplifying certain aspects of unit testing. + */ +package de.staropensource.sosengine.unittests;