test/tools/jdeps/Basic.java

Thu, 22 Dec 2016 16:21:25 -0800

author
asaha
date
Thu, 22 Dec 2016 16:21:25 -0800
changeset 3347
46a7edb68d0a
parent 2802
6b43535fb9f8
child 2893
ca5783d9a597
permissions
-rw-r--r--

Merge

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

mercurial