From 32816ecce1755d0410412b9469f090fcb30173b0 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Tue, 17 Dec 2024 03:24:50 +0100 Subject: [PATCH] Split TestBase's methods & add more functionality --- .../de/staropensource/engine/base/TestBase.kt | 47 ++++++++++ build.gradle.kts | 1 + .../staropensource/engine/testing/TestBase.kt | 86 ++++++++++++++----- 3 files changed, 111 insertions(+), 23 deletions(-) create mode 100644 base/src/test/kotlin/de/staropensource/engine/base/TestBase.kt diff --git a/base/src/test/kotlin/de/staropensource/engine/base/TestBase.kt b/base/src/test/kotlin/de/staropensource/engine/base/TestBase.kt new file mode 100644 index 0000000..0b99cbf --- /dev/null +++ b/base/src/test/kotlin/de/staropensource/engine/base/TestBase.kt @@ -0,0 +1,47 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Authors + * Licensed under the GNU Affero General Public License v3 + * with an exception allowing classpath linking. + * + * 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 . + */ + +package de.staropensource.engine.base + +import de.staropensource.engine.base.type.logging.Level +import de.staropensource.engine.testing.TestBase + +/** + * Base class for implementing tests. + * + * This class sets some nice defaults + * and adds other things only useful to us. + * + * @param autoManage automatically initializes and shuts the engine down after each test + * @param logLevels contains the allowed log levels + * @param shutdownMarksFailure whether engine shutdowns should mark the test as failed + * @since v1-alpha10 + */ +abstract class TestBase( + autoManage: Boolean? = null, + logLevels: Array? = null, + shutdownMarksFailure: Boolean? = null +) : TestBase( + autoManage = autoManage != false, + suppressInitAndShutdownLogging = true, + logLevels = logLevels ?: Level.entries.toTypedArray(), + printTestExecution = true, + shutdownMarksFailure = shutdownMarksFailure == true +) diff --git a/build.gradle.kts b/build.gradle.kts index a76d143..32426cb 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -177,6 +177,7 @@ allprojects { // -> Configure Gradle to use JUnit tasks.test { useJUnitPlatform() + testLogging { events( "passed", diff --git a/testing/src/main/kotlin/de/staropensource/engine/testing/TestBase.kt b/testing/src/main/kotlin/de/staropensource/engine/testing/TestBase.kt index 7f9a1a4..3235c3e 100644 --- a/testing/src/main/kotlin/de/staropensource/engine/testing/TestBase.kt +++ b/testing/src/main/kotlin/de/staropensource/engine/testing/TestBase.kt @@ -24,28 +24,83 @@ import de.staropensource.engine.base.Engine import de.staropensource.engine.base.Engine.State import de.staropensource.engine.base.EngineConfiguration import de.staropensource.engine.base.type.logging.ChannelSettings +import de.staropensource.engine.base.type.logging.Level import de.staropensource.engine.testing.implementation.FailureShutdownHandler +import de.staropensource.engine.testing.implementation.NoOperationShutdownHandler import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.TestInfo import org.junit.jupiter.api.fail +import kotlin.concurrent.fixedRateTimer +import kotlin.jvm.optionals.getOrDefault +import kotlin.jvm.optionals.getOrNull /** * Base class for implementing tests. * - * @param autoManage automatically initializes and shuts the engine down after each test + * @param autoManage automatically initializes and shuts the engine down after each test * @param suppressInitAndShutdownLogging if to suppress the engine's log output during engine initialization and shutdown. Only takes effect if [autoManage] is `true` - * @param shutdownMarksFailure whether engine shutdowns should mark the test as failed + * @param logLevels contains the allowed log levels + * @param printTestExecution whether to display when a test started and ended. Useful when dealing with a lot of log output + * @param shutdownMarksFailure whether engine shutdowns should mark the test as failed * @since v1-alpha10 */ abstract class TestBase( val autoManage: Boolean = true, val suppressInitAndShutdownLogging: Boolean = true, + val logLevels: Array = arrayOf(Level.INFORMATIONAL, Level.WARNING, Level.ERROR, Level.CRASH), + val printTestExecution: Boolean = true, val shutdownMarksFailure: Boolean = false ) { init { performConfiguration() } + /** + * Initializes the engine before each test. + * + * @since v1-alpha10 + */ + @BeforeEach + fun beforeTask(testInfo: TestInfo) { + when (Engine.state) { + State.INITIALIZING, State.SHUTTING_DOWN -> fail("Engine is in invalid state 'Engine.State.${Engine.state.name}'") + State.CRASHED -> fail("The StarOpenSource Engine has crashed") + else -> {} + } + + // Initialize engine + initializeEngine() + + // Print test starting + if (printTestExecution) + println("-> STARTING test '${testInfo.displayName}' [${testInfo.testClass.getOrNull()?.name ?: ""}#${testInfo.testMethod.getOrNull()?.name ?: ""}]") + } + + /** + * Shuts the engine down after each test + * + * @since v1-alpha10 + */ + @AfterEach + fun afterTask(testInfo: TestInfo) { + when (Engine.state) { + State.UNINITIALIZED -> fail("Internal inconsistency detected: Engine configuration was not performed") + State.INITIALIZING, State.SHUTTING_DOWN -> fail("Engine is in invalid state 'Engine.State.${Engine.state.name}'") + State.CRASHED -> fail("The StarOpenSource Engine has crashed") + else -> {} + } + + // Shutdown engine + shutdownEngine() + + // Print test ending + if (printTestExecution) + println("-> FINISHED test '${testInfo.displayName}' [${testInfo.testClass.getOrNull()?.name ?: ""}#${testInfo.testMethod.getOrNull()?.name ?: ""}]") + } + + + // -----> Utility methods /** * Configures the engine before * starting any tests. @@ -60,24 +115,17 @@ abstract class TestBase( if (shutdownMarksFailure) EngineConfiguration.shutdownHandler = FailureShutdownHandler.instance else - EngineConfiguration.shutdownHandler = FailureShutdownHandler.instance + EngineConfiguration.shutdownHandler = NoOperationShutdownHandler.instance - //if (Engine.state == State.UNINITIALIZED) {} + EngineConfiguration.logLevels = logLevels.toMutableSet() } /** - * Initializes the engine before each test. + * Initializes the StarOpenSource Engine. * * @since v1-alpha10 */ - @BeforeEach - fun initializeEngine() { - when (Engine.state) { - State.INITIALIZING, State.SHUTTING_DOWN -> fail("Engine is in invalid state 'Engine.State.${Engine.state.name}'") - State.CRASHED -> fail("The StarOpenSource Engine has crashed") - else -> {} - } - + private fun initializeEngine() { if (autoManage) { val originalSettings: ChannelSettings = ChannelSettings.global @@ -95,19 +143,11 @@ abstract class TestBase( } /** - * Shuts the engine down after each test + * Shuts the StarOpenSource Engine down. * * @since v1-alpha10 */ - @AfterEach - fun shutdownEngine() { - when (Engine.state) { - State.UNINITIALIZED -> fail("Internal inconsistency detected: Engine configuration was not performed") - State.INITIALIZING, State.SHUTTING_DOWN -> fail("Engine is in invalid state 'Engine.State.${Engine.state.name}'") - State.CRASHED -> fail("The StarOpenSource Engine has crashed") - else -> {} - } - + private fun shutdownEngine() { if (autoManage) { val originalSettings: ChannelSettings = ChannelSettings.global