From 7ea453597703ec447d0fbedbceb4a8fc9b7bc752 Mon Sep 17 00:00:00 2001 From: King John Date: Fri, 26 Jul 2024 16:31:49 +0800 Subject: [PATCH] [#11290] logback-it, extract TestBase --- .../pinpoint/it/plugin/logback/LogbackIT.java | 65 +------------- .../it/plugin/logback/LogbackTestBase.java | 84 +++++++++++++++++++ 2 files changed, 88 insertions(+), 61 deletions(-) create mode 100644 agent-module/plugins-it/logback-it/src/test/java/com/navercorp/pinpoint/it/plugin/logback/LogbackTestBase.java diff --git a/agent-module/plugins-it/logback-it/src/test/java/com/navercorp/pinpoint/it/plugin/logback/LogbackIT.java b/agent-module/plugins-it/logback-it/src/test/java/com/navercorp/pinpoint/it/plugin/logback/LogbackIT.java index b406ee71acd8..c7031aa533e5 100644 --- a/agent-module/plugins-it/logback-it/src/test/java/com/navercorp/pinpoint/it/plugin/logback/LogbackIT.java +++ b/agent-module/plugins-it/logback-it/src/test/java/com/navercorp/pinpoint/it/plugin/logback/LogbackIT.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 NAVER Corp. + * Copyright 2024 NAVER Corp. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,6 @@ import com.navercorp.pinpoint.it.plugin.utils.AgentPath; import com.navercorp.pinpoint.it.plugin.utils.PluginITConstants; -import com.navercorp.pinpoint.it.plugin.utils.StdoutRecorder; import com.navercorp.pinpoint.test.plugin.Dependency; import com.navercorp.pinpoint.test.plugin.ImportPlugin; import com.navercorp.pinpoint.test.plugin.JvmArgument; @@ -25,11 +24,7 @@ import com.navercorp.pinpoint.test.plugin.PinpointConfig; import com.navercorp.pinpoint.test.plugin.PluginForkedTest; import com.navercorp.pinpoint.test.plugin.TransformInclude; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.MDC; @PluginForkedTest @PinpointAgent(AgentPath.PATH) @@ -38,68 +33,16 @@ @PinpointConfig("pinpoint-spring-bean-test.config") @JvmArgument("-DtestLoggerEnable=false") @TransformInclude("org.slf4j.") -public class LogbackIT { +public class LogbackIT extends LogbackTestBase { @Test public void test() { - //Logger logger = LogManager.getLogger(getClass()); - Logger logger = LoggerFactory.getLogger(this.getClass()); - logger.error("maru"); - - checkVersion(logger); - - String ptxId = MDC.get("PtxId"); - Assertions.assertNotNull(ptxId, "TxId"); - Assertions.assertTrue(ptxId.contains("build.test.0^1"), "TxId value"); - Assertions.assertNotNull(MDC.get("PspanId"), "spanId"); - Assertions.assertNotNull(MDC.get("PreqId"), "reqId"); + checkMDC(); } - private Logger logger; - @Test public void patternUpdate() { - - final String msg = "pattern"; - StdoutRecorder stdoutRecorder = new StdoutRecorder(); - - String log = stdoutRecorder.record(new Runnable() { - @Override - public void run() { - logger = LoggerFactory.getLogger("patternUpdateLogback"); - logger.error(msg); - } - }); - - System.out.println(log); - Assertions.assertNotNull(log, "log null"); - Assertions.assertTrue(log.contains(msg), "contains msg"); - Assertions.assertTrue(log.contains("TxId"), "contains TxId"); - Assertions.assertTrue(log.contains("build.test.0^1"), "TxId value"); - - Assertions.assertNotNull(logger, "logger null"); - checkVersion(logger); - } - - private void checkVersion(Logger logger) { - final String location = getLoggerJarLocation(logger); - Assertions.assertNotNull(location, "location null"); - System.out.println("Logback classic jar location:" + location); - - final String testVersion = getTestVersion(); - Assertions.assertTrue(location.contains("/" + testVersion + "/"), "test version is not " + getTestVersion()); - } - - private String getTestVersion() { - final String[] threadInfo = Thread.currentThread().getName() - .replace(getClass().getName(), "") - .replace(" Thread", "") - .replace(" ", "").replace("logback-classic-", "").split(":"); - return threadInfo[0]; - } - - private String getLoggerJarLocation(Object object) { - return object.getClass().getProtectionDomain().getCodeSource().getLocation().toString(); + checkPatternUpdate(); } } diff --git a/agent-module/plugins-it/logback-it/src/test/java/com/navercorp/pinpoint/it/plugin/logback/LogbackTestBase.java b/agent-module/plugins-it/logback-it/src/test/java/com/navercorp/pinpoint/it/plugin/logback/LogbackTestBase.java new file mode 100644 index 000000000000..49487e812444 --- /dev/null +++ b/agent-module/plugins-it/logback-it/src/test/java/com/navercorp/pinpoint/it/plugin/logback/LogbackTestBase.java @@ -0,0 +1,84 @@ +/* + * Copyright 2024 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.navercorp.pinpoint.it.plugin.logback; + +import com.navercorp.pinpoint.it.plugin.utils.StdoutRecorder; +import org.junit.jupiter.api.Assertions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.MDC; + +public class LogbackTestBase { + + protected void checkMDC() { + Logger logger = LoggerFactory.getLogger(this.getClass()); + logger.error("maru"); + + checkVersion(logger); + + String ptxId = MDC.get("PtxId"); + Assertions.assertNotNull(ptxId, "TxId"); + Assertions.assertTrue(ptxId.contains("build.test.0^1"), "TxId value"); + Assertions.assertNotNull(MDC.get("PspanId"), "spanId"); + } + + private Logger logger; + + protected void checkPatternUpdate() { + + final String msg = "pattern"; + StdoutRecorder stdoutRecorder = new StdoutRecorder(); + + String log = stdoutRecorder.record(new Runnable() { + @Override + public void run() { + logger = LoggerFactory.getLogger("patternUpdateLogback"); + logger.error(msg); + } + }); + + System.out.println(log); + Assertions.assertNotNull(log, "log null"); + Assertions.assertTrue(log.contains(msg), "contains msg"); + Assertions.assertTrue(log.contains("TxId"), "contains TxId"); + Assertions.assertTrue(log.contains("build.test.0^1"), "TxId value"); + + Assertions.assertNotNull(logger, "logger null"); + checkVersion(logger); + } + + private void checkVersion(Logger logger) { + final String location = getLoggerJarLocation(logger); + Assertions.assertNotNull(location, "location null"); + System.out.println("Logback classic jar location:" + location); + + final String testVersion = getTestVersion(); + Assertions.assertTrue(location.contains("/" + testVersion + "/"), "test version is not " + getTestVersion()); + } + + private String getTestVersion() { + final String[] threadInfo = Thread.currentThread().getName() + .replace(getClass().getName(), "") + .replace(" Thread", "") + .replace(" ", "").replace("logback-classic-", "").split(":"); + return threadInfo[0]; + } + + private String getLoggerJarLocation(Object object) { + return object.getClass().getProtectionDomain().getCodeSource().getLocation().toString(); + } + +}