< prev index next >

test/tools/jlink/plugins/SystemModuleDescriptors/UserModuleTest.java

Print this page




  21  * questions.
  22  */
  23 
  24 import java.io.File;
  25 import java.io.IOException;
  26 import java.lang.module.ModuleDescriptor;
  27 import java.lang.reflect.Layer;
  28 import java.nio.file.Files;
  29 import java.nio.file.Path;
  30 import java.nio.file.Paths;
  31 import java.util.Arrays;
  32 import java.util.Set;
  33 import java.util.spi.ToolProvider;
  34 import java.util.stream.Collectors;
  35 import java.util.stream.Stream;
  36 
  37 import jdk.testlibrary.FileUtils;
  38 
  39 import static jdk.testlibrary.ProcessTools.*;
  40 
  41 
  42 import org.testng.annotations.BeforeTest;
  43 import org.testng.annotations.Test;
  44 import static org.testng.Assert.*;
  45 
  46 /**
  47  * @test
  48  * @bug 8142968 8173381 8174740
  49  * @library /lib/testlibrary
  50  * @modules jdk.compiler jdk.jlink
  51  * @build UserModuleTest CompilerUtils jdk.testlibrary.FileUtils jdk.testlibrary.ProcessTools


  52  * @run testng UserModuleTest
  53  */
  54 
  55 public class UserModuleTest {
  56     private static final String JAVA_HOME = System.getProperty("java.home");
  57     private static final String TEST_SRC = System.getProperty("test.src");
  58 
  59     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  60     private static final Path MODS_DIR = Paths.get("mods");
  61     private static final Path JMODS_DIR = Paths.get("jmods");
  62 
  63     private static final Path IMAGE = Paths.get("image");
  64     private static final String MAIN_MID = "m1/p1.Main";
  65 
  66     // the names of the modules in this test
  67     private static String[] modules = new String[] {"m1", "m2", "m3", "m4", "m5"};
  68 
  69 
  70     private static boolean hasJmods() {
  71         if (!Files.exists(Paths.get(JAVA_HOME, "jmods"))) {
  72             System.err.println("Test skipped. NO jmods directory");
  73             return false;
  74         }
  75         return true;
  76     }
  77 
  78     /*
  79      * Compiles all modules used by the test
  80      */
  81     @BeforeTest
  82     public void compileAll() throws Throwable {
  83         if (!hasJmods()) return;
  84 
  85         for (String mn : modules) {
  86             Path msrc = SRC_DIR.resolve(mn);
  87             assertTrue(CompilerUtils.compile(msrc, MODS_DIR,
  88                 "--module-source-path", SRC_DIR.toString()));


  89         }
  90 
  91         if (Files.exists(IMAGE)) {
  92             FileUtils.deleteFileTreeUnchecked(IMAGE);
  93         }
  94 
  95         createImage(IMAGE, "m1", "m3");
  96 
  97         createJmods("m1", "m4");
  98     }
  99 
 100     /*
 101      * Test the image created when linking with a module with
 102      * no Packages attribute
 103      */
 104     @Test
 105     public void testPackagesAttribute() throws Throwable {
 106         if (!hasJmods()) return;
 107 
 108         Path java = IMAGE.resolve("bin").resolve("java");
 109         assertTrue(executeProcess(java.toString(), "-m", MAIN_MID)



 110                         .outputTo(System.out)
 111                         .errorTo(System.out)
 112                         .getExitValue() == 0);
 113     }
 114 
 115     /*
 116      * Test the image created when linking with an open module
 117     */
 118     @Test
 119     public void testOpenModule() throws Throwable {
 120         if (!hasJmods()) return;
 121 
 122         Path java = IMAGE.resolve("bin").resolve("java");
 123         assertTrue(executeProcess(java.toString(), "-m", "m3/p3.Main")
 124                         .outputTo(System.out)
 125                         .errorTo(System.out)
 126                         .getExitValue() == 0);
 127     }
 128 
 129     /*
 130      * Disable the fast loading of system modules.
 131      * Parsing module-info.class
 132      */
 133     @Test
 134     public void disableSystemModules() throws Throwable {
 135         if (!hasJmods()) return;
 136 
 137         Path java = IMAGE.resolve("bin").resolve("java");
 138         assertTrue(executeProcess(java.toString(),


 139                                   "-Djdk.system.module.finder.disabledFastPath",
 140                                   "-m", MAIN_MID)
 141                         .outputTo(System.out)
 142                         .errorTo(System.out)
 143                         .getExitValue() == 0);
 144     }
 145 
 146     /*
 147      * Test the optimization that deduplicates Set<String> on targets of exports,
 148      * uses, provides.
 149      */
 150     @Test
 151     public void testDedupSet() throws Throwable {
 152         if (!hasJmods()) return;
 153 
 154         Path dir = Paths.get("dedupSetTest");
 155         createImage(dir, "m1", "m2", "m3", "m4");
 156         Path java = dir.resolve("bin").resolve("java");
 157         assertTrue(executeProcess(java.toString(), "-m", MAIN_MID)



 158                         .outputTo(System.out)
 159                         .errorTo(System.out)
 160                         .getExitValue() == 0);
 161     }
 162 
 163     @Test
 164     public void testRequiresStatic() throws Throwable {
 165         if (!hasJmods()) return;
 166 
 167         Path dir = Paths.get("requiresStatic");
 168         createImage(dir, "m5");
 169         Path java = dir.resolve("bin").resolve("java");
 170         assertTrue(executeProcess(java.toString(), "-m", "m5/p5.Main")
 171                         .outputTo(System.out)
 172                         .errorTo(System.out)
 173                         .getExitValue() == 0);
 174 
 175         // run with m3 present
 176         assertTrue(executeProcess(java.toString(),
 177                                   "--module-path", MODS_DIR.toString(),


 188 
 189         Path dir = Paths.get("requiresStatic2");
 190         createImage(dir, "m3", "m5");
 191 
 192         Path java = dir.resolve("bin").resolve("java");
 193         assertTrue(executeProcess(java.toString(), "-m", "m5/p5.Main")
 194                         .outputTo(System.out)
 195                         .errorTo(System.out)
 196                         .getExitValue() == 0);
 197 
 198         // boot layer with m3 and m5
 199         assertTrue(executeProcess(java.toString(),
 200                                   "--add-modules", "m3",
 201                                   "-m", "m5/p5.Main")
 202                         .outputTo(System.out)
 203                         .errorTo(System.out)
 204                         .getExitValue() == 0);
 205     }
 206 
 207     private void createJmods(String... modules) throws IOException {
 208         // use the same target platform as in java.base
 209         ModuleDescriptor md = Layer.boot().findModule("java.base").get()
 210                                    .getDescriptor();
 211         String osName = md.osName().get();
 212         String osArch = md.osArch().get();


 213 
 214         // create JMOD files
 215         Files.createDirectories(JMODS_DIR);
 216         Stream.of(modules).forEach(mn ->
 217             assertTrue(jmod("create",
 218                 "--class-path", MODS_DIR.resolve(mn).toString(),
 219                 "--os-name", osName,
 220                 "--os-arch", osArch,
 221                 "--main-class", mn.replace('m', 'p') + ".Main",
 222                 JMODS_DIR.resolve(mn + ".jmod").toString()) == 0)
 223         );
 224     }
 225 
 226 
 227     /**
 228      * Verify the module descriptor if package p4.dummy is excluded at link time.
 229      */
 230     @Test
 231     public void testModulePackagesAttribute() throws Throwable {
 232         if (!hasJmods()) return;
 233 
 234         // create an image using JMOD files
 235         Path dir = Paths.get("packagesTest");
 236         String mp = Paths.get(JAVA_HOME, "jmods").toString() +
 237             File.pathSeparator + JMODS_DIR.toString();
 238 
 239         Set<String> modules = Set.of("m1", "m4");
 240         assertTrue(JLINK_TOOL.run(System.out, System.out,
 241             "--output", dir.toString(),
 242             "--exclude-resources", "m4/p4/dummy/*",
 243             "--add-modules", modules.stream().collect(Collectors.joining(",")),
 244             "--module-path", mp) == 0);
 245 
 246         // verify ModuleDescriptor
 247         Path java = dir.resolve("bin").resolve("java");
 248         assertTrue(executeProcess(java.toString(),


 249                         "--add-modules=m1", "-m", "m4")
 250             .outputTo(System.out)
 251             .errorTo(System.out)
 252             .getExitValue() == 0);
 253     }
 254 
 255     /**
 256      * Verify the plugin to retain ModuleTarget attribute
 257      */
 258     @Test
 259     public void testRetainModuleTarget() throws Throwable {
 260         if (!hasJmods()) return;
 261 
 262         // create an image using JMOD files
 263         Path dir = Paths.get("retainModuleTargetTest");
 264         String mp = Paths.get(JAVA_HOME, "jmods").toString() +
 265             File.pathSeparator + JMODS_DIR.toString();
 266 
 267         Set<String> modules = Set.of("m1", "m4");
 268         assertTrue(JLINK_TOOL.run(System.out, System.out,
 269             "--output", dir.toString(),
 270             "--system-modules", "retainModuleTarget",
 271             "--exclude-resources", "m4/p4/dummy/*",
 272             "--add-modules", modules.stream().collect(Collectors.joining(",")),
 273             "--module-path", mp) == 0);
 274 
 275         // verify ModuleDescriptor
 276         Path java = dir.resolve("bin").resolve("java");
 277         assertTrue(executeProcess(java.toString(),


 278                         "--add-modules=m1", "-m", "m4", "retainModuleTarget")
 279             .outputTo(System.out)
 280             .errorTo(System.out)
 281             .getExitValue() == 0);
 282     }
 283 
 284     static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")
 285         .orElseThrow(() ->
 286             new RuntimeException("jlink tool not found")
 287         );
 288 
 289     static final ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod")
 290         .orElseThrow(() ->
 291             new RuntimeException("jmod tool not found")
 292         );
 293 
 294     static final String MODULE_PATH = Paths.get(JAVA_HOME, "jmods").toString()
 295         + File.pathSeparator + MODS_DIR.toString();
 296 
 297     private void createImage(Path outputDir, String... modules) throws Throwable {


  21  * questions.
  22  */
  23 
  24 import java.io.File;
  25 import java.io.IOException;
  26 import java.lang.module.ModuleDescriptor;
  27 import java.lang.reflect.Layer;
  28 import java.nio.file.Files;
  29 import java.nio.file.Path;
  30 import java.nio.file.Paths;
  31 import java.util.Arrays;
  32 import java.util.Set;
  33 import java.util.spi.ToolProvider;
  34 import java.util.stream.Collectors;
  35 import java.util.stream.Stream;
  36 
  37 import jdk.testlibrary.FileUtils;
  38 
  39 import static jdk.testlibrary.ProcessTools.*;
  40 

  41 import org.testng.annotations.BeforeTest;
  42 import org.testng.annotations.Test;
  43 import static org.testng.Assert.*;
  44 
  45 /**
  46  * @test
  47  * @bug 8142968 8173381 8174740
  48  * @library /lib/testlibrary
  49  * @modules jdk.compiler jdk.jlink
  50  * @modules java.base/jdk.internal.module
  51  * @modules java.base/jdk.internal.org.objectweb.asm
  52  * @build ModuleTargetHelper UserModuleTest CompilerUtils jdk.testlibrary.FileUtils jdk.testlibrary.ProcessTools
  53  * @run testng UserModuleTest
  54  */
  55 
  56 public class UserModuleTest {
  57     private static final String JAVA_HOME = System.getProperty("java.home");
  58     private static final String TEST_SRC = System.getProperty("test.src");
  59 
  60     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  61     private static final Path MODS_DIR = Paths.get("mods");
  62     private static final Path JMODS_DIR = Paths.get("jmods");
  63 
  64     private static final Path IMAGE = Paths.get("image");
  65     private static final String MAIN_MID = "m1/p1.Main";
  66 
  67     // the names of the modules in this test
  68     private static String[] modules = new String[] {"m1", "m2", "m3", "m4", "m5"};
  69 
  70 
  71     private static boolean hasJmods() {
  72         if (!Files.exists(Paths.get(JAVA_HOME, "jmods"))) {
  73             System.err.println("Test skipped. NO jmods directory");
  74             return false;
  75         }
  76         return true;
  77     }
  78 
  79     /*
  80      * Compiles all modules used by the test
  81      */
  82     @BeforeTest
  83     public void compileAll() throws Throwable {
  84         if (!hasJmods()) return;
  85 
  86         for (String mn : modules) {
  87             Path msrc = SRC_DIR.resolve(mn);
  88             assertTrue(CompilerUtils.compile(msrc, MODS_DIR,
  89                 "--module-source-path", SRC_DIR.toString(),
  90                 "--add-exports", "java.base/jdk.internal.module=" + mn,
  91                 "--add-exports", "java.base/jdk.internal.org.objectweb.asm=" + mn));
  92         }
  93 
  94         if (Files.exists(IMAGE)) {
  95             FileUtils.deleteFileTreeUnchecked(IMAGE);
  96         }
  97 
  98         createImage(IMAGE, "m1", "m3");
  99 
 100         createJmods("m1", "m4");
 101     }
 102 
 103     /*
 104      * Test the image created when linking with a module with
 105      * no Packages attribute
 106      */
 107     @Test
 108     public void testPackagesAttribute() throws Throwable {
 109         if (!hasJmods()) return;
 110 
 111         Path java = IMAGE.resolve("bin").resolve("java");
 112         assertTrue(executeProcess(java.toString(),
 113                         "--add-exports", "java.base/jdk.internal.module=m1,m4",
 114                         "--add-exports", "java.base/jdk.internal.org.objectweb.asm=m1,m4",
 115                         "-m", MAIN_MID)
 116                         .outputTo(System.out)
 117                         .errorTo(System.out)
 118                         .getExitValue() == 0);
 119     }
 120 
 121     /*
 122      * Test the image created when linking with an open module
 123     */
 124     @Test
 125     public void testOpenModule() throws Throwable {
 126         if (!hasJmods()) return;
 127 
 128         Path java = IMAGE.resolve("bin").resolve("java");
 129         assertTrue(executeProcess(java.toString(), "-m", "m3/p3.Main")
 130                         .outputTo(System.out)
 131                         .errorTo(System.out)
 132                         .getExitValue() == 0);
 133     }
 134 
 135     /*
 136      * Disable the fast loading of system modules.
 137      * Parsing module-info.class
 138      */
 139     @Test
 140     public void disableSystemModules() throws Throwable {
 141         if (!hasJmods()) return;
 142 
 143         Path java = IMAGE.resolve("bin").resolve("java");
 144         assertTrue(executeProcess(java.toString(),
 145                                   "--add-exports", "java.base/jdk.internal.module=m1,m4",
 146                                   "--add-exports", "java.base/jdk.internal.org.objectweb.asm=m1,m4",
 147                                   "-Djdk.system.module.finder.disabledFastPath",
 148                                   "-m", MAIN_MID)
 149                         .outputTo(System.out)
 150                         .errorTo(System.out)
 151                         .getExitValue() == 0);
 152     }
 153 
 154     /*
 155      * Test the optimization that deduplicates Set<String> on targets of exports,
 156      * uses, provides.
 157      */
 158     @Test
 159     public void testDedupSet() throws Throwable {
 160         if (!hasJmods()) return;
 161 
 162         Path dir = Paths.get("dedupSetTest");
 163         createImage(dir, "m1", "m2", "m3", "m4");
 164         Path java = dir.resolve("bin").resolve("java");
 165         assertTrue(executeProcess(java.toString(),
 166                          "--add-exports", "java.base/jdk.internal.module=m1,m4",
 167                          "--add-exports", "java.base/jdk.internal.org.objectweb.asm=m1,m4",
 168                          "-m", MAIN_MID)
 169                         .outputTo(System.out)
 170                         .errorTo(System.out)
 171                         .getExitValue() == 0);
 172     }
 173 
 174     @Test
 175     public void testRequiresStatic() throws Throwable {
 176         if (!hasJmods()) return;
 177 
 178         Path dir = Paths.get("requiresStatic");
 179         createImage(dir, "m5");
 180         Path java = dir.resolve("bin").resolve("java");
 181         assertTrue(executeProcess(java.toString(), "-m", "m5/p5.Main")
 182                         .outputTo(System.out)
 183                         .errorTo(System.out)
 184                         .getExitValue() == 0);
 185 
 186         // run with m3 present
 187         assertTrue(executeProcess(java.toString(),
 188                                   "--module-path", MODS_DIR.toString(),


 199 
 200         Path dir = Paths.get("requiresStatic2");
 201         createImage(dir, "m3", "m5");
 202 
 203         Path java = dir.resolve("bin").resolve("java");
 204         assertTrue(executeProcess(java.toString(), "-m", "m5/p5.Main")
 205                         .outputTo(System.out)
 206                         .errorTo(System.out)
 207                         .getExitValue() == 0);
 208 
 209         // boot layer with m3 and m5
 210         assertTrue(executeProcess(java.toString(),
 211                                   "--add-modules", "m3",
 212                                   "-m", "m5/p5.Main")
 213                         .outputTo(System.out)
 214                         .errorTo(System.out)
 215                         .getExitValue() == 0);
 216     }
 217 
 218     private void createJmods(String... modules) throws IOException {
 219         ModuleTargetHelper.ModuleTarget mt = ModuleTargetHelper.getJavaBaseTarget();
 220         if (mt == null) {
 221             throw new RuntimeException("ModuleTarget is missing for java.base");
 222         }
 223 
 224         String osName = mt.osName();
 225         String osArch = mt.osArch();
 226 
 227         // create JMOD files
 228         Files.createDirectories(JMODS_DIR);
 229         Stream.of(modules).forEach(mn ->
 230             assertTrue(jmod("create",
 231                 "--class-path", MODS_DIR.resolve(mn).toString(),
 232                 "--os-name", osName,
 233                 "--os-arch", osArch,
 234                 "--main-class", mn.replace('m', 'p') + ".Main",
 235                 JMODS_DIR.resolve(mn + ".jmod").toString()) == 0)
 236         );
 237     }
 238 
 239 
 240     /**
 241      * Verify the module descriptor if package p4.dummy is excluded at link time.
 242      */
 243     @Test
 244     public void testModulePackagesAttribute() throws Throwable {
 245         if (!hasJmods()) return;
 246 
 247         // create an image using JMOD files
 248         Path dir = Paths.get("packagesTest");
 249         String mp = Paths.get(JAVA_HOME, "jmods").toString() +
 250             File.pathSeparator + JMODS_DIR.toString();
 251 
 252         Set<String> modules = Set.of("m1", "m4");
 253         assertTrue(JLINK_TOOL.run(System.out, System.out,
 254             "--output", dir.toString(),
 255             "--exclude-resources", "m4/p4/dummy/*",
 256             "--add-modules", modules.stream().collect(Collectors.joining(",")),
 257             "--module-path", mp) == 0);
 258 
 259         // verify ModuleDescriptor
 260         Path java = dir.resolve("bin").resolve("java");
 261         assertTrue(executeProcess(java.toString(),
 262                         "--add-exports", "java.base/jdk.internal.module=m1,m4",
 263                         "--add-exports", "java.base/jdk.internal.org.objectweb.asm=m1,m4",
 264                         "--add-modules=m1", "-m", "m4")
 265             .outputTo(System.out)
 266             .errorTo(System.out)
 267             .getExitValue() == 0);
 268     }
 269 
 270     /**
 271      * Verify the plugin to retain ModuleTarget attribute
 272      */
 273     @Test
 274     public void testRetainModuleTarget() throws Throwable {
 275         if (!hasJmods()) return;
 276 
 277         // create an image using JMOD files
 278         Path dir = Paths.get("retainModuleTargetTest");
 279         String mp = Paths.get(JAVA_HOME, "jmods").toString() +
 280             File.pathSeparator + JMODS_DIR.toString();
 281 
 282         Set<String> modules = Set.of("m1", "m4");
 283         assertTrue(JLINK_TOOL.run(System.out, System.out,
 284             "--output", dir.toString(),
 285             "--system-modules", "retainModuleTarget",
 286             "--exclude-resources", "m4/p4/dummy/*",
 287             "--add-modules", modules.stream().collect(Collectors.joining(",")),
 288             "--module-path", mp) == 0);
 289 
 290         // verify ModuleDescriptor
 291         Path java = dir.resolve("bin").resolve("java");
 292         assertTrue(executeProcess(java.toString(),
 293                         "--add-exports", "java.base/jdk.internal.module=m1,m4",
 294                         "--add-exports", "java.base/jdk.internal.org.objectweb.asm=m1,m4",
 295                         "--add-modules=m1", "-m", "m4", "retainModuleTarget")
 296             .outputTo(System.out)
 297             .errorTo(System.out)
 298             .getExitValue() == 0);
 299     }
 300 
 301     static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")
 302         .orElseThrow(() ->
 303             new RuntimeException("jlink tool not found")
 304         );
 305 
 306     static final ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod")
 307         .orElseThrow(() ->
 308             new RuntimeException("jmod tool not found")
 309         );
 310 
 311     static final String MODULE_PATH = Paths.get(JAVA_HOME, "jmods").toString()
 312         + File.pathSeparator + MODS_DIR.toString();
 313 
 314     private void createImage(Path outputDir, String... modules) throws Throwable {
< prev index next >