Path: blob/master/test/jdk/tools/launcher/MainClassCantBeLoadedTest.java
41145 views
/*1* Copyright (c) 2017, 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 8174694 818103326* @summary improve error message shown when main class can't be loaded27* @compile MainClassCantBeLoadedTest.java28* @run main MainClassCantBeLoadedTest29*/3031import java.io.*;32import java.util.*;3334public class MainClassCantBeLoadedTest extends TestHelper {35private MainClassCantBeLoadedTest(){}3637@Test38void testLoadingClassWithMissingSuper() throws Exception {39if (!isEnglishLocale()) {40return;41}4243File cwd = new File(".");44File srcDir = new File(cwd, "src");45if (srcDir.exists()) {46recursiveDelete(srcDir);47}48srcDir.mkdirs();4950/* we want to generate two classes A and B, where B is the superclass of A51* class A has a main method52*/53ArrayList<String> scratchpad = new ArrayList<>();54scratchpad.add("public class A extends B {");55scratchpad.add(" public static void main(String... args) {}");56scratchpad.add("}");57createFile(new File(srcDir, "A.java"), scratchpad);5859scratchpad.clear();60scratchpad.add("class B {}");61createFile(new File(srcDir, "B.java"), scratchpad);6263// let's compile both64TestResult trCompilation = doExec(javacCmd,65"-d", "out",66new File(srcDir, "A.java").toString(),67new File(srcDir, "B.java").toString());68if (!trCompilation.isOK()) {69System.err.println(trCompilation);70throw new RuntimeException("Error: compiling");71}7273// and now B is removed74File outDir = new File(cwd, "out");75File bClass = new File(outDir, "B.class");76bClass.delete();7778// if A is executed79TestResult trExecution = doExec(javaCmd, "-cp", "out", "A");80// then this error message should be generated81trExecution.contains("Error: Could not find or load main class A");82trExecution.contains("Caused by: java.lang.NoClassDefFoundError: B");83if (!trExecution.testStatus)84System.err.println(trExecution);85}8687@Test88void testFailToInitializeMainClass() throws Exception {89if (!isEnglishLocale()) {90return;91}9293File cwd = new File(".");94File srcDir = new File(cwd, "src");95if (srcDir.exists()) {96recursiveDelete(srcDir);97}98srcDir.mkdirs();99100/* we want to generate class C that will resolve additional class101*/102ArrayList<String> scratchpad = new ArrayList<>();103scratchpad.add("public class C {");104scratchpad.add(" public static void main(String... args) {");105scratchpad.add(" try {");106scratchpad.add(" System.out.println(\"loading of restricted class\");");107scratchpad.add(" } catch (Exception e) {");108scratchpad.add(" java.security.Provider p = new com.sun.crypto.provider.SunJCE();");109scratchpad.add(" p.toString();");110scratchpad.add(" }");111scratchpad.add(" }");112scratchpad.add("}");113createFile(new File(srcDir, "C.java"), scratchpad);114115116// Compile and execute C should succeed117TestResult trCompilation = doExec(javacCmd,118"--add-exports", "java.base/com.sun.crypto.provider=ALL-UNNAMED",119"-d", "out",120new File(srcDir, "C.java").toString());121if (!trCompilation.isOK()) {122System.err.println(trCompilation);123throw new RuntimeException("Error: compiling");124}125126TestResult trExecution = doExec(javaCmd,127"--add-exports", "java.base/com.sun.crypto.provider=ALL-UNNAMED",128"-cp", "out", "C");129if (!trExecution.isOK()) {130System.err.println(trExecution);131throw new RuntimeException("Error: executing");132}133134// Execute C with security manager will fail with AccessControlException135trExecution = doExec(javaCmd,136"-Djava.security.manager",137"--add-exports", "java.base/com.sun.crypto.provider=ALL-UNNAMED",138"-cp", "out", "C");139140// then this error message should be generated141trExecution.contains("Error: Unable to initialize main class C");142trExecution.contains("Caused by: java.security.AccessControlException: " +143"access denied (\"java.lang.RuntimePermission\"" +144" \"accessClassInPackage.com.sun.crypto.provider\")");145if (!trExecution.testStatus)146System.err.println(trExecution);147}148149public static void main(String[] args) throws Exception {150MainClassCantBeLoadedTest a = new MainClassCantBeLoadedTest();151a.run(args);152if (testExitValue > 0) {153System.out.println("Total of " + testExitValue + " failed");154throw new RuntimeException("Test failed");155} else {156System.out.println("Test passed");157}158}159}160161162