Path: blob/master/test/langtools/tools/jdeps/missingDeps/MissingDepsTest.java
41149 views
/*1* Copyright (c) 2020, 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 Tests jdeps option on a MR jar with missing dependences26* @library ../lib27* @build CompilerUtils JdepsUtil28* @modules jdk.jdeps/com.sun.tools.jdeps29* @run testng MissingDepsTest30*/3132import java.nio.file.Files;33import java.nio.file.Path;34import java.nio.file.Paths;35import java.util.Arrays;36import java.util.List;37import java.util.Set;38import java.util.spi.ToolProvider;39import java.util.stream.Stream;4041import org.testng.annotations.BeforeTest;42import org.testng.annotations.DataProvider;43import org.testng.annotations.Test;4445import static org.testng.Assert.assertTrue;46import static org.testng.Assert.assertEquals;474849public class MissingDepsTest {50private static final String TEST_SRC = System.getProperty("test.src");51private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");5253private static final Path MODS_DIR = Paths.get("mods");54private static final Path CLASSES_DIR = Paths.get("classes");5556private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar").orElseThrow();57private static final String VERSION = "13";5859private static final Set<String> modules = Set.of("m1", "m2");6061/**62* Compiles classes used by the test63*/64@BeforeTest65public void compileAll() throws Exception {66CompilerUtils.cleanDir(MODS_DIR);67modules.forEach(mn ->68assertTrue(CompilerUtils.compileModule(SRC_DIR, MODS_DIR, mn)));6970// compile a versioned class file71Path versionedFile = Paths.get(TEST_SRC, "p/internal/X.java");72assertTrue(CompilerUtils.compile(versionedFile, CLASSES_DIR, "-cp", MODS_DIR.resolve("m2").toString()));7374// create a modular multi-release m1.jar75JAR_TOOL.run(System.out, System.err, "cf", "m1.jar",76"-C", MODS_DIR.resolve("m1").toString(), ".");77JAR_TOOL.run(System.out, System.err, "uf", "m1.jar",78"--release", VERSION, "-C", CLASSES_DIR.toString(), "p/internal/X.class");79// create a non-modular multi-release mr.jar80JAR_TOOL.run(System.out, System.err, "cf", "mr.jar",81"-C", MODS_DIR.resolve("m1").toString(), "p/Foo.class",82"--release", VERSION, "-C", CLASSES_DIR.toString(), "p/internal/X.class");83}8485@Test86public void checkModuleDeps() {87JdepsTest test = new JdepsTest();88test.options(List.of("--module-path", "m1.jar", "--multi-release", VERSION, "--check", "m1"));89test.checkMissingDeps();90test.ignoreMissingDeps("requires java.management");91}9293@Test94public void genModuleInfo() {95JdepsTest test = new JdepsTest();96test.options(List.of("--generate-module-info", ".", "--multi-release", VERSION, "mr.jar"));97test.checkMissingDeps();98Path file = Paths.get("mr", "versions", VERSION, "module-info.java");99test.ignoreMissingDeps(file.toString());100assertTrue(Files.exists(file));101}102103@Test104public void listModuleDeps() {105JdepsTest test = new JdepsTest();106test.options(List.of("--list-deps", "--multi-release", VERSION, "mr.jar"));107test.checkMissingDeps();108test.ignoreMissingDeps("java.management");109}110111class JdepsTest {112// set DEBUG to true to show the jdeps output113static final boolean DEBUG = false;114List<String> options;115JdepsTest options(List<String> options) {116this.options = options;117return this;118}119120private void checkMissingDeps() {121JdepsRunner jdepsRunner = new JdepsRunner(options.toArray(new String[0]));122int rc = jdepsRunner.run(DEBUG);123assertTrue(rc != 0);124String regex = "\\s+13/p.internal.X\\s+->\\s+q.T\\s+not found";125assertTrue(Arrays.stream(jdepsRunner.output()).anyMatch(l -> l.matches(regex)));126}127128public void ignoreMissingDeps(String expected) {129JdepsRunner jdepsRunner = new JdepsRunner(Stream.concat(Stream.of("--ignore-missing-deps"), options.stream())130.toArray(String[]::new));131int rc = jdepsRunner.run(DEBUG);132assertTrue(rc == 0);133System.out.println("Expected: " + expected);134assertTrue(Arrays.stream(jdepsRunner.output()).anyMatch(l -> l.contains(expected)));135}136}137}138139140