Basic.java revision 2721:f7ce2cfa4cdb
1121934Sharti/* 2121934Sharti * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. 3121934Sharti * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4121934Sharti * 5121934Sharti * This code is free software; you can redistribute it and/or modify it 6121934Sharti * under the terms of the GNU General Public License version 2 only, as 7121934Sharti * published by the Free Software Foundation. 8121934Sharti * 9121934Sharti * This code is distributed in the hope that it will be useful, but WITHOUT 10121934Sharti * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11121934Sharti * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12121934Sharti * version 2 for more details (a copy is included in the LICENSE file that 13121934Sharti * accompanied this code). 14121934Sharti * 15121934Sharti * You should have received a copy of the GNU General Public License version 16121934Sharti * 2 along with this work; if not, write to the Free Software Foundation, 17121934Sharti * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18121934Sharti * 19121934Sharti * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20121934Sharti * or visit www.oracle.com if you need additional information or have any 21121934Sharti * questions. 22121934Sharti */ 23121934Sharti 24121934Sharti/* 25121934Sharti * @test 26121934Sharti * @bug 8003562 8005428 8015912 8027481 8048063 27121934Sharti * @summary Basic tests for jdeps tool 28121934Sharti * @build Test p.Foo p.Bar javax.activity.NotCompactProfile 29131826Sharti * @run main Basic 30121934Sharti */ 31121934Sharti 32121934Shartiimport java.io.File; 33121934Shartiimport java.io.IOException; 34121934Shartiimport java.io.PrintWriter; 35121934Shartiimport java.io.StringWriter; 36121934Shartiimport java.nio.file.Files; 37121934Shartiimport java.nio.file.Path; 38121934Shartiimport java.nio.file.Paths; 39121934Shartiimport java.util.*; 40121934Shartiimport java.util.regex.*; 41121934Shartiimport static java.nio.file.StandardCopyOption.*; 42121934Sharti 43public class Basic { 44 public static void main(String... args) throws Exception { 45 int errors = 0; 46 errors += new Basic().run(); 47 if (errors > 0) 48 throw new Exception(errors + " errors found"); 49 } 50 51 int run() throws IOException { 52 File testDir = new File(System.getProperty("test.classes", ".")); 53 // test a .class file 54 test(new File(testDir, "Test.class"), 55 new String[] {"java.lang", "p"}, 56 new String[] {"compact1", "not found"}); 57 // test a directory 58 // also test non-SE javax.activity class dependency 59 test(new File(testDir, "p"), 60 new String[] {"java.lang", "java.util", "java.lang.management", "javax.activity", "javax.crypto"}, 61 new String[] {"compact1", "compact1", "compact3", testDir.getName(), "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.activity", "javax.crypto", "p"}, 71 new String[] {"compact1", "compact1", "compact3", testDir.getName(), "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.activity", "javax.crypto"}, 76 new String[] {"compact1", "compact1", "compact3", testDir.getName(), "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 // test -classpath and -include options 94 test(null, 95 new String[] {"java.lang", "java.util", "java.lang.management", 96 "javax.activity", "javax.crypto"}, 97 new String[] {"compact1", "compact1", "compact3", testDir.getName(), "compact1"}, 98 new String[] {"-classpath", testDir.getPath(), "-include", "p.+|Test.class"}); 99 test(new File(testDir, "Test.class"), 100 new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"}, 101 new String[] {"compact1", "compact1", testDir.getName(), testDir.getName()}, 102 new String[] {"-v", "-classpath", testDir.getPath(), "Test.class"}); 103 104 // split package p - move p/Foo.class to dir1 and p/Bar.class to dir2 105 Path testClassPath = testDir.toPath(); 106 Path dirP = testClassPath.resolve("p"); 107 Path dir1 = testClassPath.resolve("dir1"); 108 Path subdir1P = dir1.resolve("p"); 109 Path dir2 = testClassPath.resolve("dir2"); 110 Path subdir2P = dir2.resolve("p"); 111 if (!Files.exists(subdir1P)) 112 Files.createDirectories(subdir1P); 113 if (!Files.exists(subdir2P)) 114 Files.createDirectories(subdir2P); 115 Files.move(dirP.resolve("Foo.class"), subdir1P.resolve("Foo.class"), REPLACE_EXISTING); 116 Files.move(dirP.resolve("Bar.class"), subdir2P.resolve("Bar.class"), REPLACE_EXISTING); 117 StringBuilder cpath = new StringBuilder(testDir.toString()); 118 cpath.append(File.pathSeparator).append(dir1.toString()); 119 cpath.append(File.pathSeparator).append(dir2.toString()); 120 test(new File(testDir, "Test.class"), 121 new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"}, 122 new String[] {"compact1", "compact1", dir1.toFile().getName(), dir2.toFile().getName()}, 123 new String[] {"-v", "-classpath", cpath.toString(), "Test.class"}); 124 return errors; 125 } 126 127 void test(File file, String[] expect, String[] profiles) { 128 test(file, expect, profiles, new String[0]); 129 } 130 131 void test(File file, String[] expect, String[] profiles, String[] options) { 132 List<String> args = new ArrayList<>(Arrays.asList(options)); 133 if (file != null) { 134 args.add(file.getPath()); 135 } 136 List<String> argsWithDashP = new ArrayList<>(); 137 argsWithDashP.add("-P"); 138 argsWithDashP.addAll(args); 139 // test without -P 140 checkResult("dependencies", expect, jdeps(args.toArray(new String[0])).keySet()); 141 // test with -P 142 checkResult("profiles", expect, profiles, jdeps(argsWithDashP.toArray(new String[0]))); 143 } 144 145 Map<String,String> jdeps(String... args) { 146 StringWriter sw = new StringWriter(); 147 PrintWriter pw = new PrintWriter(sw); 148 System.err.println("jdeps " + Arrays.toString(args)); 149 int rc = com.sun.tools.jdeps.Main.run(args, pw); 150 pw.close(); 151 String out = sw.toString(); 152 if (!out.isEmpty()) 153 System.err.println(out); 154 if (rc != 0) 155 throw new Error("jdeps failed: rc=" + rc); 156 return findDeps(out); 157 } 158 159 // Pattern used to parse lines 160 private static Pattern linePattern = Pattern.compile(".*\r?\n"); 161 private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)"); 162 163 // Use the linePattern to break the given String into lines, applying 164 // the pattern to each line to see if we have a match 165 private static Map<String,String> findDeps(String out) { 166 Map<String,String> result = new LinkedHashMap<>(); 167 Matcher lm = linePattern.matcher(out); // Line matcher 168 Matcher pm = null; // Pattern matcher 169 int lines = 0; 170 while (lm.find()) { 171 lines++; 172 CharSequence cs = lm.group(); // The current line 173 if (pm == null) 174 pm = pattern.matcher(cs); 175 else 176 pm.reset(cs); 177 if (pm.find()) 178 result.put(pm.group(1), pm.group(2).trim()); 179 if (lm.end() == out.length()) 180 break; 181 } 182 return result; 183 } 184 185 void checkResult(String label, String[] expect, Collection<String> found) { 186 List<String> list = Arrays.asList(expect); 187 if (!isEqual(list, found)) 188 error("Unexpected " + label + " found: '" + found + "', expected: '" + list + "'"); 189 } 190 191 void checkResult(String label, String[] expect, String[] profiles, Map<String,String> result) { 192 if (expect.length != profiles.length) 193 error("Invalid expected names and profiles"); 194 195 // check the dependencies 196 checkResult(label, expect, result.keySet()); 197 // check profile information 198 checkResult(label, profiles, result.values()); 199 for (int i=0; i < expect.length; i++) { 200 String profile = result.get(expect[i]); 201 if (!profile.equals(profiles[i])) 202 error("Unexpected profile: '" + profile + "', expected: '" + profiles[i] + "'"); 203 } 204 } 205 206 boolean isEqual(List<String> expected, Collection<String> found) { 207 if (expected.size() != found.size()) 208 return false; 209 210 List<String> list = new ArrayList<>(found); 211 list.removeAll(expected); 212 return list.isEmpty(); 213 } 214 215 void error(String msg) { 216 System.err.println("Error: " + msg); 217 errors++; 218 } 219 220 int errors; 221} 222