diff --git a/base/src/main/kotlin/de/staropensource/engine/base/implementation/logging/crashcategory/InfoCrashCategory.kt b/base/src/main/kotlin/de/staropensource/engine/base/implementation/logging/crashcategory/InfoCrashCategory.kt index 2355db8d1..7d2d966d5 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/implementation/logging/crashcategory/InfoCrashCategory.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/implementation/logging/crashcategory/InfoCrashCategory.kt @@ -59,17 +59,15 @@ class InfoCrashCategory private constructor() : CrashCategory { channelSettings: ChannelSettings?, throwable: Throwable?, fatal: Boolean, - ): LinkedHashMap { - return linkedMapOf( - Pair("Origin", linkedMapOf( - Pair("Class", "${call.origin.packageName}.${call.origin.className}"), - Pair("Method", call.origin.methodName), - Pair("Line", call.origin.lineNumber), - )), - Pair("Channel", "'${call.channel}'"), - Pair("Fatal", if (fatal) "yes" else "no"), - Pair("Message", call.message), - Pair("Stacktrace", if (throwable == null) "Not available." else StackTraceUtils.stacktraceRecursive(throwable)), - ) - } + ): LinkedHashMap = linkedMapOf( + Pair("Origin", linkedMapOf( + Pair("Class", "${call.origin.packageName}.${call.origin.className}"), + Pair("Method", call.origin.methodName), + Pair("Line", call.origin.lineNumber), + )), + Pair("Channel", "'${call.channel}'"), + Pair("Fatal", if (fatal) "yes" else "no"), + Pair("Message", call.message), + Pair("Stacktrace", if (throwable == null) "Not available." else StackTraceUtils.stacktraceRecursive(throwable)), + ) } diff --git a/base/src/test/kotlin/de/staropensource/engine/base/utility/misc/StackTraceUtilsTest.kt b/base/src/test/kotlin/de/staropensource/engine/base/utility/misc/StackTraceUtilsTest.kt new file mode 100644 index 000000000..f8b4e2480 --- /dev/null +++ b/base/src/test/kotlin/de/staropensource/engine/base/utility/misc/StackTraceUtilsTest.kt @@ -0,0 +1,72 @@ +/* + * STAROPENSOURCE ENGINE SOURCE FILE + * Copyright (c) 2024 The StarOpenSource Engine Authors + * Licensed under the GNU General Public License v3. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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.utility.misc + +import de.staropensource.engine.base.type.Origin +import de.staropensource.engine.testing.TestBase +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.DisplayName +import java.lang.NullPointerException +import kotlin.test.Test +import kotlin.test.assertContains + +class StackTraceUtilsTest : TestBase() { + @Test + fun methodCaller() { + val origin: Origin = methodCallerOrigin() + + assertEquals("de.staropensource.engine.base.utility.misc", origin.packageName) + assertEquals("StackTraceUtilsTest", origin.className) + assertEquals("methodCaller", origin.methodName) + assertEquals(33u, origin.lineNumber) + } + + private fun methodCallerOrigin(): Origin = StackTraceUtils.methodCaller() + + @Test + @DisplayName("stacktraceHeader (no message)") + fun stacktraceHeaderNoMessage() { + assertEquals("Caused by: java.lang.NullPointerException", StackTraceUtils.stacktraceHeader(NullPointerException())) + } + + @Test + @DisplayName("stacktraceHeader (with message)") + fun stacktraceHeaderWithMessage() { + assertEquals("Caused by: java.lang.NullPointerException: Hello World!", StackTraceUtils.stacktraceHeader(NullPointerException("Hello World!"))) + } + + @Test + @DisplayName("stacktraceBody (no indent)") + fun stacktraceBodyNoIndent() { + val body: String = StackTraceUtils.stacktraceBody(Throwable(), indent = false) + + assertTrue(body.startsWith("at "), "Body '${body}' does not start with 'at '") + assertTrue(body.contains("de.staropensource.engine.base.utility.misc.StackTraceUtilsTest"), "Body '${body}' does not contain 'de.staropensource.engine.base.utility.misc.StackTraceUtilsTest") + } + + @Test + @DisplayName("stacktraceBody (with indent)") + fun stacktraceBodyWithIndent() { + val body: String = StackTraceUtils.stacktraceBody(Throwable(), indent = true) + + assertTrue(body.startsWith("\tat "), "Body '${body}' does not start with '\tat '") + assertTrue(body.contains("de.staropensource.engine.base.utility.misc.StackTraceUtilsTest"), "Body '${body}' does not contain 'de.staropensource.engine.base.utility.misc.StackTraceUtilsTest") + } +}