Path: blob/master/test/langtools/tools/jdeps/modules/GenModuleInfo.java
41149 views
/*1* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 819319226* @library ../lib27* @build CompilerUtils JdepsUtil JdepsRunner28* @modules jdk.jdeps/com.sun.tools.jdeps29* @run testng GenModuleInfo30* @summary Tests jdeps --generate-module-info option31*/3233import java.io.File;34import java.io.IOException;35import java.io.InputStream;36import java.io.UncheckedIOException;37import java.lang.module.ModuleDescriptor;3839import java.nio.file.Files;40import java.nio.file.Path;41import java.nio.file.Paths;4243import java.util.List;44import java.util.Set;45import java.util.stream.Collectors;46import java.util.stream.Stream;4748import org.testng.annotations.BeforeTest;49import org.testng.annotations.Test;5051import static org.testng.Assert.assertEquals;52import static org.testng.Assert.assertTrue;5354public class GenModuleInfo {55private static final String MODULE_INFO = "module-info.class";56private static final String TEST_SRC = System.getProperty("test.src");5758private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");59private static final Path MODS_DIR = Paths.get("mods");60private static final Path LIBS_DIR = Paths.get("libs");61private static final Path MLIBS_DIR = Paths.get("mlibs");62private static final Path DEST_DIR = Paths.get("moduleinfosrc");63private static final Path NEW_MODS_DIR = Paths.get("new_mods");6465// the names of the modules in this test66public static final String UNSUPPORTED = "unsupported";67public static final Set<String> MODULES = Set.of(68"mI", "mII", "mIII", "provider", "test", UNSUPPORTED69);7071@BeforeTest72public void setup() throws Exception {73compileAndCreateJars();74}7576/**77* Compile modules78*/79public static void compileModules(Path dest) {80assertTrue(CompilerUtils.compileModule(SRC_DIR, dest, UNSUPPORTED,81"--add-exports", "java.base/jdk.internal.perf=" + UNSUPPORTED));82MODULES.stream()83.filter(mn -> !mn.equals(UNSUPPORTED))84.forEach(mn -> assertTrue(CompilerUtils.compileModule(SRC_DIR, dest, mn)));85}8687/**88* Create JAR files with no module-info.class89*/90public static void createModularJARs(Path mods, Path dest, String... modules) throws IOException {91Files.createDirectory(dest);92// create modular JAR93for (String mn : modules) {94Path root = mods.resolve(mn);95try (Stream<Path> stream = Files.find(root, Integer.MAX_VALUE,96(p, attr) -> { return attr.isRegularFile(); })) {97JdepsUtil.createJar(dest.resolve(mn + ".jar"), root, stream);98}99}100}101102/**103* Create JAR files with no module-info.class104*/105public static List<Path> createJARFiles(Path mods, Path libs) throws IOException {106Files.createDirectory(libs);107108for (String mn : MODULES) {109Path root = mods.resolve(mn);110Path msrc = SRC_DIR.resolve(mn);111Path metaInf = msrc.resolve("META-INF");112if (Files.exists(metaInf)) {113try (Stream<Path> resources = Files.find(metaInf, Integer.MAX_VALUE,114(p, attr) -> { return attr.isRegularFile();})) {115resources.forEach(file -> {116try {117Path path = msrc.relativize(file);118Files.createDirectories(root.resolve(path).getParent());119Files.copy(file, root.resolve(path));120} catch (IOException e) {121throw new UncheckedIOException(e);122}123});124}125}126// copy all entries except module-info.class127try (Stream<Path> stream = Files.find(root, Integer.MAX_VALUE,128(p, attr) -> { return attr.isRegularFile();})) {129Stream<Path> entries = stream.filter(f -> {130String fn = f.getFileName().toString();131if (fn.endsWith(".class")) {132return !fn.equals("module-info.class");133} else {134return true;135}136});137138JdepsUtil.createJar(libs.resolve(mn + ".jar"), root, entries);139}140}141142return MODULES.stream()143.map(mn -> LIBS_DIR.resolve(mn + ".jar"))144.collect(Collectors.toList());145}146147/**148* compile the generated module-info.java149*/150public static void compileNewGenModuleInfo(Path source, Path dest) {151152assertTrue(CompilerUtils.compileModule(source, dest, UNSUPPORTED,153"-p", dest.toString(),154"--add-exports", "java.base/jdk.internal.perf=" + UNSUPPORTED));155156MODULES.stream()157.filter(mn -> !mn.equals(UNSUPPORTED))158.forEach(mn -> assertTrue(159CompilerUtils.compileModule(source, dest,160mn, "-p", dest.toString()))161);162163}164165public static void compileAndCreateJars() throws Exception {166CompilerUtils.cleanDir(MODS_DIR);167CompilerUtils.cleanDir(LIBS_DIR);168169compileModules(MODS_DIR);170171// create modular JARs except test172createModularJARs(MODS_DIR, MLIBS_DIR, MODULES.stream().filter(mn -> !mn.equals("test"))173.toArray(String[]::new));174// create non-modular JARs175createJARFiles(MODS_DIR, LIBS_DIR);176}177178@Test179public void automaticModules() throws IOException {180Stream<String> files = MODULES.stream()181.map(mn -> LIBS_DIR.resolve(mn + ".jar"))182.map(Path::toString);183JdepsRunner.run(Stream.concat(Stream.of("-cp"), files).toArray(String[]::new));184}185186@Test187public void test() throws IOException {188Path dest = DEST_DIR.resolve("case1");189Path classes = NEW_MODS_DIR.resolve("case1");190Files.createDirectories(dest);191Files.createDirectories(classes);192193Stream<String> files = MODULES.stream()194.map(mn -> LIBS_DIR.resolve(mn + ".jar"))195.map(Path::toString);196197Stream<String> options = Stream.concat(198Stream.of("--generate-module-info", dest.toString()), files);199JdepsRunner.run(options.toArray(String[]::new));200201// check file exists202MODULES.stream()203.map(mn -> dest.resolve(mn).resolve("module-info.java"))204.forEach(f -> assertTrue(Files.exists(f)));205206// copy classes to a temporary directory207// and then compile new module-info.java208copyClasses(MODS_DIR, classes);209compileNewGenModuleInfo(dest, classes);210211for (String mn : MODULES) {212verify(mn, classes, MODS_DIR);213}214}215216@Test217public void withModulePath() throws IOException {218Path dest = DEST_DIR.resolve("case2");219Path classes = NEW_MODS_DIR.resolve("case2");220Files.createDirectories(dest);221Files.createDirectories(classes);222223JdepsRunner.run("--module-path", MLIBS_DIR.toString(),224"--generate-module-info", dest.toString(),225LIBS_DIR.resolve("test.jar").toString());226227String name = "test";228Path gensrc = dest.resolve(name).resolve("module-info.java");229assertTrue(Files.exists(gensrc));230231// copy classes to a temporary directory232// and then compile new module-info.java233copyClasses(MODS_DIR.resolve(name), classes.resolve(name));234assertTrue(CompilerUtils.compileModule(dest, classes, name, "-p", MLIBS_DIR.toString()));235236verify(name, classes, MODS_DIR);237}238239/**240* Verify the dependences from the given module-info.class files241*/242public void verify(String mn, Path mdir1, Path mdir2) throws IOException {243Path p1 = mdir1.resolve(mn).resolve(MODULE_INFO);244Path p2 = mdir2.resolve(mn).resolve(MODULE_INFO);245try (InputStream in1 = Files.newInputStream(p1);246InputStream in2 = Files.newInputStream(p2)) {247verify(ModuleDescriptor.read(in1),248ModuleDescriptor.read(in2, () -> packages(mdir2.resolve(mn))));249}250}251252/**253* Copy classes except the module-info.class to the destination directory254*/255public static void copyClasses(Path from, Path dest) throws IOException {256try (Stream<Path> stream = Files.walk(from, Integer.MAX_VALUE)) {257stream.filter(path -> !path.getFileName().toString().equals(MODULE_INFO) &&258path.getFileName().toString().endsWith(".class"))259.map(path -> from.relativize(path))260.forEach(path -> {261try {262Path newFile = dest.resolve(path);263Files.createDirectories(newFile.getParent());264Files.copy(from.resolve(path), newFile);265} catch (IOException e) {266throw new UncheckedIOException(e);267}268});269}270}271272/**273* Verify the generated module-info.java is equivalent to module-info.class274* compiled from source.275*/276private void verify(ModuleDescriptor md1, ModuleDescriptor md2) {277System.out.println("verifying: " + md1.name());278assertEquals(md1.name(), md2.name());279assertEquals(md1.requires(), md2.requires());280// all packages are exported281assertEquals(md1.exports().stream()282.map(ModuleDescriptor.Exports::source)283.collect(Collectors.toSet()), md2.packages());284if (!md1.opens().isEmpty()) {285throw new RuntimeException("unexpected opens: " +286md1.opens().stream()287.map(o -> o.toString())288.collect(Collectors.joining(",")));289}290291assertEquals(md1.provides(), md2.provides());292}293294private Set<String> packages(Path dir) {295try (Stream<Path> stream = Files.find(dir, Integer.MAX_VALUE,296((path, attrs) -> attrs.isRegularFile() &&297path.toString().endsWith(".class")))) {298return stream.map(path -> toPackageName(dir.relativize(path)))299.filter(pkg -> pkg.length() > 0) // module-info300.distinct()301.collect(Collectors.toSet());302} catch (IOException x) {303throw new UncheckedIOException(x);304}305}306307private String toPackageName(Path path) {308String name = path.toString();309assert name.endsWith(".class");310int index = name.lastIndexOf(File.separatorChar);311if (index != -1) {312return name.substring(0, index).replace(File.separatorChar, '.');313} else {314return "";315}316}317318}319320321