test/jdk/tools/jar/modularJar/Basic.java
changeset 54413 30067047ed88
parent 52723 f24ae8376e71
equal deleted inserted replaced
54412:41356f083e93 54413:30067047ed88
     1 /*
     1 /*
     2  * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    22  */
    22  */
    23 
    23 
    24 import java.io.*;
    24 import java.io.*;
    25 import java.lang.module.ModuleDescriptor;
    25 import java.lang.module.ModuleDescriptor;
    26 import java.lang.reflect.Method;
    26 import java.lang.reflect.Method;
    27 import java.nio.file.*;
    27 import java.nio.file.Files;
       
    28 import java.nio.file.Path;
       
    29 import java.nio.file.Paths;
    28 import java.util.*;
    30 import java.util.*;
    29 import java.util.function.Consumer;
    31 import java.util.function.Consumer;
    30 import java.util.jar.JarEntry;
    32 import java.util.jar.JarEntry;
    31 import java.util.jar.JarFile;
    33 import java.util.jar.JarFile;
    32 import java.util.jar.JarInputStream;
    34 import java.util.jar.JarInputStream;
    33 import java.util.jar.Manifest;
    35 import java.util.jar.Manifest;
    34 import java.util.regex.Pattern;
    36 import java.util.regex.Pattern;
       
    37 import java.util.spi.ToolProvider;
    35 import java.util.stream.Collectors;
    38 import java.util.stream.Collectors;
    36 import java.util.stream.Stream;
    39 import java.util.stream.Stream;
    37 
    40 
    38 import jdk.test.lib.util.FileUtils;
    41 import jdk.test.lib.util.FileUtils;
    39 import jdk.test.lib.JDKToolFinder;
    42 import jdk.test.lib.JDKToolFinder;
    57  * @run testng Basic
    60  * @run testng Basic
    58  * @summary Tests for plain Modular jars & Multi-Release Modular jars
    61  * @summary Tests for plain Modular jars & Multi-Release Modular jars
    59  */
    62  */
    60 
    63 
    61 public class Basic {
    64 public class Basic {
       
    65 
       
    66     private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
       
    67             .orElseThrow(()
       
    68                     -> new RuntimeException("jar tool not found")
       
    69             );
       
    70     private static final ToolProvider JAVAC_TOOL = ToolProvider.findFirst("javac")
       
    71             .orElseThrow(()
       
    72                     -> new RuntimeException("javac tool not found")
       
    73             );
       
    74 
    62     static final Path TEST_SRC = Paths.get(System.getProperty("test.src", "."));
    75     static final Path TEST_SRC = Paths.get(System.getProperty("test.src", "."));
    63     static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", "."));
    76     static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", "."));
    64     static final Path MODULE_CLASSES = TEST_CLASSES.resolve("build");
    77     static final Path MODULE_CLASSES = TEST_CLASSES.resolve("build");
    65     static final Path MRJAR_DIR = MODULE_CLASSES.resolve("mrjar");
    78     static final Path MRJAR_DIR = MODULE_CLASSES.resolve("mrjar");
    66 
    79 
   975         if (!TOOL_VM_OPTIONS.isEmpty()) {
   988         if (!TOOL_VM_OPTIONS.isEmpty()) {
   976             commands.addAll(Arrays.asList(TOOL_VM_OPTIONS.split("\\s+", -1)));
   989             commands.addAll(Arrays.asList(TOOL_VM_OPTIONS.split("\\s+", -1)));
   977         }
   990         }
   978         Stream.of(args).forEach(commands::add);
   991         Stream.of(args).forEach(commands::add);
   979         ProcessBuilder p = new ProcessBuilder(commands);
   992         ProcessBuilder p = new ProcessBuilder(commands);
   980         if (stdinSource != null)
   993         if (stdinSource != null) {
   981             p.redirectInput(stdinSource);
   994             p.redirectInput(stdinSource);
       
   995         }
   982         return run(p);
   996         return run(p);
   983     }
   997     }
   984 
   998 
   985     static Result jar(String... args) {
   999     static Result jar(String... args) {
   986         return jarWithStdin(null, args);
  1000         return run(JAR_TOOL, args);
   987     }
  1001     }
   988 
  1002 
   989     static Path compileModule(String mn) throws IOException {
  1003     static Path compileModule(String mn) throws IOException {
   990         return compileModule(mn, null);
  1004         return compileModule(mn, null);
   991     }
  1005     }
  1069     }
  1083     }
  1070 
  1084 
  1071     static void javac(Path dest, Path modulePath, Path... sourceFiles)
  1085     static void javac(Path dest, Path modulePath, Path... sourceFiles)
  1072         throws IOException
  1086         throws IOException
  1073     {
  1087     {
  1074         String javac = getJDKTool("javac");
       
  1075 
  1088 
  1076         List<String> commands = new ArrayList<>();
  1089         List<String> commands = new ArrayList<>();
  1077         commands.add(javac);
       
  1078         if (!TOOL_VM_OPTIONS.isEmpty()) {
  1090         if (!TOOL_VM_OPTIONS.isEmpty()) {
  1079             commands.addAll(Arrays.asList(TOOL_VM_OPTIONS.split("\\s+", -1)));
  1091             commands.addAll(Arrays.asList(TOOL_VM_OPTIONS.split("\\s+", -1)));
  1080         }
  1092         }
  1081         commands.add("-d");
  1093         commands.add("-d");
  1082         commands.add(dest.toString());
  1094         commands.add(dest.toString());
  1090             commands.add("--module-path");
  1102             commands.add("--module-path");
  1091             commands.add(modulePath.toString());
  1103             commands.add(modulePath.toString());
  1092         }
  1104         }
  1093         Stream.of(sourceFiles).map(Object::toString).forEach(x -> commands.add(x));
  1105         Stream.of(sourceFiles).map(Object::toString).forEach(x -> commands.add(x));
  1094 
  1106 
  1095         quickFail(run(new ProcessBuilder(commands)));
  1107         StringWriter sw = new StringWriter();
       
  1108         try (PrintWriter pw = new PrintWriter(sw)) {
       
  1109             int rc = JAVAC_TOOL.run(pw, pw, commands.toArray(new String[0]));
       
  1110             if(rc != 0) {
       
  1111                 throw new RuntimeException(sw.toString());
       
  1112             }
       
  1113         }
  1096     }
  1114     }
  1097 
  1115 
  1098     static Result java(Path modulePath, String entryPoint, String... args) {
  1116     static Result java(Path modulePath, String entryPoint, String... args) {
  1099         String java = getJDKTool("java");
  1117         String java = getJDKTool("java");
  1100 
  1118 
  1136                 return true;
  1154                 return true;
  1137         }
  1155         }
  1138         return false;
  1156         return false;
  1139     }
  1157     }
  1140 
  1158 
  1141     static void quickFail(Result r) {
  1159     static Result run(ToolProvider tp, String[] commands) {
  1142         if (r.ec != 0)
  1160         int rc = 0;
  1143             throw new RuntimeException(r.output);
  1161         StringWriter sw = new StringWriter();
       
  1162         try (PrintWriter pw = new PrintWriter(sw)) {
       
  1163             rc = tp.run(pw, pw, commands);
       
  1164         }
       
  1165         return new Result(rc, sw.toString());
  1144     }
  1166     }
  1145 
  1167 
  1146     static Result run(ProcessBuilder pb) {
  1168     static Result run(ProcessBuilder pb) {
  1147         Process p;
  1169         Process p;
  1148         out.printf("Running: %s%n", pb.command());
  1170         out.printf("Running: %s%n", pb.command());