Skip to content

Commit 9969c11

Browse files
committed
Fix test failure due to stacktrace format changes
1 parent f486115 commit 9969c11

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/GcpLayoutTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.junit.jupiter.api.Test;
2828

2929
import static org.apache.logging.log4j.layout.template.json.TestHelpers.CONFIGURATION;
30+
import static org.apache.logging.log4j.layout.template.json.TestHelpers.JAVA_BASE_PREFIX;
3031
import static org.apache.logging.log4j.layout.template.json.TestHelpers.usingSerializedLogEventAccessor;
3132
import static org.assertj.core.api.Assertions.assertThat;
3233

@@ -88,7 +89,7 @@ private static void verifySerialization(final LogEvent logEvent) {
8889
.contains(logEvent.getMessage().getFormattedMessage())
8990
.contains(exception.getLocalizedMessage())
9091
.contains("at org.apache.logging.log4j.layout.template.json")
91-
.contains("at java.lang.reflect.Method")
92+
.contains("at " + JAVA_BASE_PREFIX + "java.lang.reflect.Method")
9293
.contains("at org.junit.platform.engine");
9394
}
9495

@@ -158,7 +159,7 @@ private static void verifySerialization(final LogEvent logEvent) {
158159
new String[]{"_exception", "stackTrace"}))
159160
.contains(exception.getLocalizedMessage())
160161
.contains("at org.apache.logging.log4j.layout.template.json")
161-
.contains("at java.lang.reflect.Method")
162+
.contains("at " + JAVA_BASE_PREFIX + "java.lang.reflect.Method")
162163
.contains("at org.junit.platform.engine");
163164

164165
} else {

log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayoutGcFreeTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ void test_no_allocation_during_steady_state_logging() throws Exception {
3535
public static void main(final String[] args) throws Exception {
3636
System.setProperty("log4j.layout.jsonTemplate.recyclerFactory", "threadLocal");
3737
System.setProperty("log4j2.garbagefree.threadContextMap", "true");
38+
System.setProperty("log4j2.clock", "SystemMillisClock");
3839
GcFreeLoggingTestUtil.executeLogging(
3940
"gcFreeJsonTemplateLayoutLogging.xml",
4041
JsonTemplateLayoutGcFreeTest.class);

log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/TestHelpers.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@
3535
import org.apache.logging.log4j.layout.template.json.util.JsonReader;
3636
import org.apache.logging.log4j.layout.template.json.util.JsonWriter;
3737
import org.apache.logging.log4j.layout.template.json.util.MapAccessor;
38+
import org.apache.logging.log4j.util.Constants;
3839

3940
public final class TestHelpers {
4041

4142
public static final Configuration CONFIGURATION = new DefaultConfiguration();
4243

44+
public static final String JAVA_BASE_PREFIX = Constants.JAVA_MAJOR_VERSION > 8 ? "java.base/" : "";
45+
4346
private static final JsonWriter JSON_WRITER = JsonWriter
4447
.newBuilder()
4548
.setMaxStringLength(10_000)

log4j-layout-template-json-test/src/test/java/org/apache/logging/log4j/layout/template/json/resolver/StackTraceStringResolverTest.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,18 @@
3333
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
3434
import org.apache.logging.log4j.layout.template.json.JsonTemplateLayout;
3535
import org.apache.logging.log4j.layout.template.json.JsonTemplateLayoutDefaults;
36+
import org.apache.logging.log4j.util.Constants;
3637
import org.assertj.core.api.AbstractStringAssert;
3738
import org.junit.jupiter.api.Nested;
3839
import org.junit.jupiter.api.Test;
3940

40-
import static org.apache.logging.log4j.layout.template.json.TestHelpers.*;
41+
import static org.apache.logging.log4j.layout.template.json.TestHelpers.CONFIGURATION;
42+
import static org.apache.logging.log4j.layout.template.json.TestHelpers.JAVA_BASE_PREFIX;
43+
import static org.apache.logging.log4j.layout.template.json.TestHelpers.asMap;
44+
import static org.apache.logging.log4j.layout.template.json.TestHelpers.usingSerializedLogEventAccessor;
45+
import static org.apache.logging.log4j.layout.template.json.TestHelpers.writeJson;
4146
import static org.assertj.core.api.Assertions.assertThat;
47+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
4248

4349
class StackTraceStringResolverTest {
4450

@@ -61,7 +67,7 @@ private static Throwable exception1() {
6167
private static String exception1Regex(final boolean truncated) {
6268
final String truncationCorrectionRegex = truncationSuffixRegexOr(truncated, ".divide\\(");
6369
return "java.lang.ArithmeticException: Division by zero\r?\n" +
64-
"\t+at java.math.BigDecimal" + truncationCorrectionRegex + ".*";
70+
"\t+at " + JAVA_BASE_PREFIX + "java.math.BigDecimal" + truncationCorrectionRegex + ".*";
6571
}
6672

6773
@SuppressWarnings("ConstantConditions")
@@ -72,7 +78,7 @@ private static Throwable exception2() {
7278
private static String exception2Regex(final boolean truncated) {
7379
final String truncationCorrectionRegex = truncationSuffixRegexOr(truncated, ".add\\(");
7480
return "java.lang.UnsupportedOperationException\r?\n" +
75-
"\t+at java.util.AbstractList" + truncationCorrectionRegex + ".*";
81+
"\t+at " + JAVA_BASE_PREFIX + "java.util.AbstractList" + truncationCorrectionRegex + ".*";
7682
}
7783

7884
private static Throwable exception3() {
@@ -82,7 +88,7 @@ private static Throwable exception3() {
8288
private static String exception3Regex(final boolean truncated) {
8389
final String truncationCorrectionRegex = truncationSuffixRegexOr(truncated, ".<init>");
8490
return "java.lang.IllegalArgumentException: Port value out of range: -1\r?\n" +
85-
"\t+at java.net.ServerSocket" + truncationCorrectionRegex + ".*";
91+
"\t+at " + JAVA_BASE_PREFIX + "java.net.ServerSocket" + truncationCorrectionRegex + ".*";
8692
}
8793

8894
private static String truncationSuffixRegexOr(final boolean truncated, final String fallback) {
@@ -576,7 +582,15 @@ void point_matchers_should_work() {
576582
private String pointMatcherString(final Throwable exception) {
577583
final StackTraceElement stackTraceElement = exception.getStackTrace()[0];
578584
final String className = stackTraceElement.getClassName();
579-
return "at " + className;
585+
final String moduleName;
586+
if (Constants.JAVA_MAJOR_VERSION > 8) {
587+
moduleName = assertDoesNotThrow(
588+
() -> (String) StackTraceElement.class.getDeclaredMethod("getModuleName")
589+
.invoke(stackTraceElement));
590+
} else {
591+
moduleName = null;
592+
}
593+
return moduleName != null ? "at " + moduleName + "/" + className : "at " + className;
580594
}
581595

582596
private String pointMatcherRegex(final Throwable exception) {

0 commit comments

Comments
 (0)