Tue, 08 May 2018 12:00:20 -0700
Merge
1 /*
2 * Copyright (c) 2012, 2014, 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 */
24 /*
25 * @test
26 * @bug 8003562 8005428 8015912 8027481 8048063 8068937
27 * @summary Basic tests for jdeps tool
28 * @build Test p.Foo p.Bar p.C p.SubClass q.Gee javax.activity.NotCompactProfile
29 * @run main Basic
30 */
32 import java.io.File;
33 import java.io.IOException;
34 import java.io.PrintWriter;
35 import java.io.StringWriter;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
39 import java.util.*;
40 import java.util.regex.*;
41 import static java.nio.file.StandardCopyOption.*;
43 public class Basic {
44 private static boolean symbolFileExist = initProfiles();
45 private static boolean initProfiles() {
46 // check if ct.sym exists; if not use the profiles.properties file
47 Path home = Paths.get(System.getProperty("java.home"));
48 if (home.endsWith("jre")) {
49 home = home.getParent();
50 }
51 Path ctsym = home.resolve("lib").resolve("ct.sym");
52 boolean symbolExists = ctsym.toFile().exists();
53 if (!symbolExists) {
54 Path testSrcProfiles =
55 Paths.get(System.getProperty("test.src", "."), "profiles.properties");
56 if (!testSrcProfiles.toFile().exists())
57 throw new Error(testSrcProfiles + " does not exist");
58 System.out.format("%s doesn't exist.%nUse %s to initialize profiles info%n",
59 ctsym, testSrcProfiles);
60 System.setProperty("jdeps.profiles", testSrcProfiles.toString());
61 }
62 return symbolExists;
63 }
65 public static void main(String... args) throws Exception {
66 int errors = 0;
67 errors += new Basic().run();
68 if (errors > 0)
69 throw new Exception(errors + " errors found");
70 }
72 int run() throws IOException {
73 File testDir = new File(System.getProperty("test.classes", "."));
74 // test a .class file
75 test(new File(testDir, "Test.class"),
76 new String[] {"java.lang", "p"},
77 new String[] {"compact1", "not found"});
78 // test a directory
79 // also test non-SE javax.activity class dependency
80 test(new File(testDir, "p"),
81 new String[] {"java.lang", "java.util", "java.lang.management", "javax.activity", "javax.crypto"},
82 new String[] {"compact1", "compact1", "compact3", testDir.getName(), "compact1"},
83 new String[] {"-classpath", testDir.getPath()});
84 // test class-level dependency output
85 test(new File(testDir, "Test.class"),
86 new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"},
87 new String[] {"compact1", "compact1", "not found", "not found"},
88 new String[] {"-verbose:class"});
89 // test -filter:none option
90 test(new File(testDir, "p"),
91 new String[] {"java.lang", "java.util", "java.lang.management", "javax.activity", "javax.crypto", "p"},
92 new String[] {"compact1", "compact1", "compact3", testDir.getName(), "compact1", "p"},
93 new String[] {"-classpath", testDir.getPath(), "-verbose:package", "-filter:none"});
94 // test -filter:archive option
95 test(new File(testDir, "p"),
96 new String[] {"java.lang", "java.util", "java.lang.management", "javax.activity", "javax.crypto"},
97 new String[] {"compact1", "compact1", "compact3", testDir.getName(), "compact1"},
98 new String[] {"-classpath", testDir.getPath(), "-verbose:package", "-filter:archive"});
99 // test -p option
100 test(new File(testDir, "Test.class"),
101 new String[] {"p.Foo", "p.Bar"},
102 new String[] {"not found", "not found"},
103 new String[] {"-verbose:class", "-p", "p"});
104 // test -e option
105 test(new File(testDir, "Test.class"),
106 new String[] {"p.Foo", "p.Bar"},
107 new String[] {"not found", "not found"},
108 new String[] {"-verbose:class", "-e", "p\\..*"});
109 test(new File(testDir, "Test.class"),
110 new String[] {"java.lang"},
111 new String[] {"compact1"},
112 new String[] {"-verbose:package", "-e", "java\\.lang\\..*"});
114 // parse p.C, p.SubClass and q.*
115 // p.SubClass have no dependency other than p.C
116 // q.Gee depends on p.SubClass that should be found
117 test(testDir,
118 new String[] {"java.lang", "p"},
119 new String[] {"compact1", testDir.getName()},
120 new String[] {"-include", "p.C|p.SubClass|q\\..*"});
121 test(testDir,
122 new String[] {"java.lang", "p"},
123 new String[] {"compact1", testDir.getName()},
124 new String[] {"-classpath", testDir.getPath(), "-include", "p.C|p.SubClass|q\\..*"});
127 // test -classpath and -include options
128 test(null,
129 new String[] {"java.lang", "java.util", "java.lang.management",
130 "javax.activity", "javax.crypto"},
131 new String[] {"compact1", "compact1", "compact3", testDir.getName(), "compact1"},
132 new String[] {"-classpath", testDir.getPath(), "-include", "p.+|Test.class"});
133 test(new File(testDir, "Test.class"),
134 new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"},
135 new String[] {"compact1", "compact1", testDir.getName(), testDir.getName()},
136 new String[] {"-v", "-classpath", testDir.getPath(), "Test.class"});
138 // split package p - move p/Foo.class to dir1 and p/Bar.class to dir2
139 Path testClassPath = testDir.toPath();
140 Path dirP = testClassPath.resolve("p");
141 Path dir1 = testClassPath.resolve("dir1");
142 Path subdir1P = dir1.resolve("p");
143 Path dir2 = testClassPath.resolve("dir2");
144 Path subdir2P = dir2.resolve("p");
145 if (!Files.exists(subdir1P))
146 Files.createDirectories(subdir1P);
147 if (!Files.exists(subdir2P))
148 Files.createDirectories(subdir2P);
149 Files.move(dirP.resolve("Foo.class"), subdir1P.resolve("Foo.class"), REPLACE_EXISTING);
150 Files.move(dirP.resolve("Bar.class"), subdir2P.resolve("Bar.class"), REPLACE_EXISTING);
151 StringBuilder cpath = new StringBuilder(testDir.toString());
152 cpath.append(File.pathSeparator).append(dir1.toString());
153 cpath.append(File.pathSeparator).append(dir2.toString());
154 test(new File(testDir, "Test.class"),
155 new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"},
156 new String[] {"compact1", "compact1", dir1.toFile().getName(), dir2.toFile().getName()},
157 new String[] {"-v", "-classpath", cpath.toString(), "Test.class"});
158 return errors;
159 }
161 void test(File file, String[] expect, String[] profiles) {
162 test(file, expect, profiles, new String[0]);
163 }
165 void test(File file, String[] expect, String[] profiles, String[] options) {
166 List<String> args = new ArrayList<>(Arrays.asList(options));
167 if (file != null) {
168 args.add(file.getPath());
169 }
170 List<String> argsWithDashP = new ArrayList<>();
171 argsWithDashP.add("-P");
172 argsWithDashP.addAll(args);
173 // test without -P
174 checkResult("dependencies", expect, jdeps(args.toArray(new String[0])).keySet());
175 // test with -P
176 checkResult("profiles", expect, profiles, jdeps(argsWithDashP.toArray(new String[0])));
177 }
179 Map<String,String> jdeps(String... args) {
180 StringWriter sw = new StringWriter();
181 PrintWriter pw = new PrintWriter(sw);
182 System.err.println("jdeps " + Arrays.toString(args));
183 int rc = com.sun.tools.jdeps.Main.run(args, pw);
184 pw.close();
185 String out = sw.toString();
186 if (!out.isEmpty())
187 System.err.println(out);
188 if (rc != 0)
189 throw new Error("jdeps failed: rc=" + rc);
190 return findDeps(out);
191 }
193 // Pattern used to parse lines
194 private static Pattern linePattern = Pattern.compile(".*\r?\n");
195 private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)");
197 // Use the linePattern to break the given String into lines, applying
198 // the pattern to each line to see if we have a match
199 private static Map<String,String> findDeps(String out) {
200 Map<String,String> result = new LinkedHashMap<>();
201 Matcher lm = linePattern.matcher(out); // Line matcher
202 Matcher pm = null; // Pattern matcher
203 int lines = 0;
204 while (lm.find()) {
205 lines++;
206 CharSequence cs = lm.group(); // The current line
207 if (pm == null)
208 pm = pattern.matcher(cs);
209 else
210 pm.reset(cs);
211 if (pm.find())
212 result.put(pm.group(1), pm.group(2).trim());
213 if (lm.end() == out.length())
214 break;
215 }
216 return result;
217 }
219 void checkResult(String label, String[] expect, Collection<String> found) {
220 List<String> list = Arrays.asList(expect);
221 if (!isEqual(list, found))
222 error("Unexpected " + label + " found: '" + found + "', expected: '" + list + "'");
223 }
225 void checkResult(String label, String[] expect, String[] profiles, Map<String,String> result) {
226 if (expect.length != profiles.length)
227 error("Invalid expected names and profiles");
229 // check the dependencies
230 checkResult(label, expect, result.keySet());
231 // check profile information
232 checkResult(label, profiles, result.values());
233 for (int i=0; i < expect.length; i++) {
234 String profile = result.get(expect[i]);
235 if (!profile.equals(profiles[i]))
236 error("Unexpected profile: '" + profile + "', expected: '" + profiles[i] + "'");
237 }
238 }
240 boolean isEqual(List<String> expected, Collection<String> found) {
241 if (expected.size() != found.size())
242 return false;
244 List<String> list = new ArrayList<>(found);
245 list.removeAll(expected);
246 return list.isEmpty();
247 }
249 void error(String msg) {
250 System.err.println("Error: " + msg);
251 errors++;
252 }
254 int errors;
255 }