1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @summary Tests split packages
* @library ../lib
* @build CompilerUtils JdepsUtil
* @modules java.logging
* jdk.jdeps/com.sun.tools.jdeps
* jdk.unsupported
* @run testng InverseDeps
*/
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.sun.tools.jdeps.Archive;
import com.sun.tools.jdeps.InverseDepsAnalyzer;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertEquals;
public class InverseDeps {
private static final String TEST_SRC = System.getProperty("test.src");
private static final String TEST_CLASSES = System.getProperty("test.classes");
private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
private static final Path MODS_DIR = Paths.get("mods");
private static final Path LIBS_DIR = Paths.get("libs");
private static final Set<String> modules = new LinkedHashSet(
List.of("unsafe", "mIV", "mV", "mVI", "mVII")
);
/**
* Compiles classes used by the test
*/
@BeforeTest
public void compileAll() throws Exception {
CompilerUtils.cleanDir(MODS_DIR);
for (String mn : modules) {
// compile a module
assertTrue(CompilerUtils.compileModule(SRC_DIR, MODS_DIR, mn));
// create JAR files with no module-info.class
Path root = MODS_DIR.resolve(mn);
try (Stream<Path> stream = Files.walk(root, Integer.MAX_VALUE)) {
Stream<Path> entries = stream.filter(f -> {
String fn = f.getFileName().toString();
return fn.endsWith(".class") && !fn.equals("module-info.class");
});
JdepsUtil.createJar(LIBS_DIR.resolve(mn + ".jar"), root, entries);
}
}
}
@DataProvider(name = "jdkModules")
public Object[][] jdkModules() {
return new Object[][]{
// --require and a subset of dependences
{ "jdk.compiler", new String[][] {
new String[] {"jdk.compiler", "jdk.jshell"},
new String[] {"jdk.compiler", "jdk.rmic"},
new String[] {"jdk.compiler", "jdk.javadoc", "jdk.rmic"},
}
},
{ "java.compiler", new String[][] {
new String[] {"java.compiler", "jdk.jshell"},
new String[] {"java.compiler", "jdk.compiler", "jdk.jshell"},
new String[] {"java.compiler", "jdk.compiler", "jdk.rmic"},
new String[] {"java.compiler", "jdk.compiler", "jdk.javadoc", "jdk.rmic"},
new String[] {"java.compiler", "java.se", "java.se.ee"},
}
},
};
}
@Test(dataProvider = "jdkModules")
public void testJDKModule(String moduleName, String[][] expected) throws Exception {
// this invokes the jdeps launcher so that all system modules are observable
JdepsRunner jdeps = JdepsRunner.run(
"--inverse", "--require", moduleName
);
List<String> output = Arrays.stream(jdeps.output())
.map(s -> s.trim())
.collect(Collectors.toList());
// verify the dependences
assertTrue(Arrays.stream(expected)
.map(path -> Arrays.stream(path)
.collect(Collectors.joining(" <- ")))
.anyMatch(output::contains));
}
@DataProvider(name = "testrequires")
public Object[][] expected1() {
return new Object[][] {
// --require and result
{ "java.sql", new String[][] {
new String[] { "java.sql", "mV" },
}
},
{ "java.compiler", new String[][] {
new String[] { "java.compiler", "mV" },
new String[] { "java.compiler", "mIV", "mV" },
}
},
{ "java.logging", new String[][]{
new String[] {"java.logging", "mV"},
new String[] {"java.logging", "mIV", "mV"},
new String[] {"java.logging", "java.sql", "mV"},
}
},
{ "jdk.unsupported", new String[][] {
new String[] {"jdk.unsupported", "unsafe", "mVI", "mVII"},
new String[] {"jdk.unsupported", "unsafe", "mVII"}
}
},
};
}
@Test(dataProvider = "testrequires")
public void testrequires(String name, String[][] expected) throws Exception {
String cmd1 = String.format("jdeps --inverse --module-path %s --require %s --add-modules %s%n",
MODS_DIR, name, modules.stream().collect(Collectors.joining(",")));
try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd1)) {
jdeps.appModulePath(MODS_DIR.toString())
.addmods(modules)
.requires(Set.of(name));
runJdeps(jdeps, expected);
}
String cmd2 = String.format("jdeps --inverse --module-path %s --require %s" +
" --add-modules ALL-MODULE-PATH%n", LIBS_DIR, name);
// automatic module
try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd2)) {
jdeps.appModulePath(MODS_DIR.toString())
.addmods(Set.of("ALL-MODULE-PATH"))
.requires(Set.of(name));
runJdeps(jdeps, expected);
}
}
@DataProvider(name = "testpackage")
public Object[][] expected2() {
return new Object[][] {
// -package and result
{ "p4", new String[][] {
new String[] { "mIV", "mV"},
}
},
{ "javax.tools", new String[][] {
new String[] {"java.compiler", "mV"},
new String[] {"java.compiler", "mIV", "mV"},
}
},
{ "sun.misc", new String[][] {
new String[] {"jdk.unsupported", "unsafe", "mVI", "mVII"},
new String[] {"jdk.unsupported", "unsafe", "mVII"}
}
}
};
}
@Test(dataProvider = "testpackage")
public void testpackage(String name, String[][] expected) throws Exception {
String cmd = String.format("jdeps --inverse --module-path %s -package %s --add-modules %s%n",
MODS_DIR, name, modules.stream().collect(Collectors.joining(",")));
try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd)) {
jdeps.appModulePath(MODS_DIR.toString())
.addmods(modules)
.matchPackages(Set.of(name));
runJdeps(jdeps, expected);
}
}
@DataProvider(name = "testregex")
public Object[][] expected3() {
return new Object[][] {
// -regex and result
{ "org.safe.Lib", new String[][] {
new String[] { "unsafe", "mVII"},
new String[] { "unsafe", "mVI", "mVII"},
}
},
{ "java.util.logging.*|org.safe.Lib", new String[][] {
new String[] { "unsafe", "mVII"},
new String[] { "unsafe", "mVI", "mVII"},
new String[] { "java.logging", "mV"},
}
}
};
}
@Test(dataProvider = "testregex")
public void testregex(String name, String[][] expected) throws Exception {
String cmd = String.format("jdeps --inverse --module-path %s -regex %s --add-modules %s%n",
MODS_DIR, name, modules.stream().collect(Collectors.joining(",")));
try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd)) {
jdeps.appModulePath(MODS_DIR.toString())
.addmods(modules)
.regex(name);
runJdeps(jdeps, expected);
}
}
@DataProvider(name = "classpath")
public Object[][] expected4() {
return new Object[][] {
// -regex and result
{ "sun.misc.Unsafe", new String[][] {
new String[] {"jdk.unsupported", "unsafe.jar", "mVI.jar", "mVII.jar"},
new String[] {"jdk.unsupported", "unsafe.jar", "mVII.jar"}
}
},
{ "org.safe.Lib", new String[][] {
new String[] { "unsafe.jar", "mVII.jar"},
new String[] { "unsafe.jar", "mVI.jar", "mVII.jar"},
}
},
{ "java.util.logging.*|org.safe.Lib", new String[][] {
new String[] { "unsafe.jar", "mVII.jar"},
new String[] { "unsafe.jar", "mVI.jar", "mVII.jar"},
new String[] { "java.logging", "mV.jar"},
}
}
};
}
@Test(dataProvider = "classpath")
public void testClassPath(String name, String[][] expected) throws Exception {
// -classpath
String cpath = modules.stream()
.filter(mn -> !mn.equals("mVII"))
.map(mn -> LIBS_DIR.resolve(mn + ".jar").toString())
.collect(Collectors.joining(File.pathSeparator));
Path jarfile = LIBS_DIR.resolve("mVII.jar");
String cmd1 = String.format("jdeps --inverse -classpath %s -regex %s %s%n",
cpath, name, jarfile);
try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd1)) {
jdeps.verbose("-verbose:class")
.addClassPath(cpath)
.regex(name).addRoot(jarfile);
runJdeps(jdeps, expected);
}
// all JAR files on the command-line arguments
Set<Path> paths = modules.stream()
.map(mn -> LIBS_DIR.resolve(mn + ".jar"))
.collect(Collectors.toSet());
String cmd2 = String.format("jdeps --inverse -regex %s %s%n", name, paths);
try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd2)) {
jdeps.verbose("-verbose:class").regex(name);
paths.forEach(jdeps::addRoot);
runJdeps(jdeps, expected);
}
}
private void runJdeps(JdepsUtil.Command jdeps, String[][] expected) throws Exception {
InverseDepsAnalyzer analyzer = jdeps.getInverseDepsAnalyzer();
assertTrue(analyzer.run());
// get the inverse transitive dependences
List<String[]> paths = analyzer.inverseDependences().stream()
.map(deque -> deque.stream()
.map(Archive::getName)
.collect(Collectors.toList()).toArray(new String[0]))
.collect(Collectors.toList());
jdeps.dumpOutput(System.err);
paths.forEach(path -> System.err.println(Arrays.stream(path)
.collect(Collectors.joining(" <- "))));
// verify the dependences
assertEquals(paths.size(), expected.length);
for (int i=0; i < paths.size(); i++) {
String[] path = paths.get(i);
boolean noneMatched = Arrays.stream(expected)
.filter(array -> array.length == path.length)
.noneMatch(array -> Arrays.equals(array, path));
if (noneMatched)
System.err.format("Expected: %s found: %s%n",
Arrays.stream(expected)
.map(Arrays::toString)
.collect(Collectors.joining(", ")),
Arrays.toString(path));
assertFalse(noneMatched);
}
}
}
|