< prev index next >

test/java/lang/ClassLoader/securityManager/ClassLoaderTest.java

Print this page




  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24  /*
  25  * @test
  26  * @bug 8168423
  27  * @summary Different types of ClassLoader running with(out) SecurityManager and
  28  *          (in)valid security policy file.
  29  * @library /lib/testlibrary
  30  * @modules java.base/jdk.internal.module
  31  * @build JarUtils CompilerUtils
  32  * @run main/timeout=240 ClassLoaderTest






  33  */
  34 import java.io.File;
  35 import java.io.OutputStream;
  36 import java.nio.file.Files;
  37 import java.nio.file.Path;
  38 import java.nio.file.Paths;
  39 import java.nio.file.StandardCopyOption;
  40 import java.util.Map;
  41 import java.util.HashMap;
  42 import java.lang.module.ModuleDescriptor;



  43 import jdk.internal.module.ModuleInfoWriter;
  44 import jdk.testlibrary.ProcessTools;
  45 
  46 public class ClassLoaderTest {
  47 
  48     private static final String SRC = System.getProperty("test.src");
  49     private static final Path CL_SRC = Paths.get(SRC, "TestClassLoader.java");
  50     private static final Path C_SRC = Paths.get(SRC, "TestClient.java");
  51     private static final Path CL_BIN = Paths.get("classes", "clbin");
  52     private static final Path C_BIN = Paths.get("classes", "cbin");
  53     private static final Path ARTIFACT_DIR = Paths.get("jars");
  54     private static final Path VALID_POLICY = Paths.get(SRC, "valid.policy");
  55     private static final Path INVALID_POLICY
  56             = Paths.get(SRC, "malformed.policy");
  57     private static final Path NO_POLICY = null;
  58     private static final String LOCALE = "-Duser.language=en -Duser.region=US";
  59     /*
  60      * Here is the naming convention followed for each jar.
  61      * cl.jar   - Regular custom class loader jar.
  62      * mcl.jar  - Modular custom class loader jar.
  63      * c.jar    - Regular client jar.
  64      * mc.jar   - Modular client jar.
  65      * amc.jar  - Modular client referring automated custom class loader jar.
  66      */
  67     private static final Path CL_JAR = ARTIFACT_DIR.resolve("cl.jar");
  68     private static final Path MCL_JAR = ARTIFACT_DIR.resolve("mcl.jar");
  69     private static final Path C_JAR = ARTIFACT_DIR.resolve("c.jar");
  70     private static final Path MC_JAR = ARTIFACT_DIR.resolve("mc.jar");
  71     private static final Path AMC_JAR = ARTIFACT_DIR.resolve("amc.jar");
  72     private static final Map<String, String> MSG_MAP = new HashMap<>();
  73 
  74     static {
  75         // This mapping help process finding expected message based
  76         // on the key passed as argument while executing java command.
  77         MSG_MAP.put("MissingModule", "Module cl not found, required by mc");
  78         MSG_MAP.put("ErrorPolicy", "java.security.policy: error parsing file");
  79         MSG_MAP.put(
  80                 "SystemCL", "jdk.internal.loader.ClassLoaders$AppClassLoader");
  81         MSG_MAP.put("CustomCL", "cl.TestClassLoader");



















































  82     }
  83 
  84     public static void main(String[] args) throws Exception {



















  85 
  86         // Generates regular and modular jars before start processing it.
  87         setUp();
  88         processForEachPolicyFile();
  89     }
  90 
  91     /**
  92      * Test cases are based on the following logic,
  93      *  for (policyFile : {"NO_POLICY", "VALID", "MALFORMED"}) {
  94      *      for (classLoader : {"SystemClassLoader", "CustomClassLoader"}){
  95      *          for (clientModule : {"NAMED", "AUTOMATIC", "UNNAMED"}) {
  96      *              for (classLoaderModule : {"NAMED", "AUTOMATIC", "UNNAMED"}) {
  97      *                  Create and run java command for each possible Test case
  98      *              }
  99      *          }
 100      *      }
 101      *  }
 102      */
 103     private static void processForEachPolicyFile() throws Exception {
 104 
 105         final String regCLloc = CL_JAR.toFile().getAbsolutePath();
 106         final String modCLloc = MCL_JAR.toFile().getAbsolutePath();
 107         final String regCloc = C_JAR.toFile().getAbsolutePath();
 108         final String modCloc = MC_JAR.toFile().getAbsolutePath();
 109         final String autoModCloc = AMC_JAR.toFile().getAbsolutePath();
 110         final String separator = File.pathSeparator;
 111 
 112         for (Path policy
 113                 : new Path[]{NO_POLICY, VALID_POLICY, INVALID_POLICY}) {
 114             final String policyFile = (policy != null)
 115                     ? policy.toFile().getAbsolutePath() : null;
 116             final boolean malformedPolicy
 117                     = (policy == null) ? false : policy.equals(INVALID_POLICY);
 118 
 119             for (boolean useSCL : new boolean[]{true, false}) {
 120                 final String clVmArg = (useSCL) ? ""
 121                         : "-Djava.system.class.loader=cl.TestClassLoader";
 122                 final String autoAddModArg
 123                         = (useSCL) ? "" : "--add-modules=cl";
 124                 final String addmodArg = (useSCL) ? "" : "--add-modules=mcl";
 125                 final String sMArg = (policy != null) ? String.format(
 126                         "-Djava.security.manager -Djava.security.policy=%s",
 127                         policyFile) : "";
 128                 final String smMsg = (policy != null) ? "With SecurityManager"
 129                         : "Without SecurityManager";
 130                 final String expectedResult = ((!malformedPolicy)
 131                         ? ((useSCL) ? "PASS SystemCL" : "PASS CustomCL")
 132                         : "FAIL ErrorPolicy");
 133 
 134                 // NAMED-NAMED, NAMED-AUTOMATIC, NAMED-UNNAMED
 135                 System.out.printf("Case:- Modular Client and %s %s%n",
 136                         ((useSCL) ? "SystemClassLoader"
 137                                 : "Modular CustomClassLoader"), smMsg);
 138                 execute(new String[]{String.format(
 139                     "--module-path %s%s%s %s %s %s -m mc/c.TestClient",
 140                     modCloc, separator, modCLloc, LOCALE, clVmArg, sMArg),
 141                     expectedResult});
 142                 System.out.printf("Case:- Modular Client and %s %s%n", ((useSCL)
 143                         ? "SystemClassLoader"
 144                         : "Automatic modular CustomClassLoader"), smMsg);
 145                 execute(new String[]{String.format(
 146                     "--module-path %s%s%s %s %s %s -m mc/c.TestClient",
 147                     autoModCloc, separator, regCLloc, LOCALE, clVmArg, sMArg),
 148                     expectedResult});
 149                 System.out.printf("Case:- Modular Client and %s %s%n", ((useSCL)
 150                         ? "SystemClassLoader"
 151                         : "Unknown modular CustomClassLoader"), smMsg);
 152                 execute(new String[]{String.format(
 153                     "--module-path %s -cp %s %s %s %s -m mc/c.TestClient",
 154                     autoModCloc, regCLloc, LOCALE, clVmArg, sMArg),
 155                     "FAIL MissingModule"});
 156 
 157                 // AUTOMATIC-NAMED, AUTOMATIC-AUTOMATIC, AUTOMATIC-UNNAMED
 158                 System.out.printf("Case:- Automated modular Client and %s %s%n",
 159                         ((useSCL) ? "SystemClassLoader"
 160                                 : "Modular CustomClassLoader"), smMsg);
 161                 execute(new String[]{String.format(
 162                     "--module-path %s%s%s %s %s %s %s -m c/c.TestClient",
 163                     regCloc, separator, modCLloc, addmodArg, LOCALE, clVmArg,
 164                     sMArg), expectedResult});
 165                 System.out.printf("Case:- Automated modular Client and %s %s%n",
 166                         ((useSCL) ? "SystemClassLoader"
 167                                 : "Automatic modular CustomClassLoader"),
 168                         smMsg);
 169                 execute(new String[]{String.format(
 170                     "--module-path %s%s%s %s %s %s %s -m c/c.TestClient",
 171                     regCloc, separator, regCLloc, autoAddModArg, LOCALE,
 172                     clVmArg, sMArg), expectedResult});
 173                 System.out.printf("Case:- Automated modular Client and %s %s%n",
 174                         ((useSCL) ? "SystemClassLoader"
 175                                 : "Unknown modular CustomClassLoader"), smMsg);
 176                 execute(new String[]{String.format(
 177                     "--module-path %s -cp %s %s %s %s -m c/c.TestClient",
 178                     regCloc, regCLloc, LOCALE, clVmArg, sMArg),
 179                     expectedResult});
 180 
 181                 // UNNAMED-NAMED, UNNAMED-AUTOMATIC, UNNAMED-UNNAMED
 182                 System.out.printf("Case:- Unknown modular Client and %s %s%n",
 183                         ((useSCL) ? "SystemClassLoader"
 184                                 : "Modular CustomClassLoader"), smMsg);
 185                 execute(new String[]{String.format(
 186                     "-cp %s --module-path %s %s %s %s %s c.TestClient",
 187                     regCloc, modCLloc, addmodArg, LOCALE, clVmArg, sMArg),
 188                     expectedResult});
 189                 System.out.printf("Case:- Unknown modular Client and %s %s%n",
 190                         ((useSCL) ? "SystemClassLoader"
 191                                 : "Automatic modular CustomClassLoader"),
 192                         smMsg);
 193                 execute(new String[]{String.format(
 194                     "-cp %s --module-path %s %s %s %s %s c.TestClient",
 195                     regCloc, regCLloc, autoAddModArg, LOCALE, clVmArg, sMArg),
 196                     expectedResult});
 197                 System.out.printf("Case:- Unknown modular Client and %s %s%n",
 198                         ((useSCL) ? "SystemClassLoader"
 199                                 : "Unknown modular CustomClassLoader"), smMsg);
 200                 execute(new String[]{String.format(
 201                     "-cp %s%s%s %s %s %s c.TestClient", regCloc, separator,
 202                     regCLloc, LOCALE, clVmArg, sMArg), expectedResult});
 203 
 204                 // Regular jars in module-path and Modular jars in class-path.
 205                 System.out.printf("Case:- Regular Client and %s "
 206                         + "inside --module-path %s.%n", ((useSCL)
 207                                 ? "SystemClassLoader"
 208                                 : "Unknown modular CustomClassLoader"), smMsg);
 209                 execute(new String[]{String.format(
 210                     "--module-path %s%s%s %s %s %s %s -m c/c.TestClient",
 211                     regCloc, separator, regCLloc, autoAddModArg, LOCALE,
 212                     clVmArg, sMArg), expectedResult});
 213                 System.out.printf("Case:- Modular Client and %s in -cp %s%n",

 214                         ((useSCL) ? "SystemClassLoader"
 215                                 : "Modular CustomClassLoader"), smMsg);
 216                 execute(new String[]{String.format(
 217                     "-cp %s%s%s %s %s %s c.TestClient", modCloc, separator,
 218                     modCLloc, LOCALE, clVmArg, sMArg), expectedResult});
 219             }
 220         }



 221     }
 222 
 223     /**
 224      * Execute with command arguments and process the result.
 225      */
 226     private static void execute(String[] args) throws Exception {
 227 
 228         String status = null;
 229         String msgKey = null;
 230         if ((args != null && args.length > 1)) {
 231             String[] secArgs = args[1].split("\\s+");
 232             status = (secArgs.length > 0) ? secArgs[0] : null;
 233             msgKey = (secArgs.length > 1) ? secArgs[1] : null;
 234         }
 235         String out = ProcessTools.executeTestJvm(args[0].split("\\s+"))
 236                 .getOutput();
 237         // Handle response.
 238         if ((status != null && "PASS".equals(status) && msgKey != null
 239                 && out.contains(MSG_MAP.get(msgKey)))) {
 240             System.out.printf("PASS: Expected Result: %s.%n",
 241                     MSG_MAP.get(msgKey));
 242         } else if ((status != null && "FAIL".equals(status) && msgKey != null
 243                 && out.contains(MSG_MAP.get(msgKey)))) {
 244             System.out.printf("PASS: Expected Failure: %s.%n",
 245                     MSG_MAP.get(msgKey));
 246         } else if (out.contains("Exception") || out.contains("Error")) {
 247             System.out.printf("OUTPUT: %s", out);
 248             throw new RuntimeException("FAIL: Unknown Exception.");
 249         } else {
 250             System.out.printf("OUTPUT: %s", out);
 251             throw new RuntimeException("FAIL: Unknown Test case found");
 252         }
 253     }
 254 
 255     /**
 256      * Creates regular/modular jar files for TestClient and TestClassLoader.
 257      */
 258     private static void setUp() throws Exception {
 259 
 260         boolean compiled = CompilerUtils.compile(CL_SRC, CL_BIN);
 261         compiled &= CompilerUtils.compile(C_SRC, C_BIN);
 262         if (!compiled) {
 263             throw new RuntimeException("Test Setup failed.");
 264         }
 265         // Generate regular jar files for TestClient and TestClassLoader
 266         JarUtils.createJarFile(CL_JAR, CL_BIN);
 267         JarUtils.createJarFile(C_JAR, C_BIN);


 268         // Generate modular jar files for TestClient and TestClassLoader with
 269         // their corresponding ModuleDescriptor.
 270         Files.copy(CL_JAR, MCL_JAR, StandardCopyOption.REPLACE_EXISTING);

 271         updateModuleDescr(MCL_JAR, ModuleDescriptor.newModule("mcl")
 272                 .exports("cl").requires("java.base").build());
 273         Files.copy(C_JAR, MC_JAR, StandardCopyOption.REPLACE_EXISTING);

 274         updateModuleDescr(MC_JAR, ModuleDescriptor.newModule("mc")
 275                 .exports("c").requires("java.base").requires("mcl").build());
 276         Files.copy(C_JAR, AMC_JAR, StandardCopyOption.REPLACE_EXISTING);

 277         updateModuleDescr(AMC_JAR, ModuleDescriptor.newModule("mc")
 278                 .exports("c").requires("java.base").requires("cl").build());
 279     }
 280 
 281     /**
 282      * Update regular jars and include module-info.class inside it to make
 283      * modular jars.
 284      */
 285     private static void updateModuleDescr(Path jar, ModuleDescriptor mDescr)
 286             throws Exception {
 287         if (mDescr != null) {
 288             Path dir = Files.createTempDirectory("tmp");
 289             Path mi = dir.resolve("module-info.class");
 290             try (OutputStream out = Files.newOutputStream(mi)) {
 291                 ModuleInfoWriter.write(mDescr, out);
 292             }
 293             System.out.format("Adding 'module-info.class' to jar '%s'%n", jar);
 294             JarUtils.updateJarFile(jar, dir);
 295         }
 296     }


  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24  /*
  25  * @test
  26  * @bug 8168423
  27  * @summary Different types of ClassLoader running with(out) SecurityManager and
  28  *          (in)valid security policy file.
  29  * @library /lib/testlibrary
  30  * @modules java.base/jdk.internal.module
  31  * @build JarUtils
  32  * @build TestClassLoader TestClient
  33  * @run main ClassLoaderTest -noPolicy
  34  * @run main ClassLoaderTest -validPolicy
  35  * @run main ClassLoaderTest -invalidPolicy
  36  * @run main ClassLoaderTest -noPolicy      -customSCL
  37  * @run main ClassLoaderTest -validPolicy   -customSCL
  38  * @run main ClassLoaderTest -invalidPolicy -customSCL
  39  */
  40 import java.io.File;
  41 import java.io.OutputStream;
  42 import java.nio.file.Files;
  43 import java.nio.file.Path;
  44 import java.nio.file.Paths;
  45 import java.nio.file.StandardCopyOption;
  46 import java.util.stream.Stream;

  47 import java.lang.module.ModuleDescriptor;
  48 import java.util.Collections;
  49 import java.util.LinkedList;
  50 import java.util.List;
  51 import jdk.internal.module.ModuleInfoWriter;
  52 import jdk.testlibrary.ProcessTools;
  53 
  54 public class ClassLoaderTest {
  55 
  56     private static final String SRC = System.getProperty("test.src");
  57     private static final Path TEST_CLASSES =
  58             Paths.get(System.getProperty("test.classes"));


  59     private static final Path ARTIFACT_DIR = Paths.get("jars");
  60     private static final Path VALID_POLICY = Paths.get(SRC, "valid.policy");
  61     private static final Path INVALID_POLICY
  62             = Paths.get(SRC, "malformed.policy");


  63     /*
  64      * Here is the naming convention followed for each jar.
  65      * cl.jar   - Regular custom class loader jar.
  66      * mcl.jar  - Modular custom class loader jar.
  67      * c.jar    - Regular client jar.
  68      * mc.jar   - Modular client jar.
  69      * amc.jar  - Modular client referring automated custom class loader jar.
  70      */
  71     private static final Path CL_JAR = ARTIFACT_DIR.resolve("cl.jar");
  72     private static final Path MCL_JAR = ARTIFACT_DIR.resolve("mcl.jar");
  73     private static final Path C_JAR = ARTIFACT_DIR.resolve("c.jar");
  74     private static final Path MC_JAR = ARTIFACT_DIR.resolve("mc.jar");
  75     private static final Path AMC_JAR = ARTIFACT_DIR.resolve("amc.jar");

  76 
  77     // Expected output messages
  78     private static final String MISSING_MODULE =
  79             "Module cl not found, required by mc";
  80     private static final String POLICY_ERROR =
  81             "java.security.policy: error parsing file";
  82     private static final String SYSTEM_CL_MSG =
  83             "jdk.internal.loader.ClassLoaders$AppClassLoader";
  84     private static final String CUSTOM_CL_MSG = "cl.TestClassLoader";
  85 
  86     // Member vars
  87     private final boolean useSCL;       // Use default system loader, or custom
  88     private final String smMsg;         // Security manager message, or ""
  89     private final String autoAddModArg; // Flag to add cl modules, or ""
  90     private final String addmodArg;     // Flag to add mcl modules, or ""
  91     private final String expectedStatus;// Expected exit status from client
  92     private final String expectedMsg;   // Expected output message from client
  93 
  94     // Common set of VM arguments used in all test cases
  95     private final List<String> COMMON_ARGS;
  96     
  97     public ClassLoaderTest(Path policy, boolean useSCL) {
  98         this.useSCL = useSCL;
  99 
 100         List<String> argList = new LinkedList<>();
 101         argList.add("-Duser.language=en");
 102         argList.add("-Duser.region=US");
 103         
 104         boolean malformedPolicy = false;
 105         if (policy == null) {
 106             smMsg = "Without SecurityManager";
 107         } else {
 108             malformedPolicy = policy.equals(INVALID_POLICY);
 109             argList.add("-Djava.security.manager");
 110             argList.add("-Djava.security.policy=" +
 111                     policy.toFile().getAbsolutePath());
 112             smMsg = "With SecurityManager";
 113         }
 114 
 115         if (useSCL) {
 116             autoAddModArg = "";
 117             addmodArg = "";
 118         } else {
 119             argList.add("-Djava.system.class.loader=cl.TestClassLoader");
 120             autoAddModArg = "--add-modules=cl";
 121             addmodArg = "--add-modules=mcl";
 122         }
 123 
 124         if (malformedPolicy) {
 125             expectedStatus = "FAIL";
 126             expectedMsg = POLICY_ERROR;
 127         } else if (useSCL) {
 128             expectedStatus = "PASS";
 129             expectedMsg = SYSTEM_CL_MSG;
 130         } else {
 131             expectedStatus = "PASS";
 132             expectedMsg = CUSTOM_CL_MSG;
 133         }
 134         
 135         COMMON_ARGS = Collections.unmodifiableList(argList);
 136     }
 137 
 138     public static void main(String[] args) throws Exception {
 139         Path policy;
 140         if (args[0].equals("-noPolicy")) {
 141             policy = null;
 142         } else if (args[0].equals("-validPolicy")) {
 143             policy = VALID_POLICY;
 144         } else if (args[0].equals("-invalidPolicy")) {
 145             policy = INVALID_POLICY;
 146         } else {
 147             throw new RuntimeException("Unknown policy arg: " + args[0]);
 148         }
 149 
 150         boolean useSystemLoader = true;
 151         if (args.length > 1) {
 152             if (args[1].equals("-customSCL")) {
 153                 useSystemLoader = false;
 154             } else {
 155                 throw new RuntimeException("Unknown custom loader arg: " + args[1]);
 156             }
 157         }
 158 
 159         ClassLoaderTest test = new ClassLoaderTest(policy, useSystemLoader);
 160         setUp();
 161         test.processForPolicyFile();
 162     }
 163 
 164     /**
 165      * Test cases are based on the following logic,
 166      *  given: a policyFile in {none, valid, malformed} and
 167      *         a classLoader in {SystemClassLoader, CustomClassLoader}:
 168      *  for (clientModule : {"NAMED", "UNNAMED"}) {
 169      *      for (classLoaderModule : {"NAMED", "UNNAMED"}) {
 170      *          Create and run java command for each possible Test case
 171      *      }
 172      *  }


 173      */
 174     private void processForPolicyFile() throws Exception {
 175         final String regLoaderLoc = CL_JAR.toFile().getAbsolutePath();
 176         final String modLoadrLoc = MCL_JAR.toFile().getAbsolutePath();
 177         final String regClientLoc = C_JAR.toFile().getAbsolutePath();
 178         final String modClientLoc = MC_JAR.toFile().getAbsolutePath();

 179         final String autoModCloc = AMC_JAR.toFile().getAbsolutePath();
 180         final String separator = File.pathSeparator;
 181 
 182         // NAMED-NAMED:
 183         System.out.println("Case:- Modular Client and " +






















 184                 ((useSCL) ? "SystemClassLoader"
 185                         : "Modular CustomClassLoader") + " " + smMsg);
 186         execute("--module-path", modClientLoc + separator + modLoadrLoc, "-m",
 187                 "mc/c.TestClient");
 188 
 189         // NAMED-UNNAMED:
 190         System.out.println("Case:- Modular Client and " + ((useSCL)







 191                 ? "SystemClassLoader"
 192                 : "Unknown modular CustomClassLoader") + " " + smMsg);
 193         execute(new String[] {"--module-path", autoModCloc, "-cp", regLoaderLoc,
 194                 "-m", "mc/c.TestClient"},
 195                 "FAIL", MISSING_MODULE);

 196 
 197         // UNNAMED-NAMED:
 198         System.out.println("Case:- Unknown modular Client and " +







 199                 ((useSCL) ? "SystemClassLoader"
 200                       : "Modular CustomClassLoader") + " " + smMsg);
 201         execute("-cp", regClientLoc, "--module-path", modLoadrLoc, addmodArg,
 202                 "c.TestClient");










 203 
 204         // UNNAMED-UNNAMED:
 205         System.out.println("Case:- Unknown modular Client and " +







 206                 ((useSCL) ? "SystemClassLoader"
 207                         : "Unknown modular CustomClassLoader") + " " + smMsg);
 208         execute("-cp", regClientLoc + separator + regLoaderLoc, "c.TestClient");
 209 
 210         // Regular jars in module-path
 211         System.out.println("Case:- Regular Client and " + ((useSCL)











 212                 ? "SystemClassLoader"
 213                 : "Unknown modular CustomClassLoader") +
 214                 " inside --module-path " + smMsg);
 215         execute("--module-path", regClientLoc + separator + regLoaderLoc,
 216                 autoAddModArg, "-m", "c/c.TestClient");
 217 
 218         // Modular jars in class-path
 219         System.out.println("Case:- Modular Client and " +
 220                 ((useSCL) ? "SystemClassLoader"
 221                         : "Modular CustomClassLoader") + " in -cp " + smMsg);
 222         execute("-cp", modClientLoc + separator + modLoadrLoc, "c.TestClient");



 223     }
 224 
 225     private void execute(String... args) throws Exception {
 226         execute(args, this.expectedStatus, this.expectedMsg);
 227     }
 228     
 229     /**
 230      * Execute with command arguments and process the result.
 231      */
 232     private void execute(String[] args, String status, String msg) throws Exception {
 233 
 234         // Combine with COMMON_ARGS, and perform sanity check
 235         String[] safeArgs = Stream.concat(COMMON_ARGS.stream(), Stream.of(args))
 236                 .filter(s -> {
 237                     if (s.contains(" ")) { throw new RuntimeException("No spaces in args");}
 238                     return !s.equals("");                
 239                 }).toArray(String[]::new);
 240         String out = ProcessTools.executeTestJvm(safeArgs).getOutput();


 241         // Handle response.
 242         if ("PASS".equals(status) && out.contains(msg)) {
 243             System.out.println("PASS: Expected Result: " + msg);
 244         } else if ("FAIL".equals(status) && out.contains(msg)) {
 245             System.out.printf("PASS: Expected Failure: " +  msg);




 246         } else if (out.contains("Exception") || out.contains("Error")) {
 247             System.out.printf("OUTPUT: %s", out);
 248             throw new RuntimeException("FAIL: Unknown Exception.");
 249         } else {
 250             System.out.printf("OUTPUT: %s", out);
 251             throw new RuntimeException("FAIL: Unknown Test case found");
 252         }
 253     }
 254     
 255     /**
 256      * Creates regular/modular jar files for TestClient and TestClassLoader.
 257      */
 258     private static void setUp() throws Exception {
 259 





 260         // Generate regular jar files for TestClient and TestClassLoader
 261         JarUtils.createJarFile(CL_JAR, TEST_CLASSES,
 262                                "cl/TestClassLoader.class");
 263         JarUtils.createJarFile(C_JAR, TEST_CLASSES,
 264                                "c/TestClient.class");
 265         // Generate modular jar files for TestClient and TestClassLoader with
 266         // their corresponding ModuleDescriptor.
 267         Files.copy(CL_JAR, MCL_JAR,
 268                 StandardCopyOption.REPLACE_EXISTING);
 269         updateModuleDescr(MCL_JAR, ModuleDescriptor.newModule("mcl")
 270                 .exports("cl").requires("java.base").build());
 271         Files.copy(C_JAR, MC_JAR,
 272                 StandardCopyOption.REPLACE_EXISTING);
 273         updateModuleDescr(MC_JAR, ModuleDescriptor.newModule("mc")
 274                 .exports("c").requires("java.base").requires("mcl").build());
 275         Files.copy(C_JAR, AMC_JAR,
 276                 StandardCopyOption.REPLACE_EXISTING);
 277         updateModuleDescr(AMC_JAR, ModuleDescriptor.newModule("mc")
 278                 .exports("c").requires("java.base").requires("cl").build());
 279     }
 280 
 281     /**
 282      * Update regular jars and include module-info.class inside it to make
 283      * modular jars.
 284      */
 285     private static void updateModuleDescr(Path jar, ModuleDescriptor mDescr)
 286             throws Exception {
 287         if (mDescr != null) {
 288             Path dir = Files.createTempDirectory("tmp");
 289             Path mi = dir.resolve("module-info.class");
 290             try (OutputStream out = Files.newOutputStream(mi)) {
 291                 ModuleInfoWriter.write(mDescr, out);
 292             }
 293             System.out.format("Adding 'module-info.class' to jar '%s'%n", jar);
 294             JarUtils.updateJarFile(jar, dir);
 295         }
 296     }
< prev index next >