From 4b5f9413c1114071a34b9d2ac6eeb68823d0b1c3 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Sun, 9 Jun 2024 00:22:29 +0200 Subject: [PATCH] Add partial unit tests for some classes Will continue creating unit tests tomorrow. These classes are now tested against: - base.Engine - base.EngineConfiguation - base.utility.Miscellaneous - base.utility.PlaceholderEngine --- .../srctests/EngineConfigurationTest.java | 87 +++++++++++++++++++ .../sosengine/base/srctests/EngineTest.java | 31 +++++++ .../srctests/utility/MiscellaneousTest.java | 39 +++++++++ .../utility/PlaceholderEngineTest.java | 66 ++++++++++++++ 4 files changed, 223 insertions(+) create mode 100644 base/src/test/java/de/staropensource/sosengine/base/srctests/EngineConfigurationTest.java create mode 100644 base/src/test/java/de/staropensource/sosengine/base/srctests/EngineTest.java create mode 100644 base/src/test/java/de/staropensource/sosengine/base/srctests/utility/MiscellaneousTest.java create mode 100644 base/src/test/java/de/staropensource/sosengine/base/srctests/utility/PlaceholderEngineTest.java diff --git a/base/src/test/java/de/staropensource/sosengine/base/srctests/EngineConfigurationTest.java b/base/src/test/java/de/staropensource/sosengine/base/srctests/EngineConfigurationTest.java new file mode 100644 index 0000000..91cbcfe --- /dev/null +++ b/base/src/test/java/de/staropensource/sosengine/base/srctests/EngineConfigurationTest.java @@ -0,0 +1,87 @@ +package de.staropensource.sosengine.base.srctests; + +import de.staropensource.sosengine.base.EngineConfiguration; +import de.staropensource.sosengine.unittests.UnitLogger; +import org.joor.Reflect; +import org.junit.jupiter.api.*; + +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Tests the class {@link EngineConfiguration}. + */ +@SuppressWarnings("unused") +@DisplayName("EngineConfiguration") +class EngineConfigurationTest { + /** + * The unit logger for this instance. + */ + private final UnitLogger logger = new UnitLogger(getClass()); + + /** + * 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 clearClass() { + Reflect.on(EngineConfiguration.getInstance()).set("instance", null); + } + + /** + * Tests the method {@code loadConfiguration}. + */ + @Test + @DisplayName("Method loadConfiguration") + void testLoadConfiguration() { + logger.testCall("testLoadConfiguration"); + + EngineConfiguration config = EngineConfiguration.getInstance(); + String group = EngineConfiguration.getGroup(); + String[] keys = new String[]{ + "debug", + "debugShortcodeConverter", + "errorShortcodeConverter", + "loggerLevel", + "loggerTemplate" + }; + Map defaultValues = new HashMap<>(); + + // Load default configuration + config.loadDefaultConfiguration(); + + // Save default values + for (String setting : keys) + defaultValues.put(setting, config.getSetting(setting)); + + // Load custom configuration + Properties properties = new Properties(); + + properties.setProperty(group + "debug", "true"); + properties.setProperty(group + "debugShortcodeConverter", "true"); + properties.setProperty(group + "errorShortcodeConverter", "false"); + properties.setProperty(group + "loggerLevel", "diagnostic"); + properties.setProperty(group + "loggerTemplate", "%log_path% says: %message%"); + + config.loadConfiguration(properties); + + // Compare custom values against default ones + for (String setting : keys) { + Object defaultValue = defaultValues.get(setting); + Object customValue = config.getSetting(setting); + + logger.diag("Comparing setting " + setting); + assertNotEquals(defaultValue, customValue); + } + } +} diff --git a/base/src/test/java/de/staropensource/sosengine/base/srctests/EngineTest.java b/base/src/test/java/de/staropensource/sosengine/base/srctests/EngineTest.java new file mode 100644 index 0000000..20bcab6 --- /dev/null +++ b/base/src/test/java/de/staropensource/sosengine/base/srctests/EngineTest.java @@ -0,0 +1,31 @@ +package de.staropensource.sosengine.base.srctests; + +import de.staropensource.sosengine.base.Engine; +import de.staropensource.sosengine.unittests.UnitLogger; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Tests the class {@link EngineTest}. + */ +@SuppressWarnings("unused") +@DisplayName("Engine") +class EngineTest { + /** + * The {@link UnitLogger} for this instance. + */ + private final UnitLogger logger = new UnitLogger(getClass()); + + /** + * Tests the constructor. + */ + @Test + @DisplayName("Constructor") + void testConstructor() { + logger.testCall("testConstructor"); + + assertNotEquals(new Engine(), null, "Engine should not be null"); + } +} diff --git a/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/MiscellaneousTest.java b/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/MiscellaneousTest.java new file mode 100644 index 0000000..37b776c --- /dev/null +++ b/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/MiscellaneousTest.java @@ -0,0 +1,39 @@ +package de.staropensource.sosengine.base.srctests.utility; + +import de.staropensource.sosengine.base.utility.Miscellaneous; +import de.staropensource.sosengine.unittests.UnitLogger; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Tests the class {@link Miscellaneous}. + */ +@SuppressWarnings("unused") +@DisplayName("Miscellaneous") +class MiscellaneousTest { + /** + * The {@link UnitLogger} for this instance. + */ + private final UnitLogger logger = new UnitLogger(getClass()); + + /** + * Tests the method {@code padNumbers}. + */ + @ParameterizedTest + @DisplayName("padNumbers") + @CsvSource({ + "5, 3, 005", + "15, 2, 15", + "666, 2, 666", + "5918, 6, 005918", + "5819853, 10, 0005819853" + }) + void testPadNumbers(int number, int length, String expected) { + logger.testCall("testPadNumbers", number, length, expected); + String result = Miscellaneous.padNumbers(number, length); + assertEquals(result, expected, "Result \"" + result + "\" does not match expected output \"" + expected + "\""); + } +} diff --git a/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/PlaceholderEngineTest.java b/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/PlaceholderEngineTest.java new file mode 100644 index 0000000..d2ec108 --- /dev/null +++ b/base/src/test/java/de/staropensource/sosengine/base/srctests/utility/PlaceholderEngineTest.java @@ -0,0 +1,66 @@ +package de.staropensource.sosengine.base.srctests.utility; + +import de.staropensource.sosengine.base.classes.Placeholder; +import de.staropensource.sosengine.base.utility.PlaceholderEngine; +import de.staropensource.sosengine.unittests.UnitLogger; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Tests the class {@link PlaceholderEngine}. + */ +@SuppressWarnings("unused") +@DisplayName("PlaceholderEngine") +class PlaceholderEngineTest { + /** + * The {@link UnitLogger} for this instance. + */ + private final UnitLogger logger = new UnitLogger(getClass()); + + /** + * Tests the method {@code process}. + */ + @ParameterizedTest + @DisplayName("process") + @CsvSource({ + "Test placeholder %test%, Test placeholder TEST", + "%invalid% must be invalid, %invalid% must be invalid", + "This %status%!, This works!" + }) + void testProcess(String text, String expected) { + logger.testCall("testProcess", text, expected); + + List placeholders = new ArrayList<>(); + placeholders.add(new Placeholder() { + @Override + public @NotNull String getName() { + return "test"; + } + @Override + public @NotNull String replace(@NotNull String text) { + return text.replace("%" + getName() + "%", "TEST"); + } + }); + placeholders.add(new Placeholder() { + @Override + public @NotNull String getName() { + return "status"; + } + @Override + public @NotNull String replace(@NotNull String text) { + return text.replace("%" + getName() + "%", "works"); + } + }); + + text = PlaceholderEngine.getInstance().process(text, placeholders); + + assertEquals(text, expected); + } +}