Path: blob/master/test/jdk/tools/launcher/modules/basic/InitErrors.java
41153 views
/*1* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @library /test/lib26* @build InitErrors27* @run testng InitErrors28* @summary Basic test to ensure that module system initialization errors29* go the right stream and with the right level of verbosity30*/313233import java.util.Arrays;34import jdk.test.lib.process.ProcessTools;35import jdk.test.lib.process.OutputAnalyzer;36import org.testng.annotations.Test;37import static org.testng.Assert.*;3839public class InitErrors {4041// the option to cause module initialization to fail42private static final String ADD_UNKNOWN_MODULE = "--add-modules=XXX";4344// the expected error message45private static final String UNKNOWN_MODULE_NOT_FOUND= "Module XXX not found";4647// output expected in the stack trace when using -Xlog:init=debug48private static final String STACK_FRAME = "java.base/java.lang.System.initPhase2";495051/**52* Default behavior, send error message to stdout53*/54@Test55public void testDefaultOutput() throws Exception {56expectFail(showVersion(ADD_UNKNOWN_MODULE)57.stdoutShouldContain(UNKNOWN_MODULE_NOT_FOUND)58.stdoutShouldNotContain(STACK_FRAME)59.stderrShouldNotContain(UNKNOWN_MODULE_NOT_FOUND)60.stderrShouldNotContain(STACK_FRAME));61}6263/**64* -XX:+DisplayVMOutputToStderr should send error message to stderr65*/66@Test67public void testOutputToStderr() throws Exception {68expectFail(showVersion(ADD_UNKNOWN_MODULE, "-XX:+DisplayVMOutputToStderr")69.stdoutShouldNotContain(UNKNOWN_MODULE_NOT_FOUND)70.stdoutShouldNotContain(STACK_FRAME)71.stderrShouldContain(UNKNOWN_MODULE_NOT_FOUND)72.stderrShouldNotContain(STACK_FRAME));73}7475/**76* -Xlog:init=debug should print stack trace to stdout77*/78@Test79public void testStackTrace() throws Exception {80expectFail(showVersion(ADD_UNKNOWN_MODULE, "-Xlog:init=debug")81.stdoutShouldContain(UNKNOWN_MODULE_NOT_FOUND)82.stdoutShouldContain(STACK_FRAME)83.stderrShouldNotContain(UNKNOWN_MODULE_NOT_FOUND)84.stderrShouldNotContain(STACK_FRAME));85}8687/**88* -Xlog:init=debug -XX:+DisplayVMOutputToStderr should print stack trace89* to stderr90*/91@Test92public void testStackTraceToStderr() throws Exception {93expectFail(showVersion(ADD_UNKNOWN_MODULE,94"-Xlog:init=debug",95"-XX:+DisplayVMOutputToStderr")96.stdoutShouldNotContain(UNKNOWN_MODULE_NOT_FOUND)97.stdoutShouldNotContain(STACK_FRAME)98.stderrShouldContain(UNKNOWN_MODULE_NOT_FOUND)99.stderrShouldContain(STACK_FRAME));100}101102private OutputAnalyzer showVersion(String... args) throws Exception {103int len = args.length;104args = Arrays.copyOf(args, len+1);105args[len] = "-version";106return ProcessTools.executeTestJava(args)107.outputTo(System.out)108.errorTo(System.out);109}110111private void expectFail(OutputAnalyzer output) {112assertFalse(output.getExitValue() == 0);113}114115}116117118