44 import com.sun.tools.jdeps.Archive; 45 import com.sun.tools.jdeps.InverseDepsAnalyzer; 46 import org.testng.annotations.BeforeTest; 47 import org.testng.annotations.DataProvider; 48 import org.testng.annotations.Test; 49 50 import static org.testng.Assert.assertTrue; 51 import static org.testng.Assert.assertFalse; 52 import static org.testng.Assert.assertEquals; 53 54 55 public class InverseDeps { 56 private static final String TEST_SRC = System.getProperty("test.src"); 57 private static final String TEST_CLASSES = System.getProperty("test.classes"); 58 59 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); 60 private static final Path MODS_DIR = Paths.get("mods"); 61 private static final Path LIBS_DIR = Paths.get("libs"); 62 63 private static final Set<String> modules = new LinkedHashSet( 64 List.of("unsafe", "m4", "m5", "mVI", "mVII") 65 ); 66 67 /** 68 * Compiles classes used by the test 69 */ 70 @BeforeTest 71 public void compileAll() throws Exception { 72 CompilerUtils.cleanDir(MODS_DIR); 73 74 for (String mn : modules) { 75 // compile a module 76 assertTrue(CompilerUtils.compileModule(SRC_DIR, MODS_DIR, mn)); 77 78 // create JAR files with no module-info.class 79 Path root = MODS_DIR.resolve(mn); 80 81 try (Stream<Path> stream = Files.walk(root, Integer.MAX_VALUE)) { 82 Stream<Path> entries = stream.filter(f -> { 83 String fn = f.getFileName().toString(); 84 return fn.endsWith(".class") && !fn.equals("module-info.class"); 85 }); 86 JdepsUtil.createJar(LIBS_DIR.resolve(mn + ".jar"), root, entries); 87 } 88 } 89 } 90 91 @DataProvider(name = "testrequires") 92 public Object[][] expected1() { 93 return new Object[][] { 94 // --require and result 95 { "java.sql", new String[][] { 96 new String[] { "java.sql", "m5" }, 97 } 98 }, 99 { "java.compiler", new String[][] { 100 new String[] { "java.compiler", "m5" }, 101 new String[] { "java.compiler", "m4", "m5" }, 102 } 103 }, 104 { "java.logging", new String[][]{ 105 new String[] {"java.logging", "m5"}, 106 new String[] {"java.logging", "m4", "m5"}, 107 new String[] {"java.logging", "java.sql", "m5"}, 108 } 109 }, 110 { "jdk.unsupported", new String[][] { 111 new String[] {"jdk.unsupported", "unsafe", "mVI", "mVII"}, 112 new String[] {"jdk.unsupported", "unsafe", "mVII"} 113 } 114 }, 115 }; 116 } 117 118 @Test(dataProvider = "testrequires") 119 public void testrequires(String name, String[][] expected) throws Exception { 120 String cmd1 = String.format("jdeps --inverse --module-path %s --require %s --add-modules %s%n", 121 MODS_DIR, name, modules.stream().collect(Collectors.joining(","))); 122 123 try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd1)) { 124 jdeps.appModulePath(MODS_DIR.toString()) 125 .addmods(modules) 126 .requires(Set.of(name)); 127 129 } 130 131 String cmd2 = String.format("jdeps --inverse --module-path %s --require %s" + 132 " --add-modules ALL-MODULE-PATH%n", LIBS_DIR, name); 133 134 // automatic module 135 try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd2)) { 136 jdeps.appModulePath(MODS_DIR.toString()) 137 .addmods(Set.of("ALL-MODULE-PATH")) 138 .requires(Set.of(name)); 139 140 runJdeps(jdeps, expected); 141 } 142 } 143 144 @DataProvider(name = "testpackage") 145 public Object[][] expected2() { 146 return new Object[][] { 147 // -package and result 148 { "p4", new String[][] { 149 new String[] { "m4", "m5"}, 150 } 151 }, 152 { "javax.tools", new String[][] { 153 new String[] {"java.compiler", "m5"}, 154 new String[] {"java.compiler", "m4", "m5"}, 155 } 156 }, 157 { "sun.misc", new String[][] { 158 new String[] {"jdk.unsupported", "unsafe", "mVI", "mVII"}, 159 new String[] {"jdk.unsupported", "unsafe", "mVII"} 160 } 161 } 162 }; 163 } 164 165 @Test(dataProvider = "testpackage") 166 public void testpackage(String name, String[][] expected) throws Exception { 167 String cmd = String.format("jdeps --inverse --module-path %s -package %s --add-modules %s%n", 168 MODS_DIR, name, modules.stream().collect(Collectors.joining(","))); 169 try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd)) { 170 jdeps.appModulePath(MODS_DIR.toString()) 171 .addmods(modules) 172 .matchPackages(Set.of(name)); 173 174 runJdeps(jdeps, expected); 175 } 176 } 177 178 @DataProvider(name = "testregex") 179 public Object[][] expected3() { 180 return new Object[][] { 181 // -regex and result 182 { "org.safe.Lib", new String[][] { 183 new String[] { "unsafe", "mVII"}, 184 new String[] { "unsafe", "mVI", "mVII"}, 185 } 186 }, 187 { "java.util.logging.*|org.safe.Lib", new String[][] { 188 new String[] { "unsafe", "mVII"}, 189 new String[] { "unsafe", "mVI", "mVII"}, 190 new String[] { "java.logging", "m5"}, 191 } 192 } 193 }; 194 } 195 196 @Test(dataProvider = "testregex") 197 public void testregex(String name, String[][] expected) throws Exception { 198 String cmd = String.format("jdeps --inverse --module-path %s -regex %s --add-modules %s%n", 199 MODS_DIR, name, modules.stream().collect(Collectors.joining(","))); 200 201 try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd)) { 202 jdeps.appModulePath(MODS_DIR.toString()) 203 .addmods(modules) 204 .regex(name); 205 206 runJdeps(jdeps, expected); 207 } 208 } 209 210 @DataProvider(name = "classpath") 211 public Object[][] expected4() { 212 return new Object[][] { 213 // -regex and result 214 { "sun.misc.Unsafe", new String[][] { 215 new String[] {"jdk.unsupported", "unsafe.jar", "mVI.jar", "mVII.jar"}, 216 new String[] {"jdk.unsupported", "unsafe.jar", "mVII.jar"} 217 } 218 }, 219 { "org.safe.Lib", new String[][] { 220 new String[] { "unsafe.jar", "mVII.jar"}, 221 new String[] { "unsafe.jar", "mVI.jar", "mVII.jar"}, 222 } 223 }, 224 { "java.util.logging.*|org.safe.Lib", new String[][] { 225 new String[] { "unsafe.jar", "mVII.jar"}, 226 new String[] { "unsafe.jar", "mVI.jar", "mVII.jar"}, 227 new String[] { "java.logging", "m5.jar"}, 228 } 229 } 230 }; 231 } 232 233 @Test(dataProvider = "classpath") 234 public void testClassPath(String name, String[][] expected) throws Exception { 235 // -classpath 236 String cpath = modules.stream() 237 .filter(mn -> !mn.equals("mVII")) 238 .map(mn -> LIBS_DIR.resolve(mn + ".jar").toString()) 239 .collect(Collectors.joining(File.pathSeparator)); 240 241 Path jarfile = LIBS_DIR.resolve("mVII.jar"); 242 243 String cmd1 = String.format("jdeps --inverse -classpath %s -regex %s %s%n", 244 cpath, name, jarfile); 245 try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd1)) { 246 jdeps.verbose("-verbose:class") 247 .addClassPath(cpath) | 44 import com.sun.tools.jdeps.Archive; 45 import com.sun.tools.jdeps.InverseDepsAnalyzer; 46 import org.testng.annotations.BeforeTest; 47 import org.testng.annotations.DataProvider; 48 import org.testng.annotations.Test; 49 50 import static org.testng.Assert.assertTrue; 51 import static org.testng.Assert.assertFalse; 52 import static org.testng.Assert.assertEquals; 53 54 55 public class InverseDeps { 56 private static final String TEST_SRC = System.getProperty("test.src"); 57 private static final String TEST_CLASSES = System.getProperty("test.classes"); 58 59 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); 60 private static final Path MODS_DIR = Paths.get("mods"); 61 private static final Path LIBS_DIR = Paths.get("libs"); 62 63 private static final Set<String> modules = new LinkedHashSet( 64 List.of("unsafe", "mIV", "mV", "mVI", "mVII") 65 ); 66 67 /** 68 * Compiles classes used by the test 69 */ 70 @BeforeTest 71 public void compileAll() throws Exception { 72 CompilerUtils.cleanDir(MODS_DIR); 73 74 for (String mn : modules) { 75 // compile a module 76 assertTrue(CompilerUtils.compileModule(SRC_DIR, MODS_DIR, mn)); 77 78 // create JAR files with no module-info.class 79 Path root = MODS_DIR.resolve(mn); 80 81 try (Stream<Path> stream = Files.walk(root, Integer.MAX_VALUE)) { 82 Stream<Path> entries = stream.filter(f -> { 83 String fn = f.getFileName().toString(); 84 return fn.endsWith(".class") && !fn.equals("module-info.class"); 85 }); 86 JdepsUtil.createJar(LIBS_DIR.resolve(mn + ".jar"), root, entries); 87 } 88 } 89 } 90 91 @DataProvider(name = "testrequires") 92 public Object[][] expected1() { 93 return new Object[][] { 94 // --require and result 95 { "java.sql", new String[][] { 96 new String[] { "java.sql", "mV" }, 97 } 98 }, 99 { "java.compiler", new String[][] { 100 new String[] { "java.compiler", "mV" }, 101 new String[] { "java.compiler", "mIV", "mV" }, 102 } 103 }, 104 { "java.logging", new String[][]{ 105 new String[] {"java.logging", "mV"}, 106 new String[] {"java.logging", "mIV", "mV"}, 107 new String[] {"java.logging", "java.sql", "mV"}, 108 } 109 }, 110 { "jdk.unsupported", new String[][] { 111 new String[] {"jdk.unsupported", "unsafe", "mVI", "mVII"}, 112 new String[] {"jdk.unsupported", "unsafe", "mVII"} 113 } 114 }, 115 }; 116 } 117 118 @Test(dataProvider = "testrequires") 119 public void testrequires(String name, String[][] expected) throws Exception { 120 String cmd1 = String.format("jdeps --inverse --module-path %s --require %s --add-modules %s%n", 121 MODS_DIR, name, modules.stream().collect(Collectors.joining(","))); 122 123 try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd1)) { 124 jdeps.appModulePath(MODS_DIR.toString()) 125 .addmods(modules) 126 .requires(Set.of(name)); 127 129 } 130 131 String cmd2 = String.format("jdeps --inverse --module-path %s --require %s" + 132 " --add-modules ALL-MODULE-PATH%n", LIBS_DIR, name); 133 134 // automatic module 135 try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd2)) { 136 jdeps.appModulePath(MODS_DIR.toString()) 137 .addmods(Set.of("ALL-MODULE-PATH")) 138 .requires(Set.of(name)); 139 140 runJdeps(jdeps, expected); 141 } 142 } 143 144 @DataProvider(name = "testpackage") 145 public Object[][] expected2() { 146 return new Object[][] { 147 // -package and result 148 { "p4", new String[][] { 149 new String[] { "mIV", "mV"}, 150 } 151 }, 152 { "javax.tools", new String[][] { 153 new String[] {"java.compiler", "mV"}, 154 new String[] {"java.compiler", "mIV", "mV"}, 155 } 156 }, 157 { "sun.misc", new String[][] { 158 new String[] {"jdk.unsupported", "unsafe", "mVI", "mVII"}, 159 new String[] {"jdk.unsupported", "unsafe", "mVII"} 160 } 161 } 162 }; 163 } 164 165 @Test(dataProvider = "testpackage") 166 public void testpackage(String name, String[][] expected) throws Exception { 167 String cmd = String.format("jdeps --inverse --module-path %s -package %s --add-modules %s%n", 168 MODS_DIR, name, modules.stream().collect(Collectors.joining(","))); 169 try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd)) { 170 jdeps.appModulePath(MODS_DIR.toString()) 171 .addmods(modules) 172 .matchPackages(Set.of(name)); 173 174 runJdeps(jdeps, expected); 175 } 176 } 177 178 @DataProvider(name = "testregex") 179 public Object[][] expected3() { 180 return new Object[][] { 181 // -regex and result 182 { "org.safe.Lib", new String[][] { 183 new String[] { "unsafe", "mVII"}, 184 new String[] { "unsafe", "mVI", "mVII"}, 185 } 186 }, 187 { "java.util.logging.*|org.safe.Lib", new String[][] { 188 new String[] { "unsafe", "mVII"}, 189 new String[] { "unsafe", "mVI", "mVII"}, 190 new String[] { "java.logging", "mV"}, 191 } 192 } 193 }; 194 } 195 196 @Test(dataProvider = "testregex") 197 public void testregex(String name, String[][] expected) throws Exception { 198 String cmd = String.format("jdeps --inverse --module-path %s -regex %s --add-modules %s%n", 199 MODS_DIR, name, modules.stream().collect(Collectors.joining(","))); 200 201 try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd)) { 202 jdeps.appModulePath(MODS_DIR.toString()) 203 .addmods(modules) 204 .regex(name); 205 206 runJdeps(jdeps, expected); 207 } 208 } 209 210 @DataProvider(name = "classpath") 211 public Object[][] expected4() { 212 return new Object[][] { 213 // -regex and result 214 { "sun.misc.Unsafe", new String[][] { 215 new String[] {"jdk.unsupported", "unsafe.jar", "mVI.jar", "mVII.jar"}, 216 new String[] {"jdk.unsupported", "unsafe.jar", "mVII.jar"} 217 } 218 }, 219 { "org.safe.Lib", new String[][] { 220 new String[] { "unsafe.jar", "mVII.jar"}, 221 new String[] { "unsafe.jar", "mVI.jar", "mVII.jar"}, 222 } 223 }, 224 { "java.util.logging.*|org.safe.Lib", new String[][] { 225 new String[] { "unsafe.jar", "mVII.jar"}, 226 new String[] { "unsafe.jar", "mVI.jar", "mVII.jar"}, 227 new String[] { "java.logging", "mV.jar"}, 228 } 229 } 230 }; 231 } 232 233 @Test(dataProvider = "classpath") 234 public void testClassPath(String name, String[][] expected) throws Exception { 235 // -classpath 236 String cpath = modules.stream() 237 .filter(mn -> !mn.equals("mVII")) 238 .map(mn -> LIBS_DIR.resolve(mn + ".jar").toString()) 239 .collect(Collectors.joining(File.pathSeparator)); 240 241 Path jarfile = LIBS_DIR.resolve("mVII.jar"); 242 243 String cmd1 = String.format("jdeps --inverse -classpath %s -regex %s %s%n", 244 cpath, name, jarfile); 245 try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd1)) { 246 jdeps.verbose("-verbose:class") 247 .addClassPath(cpath) |