Path: blob/master/test/jdk/tools/jlink/plugins/ExcludeJmodSectionPluginTest.java
41152 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* @summary Test --no-man-pages and --no-header-files26* @library /test/lib27* @modules jdk.compiler28* jdk.jlink29* @build jdk.test.lib.compiler.CompilerUtils30* @run testng ExcludeJmodSectionPluginTest31*/3233import java.io.BufferedWriter;34import java.io.File;35import java.io.IOException;36import java.io.PrintWriter;37import java.nio.file.FileVisitResult;38import java.nio.file.Files;39import java.nio.file.Path;40import java.nio.file.Paths;41import java.nio.file.SimpleFileVisitor;42import java.nio.file.attribute.BasicFileAttributes;43import java.util.ArrayList;44import java.util.HashSet;45import java.util.List;46import java.util.Set;47import java.util.spi.ToolProvider;48import java.util.stream.Collectors;49import java.util.stream.Stream;50import jdk.test.lib.compiler.CompilerUtils;5152import org.testng.annotations.BeforeTest;53import org.testng.annotations.DataProvider;54import org.testng.annotations.Test;5556import static org.testng.Assert.*;5758public class ExcludeJmodSectionPluginTest {59static final ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod")60.orElseThrow(() ->61new RuntimeException("jmod tool not found")62);6364static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")65.orElseThrow(() ->66new RuntimeException("jlink tool not found")67);6869static final Path MODULE_PATH = Paths.get(System.getProperty("java.home"), "jmods");70static final Path SRC_DIR = Paths.get("src");71static final Path MODS_DIR = Paths.get("mods");72static final Path JMODS_DIR = Paths.get("jmods");73static final Path MAN_DIR = Paths.get("man");74static final Path INCLUDE_DIR = Paths.get("include");75static final Path IMAGES_DIR = Paths.get("images");7677@BeforeTest78private void setup() throws Exception {79// build jmod files80JmodFileBuilder m1 = new JmodFileBuilder("m1");81m1.headerFile("m1a.h");82m1.headerFile("m1b.h");83m1.build();8485JmodFileBuilder m2 = new JmodFileBuilder("m2");86m2.headerFile("m2.h");87m2.manPage("tool2.1");88m2.build();8990JmodFileBuilder m3 = new JmodFileBuilder("m3");91m3.manPage("tool3.1");92m3.build();93}9495private String imageDir(String dir) {96return IMAGES_DIR.resolve(dir).toString();97}9899100@DataProvider(name = "jlinkoptions")101public Object[][] jlinkoptions() {102// options and expected header files & man pages103return new Object[][] {104{ new String [] {105"test1",106"--exclude-files=/java.base/include/**,/java.base/man/**",107},108List.of("include/m1a.h",109"include/m1b.h",110"include/m2.h",111"man/tool2.1",112"man/tool3.1")113},114115{ new String [] {116"test2",117"--no-man-pages",118"--no-header-files",119},120List.of()121},122123{ new String[] {124"test3",125"--no-header-files",126"--exclude-files=/java.base/man/**"127},128List.of("man/tool2.1",129"man/tool3.1") },130131{ new String [] {132"test4",133"--no-man-pages",134"--exclude-files=/java.base/include/**,/m2/include/**",135},136List.of("include/m1a.h",137"include/m1b.h")138},139140{ new String [] {141"test5",142"--no-header-files",143"--exclude-files=/java.base/man/**,/m2/man/**"144},145List.of("man/tool3.1")146},147};148}149150@Test(dataProvider = "jlinkoptions")151public void test(String[] opts, List<String> expectedFiles) throws Exception {152if (Files.notExists(MODULE_PATH)) {153// exploded image154return;155}156157String dir = opts[0];158List<String> options = new ArrayList<>();159for (int i = 1; i < opts.length; i++) {160options.add(opts[i]);161}162163String mpath = MODULE_PATH.toString() + File.pathSeparator +164JMODS_DIR.toString();165Stream.of("--module-path", mpath,166"--add-modules", "m1,m2,m3",167"--output", imageDir(dir))168.forEach(options::add);169170Path image = createImage(dir, options, expectedFiles);171172// check if any unexpected header file or man page173Set<Path> extraFiles = Files.walk(image, Integer.MAX_VALUE)174.filter(p -> Files.isRegularFile(p))175.filter(p -> p.getParent().endsWith("include") ||176p.getParent().endsWith("man"))177.filter(p -> {178String fn = String.format("%s/%s",179p.getParent().getFileName().toString(),180p.getFileName().toString());181return !expectedFiles.contains(fn);182})183.collect(Collectors.toSet());184185if (extraFiles.size() > 0) {186System.out.println("Unexpected files: " + extraFiles.toString());187assertTrue(extraFiles.isEmpty());188}189}190191/**192* Test java.base's include header files193*/194@Test195public void testJavaBase() {196if (Files.notExists(MODULE_PATH)) {197// exploded image198return;199}200List<String> options = List.of("--module-path",201MODULE_PATH.toString(),202"--add-modules", "java.base",203"--output", imageDir("base"));204createImage("base", options,205List.of("include/jni.h", "include/jvmti.h"));206207}208209private Path createImage(String outputDir, List<String> options,210List<String> expectedFiles) {211System.out.println("jlink " + options.toString());212int rc = JLINK_TOOL.run(System.out, System.out,213options.toArray(new String[0]));214assertTrue(rc == 0);215216Path d = IMAGES_DIR.resolve(outputDir);217for (String fn : expectedFiles) {218Path path = d.resolve(fn);219if (Files.notExists(path)) {220throw new RuntimeException(path + " not found");221}222}223return d;224}225226private void deleteDirectory(Path dir) throws IOException {227Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {228@Override229public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)230throws IOException231{232Files.delete(file);233return FileVisitResult.CONTINUE;234}235236@Override237public FileVisitResult postVisitDirectory(Path dir, IOException exc)238throws IOException239{240Files.delete(dir);241return FileVisitResult.CONTINUE;242}243});244}245246/**247* Builder to create JMOD file248*/249class JmodFileBuilder {250251final String name;252final Set<String> manPages = new HashSet<>();253final Set<String> headerFiles = new HashSet<>();254255JmodFileBuilder(String name) throws IOException {256this.name = name;257258Path msrc = SRC_DIR.resolve(name);259if (Files.exists(msrc)) {260deleteDirectory(msrc);261}262}263264JmodFileBuilder manPage(String filename) {265manPages.add(filename);266return this;267}268269JmodFileBuilder headerFile(String filename) {270headerFiles.add(filename);271return this;272}273274Path build() throws IOException {275compileModule();276// create man pages277Path mdir = MAN_DIR.resolve(name);278for (String filename : manPages) {279Files.createDirectories(mdir);280Files.createFile(mdir.resolve(filename));281}282// create header files283mdir = INCLUDE_DIR.resolve(name);284for (String filename : headerFiles) {285Files.createDirectories(mdir);286Files.createFile(mdir.resolve(filename));287}288return createJmodFile();289}290291void compileModule() throws IOException {292Path msrc = SRC_DIR.resolve(name);293Files.createDirectories(msrc);294Path minfo = msrc.resolve("module-info.java");295try (BufferedWriter bw = Files.newBufferedWriter(minfo);296PrintWriter writer = new PrintWriter(bw)) {297writer.format("module %s { }%n", name);298}299300assertTrue(CompilerUtils.compile(msrc, MODS_DIR,301"--module-source-path",302SRC_DIR.toString()));303}304305Path createJmodFile() throws IOException {306Path mclasses = MODS_DIR.resolve(name);307Files.createDirectories(JMODS_DIR);308Path outfile = JMODS_DIR.resolve(name + ".jmod");309List<String> args = new ArrayList<>();310args.add("create");311// add classes312args.add("--class-path");313args.add(mclasses.toString());314// man pages315if (manPages.size() > 0) {316args.add("--man-pages");317args.add(MAN_DIR.resolve(name).toString());318}319// header files320if (headerFiles.size() > 0) {321args.add("--header-files");322args.add(INCLUDE_DIR.resolve(name).toString());323}324args.add(outfile.toString());325326if (Files.exists(outfile))327Files.delete(outfile);328329System.out.println("jmod " +330args.stream().collect(Collectors.joining(" ")));331332int rc = JMOD_TOOL.run(System.out, System.out,333args.toArray(new String[args.size()]));334if (rc != 0) {335throw new AssertionError("jmod failed: rc = " + rc);336}337return outfile;338}339}340}341342343