Path: blob/master/test/langtools/tools/jdeps/modules/CheckModuleTest.java
41149 views
/*1* Copyright (c) 2016, 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 split packages26* @library ../lib27* @build CompilerUtils JdepsUtil28* @modules jdk.jdeps/com.sun.tools.jdeps29* @run testng CheckModuleTest30*/3132import java.lang.module.ModuleDescriptor;33import java.nio.file.Path;34import java.nio.file.Paths;35import java.util.Map;36import java.util.Set;3738import com.sun.tools.jdeps.ModuleAnalyzer;39import org.testng.annotations.BeforeTest;40import org.testng.annotations.DataProvider;41import org.testng.annotations.Test;4243import static org.testng.Assert.assertTrue;44import static org.testng.Assert.assertEquals;454647public class CheckModuleTest {48private static final String TEST_SRC = System.getProperty("test.src");49private static final String TEST_CLASSES = System.getProperty("test.classes");5051private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");52private static final Path MODS_DIR = Paths.get("mods");5354// mIV and mV are analyzed. Others are compiled to make sure they are present55// on the module path for analysis56private static final Set<String> modules = Set.of("unsafe", "mIV", "mV", "mVI", "mVII", "mVIII");5758private static final String JAVA_BASE = "java.base";59private static final String JAVA_COMPILER = "java.compiler";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)));69}7071@DataProvider(name = "javaBase")72public Object[][] base() {73return new Object[][] {74{ JAVA_BASE, new ModuleMetaData(JAVA_BASE)75},76{ JAVA_COMPILER, new ModuleMetaData(JAVA_BASE)77},78};79};8081@Test(dataProvider = "javaBase")82public void testJavaBase(String name, ModuleMetaData data) throws Exception {83String cmd = String.format("jdeps --check %s --module-path %s%n", name, MODS_DIR);84try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd)) {85jdeps.appModulePath(MODS_DIR.toString());8687ModuleAnalyzer analyzer = jdeps.getModuleAnalyzer(Set.of(name));88assertTrue(analyzer.run(false));89jdeps.dumpOutput(System.err);9091ModuleDescriptor[] descriptors = analyzer.descriptors(name);92for (int i = 0; i < 3; i++) {93descriptors[i].requires().stream()94/* jcov has a dependency on java.logging, just ignore it in case this test is being executed with jcov95* this dependency from jcov should be fixed once bug: CODETOOLS-7902642 gets fixed96*/97.filter(req -> !req.toString().equals("java.logging"))98.forEach(req -> data.checkRequires(req));99}100}101}102103@DataProvider(name = "modules")104public Object[][] unnamed() {105return new Object[][]{106{ "mIV", new ModuleMetaData[] {107// original108new ModuleMetaData("mIV")109.requiresTransitive("java.compiler")110.requires("java.logging")111// unnused exports112.exports("p4.internal", Set.of("mVI", "mVII")),113// suggested version114new ModuleMetaData("mIV")115.requires("java.compiler"),116// reduced version117new ModuleMetaData("mIV")118.requires("java.compiler")119}120},121{ "mV", new ModuleMetaData[] {122// original123new ModuleMetaData("mV")124.requiresTransitive("java.compiler")125.requiresTransitive("java.logging")126.requires("java.sql")127.requiresTransitive("mIV"),128// suggested version129new ModuleMetaData("mV")130.requiresTransitive("java.compiler")131.requires("java.logging")132.requiresTransitive("java.sql")133.requiresTransitive("mIV"),134// reduced version135new ModuleMetaData("mV")136.requiresTransitive("java.compiler")137.requiresTransitive("java.sql")138.requiresTransitive("mIV"),139}140},141};142}143144@Test(dataProvider = "modules")145public void modularTest(String name, ModuleMetaData[] data) throws Exception {146String cmd = String.format("jdeps --check %s --module-path %s%n", name, MODS_DIR);147148try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd)) {149jdeps.appModulePath(MODS_DIR.toString());150151ModuleAnalyzer analyzer = jdeps.getModuleAnalyzer(Set.of(name));152assertTrue(analyzer.run(false));153jdeps.dumpOutput(System.err);154155// compare the module descriptors and the suggested versions156ModuleDescriptor[] descriptors = analyzer.descriptors(name);157for (int i = 0; i < 3; i++) {158ModuleMetaData metaData = data[i];159descriptors[i].requires().stream()160.forEach(req -> metaData.checkRequires(req));161}162163Map<String, Set<String>> unused = analyzer.unusedQualifiedExports(name);164// verify unuused qualified exports165assertEquals(unused, data[0].exports);166}167}168169}170171172