test/langtools/tools/jdeps/modules/GenModuleInfo.java
changeset 48253 82767203606e
parent 47216 71c04702a3d5
equal deleted inserted replaced
48252:77b88d8f8380 48253:82767203606e
     1 /*
     1 /*
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @summary Tests jdeps --generate-module-info option
    26  * @bug 8193192
    27  * @library ../lib
    27  * @library ../lib
    28  * @build CompilerUtils JdepsUtil JdepsRunner
    28  * @build CompilerUtils JdepsUtil JdepsRunner
    29  * @modules jdk.jdeps/com.sun.tools.jdeps
    29  * @modules jdk.jdeps/com.sun.tools.jdeps
    30  * @run testng GenModuleInfo
    30  * @run testng GenModuleInfo
       
    31  * @summary Tests jdeps --generate-module-info option
    31  */
    32  */
    32 
    33 
    33 import java.io.File;
    34 import java.io.File;
    34 import java.io.IOException;
    35 import java.io.IOException;
    35 import java.io.InputStream;
    36 import java.io.InputStream;
    56     private static final String TEST_SRC = System.getProperty("test.src");
    57     private static final String TEST_SRC = System.getProperty("test.src");
    57 
    58 
    58     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
    59     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
    59     private static final Path MODS_DIR = Paths.get("mods");
    60     private static final Path MODS_DIR = Paths.get("mods");
    60     private static final Path LIBS_DIR = Paths.get("libs");
    61     private static final Path LIBS_DIR = Paths.get("libs");
       
    62     private static final Path MLIBS_DIR = Paths.get("mlibs");
    61     private static final Path DEST_DIR = Paths.get("moduleinfosrc");
    63     private static final Path DEST_DIR = Paths.get("moduleinfosrc");
    62     private static final Path NEW_MODS_DIR = Paths.get("new_mods");
    64     private static final Path NEW_MODS_DIR = Paths.get("new_mods");
    63 
    65 
    64     // the names of the modules in this test
    66     // the names of the modules in this test
    65     public static final String UNSUPPORTED = "unsupported";
    67     public static final String UNSUPPORTED = "unsupported";
    66     public static final Set<String> MODULES = Set.of(
    68     public static final Set<String> MODULES = Set.of(
    67         "mI", "mII", "mIII", "provider", UNSUPPORTED
    69         "mI", "mII", "mIII", "provider", "test", UNSUPPORTED
    68     );
    70     );
       
    71 
       
    72     @BeforeTest
       
    73     public void setup() throws Exception {
       
    74         compileAndCreateJars();
       
    75     }
    69 
    76 
    70     /**
    77     /**
    71      * Compile modules
    78      * Compile modules
    72      */
    79      */
    73     public static void compileModules(Path dest) {
    80     public static void compileModules(Path dest) {
    74         assertTrue(CompilerUtils.compileModule(SRC_DIR, dest, UNSUPPORTED,
    81         assertTrue(CompilerUtils.compileModule(SRC_DIR, dest, UNSUPPORTED,
    75             "--add-exports", "java.base/jdk.internal.perf=" + UNSUPPORTED));
    82             "--add-exports", "java.base/jdk.internal.perf=" + UNSUPPORTED));
    76         MODULES.stream()
    83         MODULES.stream()
    77                .filter(mn -> !mn.equals(UNSUPPORTED))
    84                .filter(mn -> !mn.equals(UNSUPPORTED))
    78                .forEach(mn -> assertTrue(CompilerUtils.compileModule(SRC_DIR, dest, mn)));
    85                .forEach(mn -> assertTrue(CompilerUtils.compileModule(SRC_DIR, dest, mn)));
       
    86     }
       
    87 
       
    88     /**
       
    89      * Create JAR files with no module-info.class
       
    90      */
       
    91     public static void createModularJARs(Path mods, Path dest, String... modules) throws IOException {
       
    92         Files.createDirectory(dest);
       
    93         // create modular JAR
       
    94         for (String mn : modules) {
       
    95             Path root = mods.resolve(mn);
       
    96             try (Stream<Path> stream = Files.find(root, Integer.MAX_VALUE,
       
    97                         (p, attr) -> { return attr.isRegularFile(); })) {
       
    98                 JdepsUtil.createJar(dest.resolve(mn + ".jar"), root, stream);
       
    99             }
       
   100         }
    79     }
   101     }
    80 
   102 
    81     /**
   103     /**
    82      * Create JAR files with no module-info.class
   104      * Create JAR files with no module-info.class
    83      */
   105      */
   139                                             mn, "-p", dest.toString()))
   161                                             mn, "-p", dest.toString()))
   140             );
   162             );
   141 
   163 
   142     }
   164     }
   143 
   165 
   144     /**
   166     public static void compileAndCreateJars() throws Exception {
   145      * Compiles all modules used by the test
       
   146      */
       
   147     @BeforeTest
       
   148     public void compileAll() throws Exception {
       
   149         CompilerUtils.cleanDir(MODS_DIR);
   167         CompilerUtils.cleanDir(MODS_DIR);
   150         CompilerUtils.cleanDir(LIBS_DIR);
   168         CompilerUtils.cleanDir(LIBS_DIR);
   151         CompilerUtils.cleanDir(DEST_DIR);
       
   152         CompilerUtils.cleanDir(NEW_MODS_DIR);
       
   153 
   169 
   154         compileModules(MODS_DIR);
   170         compileModules(MODS_DIR);
   155 
   171 
       
   172         // create modular JARs except test
       
   173         createModularJARs(MODS_DIR, MLIBS_DIR, MODULES.stream().filter(mn -> !mn.equals("test"))
       
   174                                                       .toArray(String[]::new));
       
   175         // create non-modular JARs
   156         createJARFiles(MODS_DIR, LIBS_DIR);
   176         createJARFiles(MODS_DIR, LIBS_DIR);
   157     }
   177     }
   158 
   178 
   159     @Test
   179     @Test
   160     public void automaticModules() throws IOException {
   180     public void automaticModules() throws IOException {
   164         JdepsRunner.run(Stream.concat(Stream.of("-cp"), files).toArray(String[]::new));
   184         JdepsRunner.run(Stream.concat(Stream.of("-cp"), files).toArray(String[]::new));
   165     }
   185     }
   166 
   186 
   167     @Test
   187     @Test
   168     public void test() throws IOException {
   188     public void test() throws IOException {
   169         Files.createDirectory(DEST_DIR);
   189         Path dest = DEST_DIR.resolve("case1");
       
   190         Path classes = NEW_MODS_DIR.resolve("case1");
       
   191         Files.createDirectories(dest);
       
   192         Files.createDirectories(classes);
   170 
   193 
   171         Stream<String> files = MODULES.stream()
   194         Stream<String> files = MODULES.stream()
   172                 .map(mn -> LIBS_DIR.resolve(mn + ".jar"))
   195                 .map(mn -> LIBS_DIR.resolve(mn + ".jar"))
   173                 .map(Path::toString);
   196                 .map(Path::toString);
   174 
   197 
   175         Stream<String> options = Stream.concat(
   198         Stream<String> options = Stream.concat(
   176             Stream.of("--generate-module-info", DEST_DIR.toString()), files);
   199             Stream.of("--generate-module-info", dest.toString()), files);
   177         JdepsRunner.run(options.toArray(String[]::new));
   200         JdepsRunner.run(options.toArray(String[]::new));
   178 
   201 
   179         // check file exists
   202         // check file exists
   180         MODULES.stream()
   203         MODULES.stream()
   181              .map(mn -> DEST_DIR.resolve(mn).resolve("module-info.java"))
   204              .map(mn -> dest.resolve(mn).resolve("module-info.java"))
   182              .forEach(f -> assertTrue(Files.exists(f)));
   205              .forEach(f -> assertTrue(Files.exists(f)));
   183 
   206 
   184         // copy classes to a temporary directory
   207         // copy classes to a temporary directory
   185         // and then compile new module-info.java
   208         // and then compile new module-info.java
   186         copyClasses(MODS_DIR, NEW_MODS_DIR);
   209         copyClasses(MODS_DIR, classes);
   187         compileNewGenModuleInfo(DEST_DIR, NEW_MODS_DIR);
   210         compileNewGenModuleInfo(dest, classes);
   188 
   211 
   189         for (String mn : MODULES) {
   212         for (String mn : MODULES) {
   190             Path p1 = NEW_MODS_DIR.resolve(mn).resolve(MODULE_INFO);
   213             verify(mn, classes, MODS_DIR);
   191             Path p2 = MODS_DIR.resolve(mn).resolve(MODULE_INFO);
   214         }
   192 
   215     }
   193             try (InputStream in1 = Files.newInputStream(p1);
   216 
   194                  InputStream in2 = Files.newInputStream(p2)) {
   217     @Test
   195                 verify(ModuleDescriptor.read(in1),
   218     public void withModulePath() throws IOException {
   196                        ModuleDescriptor.read(in2, () -> packages(MODS_DIR.resolve(mn))));
   219         Path dest = DEST_DIR.resolve("case2");
   197             }
   220         Path classes = NEW_MODS_DIR.resolve("case2");
       
   221         Files.createDirectories(dest);
       
   222         Files.createDirectories(classes);
       
   223 
       
   224         JdepsRunner.run("--module-path", MLIBS_DIR.toString(),
       
   225                         "--generate-module-info", dest.toString(),
       
   226                         LIBS_DIR.resolve("test.jar").toString());
       
   227 
       
   228         String name = "test";
       
   229         Path gensrc = dest.resolve(name).resolve("module-info.java");
       
   230         assertTrue(Files.exists(gensrc));
       
   231 
       
   232         // copy classes to a temporary directory
       
   233         // and then compile new module-info.java
       
   234         copyClasses(MODS_DIR.resolve(name), classes.resolve(name));
       
   235         assertTrue(CompilerUtils.compileModule(dest, classes, name, "-p", MLIBS_DIR.toString()));
       
   236 
       
   237         verify(name, classes, MODS_DIR);
       
   238     }
       
   239 
       
   240     /**
       
   241      * Verify the dependences from the given module-info.class files
       
   242      */
       
   243     public void verify(String mn, Path mdir1, Path mdir2) throws IOException {
       
   244         Path p1 = mdir1.resolve(mn).resolve(MODULE_INFO);
       
   245         Path p2 = mdir2.resolve(mn).resolve(MODULE_INFO);
       
   246         try (InputStream in1 = Files.newInputStream(p1);
       
   247              InputStream in2 = Files.newInputStream(p2)) {
       
   248             verify(ModuleDescriptor.read(in1),
       
   249                    ModuleDescriptor.read(in2, () -> packages(mdir2.resolve(mn))));
   198         }
   250         }
   199     }
   251     }
   200 
   252 
   201     /**
   253     /**
   202      * Copy classes except the module-info.class to the destination directory
   254      * Copy classes except the module-info.class to the destination directory