jdk/test/tools/launcher/MainClassCantBeLoadedTest.java
changeset 45339 f2ab1225d11f
parent 43809 c3669a70a7ab
--- a/jdk/test/tools/launcher/MainClassCantBeLoadedTest.java	Thu May 25 11:54:42 2017 +0100
+++ b/jdk/test/tools/launcher/MainClassCantBeLoadedTest.java	Thu May 25 10:40:08 2017 -0700
@@ -23,7 +23,7 @@
 
 /**
  * @test
- * @bug 8174694
+ * @bug 8174694 8181033
  * @summary improve error message shown when main class can't be loaded
  * @compile MainClassCantBeLoadedTest.java
  * @run main MainClassCantBeLoadedTest
@@ -85,6 +85,68 @@
             System.err.println(trExecution);
     }
 
+    @Test
+    void testFailToInitializeMainClass() throws Exception {
+        if (!isEnglishLocale()) {
+            return;
+        }
+
+        File cwd = new File(".");
+        File srcDir = new File(cwd, "src");
+        if (srcDir.exists()) {
+            recursiveDelete(srcDir);
+        }
+        srcDir.mkdirs();
+
+        /* we want to generate class C that will resolve additional class
+         */
+        ArrayList<String> scratchpad = new ArrayList<>();
+        scratchpad.add("public class C {");
+        scratchpad.add("    public static void main(String... args) {");
+        scratchpad.add("        try {");
+        scratchpad.add("            System.out.println(\"loading of restricted class\");");
+        scratchpad.add("        } catch (Exception e) {");
+        scratchpad.add("            java.security.Provider p = new com.sun.crypto.provider.SunJCE();");
+        scratchpad.add("            p.toString();");
+        scratchpad.add("        }");
+        scratchpad.add("    }");
+        scratchpad.add("}");
+        createFile(new File(srcDir, "C.java"), scratchpad);
+
+
+        // Compile and execute C should succeed
+        TestResult trCompilation = doExec(javacCmd,
+            "--add-exports", "java.base/com.sun.crypto.provider=ALL-UNNAMED",
+            "-d", "out",
+            new File(srcDir, "C.java").toString());
+        if (!trCompilation.isOK()) {
+            System.err.println(trCompilation);
+            throw new RuntimeException("Error: compiling");
+        }
+
+        TestResult trExecution = doExec(javaCmd,
+            "--add-exports", "java.base/com.sun.crypto.provider=ALL-UNNAMED",
+            "-cp", "out", "C");
+        if (!trExecution.isOK()) {
+            System.err.println(trExecution);
+            throw new RuntimeException("Error: executing");
+        }
+
+        // Execute C with security manager will fail with AccessControlException
+        trExecution = doExec(javaCmd,
+            "-Djava.security.manager",
+            "--add-exports", "java.base/com.sun.crypto.provider=ALL-UNNAMED",
+            "-cp", "out", "C");
+
+        // then this error message should be generated
+        trExecution.contains("Error: Unable to initialize main class C");
+        trExecution.contains("Caused by: java.security.AccessControlException: " +
+            "access denied (\"java.lang.RuntimePermission\"" +
+            " \"accessClassInPackage.com.sun.crypto.provider\")");
+        if (!trExecution.testStatus)
+            System.err.println(trExecution);
+    }
+
     public static void main(String[] args) throws Exception {
         MainClassCantBeLoadedTest a = new MainClassCantBeLoadedTest();
         a.run(args);