Skip to content

Commit 2e311a8

Browse files
committed
[#11290] logback-it, extract TestBase
1 parent 456a2ad commit 2e311a8

File tree

2 files changed

+88
-60
lines changed

2 files changed

+88
-60
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 NAVER Corp.
2+
* Copyright 2024 NAVER Corp.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,19 +17,14 @@
1717

1818
import com.navercorp.pinpoint.it.plugin.utils.AgentPath;
1919
import com.navercorp.pinpoint.it.plugin.utils.PluginITConstants;
20-
import com.navercorp.pinpoint.it.plugin.utils.StdoutRecorder;
2120
import com.navercorp.pinpoint.test.plugin.Dependency;
2221
import com.navercorp.pinpoint.test.plugin.ImportPlugin;
2322
import com.navercorp.pinpoint.test.plugin.JvmArgument;
2423
import com.navercorp.pinpoint.test.plugin.PinpointAgent;
2524
import com.navercorp.pinpoint.test.plugin.PinpointConfig;
2625
import com.navercorp.pinpoint.test.plugin.PluginForkedTest;
2726
import com.navercorp.pinpoint.test.plugin.TransformInclude;
28-
import org.junit.jupiter.api.Assertions;
2927
import org.junit.jupiter.api.Test;
30-
import org.slf4j.Logger;
31-
import org.slf4j.LoggerFactory;
32-
import org.slf4j.MDC;
3328

3429
@PluginForkedTest
3530
@PinpointAgent(AgentPath.PATH)
@@ -38,67 +33,16 @@
3833
@PinpointConfig("pinpoint-spring-bean-test.config")
3934
@JvmArgument("-DtestLoggerEnable=false")
4035
@TransformInclude("org.slf4j.")
41-
public class LogbackIT {
36+
public class LogbackIT extends LogbackTestBase {
4237

4338
@Test
4439
public void test() {
45-
//Logger logger = LogManager.getLogger(getClass());
46-
Logger logger = LoggerFactory.getLogger(this.getClass());
47-
logger.error("maru");
48-
49-
checkVersion(logger);
50-
51-
String ptxId = MDC.get("PtxId");
52-
Assertions.assertNotNull(ptxId, "TxId");
53-
Assertions.assertTrue(ptxId.contains("build.test.0^1"), "TxId value");
54-
Assertions.assertNotNull(MDC.get("PspanId"), "spanId");
40+
checkMDC();
5541
}
5642

57-
private Logger logger;
58-
5943
@Test
6044
public void patternUpdate() {
61-
62-
final String msg = "pattern";
63-
StdoutRecorder stdoutRecorder = new StdoutRecorder();
64-
65-
String log = stdoutRecorder.record(new Runnable() {
66-
@Override
67-
public void run() {
68-
logger = LoggerFactory.getLogger("patternUpdateLogback");
69-
logger.error(msg);
70-
}
71-
});
72-
73-
System.out.println(log);
74-
Assertions.assertNotNull(log, "log null");
75-
Assertions.assertTrue(log.contains(msg), "contains msg");
76-
Assertions.assertTrue(log.contains("TxId"), "contains TxId");
77-
Assertions.assertTrue(log.contains("build.test.0^1"), "TxId value");
78-
79-
Assertions.assertNotNull(logger, "logger null");
80-
checkVersion(logger);
81-
}
82-
83-
private void checkVersion(Logger logger) {
84-
final String location = getLoggerJarLocation(logger);
85-
Assertions.assertNotNull(location, "location null");
86-
System.out.println("Logback classic jar location:" + location);
87-
88-
final String testVersion = getTestVersion();
89-
Assertions.assertTrue(location.contains("/" + testVersion + "/"), "test version is not " + getTestVersion());
90-
}
91-
92-
private String getTestVersion() {
93-
final String[] threadInfo = Thread.currentThread().getName()
94-
.replace(getClass().getName(), "")
95-
.replace(" Thread", "")
96-
.replace(" ", "").replace("logback-classic-", "").split(":");
97-
return threadInfo[0];
98-
}
99-
100-
private String getLoggerJarLocation(Object object) {
101-
return object.getClass().getProtectionDomain().getCodeSource().getLocation().toString();
45+
checkPatternUpdate();
10246
}
10347

10448
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2024 NAVER Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.navercorp.pinpoint.it.plugin.logback;
17+
18+
import com.navercorp.pinpoint.it.plugin.utils.StdoutRecorder;
19+
import org.junit.jupiter.api.Assertions;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
import org.slf4j.MDC;
23+
24+
public class LogbackTestBase {
25+
26+
protected void checkMDC() {
27+
Logger logger = LoggerFactory.getLogger(this.getClass());
28+
logger.error("maru");
29+
30+
checkVersion(logger);
31+
32+
String ptxId = MDC.get("PtxId");
33+
Assertions.assertNotNull(ptxId, "TxId");
34+
Assertions.assertTrue(ptxId.contains("build.test.0^1"), "TxId value");
35+
Assertions.assertNotNull(MDC.get("PspanId"), "spanId");
36+
}
37+
38+
private Logger logger;
39+
40+
protected void checkPatternUpdate() {
41+
42+
final String msg = "pattern";
43+
StdoutRecorder stdoutRecorder = new StdoutRecorder();
44+
45+
String log = stdoutRecorder.record(new Runnable() {
46+
@Override
47+
public void run() {
48+
logger = LoggerFactory.getLogger("patternUpdateLogback");
49+
logger.error(msg);
50+
}
51+
});
52+
53+
System.out.println(log);
54+
Assertions.assertNotNull(log, "log null");
55+
Assertions.assertTrue(log.contains(msg), "contains msg");
56+
Assertions.assertTrue(log.contains("TxId"), "contains TxId");
57+
Assertions.assertTrue(log.contains("build.test.0^1"), "TxId value");
58+
59+
Assertions.assertNotNull(logger, "logger null");
60+
checkVersion(logger);
61+
}
62+
63+
private void checkVersion(Logger logger) {
64+
final String location = getLoggerJarLocation(logger);
65+
Assertions.assertNotNull(location, "location null");
66+
System.out.println("Logback classic jar location:" + location);
67+
68+
final String testVersion = getTestVersion();
69+
Assertions.assertTrue(location.contains("/" + testVersion + "/"), "test version is not " + getTestVersion());
70+
}
71+
72+
private String getTestVersion() {
73+
final String[] threadInfo = Thread.currentThread().getName()
74+
.replace(getClass().getName(), "")
75+
.replace(" Thread", "")
76+
.replace(" ", "").replace("logback-classic-", "").split(":");
77+
return threadInfo[0];
78+
}
79+
80+
private String getLoggerJarLocation(Object object) {
81+
return object.getClass().getProtectionDomain().getCodeSource().getLocation().toString();
82+
}
83+
84+
}

0 commit comments

Comments
 (0)