/* * STAROPENSOURCE ENGINE SOURCE FILE * Copyright (c) 2024 The StarOpenSource Engine Contributors * Licensed under the GNU Affero General Public License v3 * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ package de.staropensource.sosengine.unittests; import de.staropensource.sosengine.base.logging.LoggerInstance; import de.staropensource.sosengine.base.types.logging.LogLevel; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** * Provides an extremely dead simple logging system for unit tests. * * @since v1-alpha0 */ @SuppressWarnings("unused") public class UnitLogger { /** * The parent class, similar to {@link LoggerInstance}. * * @since v1-alpha0 */ private final Class clazz; /** * Creates a new unit logger. * * @param clazz parent class, similar to {@link LoggerInstance} * @since v1-alpha0 */ public UnitLogger(Class clazz) { this.clazz = clazz; } /** * Prints a log message. * * @param level level * @param message message * @param additionalStuff additional arguments * @since v1-alpha0 */ private void log(@NotNull LogLevel level, @NotNull String message, @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) { boolean stringQuotes = arg instanceof String; // Print full class path for enums if (arg instanceof Enum) { arg = arg.getClass().getName() + "." + ((Enum) arg).name(); } if (!args.isEmpty()) args.append(" "); args.append("'"); if (stringQuotes) 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 message * @since v1-alpha0 */ public void diag(@NotNull String message) { log(LogLevel.DIAGNOSTIC, message); } /** * Prints a verbose message. * * @param message message * @since v1-alpha0 */ public void verb(@NotNull String message) { log(LogLevel.VERBOSE, message); } /** * Prints a test call message. * * @param methodName name of the test method * @param methodArgs arguments of the test method * @since v1-alpha0 */ public void testCall(@NotNull String methodName, @Nullable Object... methodArgs) { log(LogLevel.SILENT_WARNING, methodName, methodArgs); } /** * Prints a informational message. * * @param message message * @since v1-alpha0 */ public void info(@NotNull String message) { log(LogLevel.INFORMATIONAL, message); } /** * Prints a warning message. * * @param message message * @since v1-alpha0 */ public void warn(@NotNull String message) { log(LogLevel.WARNING, message); } /** * Prints a error message. * * @param message message * @since v1-alpha0 */ public void error(@NotNull String message) { log(LogLevel.ERROR, message); } }