Skip to content

Commit 1926aeb

Browse files
author
Takuya Kiriyama
committed
8352016: Improve java/lang/RuntimeTests/RuntimeExitLogTest.java
Reviewed-by: rriggs
1 parent 74822ce commit 1926aeb

8 files changed

+110
-20
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
############################################################
2+
# java.lang.Runtime logging level to console to ALL
3+
############################################################
4+
5+
handlers= java.util.logging.ConsoleHandler
6+
7+
java.util.logging.ConsoleHandler.level = ALL
8+
java.lang.Runtime.level = ALL

test/jdk/java/lang/RuntimeTests/ExitLogging-FINE.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
############################################################
2-
# Enable logging java.lang.Runtime to the console
2+
# java.lang.Runtime logging level to console to FINE
33
############################################################
44

55
handlers= java.util.logging.ConsoleHandler
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
############################################################
2+
# java.lang.Runtime logging level to console to FINER
3+
############################################################
4+
5+
handlers= java.util.logging.ConsoleHandler
6+
7+
java.util.logging.ConsoleHandler.level = ALL
8+
java.lang.Runtime.level = FINER

test/jdk/java/lang/RuntimeTests/ExitLogging-INFO.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
############################################################
2-
# Enable logging java.lang.Runtime to the console
2+
# java.lang.Runtime logging level to console to INFO
33
############################################################
44

55
handlers= java.util.logging.ConsoleHandler
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
############################################################
2+
# java.lang.Runtime logging level to console to OFF
3+
############################################################
4+
5+
handlers= java.util.logging.ConsoleHandler
6+
7+
java.util.logging.ConsoleHandler.level = ALL
8+
java.lang.Runtime.level = OFF
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
############################################################
2+
# java.lang.Runtime logging level to console to SEVERE
3+
############################################################
4+
5+
handlers= java.util.logging.ConsoleHandler
6+
7+
java.util.logging.ConsoleHandler.level = ALL
8+
java.lang.Runtime.level = SEVERE
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
############################################################
2+
# java.lang.Runtime logging level to console to WARNING
3+
############################################################
4+
5+
handlers= java.util.logging.ConsoleHandler
6+
7+
java.util.logging.ConsoleHandler.level = ALL
8+
java.lang.Runtime.level = WARNING

test/jdk/java/lang/RuntimeTests/RuntimeExitLogTest.java

Lines changed: 68 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -49,7 +49,7 @@ public class RuntimeExitLogTest {
4949

5050
private static final String TEST_JDK = System.getProperty("test.jdk");
5151
private static final String TEST_SRC = System.getProperty("test.src");
52-
52+
private static final String NEW_LINE = System.lineSeparator();
5353
private static Object HOLD_LOGGER;
5454

5555
/**
@@ -64,31 +64,80 @@ public static void main(String[] args) throws InterruptedException {
6464
System.exit(status);
6565
}
6666

67+
/**
68+
* Generate a regular expression pattern that match the expected log output for a Runtime.exit() call.
69+
* The pattern includes the method call stack trace and the exit status.
70+
* @param status the exit status passed to the Runtime.exit() call
71+
* @return the regex pattern as a string
72+
*/
73+
private static String generateStackTraceLogPattern(int status) {
74+
return "(?s)^.+ java\\.lang\\.Shutdown logRuntimeExit\\n" +
75+
".*: Runtime\\.exit\\(\\) called with status: " + status + "\\n" +
76+
"java\\.lang\\.Throwable: Runtime\\.exit\\(" + status + "\\)\\n" +
77+
"\\s+at java\\.base/java\\.lang\\.Shutdown\\.logRuntimeExit\\(Shutdown\\.java:\\d+\\)\\n" +
78+
"\\s+at(?: .+)";
79+
}
80+
6781
/**
6882
* Test various log level settings, and none.
6983
* @return a stream of arguments for parameterized test
7084
*/
7185
private static Stream<Arguments> logParamProvider() {
7286
return Stream.of(
73-
// Logging enabled with level DEBUG
87+
// Logging configuration using the java.util.logging.config.file property
88+
Arguments.of(List.of("-Djava.util.logging.config.file=" +
89+
Path.of(TEST_SRC, "ExitLogging-ALL.properties").toString()), 1,
90+
generateStackTraceLogPattern(1)),
91+
Arguments.of(List.of("-Djava.util.logging.config.file=" +
92+
Path.of(TEST_SRC, "ExitLogging-FINER.properties").toString()), 2,
93+
generateStackTraceLogPattern(2)),
7494
Arguments.of(List.of("-Djava.util.logging.config.file=" +
75-
Path.of(TEST_SRC, "ExitLogging-FINE.properties").toString()), 1,
76-
"Runtime.exit() called with status: 1"),
77-
// Logging disabled due to level
95+
Path.of(TEST_SRC, "ExitLogging-FINE.properties").toString()), 3,
96+
generateStackTraceLogPattern(3)),
97+
Arguments.of(List.of("-Djava.util.logging.config.file=" +
98+
Path.of(TEST_SRC, "ExitLogging-INFO.properties").toString()), 4,
99+
""),
100+
Arguments.of(List.of("-Djava.util.logging.config.file=" +
101+
Path.of(TEST_SRC, "ExitLogging-WARNING.properties").toString()), 5,
102+
""),
103+
Arguments.of(List.of("-Djava.util.logging.config.file=" +
104+
Path.of(TEST_SRC, "ExitLogging-SEVERE.properties").toString()), 6,
105+
""),
78106
Arguments.of(List.of("-Djava.util.logging.config.file=" +
79-
Path.of(TEST_SRC, "ExitLogging-INFO.properties").toString()), 2,
107+
Path.of(TEST_SRC, "ExitLogging-OFF.properties").toString()), 7,
80108
""),
81-
// Console logger
109+
110+
// Logging configuration using the jdk.system.logger.level property
111+
Arguments.of(List.of("--limit-modules", "java.base",
112+
"-Djdk.system.logger.level=ALL"), 8,
113+
generateStackTraceLogPattern(8)),
114+
Arguments.of(List.of("--limit-modules", "java.base",
115+
"-Djdk.system.logger.level=TRACE"), 9,
116+
generateStackTraceLogPattern(9)),
117+
Arguments.of(List.of("--limit-modules", "java.base",
118+
"-Djdk.system.logger.level=DEBUG"), 10,
119+
generateStackTraceLogPattern(10)),
82120
Arguments.of(List.of("--limit-modules", "java.base",
83-
"-Djdk.system.logger.level=DEBUG"), 3,
84-
"Runtime.exit() called with status: 3"),
85-
// Console logger
86-
Arguments.of(List.of(), 4, ""),
121+
"-Djdk.system.logger.level=INFO"), 11,
122+
""),
123+
Arguments.of(List.of("--limit-modules", "java.base",
124+
"-Djdk.system.logger.level=WARNING"), 12,
125+
""),
126+
Arguments.of(List.of("--limit-modules", "java.base",
127+
"-Djdk.system.logger.level=ERROR"), 13,
128+
""),
129+
Arguments.of(List.of("--limit-modules", "java.base",
130+
"-Djdk.system.logger.level=OFF"), 14,
131+
""),
132+
87133
// Throwing Handler
88134
Arguments.of(List.of("-DThrowingHandler",
89135
"-Djava.util.logging.config.file=" +
90-
Path.of(TEST_SRC, "ExitLogging-FINE.properties").toString()), 5,
91-
"Runtime.exit(5) logging failed: Exception in publish")
136+
Path.of(TEST_SRC, "ExitLogging-FINE.properties").toString()), 15,
137+
"Runtime\\.exit\\(15\\) logging failed: Exception in publish"),
138+
139+
// Default console logging configuration with no additional parameters
140+
Arguments.of(List.of(), 16, "")
92141
);
93142
}
94143

@@ -115,13 +164,14 @@ public void checkLogger(List<String> logProps, int status, String expectMessage)
115164
try (BufferedReader reader = process.inputReader()) {
116165
List<String> lines = reader.lines().toList();
117166
boolean match = (expectMessage.isEmpty())
118-
? lines.size() == 0
119-
: lines.stream().filter(s -> s.contains(expectMessage)).findFirst().isPresent();
167+
? lines.isEmpty()
168+
: String.join("\n", lines).matches(expectMessage);
120169
if (!match) {
121170
// Output lines for debug
122-
System.err.println("Expected: \"" + expectMessage + "\"");
171+
System.err.println("Expected pattern (line-break):");
172+
System.err.println(expectMessage.replaceAll("\\n", NEW_LINE));
123173
System.err.println("---- Actual output begin");
124-
lines.forEach(l -> System.err.println(l));
174+
lines.forEach(System.err::println);
125175
System.err.println("---- Actual output end");
126176
fail("Unexpected log contents");
127177
}

0 commit comments

Comments
 (0)