Path: blob/master/test/jdk/tools/launcher/modules/basic/LauncherErrors.java
41153 views
/*1* Copyright (c) 2019, 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* @bug 822136826* @library /test/lib27* @modules jdk.compiler28* @build LauncherErrors jdk.test.lib.compiler.CompilerUtils29* @run testng LauncherErrors30* @summary Test launcher error message when fails to launch main class31*/323334import java.nio.file.Path;35import java.nio.file.Paths;3637import jdk.test.lib.compiler.CompilerUtils;38import jdk.test.lib.process.ProcessTools;39import org.testng.annotations.BeforeTest;40import org.testng.annotations.Test;41import static org.testng.Assert.*;4243public class LauncherErrors {44// test source location45private static final String TEST_SRC = System.getProperty("test.src");46private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");4748// module path49private static final Path MODS_DIR = Paths.get("mods");5051// the module name of the test module52private static final String TEST_MODULE = "test2";53// the main class of the test module54private static final String MAIN_CLASS = "jdk.test2.Main";5556@BeforeTest57public void compileTestModule() throws Exception {5859// javac -d mods/$TESTMODULE src/$TESTMODULE/**60boolean compiled =61CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE),62MODS_DIR.resolve(TEST_MODULE),63"--add-exports", "java.base/com.sun.crypto.provider=" + TEST_MODULE);6465assertTrue(compiled, "test module did not compile");66}6768/*69* Run jdk.test2.Main without security manager.70*/71@Test72public void test() throws Exception {73String dir = MODS_DIR.toString();74String mid = TEST_MODULE + "/" + MAIN_CLASS;7576ProcessTools.executeTestJava("--module-path", dir, "--module", mid)77.outputTo(System.out)78.errorTo(System.out)79.shouldHaveExitValue(0);80}8182/*83* Run jdk.test2.Main with security manager such that main class will84* fail to initialize.85*/86@Test87public void testErrorMessage() throws Exception {88String dir = MODS_DIR.toString();89String mid = TEST_MODULE + "/" + MAIN_CLASS;9091ProcessTools.executeTestJava("-Djava.security.manager", "--module-path", dir, "--module", mid)92.outputTo(System.out)93.errorTo(System.out)94.shouldContain("Error: Unable to initialize main class " + MAIN_CLASS + " in module " + TEST_MODULE)95.shouldContain("Caused by: java.security.AccessControlException: access denied")96.shouldNotHaveExitValue(0);97}9899}100101102