test/tools/jdeps/Basic.java

Mon, 16 Oct 2017 16:07:48 +0800

author
aoqi
date
Mon, 16 Oct 2017 16:07:48 +0800
changeset 2893
ca5783d9a597
parent 2802
6b43535fb9f8
parent 2702
9ca8d8713094
permissions
-rw-r--r--

merge

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

mercurial