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
This commit is contained in:
JeremyStar™ 2024-06-09 00:22:29 +02:00
parent 1797be0037
commit 4b5f9413c1
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
4 changed files with 223 additions and 0 deletions

View file

@ -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<String, Object> 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);
}
}
}

View file

@ -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");
}
}

View file

@ -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 + "\"");
}
}

View file

@ -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<Placeholder> 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);
}
}