1 /*
  2  * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 
 25 import java.io.BufferedReader;
 26 import java.io.IOException;
 27 import java.nio.file.Path;
 28 import java.util.List;
 29 import java.util.logging.LogRecord;
 30 import java.util.logging.Logger;
 31 import java.util.logging.StreamHandler;
 32 import java.util.stream.Stream;
 33 
 34 
 35 import org.junit.jupiter.params.provider.Arguments;
 36 import org.junit.jupiter.params.provider.MethodSource;
 37 import org.junit.jupiter.params.ParameterizedTest;
 38 import static org.junit.jupiter.api.Assertions.assertEquals;
 39 import static org.junit.jupiter.api.Assertions.fail;
 40 
 41 /*
 42  * @test
 43  * @summary verify logging of call to System.exit or Runtime.exit.
 44  * @requires vm.flagless
 45  * @run junit/othervm RuntimeExitLogTest
 46  */
 47 
 48 public class RuntimeExitLogTest {
 49 
 50     private static final String TEST_JDK = System.getProperty("test.jdk");
 51     private static final String TEST_SRC = System.getProperty("test.src");
 52 
 53     private static Object HOLD_LOGGER;
 54 
 55     /**
 56      * Call System.exit() with the parameter (or zero if not supplied).
 57      * @param args zero or 1 argument, an exit status
 58      */
 59     public static void main(String[] args) throws InterruptedException {
 60         int status = args.length > 0 ? Integer.parseInt(args[0]) : 0;
 61         if (System.getProperty("ThrowingHandler") != null) {
 62             HOLD_LOGGER = ThrowingHandler.installHandler();
 63         }
 64         System.exit(status);
 65     }
 66 
 67     /**
 68      * Test various log level settings, and none.
 69      * @return a stream of arguments for parameterized test
 70      */
 71     private static Stream<Arguments> logParamProvider() {
 72         return Stream.of(
 73                 // Logging enabled with level DEBUG
 74                 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
 78                 Arguments.of(List.of("-Djava.util.logging.config.file=" +
 79                         Path.of(TEST_SRC, "ExitLogging-INFO.properties").toString()), 2,
 80                         ""),
 81                 // Console logger
 82                 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, ""),
 87                 // Throwing Handler
 88                 Arguments.of(List.of("-DThrowingHandler",
 89                         "-Djava.util.logging.config.file=" +
 90                         Path.of(TEST_SRC, "ExitLogging-FINE.properties").toString()), 5,
 91                         "Runtime.exit(5) logging failed: Exception in publish")
 92                 );
 93     }
 94 
 95     /**
 96      * Check that the logger output of a launched process contains the expected message.
 97      * @param logProps The name of the log.properties file to set on the command line
 98      * @param status the expected exit status of the process
 99      * @param expectMessage log should contain the message
100      */
101     @ParameterizedTest
102     @MethodSource("logParamProvider")
103     public void checkLogger(List<String> logProps, int status, String expectMessage) {
104         ProcessBuilder pb = new ProcessBuilder();
105         pb.redirectErrorStream(true);
106 
107         List<String> cmd = pb.command();
108         cmd.add(Path.of(TEST_JDK,"bin", "java").toString());
109         cmd.addAll(logProps);
110         cmd.add(this.getClass().getName());
111         cmd.add(Integer.toString(status));
112 
113         try {
114             Process process = pb.start();
115             try (BufferedReader reader = process.inputReader()) {
116                 List<String> lines = reader.lines().toList();
117                 boolean match = (expectMessage.isEmpty())
118                         ? lines.size() == 0
119                         : lines.stream().filter(s -> s.contains(expectMessage)).findFirst().isPresent();
120                 if (!match) {
121                     // Output lines for debug
122                     System.err.println("Expected: \"" + expectMessage + "\"");
123                     System.err.println("---- Actual output begin");
124                     lines.forEach(l -> System.err.println(l));
125                     System.err.println("---- Actual output end");
126                     fail("Unexpected log contents");
127                 }
128             }
129             int result = process.waitFor();
130             assertEquals(status, result, "Exit status");
131         } catch (IOException | InterruptedException ex) {
132             fail(ex);
133         }
134     }
135 
136     /**
137      * A LoggingHandler that throws an Exception.
138      */
139     public static class ThrowingHandler extends StreamHandler {
140 
141         // Install this handler for java.lang.Runtime
142         public static Logger installHandler() {
143             Logger logger = Logger.getLogger("java.lang.Runtime");
144             logger.addHandler(new ThrowingHandler());
145             return logger;
146         }
147 
148         @Override
149         public synchronized void publish(LogRecord record) {
150             super.publish(record);
151             throw new RuntimeException("Exception in publish");
152         }
153     }
154 }