forked from StarOpenSource/Engine
Add unit tests package
This commit is contained in:
parent
51348cdd92
commit
2c5fd57694
3 changed files with 140 additions and 0 deletions
|
@ -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}.<br/>
|
||||
* 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;
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
Loading…
Reference in a new issue