1 /* 2 * Copyright (c) 2017, 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 * @test 26 * @library /lib/testlibrary 27 * @build InitErrors jdk.testlibrary.* 28 * @run testng InitErrors 29 * @summary Basic test to ensure that module system initialization errors 30 * go the right stream and with the right level of verbosity 31 */ 32 33 34 import java.util.Arrays; 35 import jdk.testlibrary.ProcessTools; 36 import jdk.testlibrary.OutputAnalyzer; 37 import org.testng.annotations.Test; 38 import static org.testng.Assert.*; 39 40 public class InitErrors { 41 42 // the option to cause module initialization to fail 43 private static final String ADD_UNKNOWN_MODULE = "--add-modules=XXX"; 44 45 // the expected error message 46 private static final String UNKNOWN_MODULE_NOT_FOUND= "Module XXX not found"; 47 48 // output expected in the stack trace when using -Xlog:init=debug 49 private static final String STACK_FRAME = "java.base/java.lang.System.initPhase2"; 50 51 52 /** 53 * Default behavior, send error message to stdout 54 */ 55 @Test 56 public void testDefaultOutput() throws Exception { 57 expectFail(showVersion(ADD_UNKNOWN_MODULE) 58 .stdoutShouldContain(UNKNOWN_MODULE_NOT_FOUND) 59 .stdoutShouldNotContain(STACK_FRAME) 60 .stderrShouldNotContain(UNKNOWN_MODULE_NOT_FOUND) 61 .stderrShouldNotContain(STACK_FRAME)); 62 } 63 64 /** 65 * -XX:+DisplayVMOutputToStderr should send error message to stderr 66 */ 67 @Test 68 public void testOutputToStderr() throws Exception { 69 expectFail(showVersion(ADD_UNKNOWN_MODULE, "-XX:+DisplayVMOutputToStderr") 70 .stdoutShouldNotContain(UNKNOWN_MODULE_NOT_FOUND) 71 .stdoutShouldNotContain(STACK_FRAME) 72 .stderrShouldContain(UNKNOWN_MODULE_NOT_FOUND) 73 .stderrShouldNotContain(STACK_FRAME)); 74 } 75 76 /** 77 * -Xlog:init=debug should print stack trace to stdout 78 */ 79 @Test 80 public void testStackTrace() throws Exception { 81 expectFail(showVersion(ADD_UNKNOWN_MODULE, "-Xlog:init=debug") 82 .stdoutShouldContain(UNKNOWN_MODULE_NOT_FOUND) 83 .stdoutShouldContain(STACK_FRAME) 84 .stderrShouldNotContain(UNKNOWN_MODULE_NOT_FOUND) 85 .stderrShouldNotContain(STACK_FRAME)); 86 } 87 88 /** 89 * -Xlog:init=debug -XX:+DisplayVMOutputToStderr should print stack trace 90 * to stderr 91 */ 92 @Test 93 public void testStackTraceToStderr() throws Exception { 94 expectFail(showVersion(ADD_UNKNOWN_MODULE, 95 "-Xlog:init=debug", 96 "-XX:+DisplayVMOutputToStderr") 97 .stdoutShouldNotContain(UNKNOWN_MODULE_NOT_FOUND) 98 .stdoutShouldNotContain(STACK_FRAME) 99 .stderrShouldContain(UNKNOWN_MODULE_NOT_FOUND) 100 .stderrShouldContain(STACK_FRAME)); 101 } 102 103 private OutputAnalyzer showVersion(String... args) throws Exception { 104 int len = args.length; 105 args = Arrays.copyOf(args, len+1); 106 args[len] = "-version"; 107 return ProcessTools.executeTestJava(args) 108 .outputTo(System.out) 109 .errorTo(System.out); 110 } 111 112 private void expectFail(OutputAnalyzer output) { 113 assertFalse(output.getExitValue() == 0); 114 } 115 116 }