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
  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 
  56     // the names of the modules in this test
  57     private static String[] modules = new String[] {"m1", "m2", "m3"};
  58 
  59     private static boolean hasJmods() {
  60         if (!Files.exists(JMODS)) {
  61             System.err.println("Test skipped. NO jmods directory");
  62             return false;
  63         }
  64         return true;
  65     }
  66 
  67     /*
  68      * Compiles all modules used by the test
  69      */
  70     @BeforeTest
  71     public void compileAll() throws Throwable {
  72         if (!hasJmods()) return;
  73 
  74         for (String mn : modules) {
  75             Path msrc = SRC_DIR.resolve(mn);
  76             assertTrue(CompilerUtils.compile(msrc, MODS_DIR, "-modulesourcepath", SRC_DIR.toString()));
  77         }
  78 
  79         if (Files.exists(IMAGE)) {
  80             FileUtils.deleteFileTreeUnchecked(IMAGE);
  81         }
  82 
  83         createImage(IMAGE, "java.base", "m1");
  84     }
  85 
  86     private void createImage(Path outputDir, String... modules) throws Throwable {
  87         Path jlink = Paths.get(JAVA_HOME, "bin", "jlink");
  88         String mp = JMODS.toString() + File.pathSeparator + MODS_DIR.toString();
  89         assertTrue(executeProcess(jlink.toString(), "--output", outputDir.toString(),
  90                         "--addmods", Arrays.stream(modules).collect(Collectors.joining(",")),
  91                         "--modulepath", mp)
  92                         .outputTo(System.out)
  93                         .errorTo(System.out)
  94                         .getExitValue() == 0);
  95     }
  96 
  97     /*
  98      * Test the image created when linking with a module with
  99      * no ConcealedPackages attribute
 100      */
 101     @Test
 102     public void test() throws Throwable {
 103         if (!hasJmods()) return;
 104 
 105         Path java = IMAGE.resolve("bin").resolve("java");
 106         assertTrue(executeProcess(java.toString(), "-m", "m1/p1.Main")
 107                         .outputTo(System.out)
 108                         .errorTo(System.out)
 109                         .getExitValue() == 0);
 110     }
 111 
 112     /*
 113      * Disable the fast loading of system modules.
 114      * Parsing module-info.class
 115      */
 116     @Test
 117     public void disableSystemModules() throws Throwable {
 118         if (!hasJmods()) return;
 119 
 120         Path java = IMAGE.resolve("bin").resolve("java");
 121         assertTrue(executeProcess(java.toString(),
 122                                   "-Djdk.system.module.finder.disabledFastPath",
 123                                   "-m", "m1/p1.Main")
 124                         .outputTo(System.out)
 125                         .errorTo(System.out)
 126                         .getExitValue() == 0);
 127     }
 128 
 129     /*
 130      * Test the optimization that deduplicates Set<String> on targets of exports,
 131      * uses, provides.
 132      */
 133     @Test
 134     public void testDedupSet() throws Throwable {
 135         if (!hasJmods()) return;
 136 
 137         Path dir = Paths.get("newImage");
 138         createImage(dir, "java.base", "m1", "m2", "m3");
 139         Path java = dir.resolve("bin").resolve("java");
 140         assertTrue(executeProcess(java.toString(), "-m", "m1/p1.Main")
 141                         .outputTo(System.out)
 142                         .errorTo(System.out)
 143                         .getExitValue() == 0);
 144     }
 145 }