changeset 45131:bc590fe5c060

8177328: java/lang/ClassLoader/securityManager/ClassLoaderTest.java times out with -Xcomp Summary: remove unneeded automatic module tests; refactor into multiple @runs Reviewed-by: mchung
author bchristi
date Fri, 12 May 2017 10:11:42 -0700
parents 469dceb426cc
children db2f2d72cd4f
files jdk/test/java/lang/ClassLoader/securityManager/ClassLoaderTest.java
diffstat 1 files changed, 165 insertions(+), 166 deletions(-) [+]
line wrap: on
line diff
--- a/jdk/test/java/lang/ClassLoader/securityManager/ClassLoaderTest.java	Fri May 12 09:42:23 2017 -0700
+++ b/jdk/test/java/lang/ClassLoader/securityManager/ClassLoaderTest.java	Fri May 12 10:11:42 2017 -0700
@@ -28,8 +28,14 @@
  *          (in)valid security policy file.
  * @library /lib/testlibrary
  * @modules java.base/jdk.internal.module
- * @build JarUtils CompilerUtils
- * @run main/timeout=240 ClassLoaderTest
+ * @build JarUtils
+ * @build TestClassLoader TestClient
+ * @run main ClassLoaderTest -noPolicy
+ * @run main ClassLoaderTest -validPolicy
+ * @run main ClassLoaderTest -invalidPolicy
+ * @run main ClassLoaderTest -noPolicy      -customSCL
+ * @run main ClassLoaderTest -validPolicy   -customSCL
+ * @run main ClassLoaderTest -invalidPolicy -customSCL
  */
 import java.io.File;
 import java.io.OutputStream;
@@ -37,25 +43,23 @@
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.nio.file.StandardCopyOption;
-import java.util.Map;
-import java.util.HashMap;
+import java.util.stream.Stream;
 import java.lang.module.ModuleDescriptor;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
 import jdk.internal.module.ModuleInfoWriter;
 import jdk.testlibrary.ProcessTools;
 
 public class ClassLoaderTest {
 
     private static final String SRC = System.getProperty("test.src");
-    private static final Path CL_SRC = Paths.get(SRC, "TestClassLoader.java");
-    private static final Path C_SRC = Paths.get(SRC, "TestClient.java");
-    private static final Path CL_BIN = Paths.get("classes", "clbin");
-    private static final Path C_BIN = Paths.get("classes", "cbin");
+    private static final Path TEST_CLASSES =
+            Paths.get(System.getProperty("test.classes"));
     private static final Path ARTIFACT_DIR = Paths.get("jars");
     private static final Path VALID_POLICY = Paths.get(SRC, "valid.policy");
     private static final Path INVALID_POLICY
             = Paths.get(SRC, "malformed.policy");
-    private static final Path NO_POLICY = null;
-    private static final String LOCALE = "-Duser.language=en -Duser.region=US";
     /*
      * Here is the naming convention followed for each jar.
      * cl.jar   - Regular custom class loader jar.
@@ -69,180 +73,175 @@
     private static final Path C_JAR = ARTIFACT_DIR.resolve("c.jar");
     private static final Path MC_JAR = ARTIFACT_DIR.resolve("mc.jar");
     private static final Path AMC_JAR = ARTIFACT_DIR.resolve("amc.jar");
-    private static final Map<String, String> MSG_MAP = new HashMap<>();
 
-    static {
-        // This mapping help process finding expected message based
-        // on the key passed as argument while executing java command.
-        MSG_MAP.put("MissingModule", "Module cl not found, required by mc");
-        MSG_MAP.put("ErrorPolicy", "java.security.policy: error parsing file");
-        MSG_MAP.put(
-                "SystemCL", "jdk.internal.loader.ClassLoaders$AppClassLoader");
-        MSG_MAP.put("CustomCL", "cl.TestClassLoader");
+    // Expected output messages
+    private static final String MISSING_MODULE =
+            "Module cl not found, required by mc";
+    private static final String POLICY_ERROR =
+            "java.security.policy: error parsing file";
+    private static final String SYSTEM_CL_MSG =
+            "jdk.internal.loader.ClassLoaders$AppClassLoader";
+    private static final String CUSTOM_CL_MSG = "cl.TestClassLoader";
+
+    // Member vars
+    private final boolean useSCL;       // Use default system loader, or custom
+    private final String smMsg;         // Security manager message, or ""
+    private final String autoAddModArg; // Flag to add cl modules, or ""
+    private final String addmodArg;     // Flag to add mcl modules, or ""
+    private final String expectedStatus;// Expected exit status from client
+    private final String expectedMsg;   // Expected output message from client
+
+    // Common set of VM arguments used in all test cases
+    private final List<String> commonArgs;
+
+    public ClassLoaderTest(Path policy, boolean useSCL) {
+        this.useSCL = useSCL;
+
+        List<String> argList = new LinkedList<>();
+        argList.add("-Duser.language=en");
+        argList.add("-Duser.region=US");
+
+        boolean malformedPolicy = false;
+        if (policy == null) {
+            smMsg = "Without SecurityManager";
+        } else {
+            malformedPolicy = policy.equals(INVALID_POLICY);
+            argList.add("-Djava.security.manager");
+            argList.add("-Djava.security.policy=" +
+                    policy.toFile().getAbsolutePath());
+            smMsg = "With SecurityManager";
+        }
+
+        if (useSCL) {
+            autoAddModArg = "";
+            addmodArg = "";
+        } else {
+            argList.add("-Djava.system.class.loader=cl.TestClassLoader");
+            autoAddModArg = "--add-modules=cl";
+            addmodArg = "--add-modules=mcl";
+        }
+
+        if (malformedPolicy) {
+            expectedStatus = "FAIL";
+            expectedMsg = POLICY_ERROR;
+        } else if (useSCL) {
+            expectedStatus = "PASS";
+            expectedMsg = SYSTEM_CL_MSG;
+        } else {
+            expectedStatus = "PASS";
+            expectedMsg = CUSTOM_CL_MSG;
+        }
+        commonArgs = Collections.unmodifiableList(argList);
     }
 
     public static void main(String[] args) throws Exception {
+        Path policy;
+        if (args[0].equals("-noPolicy")) {
+            policy = null;
+        } else if (args[0].equals("-validPolicy")) {
+            policy = VALID_POLICY;
+        } else if (args[0].equals("-invalidPolicy")) {
+            policy = INVALID_POLICY;
+        } else {
+            throw new RuntimeException("Unknown policy arg: " + args[0]);
+        }
 
-        // Generates regular and modular jars before start processing it.
+        boolean useSystemLoader = true;
+        if (args.length > 1) {
+            if (args[1].equals("-customSCL")) {
+                useSystemLoader = false;
+            } else {
+                throw new RuntimeException("Unknown custom loader arg: " + args[1]);
+            }
+        }
+
+        ClassLoaderTest test = new ClassLoaderTest(policy, useSystemLoader);
         setUp();
-        processForEachPolicyFile();
+        test.processForPolicyFile();
     }
 
     /**
      * Test cases are based on the following logic,
-     *  for (policyFile : {"NO_POLICY", "VALID", "MALFORMED"}) {
-     *      for (classLoader : {"SystemClassLoader", "CustomClassLoader"}){
-     *          for (clientModule : {"NAMED", "AUTOMATIC", "UNNAMED"}) {
-     *              for (classLoaderModule : {"NAMED", "AUTOMATIC", "UNNAMED"}) {
-     *                  Create and run java command for each possible Test case
-     *              }
-     *          }
+     *  given: a policyFile in {none, valid, malformed} and
+     *         a classLoader in {SystemClassLoader, CustomClassLoader}:
+     *  for (clientModule : {"NAMED", "UNNAMED"}) {
+     *      for (classLoaderModule : {"NAMED", "UNNAMED"}) {
+     *          Create and run java command for each possible Test case
      *      }
      *  }
      */
-    private static void processForEachPolicyFile() throws Exception {
-
-        final String regCLloc = CL_JAR.toFile().getAbsolutePath();
-        final String modCLloc = MCL_JAR.toFile().getAbsolutePath();
-        final String regCloc = C_JAR.toFile().getAbsolutePath();
-        final String modCloc = MC_JAR.toFile().getAbsolutePath();
+    private void processForPolicyFile() throws Exception {
+        final String regLoaderLoc = CL_JAR.toFile().getAbsolutePath();
+        final String modLoadrLoc = MCL_JAR.toFile().getAbsolutePath();
+        final String regClientLoc = C_JAR.toFile().getAbsolutePath();
+        final String modClientLoc = MC_JAR.toFile().getAbsolutePath();
         final String autoModCloc = AMC_JAR.toFile().getAbsolutePath();
         final String separator = File.pathSeparator;
 
-        for (Path policy
-                : new Path[]{NO_POLICY, VALID_POLICY, INVALID_POLICY}) {
-            final String policyFile = (policy != null)
-                    ? policy.toFile().getAbsolutePath() : null;
-            final boolean malformedPolicy
-                    = (policy == null) ? false : policy.equals(INVALID_POLICY);
+        // NAMED-NAMED:
+        System.out.println("Case:- Modular Client and " +
+                ((useSCL) ? "SystemClassLoader"
+                        : "Modular CustomClassLoader") + " " + smMsg);
+        execute("--module-path", modClientLoc + separator + modLoadrLoc, "-m",
+                "mc/c.TestClient");
 
-            for (boolean useSCL : new boolean[]{true, false}) {
-                final String clVmArg = (useSCL) ? ""
-                        : "-Djava.system.class.loader=cl.TestClassLoader";
-                final String autoAddModArg
-                        = (useSCL) ? "" : "--add-modules=cl";
-                final String addmodArg = (useSCL) ? "" : "--add-modules=mcl";
-                final String sMArg = (policy != null) ? String.format(
-                        "-Djava.security.manager -Djava.security.policy=%s",
-                        policyFile) : "";
-                final String smMsg = (policy != null) ? "With SecurityManager"
-                        : "Without SecurityManager";
-                final String expectedResult = ((!malformedPolicy)
-                        ? ((useSCL) ? "PASS SystemCL" : "PASS CustomCL")
-                        : "FAIL ErrorPolicy");
+        // NAMED-UNNAMED:
+        System.out.println("Case:- Modular Client and " + ((useSCL)
+                ? "SystemClassLoader"
+                : "Unknown modular CustomClassLoader") + " " + smMsg);
+        execute(new String[] {"--module-path", autoModCloc, "-cp", regLoaderLoc,
+                "-m", "mc/c.TestClient"},
+                "FAIL", MISSING_MODULE);
 
-                // NAMED-NAMED, NAMED-AUTOMATIC, NAMED-UNNAMED
-                System.out.printf("Case:- Modular Client and %s %s%n",
-                        ((useSCL) ? "SystemClassLoader"
-                                : "Modular CustomClassLoader"), smMsg);
-                execute(new String[]{String.format(
-                    "--module-path %s%s%s %s %s %s -m mc/c.TestClient",
-                    modCloc, separator, modCLloc, LOCALE, clVmArg, sMArg),
-                    expectedResult});
-                System.out.printf("Case:- Modular Client and %s %s%n", ((useSCL)
-                        ? "SystemClassLoader"
-                        : "Automatic modular CustomClassLoader"), smMsg);
-                execute(new String[]{String.format(
-                    "--module-path %s%s%s %s %s %s -m mc/c.TestClient",
-                    autoModCloc, separator, regCLloc, LOCALE, clVmArg, sMArg),
-                    expectedResult});
-                System.out.printf("Case:- Modular Client and %s %s%n", ((useSCL)
-                        ? "SystemClassLoader"
-                        : "Unknown modular CustomClassLoader"), smMsg);
-                execute(new String[]{String.format(
-                    "--module-path %s -cp %s %s %s %s -m mc/c.TestClient",
-                    autoModCloc, regCLloc, LOCALE, clVmArg, sMArg),
-                    "FAIL MissingModule"});
+        // UNNAMED-NAMED:
+        System.out.println("Case:- Unknown modular Client and " +
+                ((useSCL) ? "SystemClassLoader"
+                      : "Modular CustomClassLoader") + " " + smMsg);
+        execute("-cp", regClientLoc, "--module-path", modLoadrLoc, addmodArg,
+                "c.TestClient");
 
-                // AUTOMATIC-NAMED, AUTOMATIC-AUTOMATIC, AUTOMATIC-UNNAMED
-                System.out.printf("Case:- Automated modular Client and %s %s%n",
-                        ((useSCL) ? "SystemClassLoader"
-                                : "Modular CustomClassLoader"), smMsg);
-                execute(new String[]{String.format(
-                    "--module-path %s%s%s %s %s %s %s -m c/c.TestClient",
-                    regCloc, separator, modCLloc, addmodArg, LOCALE, clVmArg,
-                    sMArg), expectedResult});
-                System.out.printf("Case:- Automated modular Client and %s %s%n",
-                        ((useSCL) ? "SystemClassLoader"
-                                : "Automatic modular CustomClassLoader"),
-                        smMsg);
-                execute(new String[]{String.format(
-                    "--module-path %s%s%s %s %s %s %s -m c/c.TestClient",
-                    regCloc, separator, regCLloc, autoAddModArg, LOCALE,
-                    clVmArg, sMArg), expectedResult});
-                System.out.printf("Case:- Automated modular Client and %s %s%n",
-                        ((useSCL) ? "SystemClassLoader"
-                                : "Unknown modular CustomClassLoader"), smMsg);
-                execute(new String[]{String.format(
-                    "--module-path %s -cp %s %s %s %s -m c/c.TestClient",
-                    regCloc, regCLloc, LOCALE, clVmArg, sMArg),
-                    expectedResult});
+        // UNNAMED-UNNAMED:
+        System.out.println("Case:- Unknown modular Client and " +
+                ((useSCL) ? "SystemClassLoader"
+                        : "Unknown modular CustomClassLoader") + " " + smMsg);
+        execute("-cp", regClientLoc + separator + regLoaderLoc, "c.TestClient");
 
-                // UNNAMED-NAMED, UNNAMED-AUTOMATIC, UNNAMED-UNNAMED
-                System.out.printf("Case:- Unknown modular Client and %s %s%n",
-                        ((useSCL) ? "SystemClassLoader"
-                                : "Modular CustomClassLoader"), smMsg);
-                execute(new String[]{String.format(
-                    "-cp %s --module-path %s %s %s %s %s c.TestClient",
-                    regCloc, modCLloc, addmodArg, LOCALE, clVmArg, sMArg),
-                    expectedResult});
-                System.out.printf("Case:- Unknown modular Client and %s %s%n",
-                        ((useSCL) ? "SystemClassLoader"
-                                : "Automatic modular CustomClassLoader"),
-                        smMsg);
-                execute(new String[]{String.format(
-                    "-cp %s --module-path %s %s %s %s %s c.TestClient",
-                    regCloc, regCLloc, autoAddModArg, LOCALE, clVmArg, sMArg),
-                    expectedResult});
-                System.out.printf("Case:- Unknown modular Client and %s %s%n",
-                        ((useSCL) ? "SystemClassLoader"
-                                : "Unknown modular CustomClassLoader"), smMsg);
-                execute(new String[]{String.format(
-                    "-cp %s%s%s %s %s %s c.TestClient", regCloc, separator,
-                    regCLloc, LOCALE, clVmArg, sMArg), expectedResult});
+        // Regular jars in module-path
+        System.out.println("Case:- Regular Client and " + ((useSCL)
+                ? "SystemClassLoader"
+                : "Unknown modular CustomClassLoader") +
+                " inside --module-path " + smMsg);
+        execute("--module-path", regClientLoc + separator + regLoaderLoc,
+                autoAddModArg, "-m", "c/c.TestClient");
 
-                // Regular jars in module-path and Modular jars in class-path.
-                System.out.printf("Case:- Regular Client and %s "
-                        + "inside --module-path %s.%n", ((useSCL)
-                                ? "SystemClassLoader"
-                                : "Unknown modular CustomClassLoader"), smMsg);
-                execute(new String[]{String.format(
-                    "--module-path %s%s%s %s %s %s %s -m c/c.TestClient",
-                    regCloc, separator, regCLloc, autoAddModArg, LOCALE,
-                    clVmArg, sMArg), expectedResult});
-                System.out.printf("Case:- Modular Client and %s in -cp %s%n",
-                        ((useSCL) ? "SystemClassLoader"
-                                : "Modular CustomClassLoader"), smMsg);
-                execute(new String[]{String.format(
-                    "-cp %s%s%s %s %s %s c.TestClient", modCloc, separator,
-                    modCLloc, LOCALE, clVmArg, sMArg), expectedResult});
-            }
-        }
+        // Modular jars in class-path
+        System.out.println("Case:- Modular Client and " +
+                ((useSCL) ? "SystemClassLoader"
+                        : "Modular CustomClassLoader") + " in -cp " + smMsg);
+        execute("-cp", modClientLoc + separator + modLoadrLoc, "c.TestClient");
+    }
+
+    private void execute(String... args) throws Exception {
+        execute(args, this.expectedStatus, this.expectedMsg);
     }
 
     /**
      * Execute with command arguments and process the result.
      */
-    private static void execute(String[] args) throws Exception {
+    private void execute(String[] args, String status, String msg) throws Exception {
 
-        String status = null;
-        String msgKey = null;
-        if ((args != null && args.length > 1)) {
-            String[] secArgs = args[1].split("\\s+");
-            status = (secArgs.length > 0) ? secArgs[0] : null;
-            msgKey = (secArgs.length > 1) ? secArgs[1] : null;
-        }
-        String out = ProcessTools.executeTestJvm(args[0].split("\\s+"))
-                .getOutput();
+        // Combine with commonArgs, and perform sanity check
+        String[] safeArgs = Stream.concat(commonArgs.stream(), Stream.of(args))
+                .filter(s -> {
+                    if (s.contains(" ")) { throw new RuntimeException("No spaces in args");}
+                    return !s.isEmpty();
+                }).toArray(String[]::new);
+        String out = ProcessTools.executeTestJvm(safeArgs).getOutput();
         // Handle response.
-        if ((status != null && "PASS".equals(status) && msgKey != null
-                && out.contains(MSG_MAP.get(msgKey)))) {
-            System.out.printf("PASS: Expected Result: %s.%n",
-                    MSG_MAP.get(msgKey));
-        } else if ((status != null && "FAIL".equals(status) && msgKey != null
-                && out.contains(MSG_MAP.get(msgKey)))) {
-            System.out.printf("PASS: Expected Failure: %s.%n",
-                    MSG_MAP.get(msgKey));
+        if ("PASS".equals(status) && out.contains(msg)) {
+            System.out.println("PASS: Expected Result: " + msg);
+        } else if ("FAIL".equals(status) && out.contains(msg)) {
+            System.out.printf("PASS: Expected Failure: " +  msg);
         } else if (out.contains("Exception") || out.contains("Error")) {
             System.out.printf("OUTPUT: %s", out);
             throw new RuntimeException("FAIL: Unknown Exception.");
@@ -257,23 +256,23 @@
      */
     private static void setUp() throws Exception {
 
-        boolean compiled = CompilerUtils.compile(CL_SRC, CL_BIN);
-        compiled &= CompilerUtils.compile(C_SRC, C_BIN);
-        if (!compiled) {
-            throw new RuntimeException("Test Setup failed.");
-        }
         // Generate regular jar files for TestClient and TestClassLoader
-        JarUtils.createJarFile(CL_JAR, CL_BIN);
-        JarUtils.createJarFile(C_JAR, C_BIN);
+        JarUtils.createJarFile(CL_JAR, TEST_CLASSES,
+                               "cl/TestClassLoader.class");
+        JarUtils.createJarFile(C_JAR, TEST_CLASSES,
+                               "c/TestClient.class");
         // Generate modular jar files for TestClient and TestClassLoader with
         // their corresponding ModuleDescriptor.
-        Files.copy(CL_JAR, MCL_JAR, StandardCopyOption.REPLACE_EXISTING);
+        Files.copy(CL_JAR, MCL_JAR,
+                StandardCopyOption.REPLACE_EXISTING);
         updateModuleDescr(MCL_JAR, ModuleDescriptor.newModule("mcl")
                 .exports("cl").requires("java.base").build());
-        Files.copy(C_JAR, MC_JAR, StandardCopyOption.REPLACE_EXISTING);
+        Files.copy(C_JAR, MC_JAR,
+                StandardCopyOption.REPLACE_EXISTING);
         updateModuleDescr(MC_JAR, ModuleDescriptor.newModule("mc")
                 .exports("c").requires("java.base").requires("mcl").build());
-        Files.copy(C_JAR, AMC_JAR, StandardCopyOption.REPLACE_EXISTING);
+        Files.copy(C_JAR, AMC_JAR,
+                StandardCopyOption.REPLACE_EXISTING);
         updateModuleDescr(AMC_JAR, ModuleDescriptor.newModule("mc")
                 .exports("c").requires("java.base").requires("cl").build());
     }