From a047b0b3c775ea2d1768d41dbdbb074b1fbfde68 Mon Sep 17 00:00:00 2001 From: King John Date: Fri, 26 Jul 2024 15:04:21 +0800 Subject: [PATCH] [#11290] log4j-it, extract testbase --- .../pinpoint/it/plugin/log4j/Log4jIT.java | 58 +------------- .../it/plugin/log4j/Log4jTestBase.java | 80 +++++++++++++++++++ 2 files changed, 83 insertions(+), 55 deletions(-) create mode 100644 agent-module/plugins-it/log4j-it/src/test/java/com/navercorp/pinpoint/it/plugin/log4j/Log4jTestBase.java diff --git a/agent-module/plugins-it/log4j-it/src/test/java/com/navercorp/pinpoint/it/plugin/log4j/Log4jIT.java b/agent-module/plugins-it/log4j-it/src/test/java/com/navercorp/pinpoint/it/plugin/log4j/Log4jIT.java index 6fc0df8476a78..77a751879868e 100644 --- a/agent-module/plugins-it/log4j-it/src/test/java/com/navercorp/pinpoint/it/plugin/log4j/Log4jIT.java +++ b/agent-module/plugins-it/log4j-it/src/test/java/com/navercorp/pinpoint/it/plugin/log4j/Log4jIT.java @@ -17,16 +17,12 @@ 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.PinpointAgent; import com.navercorp.pinpoint.test.plugin.PinpointConfig; import com.navercorp.pinpoint.test.plugin.PluginForkedTest; import com.navercorp.pinpoint.test.plugin.TransformInclude; -import org.apache.log4j.Logger; -import org.apache.log4j.MDC; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @PluginForkedTest @@ -35,63 +31,15 @@ @ImportPlugin({"com.navercorp.pinpoint:pinpoint-log4j-plugin"}) @PinpointConfig("pinpoint-spring-bean-test.config") @TransformInclude("org.apache.log4j.") -public class Log4jIT { - - private Logger logger; +public class Log4jIT extends Log4jTestBase { @Test public void test() { - Logger logger = Logger.getLogger(getClass()); - logger.error("maru"); - - checkVersion(logger); - - Assertions.assertNotNull(MDC.get("PtxId"), "txId"); - Assertions.assertNotNull(MDC.get("PspanId"), "spanId"); + checkMDC(); } @Test public void patternUpdate() { - final String msg = "pattern"; - - - StdoutRecorder stdoutRecorder = new StdoutRecorder(); - final String log = stdoutRecorder.record(new Runnable() { - @Override - public void run() { - logger = Logger.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.assertNotNull(logger, "logger null"); - checkVersion(logger); + checkPatternReplace(); } - - private void checkVersion(Logger logger) { - final String location = getLoggerJarLocation(logger); - Assertions.assertNotNull(location, "location null"); - System.out.println("Log4j 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("log4j-", "").split(":"); - return threadInfo[0]; - } - - private String getLoggerJarLocation(Object object) { - return object.getClass().getProtectionDomain().getCodeSource().getLocation().toString(); - } - } diff --git a/agent-module/plugins-it/log4j-it/src/test/java/com/navercorp/pinpoint/it/plugin/log4j/Log4jTestBase.java b/agent-module/plugins-it/log4j-it/src/test/java/com/navercorp/pinpoint/it/plugin/log4j/Log4jTestBase.java new file mode 100644 index 0000000000000..5337abeeee9d7 --- /dev/null +++ b/agent-module/plugins-it/log4j-it/src/test/java/com/navercorp/pinpoint/it/plugin/log4j/Log4jTestBase.java @@ -0,0 +1,80 @@ +/* + * Copyright 2018 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.log4j; + +import com.navercorp.pinpoint.it.plugin.utils.StdoutRecorder; +import org.apache.log4j.Logger; +import org.apache.log4j.MDC; +import org.junit.jupiter.api.Assertions; + +public class Log4jTestBase { + + private Logger logger; + + protected void checkMDC() { + Logger logger = Logger.getLogger(getClass()); + logger.error("maru"); + + checkVersion(logger); + + Assertions.assertNotNull(MDC.get("PtxId"), "txId"); + Assertions.assertNotNull(MDC.get("PspanId"), "spanId"); + } + + protected void checkPatternReplace() { + final String msg = "pattern"; + + + StdoutRecorder stdoutRecorder = new StdoutRecorder(); + final String log = stdoutRecorder.record(new Runnable() { + @Override + public void run() { + logger = Logger.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.assertNotNull(logger, "logger null"); + checkVersion(logger); + } + + private void checkVersion(Logger logger) { + final String location = getLoggerJarLocation(logger); + Assertions.assertNotNull(location, "location null"); + System.out.println("Log4j 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("log4j-", "").split(":"); + return threadInfo[0]; + } + + private String getLoggerJarLocation(Object object) { + return object.getClass().getProtectionDomain().getCodeSource().getLocation().toString(); + } + +}