1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  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 import java.io.File;
  25 import java.nio.file.Files;
  26 import java.nio.file.Path;
  27 import java.nio.file.Paths;
  28 import java.util.Arrays;
  29 import java.util.stream.Collectors;
  30 
  31 import jdk.testlibrary.FileUtils;
  32 import static jdk.testlibrary.ProcessTools.*;
  33 
  34 
  35 import org.testng.annotations.BeforeTest;
  36 import org.testng.annotations.Test;
  37 import static org.testng.Assert.*;
  38 
  39 /**
  40  * @test
  41  * @library /lib/testlibrary
  42  * @modules jdk.compiler jdk.jlink
  43  * @build UserModuleTest CompilerUtils jdk.testlibrary.FileUtils jdk.testlibrary.ProcessTools
  44  * @run testng UserModuleTest
  45  */
  46 
  47 public class UserModuleTest {
  48     private static final String JAVA_HOME = System.getProperty("java.home");
  49     private static final String TEST_SRC = System.getProperty("test.src");
  50 
  51     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  52     private static final Path MODS_DIR = Paths.get("mods");
  53     private static final Path IMAGE = Paths.get("image");
  54     private static final Path JMODS = Paths.get(JAVA_HOME, "jmods");
  55     private static final String MAIN_MID = "m1/p1.Main";
  56 
  57     // the names of the modules in this test
  58     private static String[] modules = new String[] {"m1", "m2", "m3", "m4"};
  59 
  60 
  61     private static boolean hasJmods() {
  62         if (!Files.exists(JMODS)) {
  63             System.err.println("Test skipped. NO jmods directory");
  64             return false;
  65         }
  66         return true;
  67     }
  68 
  69     /*
  70      * Compiles all modules used by the test
  71      */
  72     @BeforeTest
  73     public void compileAll() throws Throwable {
  74         if (!hasJmods()) return;
  75 
  76         for (String mn : modules) {
  77             Path msrc = SRC_DIR.resolve(mn);
  78             assertTrue(CompilerUtils.compile(msrc, MODS_DIR, "--module-source-path", SRC_DIR.toString()));
  79         }
  80 
  81         if (Files.exists(IMAGE)) {
  82             FileUtils.deleteFileTreeUnchecked(IMAGE);
  83         }
  84 
  85         createImage(IMAGE, "java.base", "m1", "m3");
  86     }
  87 
  88     private void createImage(Path outputDir, String... modules) throws Throwable {
  89         Path jlink = Paths.get(JAVA_HOME, "bin", "jlink");
  90         String mp = JMODS.toString() + File.pathSeparator + MODS_DIR.toString();
  91         assertTrue(executeProcess(jlink.toString(), "--output", outputDir.toString(),
  92                         "--add-modules", Arrays.stream(modules).collect(Collectors.joining(",")),
  93                         "--module-path", mp)
  94                         .outputTo(System.out)
  95                         .errorTo(System.out)
  96                         .getExitValue() == 0);
  97     }
  98 
  99     /*
 100      * Test the image created when linking with a module with
 101      * no Packages attribute
 102      */
 103     @Test
 104     public void testPackagesAttribute() throws Throwable {
 105         if (!hasJmods()) return;
 106 
 107         Path java = IMAGE.resolve("bin").resolve("java");
 108         assertTrue(executeProcess(java.toString(), "-m", MAIN_MID)
 109                         .outputTo(System.out)
 110                         .errorTo(System.out)
 111                         .getExitValue() == 0);
 112     }
 113 
 114     /*
 115      * Test the image created when linking with an open module
 116     */
 117     @Test
 118     public void testOpenModule() throws Throwable {
 119         if (!hasJmods()) return;
 120 
 121         Path java = IMAGE.resolve("bin").resolve("java");
 122         assertTrue(executeProcess(java.toString(), "-m", "m3/p3.Main")
 123             .outputTo(System.out)
 124             .errorTo(System.out)
 125             .getExitValue() == 0);
 126     }
 127 
 128     /*
 129      * Disable the fast loading of system modules.
 130      * Parsing module-info.class
 131      */
 132     @Test
 133     public void disableSystemModules() throws Throwable {
 134         if (!hasJmods()) return;
 135 
 136         Path java = IMAGE.resolve("bin").resolve("java");
 137         assertTrue(executeProcess(java.toString(),
 138                                   "-Djdk.system.module.finder.disabledFastPath",
 139                                   "-m", MAIN_MID)
 140                         .outputTo(System.out)
 141                         .errorTo(System.out)
 142                         .getExitValue() == 0);
 143     }
 144 
 145     /*
 146      * Test the optimization that deduplicates Set<String> on targets of exports,
 147      * uses, provides.
 148      */
 149     @Test
 150     public void testDedupSet() throws Throwable {
 151         if (!hasJmods()) return;
 152 
 153         Path dir = Paths.get("newImage");
 154         createImage(dir, "java.base", "m1", "m2", "m3", "m4");
 155         Path java = dir.resolve("bin").resolve("java");
 156         assertTrue(executeProcess(java.toString(), "-m", MAIN_MID)
 157                         .outputTo(System.out)
 158                         .errorTo(System.out)
 159                         .getExitValue() == 0);
 160     }
 161 }