CheckModuleTest.java revision 3792:d516975e8110
123353Sdfr/* 223353Sdfr * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 323353Sdfr * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 423353Sdfr * 523353Sdfr * This code is free software; you can redistribute it and/or modify it 623353Sdfr * under the terms of the GNU General Public License version 2 only, as 723353Sdfr * published by the Free Software Foundation. 823353Sdfr * 923353Sdfr * This code is distributed in the hope that it will be useful, but WITHOUT 1023353Sdfr * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1123353Sdfr * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1223353Sdfr * version 2 for more details (a copy is included in the LICENSE file that 1323353Sdfr * accompanied this code). 1423353Sdfr * 1523353Sdfr * You should have received a copy of the GNU General Public License version 1623353Sdfr * 2 along with this work; if not, write to the Free Software Foundation, 1723353Sdfr * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1823353Sdfr * 1923353Sdfr * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2023353Sdfr * or visit www.oracle.com if you need additional information or have any 2123353Sdfr * questions. 2223353Sdfr */ 2323353Sdfr 2423353Sdfr/* 2523353Sdfr * @test 2623353Sdfr * @summary Tests split packages 2723353Sdfr * @library ../lib 2823353Sdfr * @build CompilerUtils JdepsUtil 2950476Speter * @modules jdk.jdeps/com.sun.tools.jdeps 3023353Sdfr * @run testng CheckModuleTest 31192691Stmclaugh */ 32206622Suqs 3323353Sdfrimport java.lang.module.ModuleDescriptor; 3423353Sdfrimport java.nio.file.Path; 3523353Sdfrimport java.nio.file.Paths; 36107788Sruimport java.util.Map; 3723353Sdfrimport java.util.Set; 3884306Sru 3984306Sruimport com.sun.tools.jdeps.ModuleAnalyzer; 4084306Sruimport org.testng.annotations.BeforeTest; 4123353Sdfrimport org.testng.annotations.DataProvider; 42192691Stmclaughimport org.testng.annotations.Test; 4323353Sdfr 44121382Shmpimport static org.testng.Assert.assertTrue; 45121382Shmpimport static org.testng.Assert.assertEquals; 4623353Sdfr 4723353Sdfr 4823353Sdfrpublic class CheckModuleTest { 4929966Swosch private static final String TEST_SRC = System.getProperty("test.src"); 5029966Swosch private static final String TEST_CLASSES = System.getProperty("test.classes"); 5123353Sdfr 52121414Shmp private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); 5334504Scharnier private static final Path MODS_DIR = Paths.get("mods"); 54 55 // m4 and m5 are analyzed. Others are compiled to make sure they are present 56 // on the module path for analysis 57 private static final Set<String> modules = Set.of("unsafe", "m4", "m5", "mVI", "mVII", "m8"); 58 59 private static final String JAVA_BASE = "java.base"; 60 61 /** 62 * Compiles classes used by the test 63 */ 64 @BeforeTest 65 public void compileAll() throws Exception { 66 CompilerUtils.cleanDir(MODS_DIR); 67 modules.forEach(mn -> 68 assertTrue(CompilerUtils.compileModule(SRC_DIR, MODS_DIR, mn))); 69 } 70 71 @DataProvider(name = "javaBase") 72 public Object[][] base() { 73 return new Object[][] { 74 { JAVA_BASE, new ModuleMetaData(JAVA_BASE) 75 }, 76 }; 77 }; 78 79 @Test(dataProvider = "javaBase") 80 public void testJavaBase(String name, ModuleMetaData data) throws Exception { 81 String cmd = String.format("jdeps --check %s --module-path %s%n", name, MODS_DIR); 82 try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd)) { 83 jdeps.appModulePath(MODS_DIR.toString()); 84 85 ModuleAnalyzer analyzer = jdeps.getModuleAnalyzer(Set.of(name)); 86 assertTrue(analyzer.run()); 87 jdeps.dumpOutput(System.err); 88 89 ModuleDescriptor[] descriptors = analyzer.descriptors(name); 90 for (int i = 0; i < 3; i++) { 91 descriptors[i].requires().stream() 92 .forEach(req -> data.checkRequires(req)); 93 } 94 } 95 } 96 97 @DataProvider(name = "modules") 98 public Object[][] unnamed() { 99 return new Object[][]{ 100 { "m4", new ModuleMetaData[] { 101 // original 102 new ModuleMetaData("m4") 103 .requiresTransitive("java.compiler") 104 .requires("java.logging") 105 // unnused exports 106 .exports("p4.internal", Set.of("mVI", "mVII")), 107 // suggested version 108 new ModuleMetaData("m4") 109 .requires("java.compiler"), 110 // reduced version 111 new ModuleMetaData("m4") 112 .requires("java.compiler") 113 } 114 }, 115 { "m5", new ModuleMetaData[] { 116 // original 117 new ModuleMetaData("m5") 118 .requiresTransitive("java.compiler") 119 .requiresTransitive("java.logging") 120 .requires("java.sql") 121 .requiresTransitive("m4"), 122 // suggested version 123 new ModuleMetaData("m5") 124 .requiresTransitive("java.compiler") 125 .requires("java.logging") 126 .requiresTransitive("java.sql") 127 .requiresTransitive("m4"), 128 // reduced version 129 new ModuleMetaData("m5") 130 .requiresTransitive("java.compiler") 131 .requiresTransitive("java.sql") 132 .requiresTransitive("m4"), 133 } 134 }, 135 }; 136 } 137 138 @Test(dataProvider = "modules") 139 public void modularTest(String name, ModuleMetaData[] data) throws Exception { 140 String cmd = String.format("jdeps --check %s --module-path %s%n", name, MODS_DIR); 141 142 try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd)) { 143 jdeps.appModulePath(MODS_DIR.toString()); 144 145 ModuleAnalyzer analyzer = jdeps.getModuleAnalyzer(Set.of(name)); 146 assertTrue(analyzer.run()); 147 jdeps.dumpOutput(System.err); 148 149 // compare the module descriptors and the suggested versions 150 ModuleDescriptor[] descriptors = analyzer.descriptors(name); 151 for (int i = 0; i < 3; i++) { 152 ModuleMetaData metaData = data[i]; 153 descriptors[i].requires().stream() 154 .forEach(req -> metaData.checkRequires(req)); 155 } 156 157 Map<String, Set<String>> unused = analyzer.unusedQualifiedExports(name); 158 // verify unuused qualified exports 159 assertEquals(unused, data[0].exports); 160 } 161 } 162 163} 164