|
| 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.Optional; |
| 30 | +import java.util.stream.Stream; |
| 31 | + |
| 32 | + |
| 33 | +import org.junit.jupiter.params.provider.Arguments; |
| 34 | +import org.junit.jupiter.params.provider.MethodSource; |
| 35 | +import org.junit.jupiter.params.ParameterizedTest; |
| 36 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 37 | +import static org.junit.jupiter.api.Assertions.fail; |
| 38 | + |
| 39 | +/* |
| 40 | + * @test |
| 41 | + * @summary verify logging of call to System.exit or Runtime.exit. |
| 42 | + * @run junit/othervm RuntimeExitLogTest |
| 43 | + */ |
| 44 | + |
| 45 | +public class RuntimeExitLogTest { |
| 46 | + |
| 47 | + private static final String TEST_JDK = System.getProperty("test.jdk"); |
| 48 | + private static final String TEST_SRC = System.getProperty("test.src"); |
| 49 | + |
| 50 | + /** |
| 51 | + * Call System.exit() with the parameter (or zero if not supplied). |
| 52 | + * @param args zero or 1 argument, an exit status |
| 53 | + */ |
| 54 | + public static void main(String[] args) throws InterruptedException { |
| 55 | + int status = args.length > 0 ? Integer.parseInt(args[0]) : 0; |
| 56 | + System.exit(status); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Test various log level settings, and none. |
| 61 | + * @return a stream of arguments for parameterized test |
| 62 | + */ |
| 63 | + private static Stream<Arguments> logParamProvider() { |
| 64 | + return Stream.of( |
| 65 | + // Logging enabled with level DEBUG |
| 66 | + Arguments.of(List.of("-Djava.util.logging.config.file=" + |
| 67 | + Path.of(TEST_SRC, "ExitLogging-FINE.properties").toString()), 1, true), |
| 68 | + // Logging disabled due to level |
| 69 | + Arguments.of(List.of("-Djava.util.logging.config.file=" + |
| 70 | + Path.of(TEST_SRC, "ExitLogging-INFO.properties").toString()), 2, false), |
| 71 | + // Console logger |
| 72 | + Arguments.of(List.of("--limit-modules", "java.base", |
| 73 | + "-Djdk.system.logger.level=DEBUG"), 3, true), |
| 74 | + // Console logger |
| 75 | + Arguments.of(List.of(), 4, false) |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Check that the logger output of a launched process contains the expected message. |
| 81 | + * @param logProps The name of the log.properties file to set on the command line |
| 82 | + * @param status the expected exit status of the process |
| 83 | + * @param shouldLog true if the log should contain the message expected from Runtime.exit(status) |
| 84 | + */ |
| 85 | + @ParameterizedTest |
| 86 | + @MethodSource("logParamProvider") |
| 87 | + public void checkLogger(List<String> logProps, int status, boolean shouldLog) { |
| 88 | + ProcessBuilder pb = new ProcessBuilder(); |
| 89 | + pb.redirectErrorStream(true); |
| 90 | + |
| 91 | + List<String> cmd = pb.command(); |
| 92 | + cmd.add(Path.of(TEST_JDK,"bin", "java").toString()); |
| 93 | + cmd.addAll(logProps); |
| 94 | + cmd.add(this.getClass().getName()); |
| 95 | + cmd.add(Integer.toString(status)); |
| 96 | + |
| 97 | + try { |
| 98 | + Process process = pb.start(); |
| 99 | + try (BufferedReader reader = process.inputReader()) { |
| 100 | + List<String> lines = reader.lines().toList(); |
| 101 | + final String expected = "Runtime.exit() called with status: " + status; |
| 102 | + Optional<String> found = lines.stream().filter(s -> s.contains(expected)).findFirst(); |
| 103 | + if (found.isPresent() != shouldLog) { |
| 104 | + System.err.println("---- Process output begin"); |
| 105 | + lines.forEach(l -> System.err.println(l)); |
| 106 | + System.err.println("---- Process output end"); |
| 107 | + fail("Unexpected log contents"); |
| 108 | + } |
| 109 | + } |
| 110 | + int result = process.waitFor(); |
| 111 | + assertEquals(status, result, "Exit status"); |
| 112 | + } catch (IOException | InterruptedException ex) { |
| 113 | + fail(ex); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments