Extend tests

... for Miscellaneous, PlaceholderEngine and PropertyParser
This commit is contained in:
JeremyStar™ 2024-06-27 22:27:11 +02:00
parent 170f5fd372
commit 0d55b436da
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
3 changed files with 357 additions and 16 deletions

View file

@ -1,10 +1,43 @@
/*
* 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.utility;
import de.staropensource.sosengine.base.EngineConfiguration;
import de.staropensource.sosengine.base.annotations.EventListener;
import de.staropensource.sosengine.base.events.ThrowableCatchEvent;
import de.staropensource.sosengine.base.utility.Miscellaneous;
import de.staropensource.sosengine.unittests.UnitLogger;
import org.jetbrains.annotations.NotNull;
import org.joor.Reflect;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
@ -13,11 +46,22 @@ import static org.junit.jupiter.api.Assertions.*;
*/
@SuppressWarnings("unused")
@DisplayName("Miscellaneous")
class MiscellaneousTest {
public class MiscellaneousTest {
/**
* The {@link UnitLogger} for this instance.
*/
private final UnitLogger logger = new UnitLogger(getClass());
private static final UnitLogger logger = new UnitLogger(MiscellaneousTest.class);
/**
* Used for testing the method {@code executeSafely}.
*/
private static boolean throwableCaught;
@AfterEach
void clearConfigurationInstance() {
// Only does something if 'EngineConfiguration#instance' is set, only applies to 'testExecuteSafely1'
Reflect.onClass(EngineConfiguration.class).call("clearInstance");
}
/**
* Tests the method {@code padNumbers}.
@ -36,4 +80,128 @@ class MiscellaneousTest {
String result = Miscellaneous.padNumbers(number, length);
assertEquals(result, expected, "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}.
*/
@ParameterizedTest
@DisplayName("measureExecutionTime")
@ValueSource(ints = {
100, 250, 500
})
void testMeasureExecutionTime(int sleepingDuration) {
logger.testCall("testMeasureExecutionTime", sleepingDuration);
long executionTime = Miscellaneous.measureExecutionTime(() -> {
try {
Thread.sleep(sleepingDuration);
} catch (InterruptedException e) {
logger.error("Sleep was interrupted, test may fail");
}
});
assertTrue(executionTime >= sleepingDuration, "Execution time is " + (sleepingDuration - executionTime) + " below the sleeping duration of " + sleepingDuration);
}
/**
* Tests the method {@code getMapValues}.
*/
@Test
@DisplayName("getMapValues")
void testGetMapValues() {
logger.testCall("testGetMapValues");
Map<String, String> testMap = new HashMap<>();
testMap.put("key1", "this is the first value");
testMap.put("keytwo", "this value exists twice");
testMap.put("keydrei", "this value exists twice");
testMap.put("keylast", "this is the last value");
testMap.put("keylast2", "this is the last value");
Set<?> output = Miscellaneous.getMapValues(testMap, "this value exists twice");
assertTrue(output.contains("keytwo"), "Map key \"keytwo\" not found in output Set");
assertTrue(output.contains("keydrei"), "Map key \"keydrei\" not found in output Set");
assertEquals(2, output.size(), "There are more or less than two keys in the output Set");
}
/**
* Tests the method {@code executeSafely}.
*/
@Test
@DisplayName("executeSafely (test 0)")
void testExecuteSafely0() {
logger.testCall("testExecuteSafely0");
throwableCaught = false;
Miscellaneous.executeSafely(() -> {
System.out.println("You can safely ignore this (this comes from MiscellaneousTest#testExecuteSafely0)");
}, "MiscellaneousTest#testExecuteSafely0");
assertFalse(throwableCaught, "Event was triggered");
}
/**
* Tests the method {@code executeSafely}.
*/
@Test
@DisplayName("executeSafely (test 1)")
void testExecuteSafely1() {
logger.testCall("testExecuteSafely1");
throwableCaught = false;
// Initialize EngineConfiguration to avoid NullPointerException
// and disable event optimization for instant results
new EngineConfiguration();
Properties properties = new Properties();
properties.setProperty("sosengine.base.debug", "true");
properties.setProperty("sosengine.base.debugEvents", "true");
properties.setProperty("sosengine.base.loggerLevel", "diagnostic");
properties.setProperty("sosengine.base.optimizeLogging", "false");
properties.setProperty("sosengine.base.optimizeEvents", "false");
EngineConfiguration.getInstance().loadConfiguration(properties);
Miscellaneous.executeSafely(() -> {
throw new ArrayIndexOutOfBoundsException("This is not an accident!");
}, "MiscellaneousTest#testExecuteSafely1");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
logger.error("Sleep was interrupted, test may fail");
throw new RuntimeException(e);
}
assertTrue(throwableCaught, "Event wasn't triggered");
}
/**
* Updates {@code throwableCaught}.
*
* @param throwable throwable
* @param identifier identifier
*/
@EventListener(event = ThrowableCatchEvent.class)
public static void onCaughtThrowable(@NotNull Throwable throwable, @NotNull String identifier) {
logger.diag("ThrowableCatchEvent received");
throwableCaught = true;
throw new RuntimeException("aaaaa");
}
}

View file

@ -1,9 +1,31 @@
/*
* 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.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.joor.Reflect;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
@ -11,7 +33,7 @@ import org.junit.jupiter.params.provider.CsvSource;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Tests the class {@link PlaceholderEngine}.
@ -24,6 +46,16 @@ class PlaceholderEngineTest {
*/
private final UnitLogger logger = new UnitLogger(getClass());
@BeforeEach
void createInstance() {
new PlaceholderEngine();
}
@AfterEach
void clearInstance() {
Reflect.onClass(PlaceholderEngine.class).call("clearInstance");
}
/**
* Tests the method {@code process}.
*/

View file

@ -1,3 +1,22 @@
/*
* 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.utility;
import de.staropensource.sosengine.base.utility.PropertyParser;
@ -8,7 +27,7 @@ import org.junit.jupiter.params.provider.CsvSource;
import java.util.Properties;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Test the class {@link PropertyParser}.
@ -47,7 +66,6 @@ class PropertyParserTest {
@ParameterizedTest
@DisplayName("getBoolean")
@CsvSource({
"invalid_bool, asd, false",
"should.be.true, true, true",
"falsified, false, false",
"bit_no, 0, false",
@ -72,22 +90,145 @@ class PropertyParserTest {
@ParameterizedTest
@DisplayName("getByte")
@CsvSource({
"invalid_bool, asd, false",
"should.be.true, true, true",
"falsified, false, false",
"bit_no, 0, false",
"bit_yes, 1, true",
"no_a_bit, 2, false",
"yeah, yes, true",
"nah, no, false",
"should.be.zero, 0, 0",
"max value, 127, 127",
"uwu, 69, 69",
"i'm ~non~ binary, 21, 21",
"happy birthday, 03, 3",
"maybe a year, 21, 21",
"he's a 1 but he likes 0s, 1, 1",
})
void testGetByte(String propertyName, String propertyValue, boolean expected) {
logger.testCall("testGetBoolean", propertyName, propertyValue, expected);
void testGetByte(String propertyName, String propertyValue, byte expected) {
logger.testCall("testGetByte", propertyName, propertyValue, expected);
Properties properties = new Properties();
properties.setProperty(propertyName, propertyValue);
Boolean result = new PropertyParser(properties).getBoolean(propertyName);
Byte result = new PropertyParser(properties).getByte(propertyName);
assertEquals(expected, result, "Result \"" + result + "\" does not match expected output \"" + expected + "\"");
}
/**
* Tests the method {@code getShort}.
*/
@ParameterizedTest
@DisplayName("getShort")
@CsvSource({
"should.be.zero, 0, 0",
"max value, 255, 255",
"uwu, 69, 69",
"i'm ~non~ binary, 21, 21",
"happy birthday, 03, 3",
"maybe a year, 21, 21",
"he's a 1 but he likes 0s, 1, 1",
})
void testGetShort(String propertyName, String propertyValue, short expected) {
logger.testCall("testGetShort", propertyName, propertyValue, expected);
Properties properties = new Properties();
properties.setProperty(propertyName, propertyValue);
Short result = new PropertyParser(properties).getShort(propertyName);
assertEquals(expected, result, "Result \"" + result + "\" does not match expected output \"" + expected + "\"");
}
/**
* Tests the method {@code getInteger}.
*/
@ParameterizedTest
@DisplayName("getInteger")
@CsvSource({
"should.be.zero, 0, 0",
"max value, 2147483647, 2147483647",
"min value, -2147483648, -2147483648",
"uwu, 69, 69",
"i'm ~non~ binary, 21, 21",
"happy birthday, 03, 3",
"probably a year, 2021, 2021",
"he's a 10 but he likes 69, 10, 10",
})
void testGetInteger(String propertyName, String propertyValue, int expected) {
logger.testCall("testGetInteger", propertyName, propertyValue, expected);
Properties properties = new Properties();
properties.setProperty(propertyName, propertyValue);
Integer result = new PropertyParser(properties).getInteger(propertyName, false);
assertEquals(expected, result, "Result \"" + result + "\" does not match expected output \"" + expected + "\"");
}
/**
* Tests the method {@code getLong}.
*/
@ParameterizedTest
@DisplayName("getLong")
@CsvSource({
"should.be.zero, 0, 0",
"max value, 9223372036854775807, 9223372036854775807",
"min value, -9223372036854775808, -9223372036854775808",
"uwu, 69, 69",
"i'm ~non~ binary, 21, 21",
"happy birthday, 03, 3",
"probably a year, 2021, 2021",
"he's a 10 but he likes 69, 10, 10",
})
void testGetLong(String propertyName, String propertyValue, long expected) {
logger.testCall("testGetLong", propertyName, propertyValue, expected);
Properties properties = new Properties();
properties.setProperty(propertyName, propertyValue);
Long result = new PropertyParser(properties).getLong(propertyName, false);
assertEquals(expected, result, "Result \"" + result + "\" does not match expected output \"" + expected + "\"");
}
/**
* Tests the method {@code getFloat}.
*/
@ParameterizedTest
@DisplayName("getFloat")
@CsvSource({
"should.be.zero, 0, 0",
"max value, 340282350000000000000000000000000000000, 340282350000000000000000000000000000000",
"min value, -1400000000000000000000000000000000000000000000, -1400000000000000000000000000000000000000000000",
"uwu, 69, 69",
"i'm ~non~ binary, 21, 21",
"happy birthday, 03, 3",
"probably a year, 2021, 2021",
"new year, 31.12, 31.12",
"he's a 10 but he likes 69, 10.69, 10.69",
})
void testGetFloat(String propertyName, String propertyValue, float expected) {
logger.testCall("testGetFloat", propertyName, propertyValue, expected);
Properties properties = new Properties();
properties.setProperty(propertyName, propertyValue);
Float result = new PropertyParser(properties).getFloat(propertyName);
assertEquals(expected, result, "Result \"" + result + "\" does not match expected output \"" + expected + "\"");
}
/**
* Tests the method {@code getDouble}.
*/
@ParameterizedTest
@DisplayName("getDouble")
@CsvSource({
"should.be.zero, 0, 0",
"uwu, 69, 69",
"i'm ~non~ binary, 21, 21",
"happy birthday, 03, 3",
"probably a year, 2021, 2021",
"new year, 31.12, 31.12",
"he's a 10 but he likes 69, 10.69, 10.69",
})
void testGetDouble(String propertyName, String propertyValue, double expected) {
logger.testCall("testGetDouble", propertyName, propertyValue, expected);
Properties properties = new Properties();
properties.setProperty(propertyName, propertyValue);
Double result = new PropertyParser(properties).getDouble(propertyName);
assertEquals(expected, result, "Result \"" + result + "\" does not match expected output \"" + expected + "\"");
}
}