1 /*
   2  * Copyright (c) 2016, 2017, 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 import java.util.stream.Stream;
  31 
  32 import jdk.test.lib.compiler.CompilerUtils;
  33 import jdk.test.lib.util.FileUtils;
  34 import static jdk.testlibrary.ProcessTools.*;
  35 
  36 
  37 import org.testng.annotations.BeforeTest;
  38 import org.testng.annotations.Test;
  39 import static org.testng.Assert.*;
  40 
  41 /**
  42  * @test
  43  * @library /lib/testlibrary /test/lib
  44  * @modules jdk.compiler jdk.jlink
  45  * @build CompiledVersionTest jdk.testlibrary.ProcessTools
  46  *        jdk.test.lib.compiler.CompilerUtils
  47  * @run testng CompiledVersionTest
  48  */
  49 
  50 public class CompiledVersionTest {
  51     private static final String JAVA_HOME = System.getProperty("java.home");
  52     private static final String TEST_SRC = System.getProperty("test.src");
  53 
  54     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  55     private static final Path MODS_DIR = Paths.get("mods");
  56     private static final Path IMAGE = Paths.get("image");
  57     private static final Path JMODS = Paths.get(JAVA_HOME, "jmods");
  58     private static final String MAIN_MID = "test/jdk.test.Main";
  59 
  60     // the names of the modules in this test
  61     private static String[] modules  = new String[] { "m1", "m2", "test"};
  62     private static String[] versions = new String[] { "1.0", "2-ea", "3-internal"};
  63 
  64 
  65     private static boolean hasJmods() {
  66         if (!Files.exists(JMODS)) {
  67             System.err.println("Test skipped. NO jmods directory");
  68             return false;
  69         }
  70         return true;
  71     }
  72 
  73     /*
  74      * Compiles all modules used by the test
  75      */
  76     @BeforeTest
  77     public void compileAll() throws Throwable {
  78         if (!hasJmods()) return;
  79 
  80         for (int i=0; i < modules.length; i++) {
  81             String mn = modules[i];
  82             String version = versions[i];
  83             Path msrc = SRC_DIR.resolve(mn);
  84             if (version.equals("0")) {
  85                 assertTrue(CompilerUtils.compile(msrc, MODS_DIR,
  86                     "--add-exports", "java.base/jdk.internal.module=m1",
  87                     "--add-exports", "java.base/jdk.internal.org.objectweb.asm=m1",
  88                     "--module-source-path", SRC_DIR.toString()));
  89             } else {
  90                 assertTrue(CompilerUtils.compile(msrc, MODS_DIR,
  91                     "--add-exports", "java.base/jdk.internal.module=m1",
  92                     "--add-exports", "java.base/jdk.internal.org.objectweb.asm=m1",
  93                     "--module-source-path", SRC_DIR.toString(),
  94                     "--module-version", version));
  95             }
  96         }
  97 
  98         if (Files.exists(IMAGE)) {
  99             FileUtils.deleteFileTreeUnchecked(IMAGE);
 100         }
 101 
 102         createImage(IMAGE, modules);
 103     }
 104 
 105     private void createImage(Path outputDir, String... modules) throws Throwable {
 106         Path jlink = Paths.get(JAVA_HOME, "bin", "jlink");
 107         String mp = JMODS.toString() + File.pathSeparator + MODS_DIR.toString();
 108         assertTrue(executeProcess(jlink.toString(), "--output", outputDir.toString(),
 109                         "--add-modules", Arrays.stream(modules).collect(Collectors.joining(",")),
 110                         "--module-path", mp)
 111                         .outputTo(System.out)
 112                         .errorTo(System.out)
 113                         .getExitValue() == 0);
 114     }
 115 
 116     /*
 117      * Test the image created when linking with a module with
 118      * no Packages attribute
 119      */
 120     @Test
 121     public void testCompiledVersions() throws Throwable {
 122         if (!hasJmods()) return;
 123 
 124         Path java = IMAGE.resolve("bin").resolve("java");
 125         Stream<String> options = Stream.concat(
 126             Stream.of(java.toString(), "-m", MAIN_MID, String.valueOf(modules.length)),
 127             Stream.concat(Arrays.stream(modules), Arrays.stream(versions))
 128         );
 129 
 130         assertTrue(executeProcess(options.toArray(String[]::new))
 131                         .outputTo(System.out)
 132                         .errorTo(System.out)
 133                         .getExitValue() == 0);
 134     }
 135 }