From 5170f217ad2ca1295a031b47ca803ebd3ecc64e2 Mon Sep 17 00:00:00 2001 From: Him188 Date: Tue, 10 Aug 2021 04:53:40 +0800 Subject: [PATCH] Add test for compatibility on legacy logging overrides --- .../test/MiraiLog4JAdapterTest.kt | 11 ++++++ .../logging/LoggingCompatibilityTest.kt | 36 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 mirai-core-api/src/commonTest/kotlin/logging/LoggingCompatibilityTest.kt diff --git a/logging/mirai-logging-log4j2/test/MiraiLog4JAdapterTest.kt b/logging/mirai-logging-log4j2/test/MiraiLog4JAdapterTest.kt index 241905eaa..366f9854d 100644 --- a/logging/mirai-logging-log4j2/test/MiraiLog4JAdapterTest.kt +++ b/logging/mirai-logging-log4j2/test/MiraiLog4JAdapterTest.kt @@ -21,6 +21,17 @@ import kotlin.test.assertIs internal class MiraiLog4JAdapterTest { + @Suppress("DEPRECATION") + @Test + fun `services prevail than legacy overrides`() { + MiraiLogger.setDefaultLoggerCreator { + net.mamoe.mirai.utils.SimpleLogger("my logger") { _: String?, _: Throwable? -> } + } + + @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") + assertIs(MiraiLogger.Factory.create(this::class)) + } + @Test fun `using log4j`() { assertIs(loadService(MiraiLogger.Factory::class)) diff --git a/mirai-core-api/src/commonTest/kotlin/logging/LoggingCompatibilityTest.kt b/mirai-core-api/src/commonTest/kotlin/logging/LoggingCompatibilityTest.kt new file mode 100644 index 000000000..d3e6673f8 --- /dev/null +++ b/mirai-core-api/src/commonTest/kotlin/logging/LoggingCompatibilityTest.kt @@ -0,0 +1,36 @@ +/* + * Copyright 2019-2021 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/dev/LICENSE + */ + +package net.mamoe.mirai.logging + +import net.mamoe.mirai.utils.MiraiLogger +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertIs + +internal class LoggingCompatibilityTest { + + @Suppress("DEPRECATION") + @Test + fun `legacy overrides are still working if no services are found`() { + val messages = StringBuilder() + + MiraiLogger.setDefaultLoggerCreator { + net.mamoe.mirai.utils.SimpleLogger("my logger") { message: String?, _: Throwable? -> + messages.append(message) + } + } + + val created = MiraiLogger.Factory.create(this::class) + assertIs(created) + created.info("test") + + assertEquals("test", messages.toString().trim()) + } +} \ No newline at end of file