Mon, 20 Apr 2015 13:41:05 -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
27 * @summary Basic tests for jdeps tool
28 * @build Test p.Foo p.Bar 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 // test -classpath and -include options
115 test(null,
116 new String[] {"java.lang", "java.util", "java.lang.management",
117 "javax.activity", "javax.crypto"},
118 new String[] {"compact1", "compact1", "compact3", testDir.getName(), "compact1"},
119 new String[] {"-classpath", testDir.getPath(), "-include", "p.+|Test.class"});
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", testDir.getName(), testDir.getName()},
123 new String[] {"-v", "-classpath", testDir.getPath(), "Test.class"});
125 // split package p - move p/Foo.class to dir1 and p/Bar.class to dir2
126 Path testClassPath = testDir.toPath();
127 Path dirP = testClassPath.resolve("p");
128 Path dir1 = testClassPath.resolve("dir1");
129 Path subdir1P = dir1.resolve("p");
130 Path dir2 = testClassPath.resolve("dir2");
131 Path subdir2P = dir2.resolve("p");
132 if (!Files.exists(subdir1P))
133 Files.createDirectories(subdir1P);
134 if (!Files.exists(subdir2P))
135 Files.createDirectories(subdir2P);
136 Files.move(dirP.resolve("Foo.class"), subdir1P.resolve("Foo.class"), REPLACE_EXISTING);
137 Files.move(dirP.resolve("Bar.class"), subdir2P.resolve("Bar.class"), REPLACE_EXISTING);
138 StringBuilder cpath = new StringBuilder(testDir.toString());
139 cpath.append(File.pathSeparator).append(dir1.toString());
140 cpath.append(File.pathSeparator).append(dir2.toString());
141 test(new File(testDir, "Test.class"),
142 new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"},
143 new String[] {"compact1", "compact1", dir1.toFile().getName(), dir2.toFile().getName()},
144 new String[] {"-v", "-classpath", cpath.toString(), "Test.class"});
145 return errors;
146 }
148 void test(File file, String[] expect, String[] profiles) {
149 test(file, expect, profiles, new String[0]);
150 }
152 void test(File file, String[] expect, String[] profiles, String[] options) {
153 List<String> args = new ArrayList<>(Arrays.asList(options));
154 if (file != null) {
155 args.add(file.getPath());
156 }
157 List<String> argsWithDashP = new ArrayList<>();
158 argsWithDashP.add("-P");
159 argsWithDashP.addAll(args);
160 // test without -P
161 checkResult("dependencies", expect, jdeps(args.toArray(new String[0])).keySet());
162 // test with -P
163 checkResult("profiles", expect, profiles, jdeps(argsWithDashP.toArray(new String[0])));
164 }
166 Map<String,String> jdeps(String... args) {
167 StringWriter sw = new StringWriter();
168 PrintWriter pw = new PrintWriter(sw);
169 System.err.println("jdeps " + Arrays.toString(args));
170 int rc = com.sun.tools.jdeps.Main.run(args, pw);
171 pw.close();
172 String out = sw.toString();
173 if (!out.isEmpty())
174 System.err.println(out);
175 if (rc != 0)
176 throw new Error("jdeps failed: rc=" + rc);
177 return findDeps(out);
178 }
180 // Pattern used to parse lines
181 private static Pattern linePattern = Pattern.compile(".*\r?\n");
182 private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)");
184 // Use the linePattern to break the given String into lines, applying
185 // the pattern to each line to see if we have a match
186 private static Map<String,String> findDeps(String out) {
187 Map<String,String> result = new LinkedHashMap<>();
188 Matcher lm = linePattern.matcher(out); // Line matcher
189 Matcher pm = null; // Pattern matcher
190 int lines = 0;
191 while (lm.find()) {
192 lines++;
193 CharSequence cs = lm.group(); // The current line
194 if (pm == null)
195 pm = pattern.matcher(cs);
196 else
197 pm.reset(cs);
198 if (pm.find())
199 result.put(pm.group(1), pm.group(2).trim());
200 if (lm.end() == out.length())
201 break;
202 }
203 return result;
204 }
206 void checkResult(String label, String[] expect, Collection<String> found) {
207 List<String> list = Arrays.asList(expect);
208 if (!isEqual(list, found))
209 error("Unexpected " + label + " found: '" + found + "', expected: '" + list + "'");
210 }
212 void checkResult(String label, String[] expect, String[] profiles, Map<String,String> result) {
213 if (expect.length != profiles.length)
214 error("Invalid expected names and profiles");
216 // check the dependencies
217 checkResult(label, expect, result.keySet());
218 // check profile information
219 checkResult(label, profiles, result.values());
220 for (int i=0; i < expect.length; i++) {
221 String profile = result.get(expect[i]);
222 if (!profile.equals(profiles[i]))
223 error("Unexpected profile: '" + profile + "', expected: '" + profiles[i] + "'");
224 }
225 }
227 boolean isEqual(List<String> expected, Collection<String> found) {
228 if (expected.size() != found.size())
229 return false;
231 List<String> list = new ArrayList<>(found);
232 list.removeAll(expected);
233 return list.isEmpty();
234 }
236 void error(String msg) {
237 System.err.println("Error: " + msg);
238 errors++;
239 }
241 int errors;
242 }