1
1
/*
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.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
@@ -49,7 +49,7 @@ public class RuntimeExitLogTest {
49
49
50
50
private static final String TEST_JDK = System .getProperty ("test.jdk" );
51
51
private static final String TEST_SRC = System .getProperty ("test.src" );
52
-
52
+ private static final String NEW_LINE = System . lineSeparator ();
53
53
private static Object HOLD_LOGGER ;
54
54
55
55
/**
@@ -64,31 +64,80 @@ public static void main(String[] args) throws InterruptedException {
64
64
System .exit (status );
65
65
}
66
66
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
+
67
81
/**
68
82
* Test various log level settings, and none.
69
83
* @return a stream of arguments for parameterized test
70
84
*/
71
85
private static Stream <Arguments > logParamProvider () {
72
86
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 )),
74
94
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
+ "" ),
78
106
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 ,
80
108
"" ),
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 )),
82
120
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
+
87
133
// Throwing Handler
88
134
Arguments .of (List .of ("-DThrowingHandler" ,
89
135
"-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 , "" )
92
141
);
93
142
}
94
143
@@ -115,13 +164,14 @@ public void checkLogger(List<String> logProps, int status, String expectMessage)
115
164
try (BufferedReader reader = process .inputReader ()) {
116
165
List <String > lines = reader .lines ().toList ();
117
166
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 );
120
169
if (!match ) {
121
170
// 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 ));
123
173
System .err .println ("---- Actual output begin" );
124
- lines .forEach (l -> System .err . println ( l ) );
174
+ lines .forEach (System .err :: println );
125
175
System .err .println ("---- Actual output end" );
126
176
fail ("Unexpected log contents" );
127
177
}
0 commit comments