Basic.java revision 3294:9adfb22ff08f
1/* 2 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @bug 8003562 8005428 8015912 8027481 8048063 8068937 27 * @summary Basic tests for jdeps tool 28 * @modules java.management 29 * jdk.jdeps/com.sun.tools.jdeps 30 * @build Test p.Foo p.Bar p.C p.SubClass q.Gee 31 * @run main Basic 32 */ 33 34import java.io.File; 35import java.io.IOException; 36import java.io.PrintWriter; 37import java.io.StringWriter; 38import java.nio.file.Files; 39import java.nio.file.Path; 40import java.util.*; 41import java.util.regex.*; 42import static java.nio.file.StandardCopyOption.*; 43 44public class Basic { 45 public static void main(String... args) throws Exception { 46 int errors = 0; 47 errors += new Basic().run(); 48 if (errors > 0) 49 throw new Exception(errors + " errors found"); 50 } 51 52 int run() throws IOException { 53 File testDir = new File(System.getProperty("test.classes", ".")); 54 // test a .class file 55 test(new File(testDir, "Test.class"), 56 new String[] {"java.lang", "p"}, 57 new String[] {"compact1", "not found"}); 58 // test a directory 59 test(new File(testDir, "p"), 60 new String[] {"java.lang", "java.util", "java.lang.management", "javax.crypto"}, 61 new String[] {"compact1", "compact1", "compact3", "compact1"}, 62 new String[] {"-classpath", testDir.getPath()}); 63 // test class-level dependency output 64 test(new File(testDir, "Test.class"), 65 new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"}, 66 new String[] {"compact1", "compact1", "not found", "not found"}, 67 new String[] {"-verbose:class"}); 68 // test -filter:none option 69 test(new File(testDir, "p"), 70 new String[] {"java.lang", "java.util", "java.lang.management", "javax.crypto", "p"}, 71 new String[] {"compact1", "compact1", "compact3", "compact1", "p"}, 72 new String[] {"-classpath", testDir.getPath(), "-verbose:package", "-filter:none"}); 73 // test -filter:archive option 74 test(new File(testDir, "p"), 75 new String[] {"java.lang", "java.util", "java.lang.management", "javax.crypto"}, 76 new String[] {"compact1", "compact1", "compact3", "compact1"}, 77 new String[] {"-classpath", testDir.getPath(), "-verbose:package", "-filter:archive"}); 78 // test -p option 79 test(new File(testDir, "Test.class"), 80 new String[] {"p.Foo", "p.Bar"}, 81 new String[] {"not found", "not found"}, 82 new String[] {"-verbose:class", "-p", "p"}); 83 // test -e option 84 test(new File(testDir, "Test.class"), 85 new String[] {"p.Foo", "p.Bar"}, 86 new String[] {"not found", "not found"}, 87 new String[] {"-verbose:class", "-e", "p\\..*"}); 88 test(new File(testDir, "Test.class"), 89 new String[] {"java.lang"}, 90 new String[] {"compact1"}, 91 new String[] {"-verbose:package", "-e", "java\\.lang\\..*"}); 92 93 // parse p.C, p.SubClass and q.* 94 // p.SubClass have no dependency other than p.C 95 // q.Gee depends on p.SubClass that should be found 96 test(testDir, 97 new String[] {"java.lang", "p"}, 98 new String[] {"compact1", testDir.getName()}, 99 new String[] {"-include", "p.C|p.SubClass|q\\..*"}); 100 test(testDir, 101 new String[] {"java.lang", "p"}, 102 new String[] {"compact1", testDir.getName()}, 103 new String[] {"-classpath", testDir.getPath(), "-include", "p.C|p.SubClass|q\\..*"}); 104 105 // test -classpath and -include options 106 test(null, 107 new String[] {"java.lang", "java.util", "java.lang.management", 108 "javax.crypto"}, 109 new String[] {"compact1", "compact1", "compact3", "compact1"}, 110 new String[] {"-classpath", testDir.getPath(), "-include", "p.+|Test.class"}); 111 test(new File(testDir, "Test.class"), 112 new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"}, 113 new String[] {"compact1", "compact1", testDir.getName(), testDir.getName()}, 114 new String[] {"-v", "-classpath", testDir.getPath(), "Test.class"}); 115 116 // split package p - move p/Foo.class to dir1 and p/Bar.class to dir2 117 Path testClassPath = testDir.toPath(); 118 Path dirP = testClassPath.resolve("p"); 119 Path dir1 = testClassPath.resolve("dir1"); 120 Path subdir1P = dir1.resolve("p"); 121 Path dir2 = testClassPath.resolve("dir2"); 122 Path subdir2P = dir2.resolve("p"); 123 if (!Files.exists(subdir1P)) 124 Files.createDirectories(subdir1P); 125 if (!Files.exists(subdir2P)) 126 Files.createDirectories(subdir2P); 127 Files.move(dirP.resolve("Foo.class"), subdir1P.resolve("Foo.class"), REPLACE_EXISTING); 128 Files.move(dirP.resolve("Bar.class"), subdir2P.resolve("Bar.class"), REPLACE_EXISTING); 129 StringBuilder cpath = new StringBuilder(testDir.toString()); 130 cpath.append(File.pathSeparator).append(dir1.toString()); 131 cpath.append(File.pathSeparator).append(dir2.toString()); 132 test(new File(testDir, "Test.class"), 133 new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"}, 134 new String[] {"compact1", "compact1", dir1.toFile().getName(), dir2.toFile().getName()}, 135 new String[] {"-v", "-classpath", cpath.toString(), "Test.class"}); 136 return errors; 137 } 138 139 void test(File file, String[] expect, String[] profiles) { 140 test(file, expect, profiles, new String[0]); 141 } 142 143 void test(File file, String[] expect, String[] profiles, String[] options) { 144 List<String> args = new ArrayList<>(Arrays.asList(options)); 145 if (file != null) { 146 args.add(file.getPath()); 147 } 148 List<String> argsWithDashP = new ArrayList<>(); 149 argsWithDashP.add("-P"); 150 argsWithDashP.addAll(args); 151 // test without -P 152 checkResult("dependencies", expect, jdeps(args.toArray(new String[0])).keySet()); 153 // test with -P 154 checkResult("profiles", expect, profiles, jdeps(argsWithDashP.toArray(new String[0]))); 155 } 156 157 Map<String,String> jdeps(String... args) { 158 StringWriter sw = new StringWriter(); 159 PrintWriter pw = new PrintWriter(sw); 160 System.err.println("jdeps " + Arrays.toString(args)); 161 int rc = com.sun.tools.jdeps.Main.run(args, pw); 162 pw.close(); 163 String out = sw.toString(); 164 if (!out.isEmpty()) 165 System.err.println(out); 166 if (rc != 0) 167 throw new Error("jdeps failed: rc=" + rc); 168 return findDeps(out); 169 } 170 171 // Pattern used to parse lines 172 private static Pattern linePattern = Pattern.compile(".*\r?\n"); 173 private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)"); 174 175 // Use the linePattern to break the given String into lines, applying 176 // the pattern to each line to see if we have a match 177 private static Map<String,String> findDeps(String out) { 178 Map<String,String> result = new LinkedHashMap<>(); 179 Matcher lm = linePattern.matcher(out); // Line matcher 180 Matcher pm = null; // Pattern matcher 181 int lines = 0; 182 while (lm.find()) { 183 lines++; 184 CharSequence cs = lm.group(); // The current line 185 if (pm == null) 186 pm = pattern.matcher(cs); 187 else 188 pm.reset(cs); 189 if (pm.find()) 190 result.put(pm.group(1), pm.group(2).trim()); 191 if (lm.end() == out.length()) 192 break; 193 } 194 return result; 195 } 196 197 void checkResult(String label, String[] expect, Collection<String> found) { 198 List<String> list = Arrays.asList(expect); 199 if (!isEqual(list, found)) 200 error("Unexpected " + label + " found: '" + found + "', expected: '" + list + "'"); 201 } 202 203 void checkResult(String label, String[] expect, String[] profiles, Map<String,String> result) { 204 if (expect.length != profiles.length) 205 error("Invalid expected names and profiles"); 206 207 // check the dependencies 208 checkResult(label, expect, result.keySet()); 209 // check profile information 210 checkResult(label, profiles, result.values()); 211 for (int i=0; i < expect.length; i++) { 212 String profile = result.get(expect[i]); 213 if (!profile.equals(profiles[i])) 214 error("Unexpected profile: '" + profile + "', expected: '" + profiles[i] + "'"); 215 } 216 } 217 218 boolean isEqual(List<String> expected, Collection<String> found) { 219 if (expected.size() != found.size()) 220 return false; 221 222 List<String> list = new ArrayList<>(found); 223 list.removeAll(expected); 224 return list.isEmpty(); 225 } 226 227 void error(String msg) { 228 System.err.println("Error: " + msg); 229 errors++; 230 } 231 232 int errors; 233} 234