Split TestBase's methods & add more functionality
This commit is contained in:
parent
ce535fdca6
commit
32816ecce1
3 changed files with 111 additions and 23 deletions
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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<Level>? = null,
|
||||||
|
shutdownMarksFailure: Boolean? = null
|
||||||
|
) : TestBase(
|
||||||
|
autoManage = autoManage != false,
|
||||||
|
suppressInitAndShutdownLogging = true,
|
||||||
|
logLevels = logLevels ?: Level.entries.toTypedArray(),
|
||||||
|
printTestExecution = true,
|
||||||
|
shutdownMarksFailure = shutdownMarksFailure == true
|
||||||
|
)
|
|
@ -177,6 +177,7 @@ allprojects {
|
||||||
// -> Configure Gradle to use JUnit
|
// -> Configure Gradle to use JUnit
|
||||||
tasks.test {
|
tasks.test {
|
||||||
useJUnitPlatform()
|
useJUnitPlatform()
|
||||||
|
|
||||||
testLogging {
|
testLogging {
|
||||||
events(
|
events(
|
||||||
"passed",
|
"passed",
|
||||||
|
|
|
@ -24,28 +24,83 @@ import de.staropensource.engine.base.Engine
|
||||||
import de.staropensource.engine.base.Engine.State
|
import de.staropensource.engine.base.Engine.State
|
||||||
import de.staropensource.engine.base.EngineConfiguration
|
import de.staropensource.engine.base.EngineConfiguration
|
||||||
import de.staropensource.engine.base.type.logging.ChannelSettings
|
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.FailureShutdownHandler
|
||||||
|
import de.staropensource.engine.testing.implementation.NoOperationShutdownHandler
|
||||||
import org.junit.jupiter.api.AfterEach
|
import org.junit.jupiter.api.AfterEach
|
||||||
import org.junit.jupiter.api.BeforeEach
|
import org.junit.jupiter.api.BeforeEach
|
||||||
|
import org.junit.jupiter.api.TestInfo
|
||||||
import org.junit.jupiter.api.fail
|
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.
|
* 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 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
|
* @since v1-alpha10
|
||||||
*/
|
*/
|
||||||
abstract class TestBase(
|
abstract class TestBase(
|
||||||
val autoManage: Boolean = true,
|
val autoManage: Boolean = true,
|
||||||
val suppressInitAndShutdownLogging: Boolean = true,
|
val suppressInitAndShutdownLogging: Boolean = true,
|
||||||
|
val logLevels: Array<Level> = arrayOf(Level.INFORMATIONAL, Level.WARNING, Level.ERROR, Level.CRASH),
|
||||||
|
val printTestExecution: Boolean = true,
|
||||||
val shutdownMarksFailure: Boolean = false
|
val shutdownMarksFailure: Boolean = false
|
||||||
) {
|
) {
|
||||||
init {
|
init {
|
||||||
performConfiguration()
|
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
|
* Configures the engine before
|
||||||
* starting any tests.
|
* starting any tests.
|
||||||
|
@ -60,24 +115,17 @@ abstract class TestBase(
|
||||||
if (shutdownMarksFailure)
|
if (shutdownMarksFailure)
|
||||||
EngineConfiguration.shutdownHandler = FailureShutdownHandler.instance
|
EngineConfiguration.shutdownHandler = FailureShutdownHandler.instance
|
||||||
else
|
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
|
* @since v1-alpha10
|
||||||
*/
|
*/
|
||||||
@BeforeEach
|
private fun initializeEngine() {
|
||||||
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 -> {}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (autoManage) {
|
if (autoManage) {
|
||||||
val originalSettings: ChannelSettings = ChannelSettings.global
|
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
|
* @since v1-alpha10
|
||||||
*/
|
*/
|
||||||
@AfterEach
|
private fun shutdownEngine() {
|
||||||
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 -> {}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (autoManage) {
|
if (autoManage) {
|
||||||
val originalSettings: ChannelSettings = ChannelSettings.global
|
val originalSettings: ChannelSettings = ChannelSettings.global
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue