Fix unit tests

This commit is contained in:
JeremyStar™ 2024-07-29 19:58:41 +02:00
parent 006283acff
commit cbf434ed45
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
19 changed files with 159 additions and 223 deletions

View file

@ -18,9 +18,6 @@
*/ */
import java.nio.file.Files import java.nio.file.Files
// Plugins // Plugins
plugins { plugins {
id("java") id("java")
@ -135,6 +132,11 @@ javadoc {
// Unit testing configuration // Unit testing configuration
test { test {
useJUnitPlatform() useJUnitPlatform()
setMaxParallelForks(project.hasProperty("jobs") ? Integer.parseInt((String) project.property("jobs")) : 8)
setForkEvery(1)
setFailFast(true)
testLogging { testLogging {
events("passed", "skipped", "failed") events("passed", "skipped", "failed")
} }

View file

@ -411,13 +411,4 @@ public final class EngineConfiguration implements SubsystemConfiguration {
default -> { return null; } default -> { return null; }
} }
} }
/**
* Clears {@code instance}. Used in unit tests.
*
* @since v1-alpha1
*/
private static void clearInstance() {
instance = null;
}
} }

View file

@ -89,8 +89,6 @@ public final class PlaceholderEngine {
placeholders.add(new DateMonth()); placeholders.add(new DateMonth());
placeholders.add(new DateYear()); placeholders.add(new DateYear());
// engine_dependency_* // engine_dependency_*
placeholders.add(new EngineDependencyLombok());
placeholders.add(new EngineDependencyJetbrainsAnnotations());
placeholders.add(new EngineDependencyJansi()); placeholders.add(new EngineDependencyJansi());
placeholders.add(new EngineDependencyLwjgl()); placeholders.add(new EngineDependencyLwjgl());
placeholders.add(new EngineDependencyReflections()); placeholders.add(new EngineDependencyReflections());
@ -164,13 +162,4 @@ public final class PlaceholderEngine {
public String process(@NotNull String text) { public String process(@NotNull String text) {
return process(text, new ArrayList<>()); return process(text, new ArrayList<>());
} }
/**
* Clears {@code instance}. Used in unit tests.
*
* @since v1-alpha1
*/
private static void clearInstance() {
instance = null;
}
} }

View file

@ -20,18 +20,11 @@
package de.staropensource.sosengine.base.srctests; package de.staropensource.sosengine.base.srctests;
import de.staropensource.sosengine.base.EngineConfiguration; import de.staropensource.sosengine.base.EngineConfiguration;
import de.staropensource.sosengine.base.exceptions.UnexpectedThrowableException;
import de.staropensource.sosengine.base.exceptions.reflection.InvalidMethodException;
import de.staropensource.sosengine.base.reflection.Reflect;
import de.staropensource.sosengine.base.types.logging.LogLevel; import de.staropensource.sosengine.base.types.logging.LogLevel;
import de.staropensource.sosengine.unittests.UnitLogger;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
@ -43,40 +36,19 @@ import static org.junit.jupiter.api.Assertions.*;
*/ */
@SuppressWarnings("unused") @SuppressWarnings("unused")
@DisplayName("EngineConfiguration") @DisplayName("EngineConfiguration")
class EngineConfigurationTest { class EngineConfigurationTest extends TestBase {
/**
* The unit logger for this instance.
*/
private static final UnitLogger logger = new UnitLogger(EngineConfigurationTest.class);
/** /**
* The property group for the base engine. * The property group for the base engine.
*/ */
private final String group = EngineConfiguration.getGroup(); private final String group = EngineConfiguration.getGroup();
/**
* Initializes the {@link EngineConfiguration} class before each test.
*/
@BeforeEach
void initializeClass() {
new EngineConfiguration();
}
/**
* Sets the {@link EngineConfiguration}#{@code instance} to {@code null} after each test.
*/
@AfterEach
void clearInstance() throws InvalidMethodException, UnexpectedThrowableException, InvocationTargetException {
Reflect.reflectOn(EngineConfiguration.class).getMethod("clearInstance").invoke();
}
/** /**
* Tests the method {@code loadConfiguration}. * Tests the method {@code loadConfiguration}.
*/ */
@Test @Test
@DisplayName("Method loadConfiguration") @DisplayName("Method loadConfiguration")
void testLoadConfiguration() { void testLoadConfiguration() {
logger.testCall("testLoadConfiguration"); getLogger().testCall("testLoadConfiguration");
Map<@NotNull String, @NotNull Object[]> settings = new HashMap<>(); Map<@NotNull String, @NotNull Object[]> settings = new HashMap<>();
Map<@NotNull String, @NotNull Object> defaultValues = new HashMap<>(); Map<@NotNull String, @NotNull Object> defaultValues = new HashMap<>();
@ -120,9 +92,9 @@ class EngineConfigurationTest {
Object defaultValue = defaultValues.get(setting); Object defaultValue = defaultValues.get(setting);
Object customValue = EngineConfiguration.getInstance().getSetting(setting); Object customValue = EngineConfiguration.getInstance().getSetting(setting);
logger.diag("Comparing setting " + setting + "(not equals defaultValue)"); getLogger().diag("Comparing setting " + setting + "(not equals defaultValue)");
assertNotEquals(defaultValue, customValue); assertNotEquals(defaultValue, customValue);
logger.diag("Comparing setting " + setting + "(equals expected value)"); getLogger().diag("Comparing setting " + setting + "(equals expected value)");
assertEquals(settings.get(setting)[1], customValue); assertEquals(settings.get(setting)[1], customValue);
} }
} }

View file

@ -0,0 +1,72 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
package de.staropensource.sosengine.base.srctests;
import de.staropensource.sosengine.base.Engine;
import de.staropensource.sosengine.unittests.UnitLogger;
import lombok.Getter;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
/**
* A class implemented by all tests.
*/
@SuppressWarnings({ "unused" })
public class TestBase {
/**
* Instance of this test class.
* -- GETTER --
* Returns the instance of this test class.
*/
@Getter
private static TestBase instance;
/**
* The unit logger for this test class.
* -- GETTER --
* Returns the unit logger for this test class.
*/
@Getter
private final UnitLogger logger = new UnitLogger(getClass());
/**
* Determines whether the engine should be automatically initialized or not.
*/
protected boolean doInitialization = true;
/**
* Constructs this class.
*/
public TestBase() {
// JUnit for some reason recreates the test class every time a test method is called
// Therefore we don't throw or complain here, instead we just override 'instance' and
// hope nothing breaks.
instance = this;
}
/**
* Initializes the engine before running tests.
*/
@BeforeEach
void initializeEngine() {
if (doInitialization && Engine.getInstance() == null)
new Engine();
}
}

View file

@ -21,12 +21,10 @@ package de.staropensource.sosengine.base.srctests.data.versioning;
import de.staropensource.sosengine.base.data.versioning.FourNumberVersioningSystem; import de.staropensource.sosengine.base.data.versioning.FourNumberVersioningSystem;
import de.staropensource.sosengine.base.exceptions.versioning.InvalidVersionStringException; import de.staropensource.sosengine.base.exceptions.versioning.InvalidVersionStringException;
import de.staropensource.sosengine.unittests.UnitLogger; import de.staropensource.sosengine.base.srctests.TestBase;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.CsvSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
@ -36,13 +34,7 @@ import static org.junit.jupiter.api.Assertions.fail;
*/ */
@SuppressWarnings({ "unused" }) @SuppressWarnings({ "unused" })
@DisplayName("FourNumberVersioningSystem") @DisplayName("FourNumberVersioningSystem")
public class FourNumberVersioningSystemTest { public class FourNumberVersioningSystemTest extends TestBase {
/**
* The unit logger for this instance.
*/
private static final UnitLogger logger = new UnitLogger(FourNumberVersioningSystemTest.class);
private static final Logger log = LoggerFactory.getLogger(FourNumberVersioningSystemTest.class);
/** /**
* Tests the method {@code compare}. * Tests the method {@code compare}.
*/ */
@ -58,14 +50,14 @@ public class FourNumberVersioningSystemTest {
"1.1.0.0, 1.1.1.1, 2", "1.1.0.0, 1.1.1.1, 2",
}) })
void testCompare(String a, String b, int expected) throws Throwable { void testCompare(String a, String b, int expected) throws Throwable {
logger.testCall("testCompare", a, b, expected); getLogger().testCall("testCompare", a, b, expected);
try { try {
assertEquals(expected, new FourNumberVersioningSystem(a).compare(new FourNumberVersioningSystem(b))); assertEquals(expected, new FourNumberVersioningSystem(a).compare(new FourNumberVersioningSystem(b)));
} catch (InvalidVersionStringException exception) { } catch (InvalidVersionStringException exception) {
logger.error("Got InvalidVersionStringException: " + exception.getMessage()); getLogger().error("Got InvalidVersionStringException: " + exception.getMessage());
if (exception.getThrowable() != null) { if (exception.getThrowable() != null) {
logger.error("Original stack trace:"); getLogger().error("Original stack trace:");
throw exception.getThrowable(); throw exception.getThrowable();
} }
fail("Got InvalidVersionStringException"); fail("Got InvalidVersionStringException");

View file

@ -22,12 +22,10 @@ package de.staropensource.sosengine.base.srctests.data.versioning;
import de.staropensource.sosengine.base.data.versioning.OneNumberVersioningSystem; import de.staropensource.sosengine.base.data.versioning.OneNumberVersioningSystem;
import de.staropensource.sosengine.base.data.versioning.TwoNumberVersioningSystem; import de.staropensource.sosengine.base.data.versioning.TwoNumberVersioningSystem;
import de.staropensource.sosengine.base.exceptions.versioning.InvalidVersionStringException; import de.staropensource.sosengine.base.exceptions.versioning.InvalidVersionStringException;
import de.staropensource.sosengine.unittests.UnitLogger; import de.staropensource.sosengine.base.srctests.TestBase;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.CsvSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
@ -37,13 +35,7 @@ import static org.junit.jupiter.api.Assertions.fail;
*/ */
@SuppressWarnings({ "unused" }) @SuppressWarnings({ "unused" })
@DisplayName("OneNumberVersioningSystem") @DisplayName("OneNumberVersioningSystem")
public class OneNumberVersioningSystemTest { public class OneNumberVersioningSystemTest extends TestBase {
/**
* The unit logger for this instance.
*/
private static final UnitLogger logger = new UnitLogger(OneNumberVersioningSystemTest.class);
private static final Logger log = LoggerFactory.getLogger(OneNumberVersioningSystemTest.class);
/** /**
* Tests the method {@code compare}. * Tests the method {@code compare}.
*/ */
@ -55,14 +47,14 @@ public class OneNumberVersioningSystemTest {
"1, 2, 2", "1, 2, 2",
}) })
void testCompare(String a, String b, int expected) throws Throwable { void testCompare(String a, String b, int expected) throws Throwable {
logger.testCall("testCompare", a, b, expected); getLogger().testCall("testCompare", a, b, expected);
try { try {
assertEquals(expected, new OneNumberVersioningSystem(a).compare(new OneNumberVersioningSystem(b))); assertEquals(expected, new OneNumberVersioningSystem(a).compare(new OneNumberVersioningSystem(b)));
} catch (InvalidVersionStringException exception) { } catch (InvalidVersionStringException exception) {
logger.error("Got InvalidVersionStringException: " + exception.getMessage()); getLogger().error("Got InvalidVersionStringException: " + exception.getMessage());
if (exception.getThrowable() != null) { if (exception.getThrowable() != null) {
logger.error("Original stack trace:"); getLogger().error("Original stack trace:");
throw exception.getThrowable(); throw exception.getThrowable();
} }
fail("Got InvalidVersionStringException"); fail("Got InvalidVersionStringException");

View file

@ -21,12 +21,10 @@ package de.staropensource.sosengine.base.srctests.data.versioning;
import de.staropensource.sosengine.base.data.versioning.SemanticVersioningSystem; import de.staropensource.sosengine.base.data.versioning.SemanticVersioningSystem;
import de.staropensource.sosengine.base.exceptions.versioning.InvalidVersionStringException; import de.staropensource.sosengine.base.exceptions.versioning.InvalidVersionStringException;
import de.staropensource.sosengine.unittests.UnitLogger; import de.staropensource.sosengine.base.srctests.TestBase;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.CsvSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
@ -36,13 +34,7 @@ import static org.junit.jupiter.api.Assertions.fail;
*/ */
@SuppressWarnings({ "unused" }) @SuppressWarnings({ "unused" })
@DisplayName("SemanticVersioningSystem") @DisplayName("SemanticVersioningSystem")
public class SemanticVersioningSystemTest { public class SemanticVersioningSystemTest extends TestBase {
/**
* The unit logger for this instance.
*/
private static final UnitLogger logger = new UnitLogger(SemanticVersioningSystemTest.class);
private static final Logger log = LoggerFactory.getLogger(SemanticVersioningSystemTest.class);
/** /**
* Tests the method {@code compare}. * Tests the method {@code compare}.
*/ */
@ -63,14 +55,14 @@ public class SemanticVersioningSystemTest {
"1.1.0+5, 1.1.0+6, 2", "1.1.0+5, 1.1.0+6, 2",
}) })
void testCompare(String a, String b, int expected) throws Throwable { void testCompare(String a, String b, int expected) throws Throwable {
logger.testCall("testCompare", a, b, expected); getLogger().testCall("testCompare", a, b, expected);
try { try {
assertEquals(expected, new SemanticVersioningSystem(a).compare(new SemanticVersioningSystem(b))); assertEquals(expected, new SemanticVersioningSystem(a).compare(new SemanticVersioningSystem(b)));
} catch (InvalidVersionStringException exception) { } catch (InvalidVersionStringException exception) {
logger.error("Got InvalidVersionStringException: " + exception.getMessage()); getLogger().error("Got InvalidVersionStringException: " + exception.getMessage());
if (exception.getThrowable() != null) { if (exception.getThrowable() != null) {
logger.error("Original stack trace:"); getLogger().error("Original stack trace:");
throw exception.getThrowable(); throw exception.getThrowable();
} }
fail("Got InvalidVersionStringException"); fail("Got InvalidVersionStringException");

View file

@ -22,7 +22,7 @@ package de.staropensource.sosengine.base.srctests.data.versioning;
import de.staropensource.sosengine.base.data.versioning.SemanticVersioningSystem; import de.staropensource.sosengine.base.data.versioning.SemanticVersioningSystem;
import de.staropensource.sosengine.base.data.versioning.StarOpenSourceVersioningSystem; import de.staropensource.sosengine.base.data.versioning.StarOpenSourceVersioningSystem;
import de.staropensource.sosengine.base.exceptions.versioning.InvalidVersionStringException; import de.staropensource.sosengine.base.exceptions.versioning.InvalidVersionStringException;
import de.staropensource.sosengine.unittests.UnitLogger; import de.staropensource.sosengine.base.srctests.TestBase;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.CsvSource;
@ -35,12 +35,7 @@ import static org.junit.jupiter.api.Assertions.fail;
*/ */
@SuppressWarnings({ "unused" }) @SuppressWarnings({ "unused" })
@DisplayName("StarOpenSourceVersioningSystem") @DisplayName("StarOpenSourceVersioningSystem")
public class StarOpenSourceVersioningSystemTest { public class StarOpenSourceVersioningSystemTest extends TestBase {
/**
* The unit logger for this instance.
*/
private static final UnitLogger logger = new UnitLogger(StarOpenSourceVersioningSystemTest.class);
/** /**
* Tests the method {@code compare}. * Tests the method {@code compare}.
*/ */
@ -59,14 +54,14 @@ public class StarOpenSourceVersioningSystemTest {
"v9-beta5, v9-releasecandidate1, 2", "v9-beta5, v9-releasecandidate1, 2",
}) })
void testCompare(String a, String b, int expected) throws Throwable { void testCompare(String a, String b, int expected) throws Throwable {
logger.testCall("testCompare", a, b, expected); getLogger().testCall("testCompare", a, b, expected);
try { try {
assertEquals(expected, new StarOpenSourceVersioningSystem(a).compare(new StarOpenSourceVersioningSystem(b))); assertEquals(expected, new StarOpenSourceVersioningSystem(a).compare(new StarOpenSourceVersioningSystem(b)));
} catch (InvalidVersionStringException exception) { } catch (InvalidVersionStringException exception) {
logger.error("Got InvalidVersionStringException: " + exception.getMessage()); getLogger().error("Got InvalidVersionStringException: " + exception.getMessage());
if (exception.getThrowable() != null) { if (exception.getThrowable() != null) {
logger.error("Original stack trace:"); getLogger().error("Original stack trace:");
throw exception.getThrowable(); throw exception.getThrowable();
} }
fail("Got InvalidVersionStringException"); fail("Got InvalidVersionStringException");

View file

@ -21,12 +21,10 @@ package de.staropensource.sosengine.base.srctests.data.versioning;
import de.staropensource.sosengine.base.data.versioning.ThreeNumberVersioningSystem; import de.staropensource.sosengine.base.data.versioning.ThreeNumberVersioningSystem;
import de.staropensource.sosengine.base.exceptions.versioning.InvalidVersionStringException; import de.staropensource.sosengine.base.exceptions.versioning.InvalidVersionStringException;
import de.staropensource.sosengine.unittests.UnitLogger; import de.staropensource.sosengine.base.srctests.TestBase;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.CsvSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
@ -36,13 +34,7 @@ import static org.junit.jupiter.api.Assertions.fail;
*/ */
@SuppressWarnings({ "unused" }) @SuppressWarnings({ "unused" })
@DisplayName("ThreeNumberVersioningSystem") @DisplayName("ThreeNumberVersioningSystem")
public class ThreeNumberVersioningSystemTest { public class ThreeNumberVersioningSystemTest extends TestBase {
/**
* The unit logger for this instance.
*/
private static final UnitLogger logger = new UnitLogger(ThreeNumberVersioningSystemTest.class);
private static final Logger log = LoggerFactory.getLogger(ThreeNumberVersioningSystemTest.class);
/** /**
* Tests the method {@code compare}. * Tests the method {@code compare}.
*/ */
@ -54,14 +46,14 @@ public class ThreeNumberVersioningSystemTest {
"1.1.0, 1.1.1, 2", "1.1.0, 1.1.1, 2",
}) })
void testCompare(String a, String b, int expected) throws Throwable { void testCompare(String a, String b, int expected) throws Throwable {
logger.testCall("testCompare", a, b, expected); getLogger().testCall("testCompare", a, b, expected);
try { try {
assertEquals(expected, new ThreeNumberVersioningSystem(a).compare(new ThreeNumberVersioningSystem(b))); assertEquals(expected, new ThreeNumberVersioningSystem(a).compare(new ThreeNumberVersioningSystem(b)));
} catch (InvalidVersionStringException exception) { } catch (InvalidVersionStringException exception) {
logger.error("Got InvalidVersionStringException: " + exception.getMessage()); getLogger().error("Got InvalidVersionStringException: " + exception.getMessage());
if (exception.getThrowable() != null) { if (exception.getThrowable() != null) {
logger.error("Original stack trace:"); getLogger().error("Original stack trace:");
throw exception.getThrowable(); throw exception.getThrowable();
} }
fail("Got InvalidVersionStringException"); fail("Got InvalidVersionStringException");

View file

@ -21,12 +21,10 @@ package de.staropensource.sosengine.base.srctests.data.versioning;
import de.staropensource.sosengine.base.data.versioning.TwoNumberVersioningSystem; import de.staropensource.sosengine.base.data.versioning.TwoNumberVersioningSystem;
import de.staropensource.sosengine.base.exceptions.versioning.InvalidVersionStringException; import de.staropensource.sosengine.base.exceptions.versioning.InvalidVersionStringException;
import de.staropensource.sosengine.unittests.UnitLogger; import de.staropensource.sosengine.base.srctests.TestBase;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.CsvSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
@ -36,13 +34,7 @@ import static org.junit.jupiter.api.Assertions.fail;
*/ */
@SuppressWarnings({ "unused" }) @SuppressWarnings({ "unused" })
@DisplayName("TwoNumberVersioningSystem") @DisplayName("TwoNumberVersioningSystem")
public class TwoNumberVersioningSystemTest { public class TwoNumberVersioningSystemTest extends TestBase {
/**
* The unit logger for this instance.
*/
private static final UnitLogger logger = new UnitLogger(TwoNumberVersioningSystemTest.class);
private static final Logger log = LoggerFactory.getLogger(TwoNumberVersioningSystemTest.class);
/** /**
* Tests the method {@code compare}. * Tests the method {@code compare}.
*/ */
@ -54,14 +46,14 @@ public class TwoNumberVersioningSystemTest {
"1.5, 2.0, 2", "1.5, 2.0, 2",
}) })
void testCompare(String a, String b, int expected) throws Throwable { void testCompare(String a, String b, int expected) throws Throwable {
logger.testCall("testCompare", a, b, expected); getLogger().testCall("testCompare", a, b, expected);
try { try {
assertEquals(expected, new TwoNumberVersioningSystem(a).compare(new TwoNumberVersioningSystem(b))); assertEquals(expected, new TwoNumberVersioningSystem(a).compare(new TwoNumberVersioningSystem(b)));
} catch (InvalidVersionStringException exception) { } catch (InvalidVersionStringException exception) {
logger.error("Got InvalidVersionStringException: " + exception.getMessage()); getLogger().error("Got InvalidVersionStringException: " + exception.getMessage());
if (exception.getThrowable() != null) { if (exception.getThrowable() != null) {
logger.error("Original stack trace:"); getLogger().error("Original stack trace:");
throw exception.getThrowable(); throw exception.getThrowable();
} }
fail("Got InvalidVersionStringException"); fail("Got InvalidVersionStringException");

View file

@ -22,12 +22,10 @@ package de.staropensource.sosengine.base.srctests.utility;
import de.staropensource.sosengine.base.EngineConfiguration; import de.staropensource.sosengine.base.EngineConfiguration;
import de.staropensource.sosengine.base.annotations.EventListener; import de.staropensource.sosengine.base.annotations.EventListener;
import de.staropensource.sosengine.base.events.ThrowableCatchEvent; import de.staropensource.sosengine.base.events.ThrowableCatchEvent;
import de.staropensource.sosengine.base.exceptions.UnexpectedThrowableException;
import de.staropensource.sosengine.base.exceptions.reflection.InvalidMethodException;
import de.staropensource.sosengine.base.reflection.Reflect; import de.staropensource.sosengine.base.reflection.Reflect;
import de.staropensource.sosengine.base.srctests.TestBase;
import de.staropensource.sosengine.base.utility.Math; import de.staropensource.sosengine.base.utility.Math;
import de.staropensource.sosengine.base.utility.Miscellaneous; import de.staropensource.sosengine.base.utility.Miscellaneous;
import de.staropensource.sosengine.unittests.UnitLogger;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
@ -36,8 +34,6 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource; import org.junit.jupiter.params.provider.ValueSource;
import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
@ -50,12 +46,7 @@ import static org.junit.jupiter.api.Assertions.*;
*/ */
@SuppressWarnings("unused") @SuppressWarnings("unused")
@DisplayName("Miscellaneous") @DisplayName("Miscellaneous")
public class MiscellaneousTest { public class MiscellaneousTest extends TestBase {
/**
* The {@link UnitLogger} for this instance.
*/
private static final UnitLogger logger = new UnitLogger(MiscellaneousTest.class);
/** /**
* Used for testing the method {@code executeSafely}. * Used for testing the method {@code executeSafely}.
*/ */
@ -65,7 +56,7 @@ public class MiscellaneousTest {
* Sets the {@link EngineConfiguration}#{@code instance} to {@code null} after each test. * Sets the {@link EngineConfiguration}#{@code instance} to {@code null} after each test.
*/ */
@AfterEach @AfterEach
void clearConfiguration() throws InvalidMethodException, UnexpectedThrowableException, InvocationTargetException { void clearConfiguration() throws Exception {
// For method testExecuteSafely1() // For method testExecuteSafely1()
Reflect.reflectOn(EngineConfiguration.class).getMethod("clearInstance").invoke(); Reflect.reflectOn(EngineConfiguration.class).getMethod("clearInstance").invoke();
} }
@ -83,26 +74,11 @@ public class MiscellaneousTest {
"5819853, 10, 0005819853" "5819853, 10, 0005819853"
}) })
void testPadNumbers(int number, int length, String expected) { void testPadNumbers(int number, int length, String expected) {
logger.testCall("testPadNumbers", number, length, expected); getLogger().testCall("testPadNumbers", number, length, expected);
String result = Math.padNumbers(number, length); String result = Math.padNumbers(number, length);
assertEquals(expected, result, "Result \"" + result + "\" does not match expected output \"" + expected + "\""); assertEquals(expected, result, "Result \"" + result + "\" does not match expected output \"" + expected + "\"");
} }
/**
* Tests the method {@code invokeGarbageCollector}.
*/
@Test
@DisplayName("invokeGarbageCollector")
void testInvokeGarbageCollector(){
logger.testCall("testInvokeGarbageCollector");
Object object = new Object();
WeakReference<Object> weakReference = new WeakReference<>(object);
object = null;
Miscellaneous.invokeGarbageCollector();
//noinspection ConstantValue // my god we know that it's very very likely true, that's what we're testing for here!
assertNull(object, "Garbage collector failed collecting nullified object");
}
/** /**
* Tests the method {@code measureExecutionTime}. * Tests the method {@code measureExecutionTime}.
*/ */
@ -112,13 +88,13 @@ public class MiscellaneousTest {
100, 250, 500 100, 250, 500
}) })
void testMeasureExecutionTime(int sleepingDuration) { void testMeasureExecutionTime(int sleepingDuration) {
logger.testCall("testMeasureExecutionTime", sleepingDuration); getLogger().testCall("testMeasureExecutionTime", sleepingDuration);
long executionTime = Miscellaneous.measureExecutionTime(() -> { long executionTime = Miscellaneous.measureExecutionTime(() -> {
try { try {
Thread.sleep(sleepingDuration); Thread.sleep(sleepingDuration);
} catch (InterruptedException e) { } catch (InterruptedException e) {
logger.error("Sleep was interrupted, test may fail"); getLogger().error("Sleep was interrupted, test may fail");
} }
}); });
@ -131,7 +107,7 @@ public class MiscellaneousTest {
@Test @Test
@DisplayName("getMapValues") @DisplayName("getMapValues")
void testGetMapValues() { void testGetMapValues() {
logger.testCall("testGetMapValues"); getLogger().testCall("testGetMapValues");
Map<String, String> testMap = new HashMap<>(); Map<String, String> testMap = new HashMap<>();
testMap.put("key1", "this is the first value"); testMap.put("key1", "this is the first value");
@ -153,13 +129,11 @@ public class MiscellaneousTest {
@Test @Test
@DisplayName("executeSafely (test 0)") @DisplayName("executeSafely (test 0)")
void testExecuteSafely0() { void testExecuteSafely0() {
logger.testCall("testExecuteSafely0"); getLogger().testCall("testExecuteSafely0");
throwableCaught = false; throwableCaught = false;
Miscellaneous.executeSafely(() -> { Miscellaneous.executeSafely(() -> System.out.println("You can safely ignore this message (this comes from MiscellaneousTest#testExecuteSafely0)"), "MiscellaneousTest#testExecuteSafely0");
System.out.println("You can safely ignore this message (this comes from MiscellaneousTest#testExecuteSafely0)");
}, "MiscellaneousTest#testExecuteSafely0");
assertFalse(throwableCaught, "Event was triggered"); assertFalse(throwableCaught, "Event was triggered");
} }
@ -170,7 +144,7 @@ public class MiscellaneousTest {
@Test @Test
@DisplayName("executeSafely (test 1)") @DisplayName("executeSafely (test 1)")
void testExecuteSafely1() { void testExecuteSafely1() {
logger.testCall("testExecuteSafely1"); getLogger().testCall("testExecuteSafely1");
throwableCaught = false; throwableCaught = false;
@ -192,7 +166,7 @@ public class MiscellaneousTest {
try { try {
Thread.sleep(200); Thread.sleep(200);
} catch (InterruptedException e) { } catch (InterruptedException e) {
logger.error("Sleep was interrupted, test may fail"); getLogger().error("Sleep was interrupted, test may fail");
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -207,8 +181,7 @@ public class MiscellaneousTest {
*/ */
@EventListener(event = ThrowableCatchEvent.class) @EventListener(event = ThrowableCatchEvent.class)
public static void onCaughtThrowable(@NotNull Throwable throwable, @NotNull String identifier) { public static void onCaughtThrowable(@NotNull Throwable throwable, @NotNull String identifier) {
logger.diag("ThrowableCatchEvent received"); getInstance().getLogger().diag("ThrowableCatchEvent received");
throwableCaught = true; throwableCaught = true;
throw new RuntimeException("aaaaa");
} }
} }

View file

@ -20,20 +20,13 @@
package de.staropensource.sosengine.base.srctests.utility; package de.staropensource.sosengine.base.srctests.utility;
import de.staropensource.sosengine.base.classes.Placeholder; import de.staropensource.sosengine.base.classes.Placeholder;
import de.staropensource.sosengine.base.exceptions.UnexpectedThrowableException; import de.staropensource.sosengine.base.srctests.TestBase;
import de.staropensource.sosengine.base.exceptions.reflection.InvalidMethodException;
import de.staropensource.sosengine.base.exceptions.reflection.NoAccessException;
import de.staropensource.sosengine.base.reflection.Reflect;
import de.staropensource.sosengine.base.utility.PlaceholderEngine; import de.staropensource.sosengine.base.utility.PlaceholderEngine;
import de.staropensource.sosengine.unittests.UnitLogger;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.CsvSource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -44,28 +37,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
*/ */
@SuppressWarnings("unused") @SuppressWarnings("unused")
@DisplayName("PlaceholderEngine") @DisplayName("PlaceholderEngine")
class PlaceholderEngineTest { class PlaceholderEngineTest extends TestBase {
/**
* The {@link UnitLogger} for this instance.
*/
private static final UnitLogger logger = new UnitLogger(PlaceholderEngineTest.class);
/**
* Creates a new {@link PlaceholderEngine} instance before each test.
*/
@BeforeEach
void createInstance() {
new PlaceholderEngine();
}
/**
* Sets the {@link PlaceholderEngine}#{@code instance} to {@code null} after each test.
*/
@AfterEach
void clearInstance() throws InvalidMethodException, UnexpectedThrowableException, InvocationTargetException, NoAccessException {
Reflect.reflectOn(PlaceholderEngine.class).getMethod("clearInstance").invoke();
}
/** /**
* Tests the method {@code process}. * Tests the method {@code process}.
*/ */
@ -77,7 +49,7 @@ class PlaceholderEngineTest {
"This %status%!, This works!", "This %status%!, This works!",
}) })
void testProcess(String text, String expected) { void testProcess(String text, String expected) {
logger.testCall("testProcess", text, expected); getLogger().testCall("testProcess", text, expected);
List<@NotNull Placeholder> placeholders = new ArrayList<>(); List<@NotNull Placeholder> placeholders = new ArrayList<>();
placeholders.add(new Placeholder() { placeholders.add(new Placeholder() {

View file

@ -19,8 +19,8 @@
package de.staropensource.sosengine.base.srctests.utility; package de.staropensource.sosengine.base.srctests.utility;
import de.staropensource.sosengine.base.srctests.TestBase;
import de.staropensource.sosengine.base.utility.parser.PropertyParser; import de.staropensource.sosengine.base.utility.parser.PropertyParser;
import de.staropensource.sosengine.unittests.UnitLogger;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.CsvSource;
@ -33,12 +33,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
* Tests the class {@link PropertyParser}. * Tests the class {@link PropertyParser}.
*/ */
@DisplayName("PropertyParser") @DisplayName("PropertyParser")
class PropertyParserTest { class PropertyParserTest extends TestBase {
/**
* The {@link UnitLogger} for this instance.
*/
private static final UnitLogger logger = new UnitLogger(PropertyParserTest.class);
/** /**
* Tests the method {@code getString}. * Tests the method {@code getString}.
*/ */
@ -51,7 +46,7 @@ class PropertyParserTest {
"a.property.mightLookLikeThis, some value" "a.property.mightLookLikeThis, some value"
}) })
void testGetString(String propertyName, String propertyValue) { void testGetString(String propertyName, String propertyValue) {
logger.testCall("testGetString", propertyName, propertyValue); getLogger().testCall("testGetString", propertyName, propertyValue);
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty(propertyName, propertyValue); properties.setProperty(propertyName, propertyValue);
@ -75,7 +70,7 @@ class PropertyParserTest {
"nah, no, false", "nah, no, false",
}) })
void testGetBoolean(String propertyName, String propertyValue, boolean expected) { void testGetBoolean(String propertyName, String propertyValue, boolean expected) {
logger.testCall("testGetBoolean", propertyName, propertyValue, expected); getLogger().testCall("testGetBoolean", propertyName, propertyValue, expected);
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty(propertyName, propertyValue); properties.setProperty(propertyName, propertyValue);
@ -99,7 +94,7 @@ class PropertyParserTest {
"he's a 1 but he likes 0s, 1, 1", "he's a 1 but he likes 0s, 1, 1",
}) })
void testGetByte(String propertyName, String propertyValue, byte expected) { void testGetByte(String propertyName, String propertyValue, byte expected) {
logger.testCall("testGetByte", propertyName, propertyValue, expected); getLogger().testCall("testGetByte", propertyName, propertyValue, expected);
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty(propertyName, propertyValue); properties.setProperty(propertyName, propertyValue);
@ -123,7 +118,7 @@ class PropertyParserTest {
"he's a 1 but he likes 0s, 1, 1", "he's a 1 but he likes 0s, 1, 1",
}) })
void testGetShort(String propertyName, String propertyValue, short expected) { void testGetShort(String propertyName, String propertyValue, short expected) {
logger.testCall("testGetShort", propertyName, propertyValue, expected); getLogger().testCall("testGetShort", propertyName, propertyValue, expected);
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty(propertyName, propertyValue); properties.setProperty(propertyName, propertyValue);
@ -148,7 +143,7 @@ class PropertyParserTest {
"he's a 10 but he likes 69, 10, 10", "he's a 10 but he likes 69, 10, 10",
}) })
void testGetInteger(String propertyName, String propertyValue, int expected) { void testGetInteger(String propertyName, String propertyValue, int expected) {
logger.testCall("testGetInteger", propertyName, propertyValue, expected); getLogger().testCall("testGetInteger", propertyName, propertyValue, expected);
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty(propertyName, propertyValue); properties.setProperty(propertyName, propertyValue);
@ -173,7 +168,7 @@ class PropertyParserTest {
"he's a 10 but he likes 69, 10, 10", "he's a 10 but he likes 69, 10, 10",
}) })
void testGetLong(String propertyName, String propertyValue, long expected) { void testGetLong(String propertyName, String propertyValue, long expected) {
logger.testCall("testGetLong", propertyName, propertyValue, expected); getLogger().testCall("testGetLong", propertyName, propertyValue, expected);
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty(propertyName, propertyValue); properties.setProperty(propertyName, propertyValue);
@ -199,7 +194,7 @@ class PropertyParserTest {
"he's a 10 but he likes 69, 10.69, 10.69", "he's a 10 but he likes 69, 10.69, 10.69",
}) })
void testGetFloat(String propertyName, String propertyValue, float expected) { void testGetFloat(String propertyName, String propertyValue, float expected) {
logger.testCall("testGetFloat", propertyName, propertyValue, expected); getLogger().testCall("testGetFloat", propertyName, propertyValue, expected);
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty(propertyName, propertyValue); properties.setProperty(propertyName, propertyValue);
@ -223,7 +218,7 @@ class PropertyParserTest {
"he's a 10 but he likes 69, 10.69, 10.69", "he's a 10 but he likes 69, 10.69, 10.69",
}) })
void testGetDouble(String propertyName, String propertyValue, double expected) { void testGetDouble(String propertyName, String propertyValue, double expected) {
logger.testCall("testGetDouble", propertyName, propertyValue, expected); getLogger().testCall("testGetDouble", propertyName, propertyValue, expected);
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty(propertyName, propertyValue); properties.setProperty(propertyName, propertyValue);

View file

@ -19,8 +19,8 @@
package de.staropensource.sosengine.base.srctests.utility; package de.staropensource.sosengine.base.srctests.utility;
import de.staropensource.sosengine.base.srctests.TestBase;
import de.staropensource.sosengine.base.utility.parser.StackTraceParser; import de.staropensource.sosengine.base.utility.parser.StackTraceParser;
import de.staropensource.sosengine.unittests.UnitLogger;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
@ -32,12 +32,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
* Tests the class {@link StackTraceParser}. * Tests the class {@link StackTraceParser}.
*/ */
@DisplayName("StackTraceParser") @DisplayName("StackTraceParser")
class StackTraceParserTest { class StackTraceParserTest extends TestBase {
/**
* The {@link UnitLogger} for this instance.
*/
private static final UnitLogger logger = new UnitLogger(StackTraceParserTest.class);
/** /**
* Tests the method {@code getHeader}. * Tests the method {@code getHeader}.
*/ */
@ -49,7 +44,7 @@ class StackTraceParserTest {
"Some description here..." "Some description here..."
}) })
void testGetHeader(@NotNull String message) { void testGetHeader(@NotNull String message) {
logger.testCall("testGetHeader", message); getLogger().testCall("testGetHeader", message);
String output; String output;
if (message.isEmpty()) if (message.isEmpty())

View file

@ -87,6 +87,11 @@ javadoc {
// Unit testing configuration // Unit testing configuration
test { test {
useJUnitPlatform() useJUnitPlatform()
setMaxParallelForks(project.hasProperty("jobs") ? Integer.parseInt((String) project.property("jobs")) : 8)
setForkEvery(1)
setFailFast(true)
testLogging { testLogging {
events("passed", "skipped", "failed") events("passed", "skipped", "failed")
} }

View file

@ -122,6 +122,11 @@ javadoc {
// Unit testing configuration // Unit testing configuration
test { test {
useJUnitPlatform() useJUnitPlatform()
setMaxParallelForks(project.hasProperty("jobs") ? Integer.parseInt((String) project.property("jobs")) : 8)
setForkEvery(1)
setFailFast(true)
testLogging { testLogging {
events("passed", "skipped", "failed") events("passed", "skipped", "failed")
} }

View file

@ -127,6 +127,11 @@ javadoc {
// Unit testing configuration // Unit testing configuration
test { test {
useJUnitPlatform() useJUnitPlatform()
setMaxParallelForks(project.hasProperty("jobs") ? Integer.parseInt((String) project.property("jobs")) : 8)
setForkEvery(1)
setFailFast(true)
testLogging { testLogging {
events("passed", "skipped", "failed") events("passed", "skipped", "failed")
} }

View file

@ -124,6 +124,11 @@ javadoc {
// Unit testing configuration // Unit testing configuration
test { test {
useJUnitPlatform() useJUnitPlatform()
setMaxParallelForks(project.hasProperty("jobs") ? Integer.parseInt((String) project.property("jobs")) : 8)
setForkEvery(1)
setFailFast(true)
testLogging { testLogging {
events("passed", "skipped", "failed") events("passed", "skipped", "failed")
} }