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