author | akulyakh |
Thu, 21 May 2015 11:41:04 -0700 | |
changeset 30730 | d3ce7619db2c |
parent 27579 | d1a63c99cdd5 |
child 30846 | 2b3f379840f0 |
permissions | -rw-r--r-- |
21046 | 1 |
/* |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27579
diff
changeset
|
2 |
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. |
21046 | 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 |
*/ |
|
23 |
||
24 |
/* |
|
25 |
* @test |
|
25692
39537fdca12c
8050804: (jdeps) Recommend supported API to replace use of JDK internal API
mchung
parents:
25442
diff
changeset
|
26 |
* @bug 8015912 8029216 8048063 8050804 |
22007
907f7054db16
8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents:
21046
diff
changeset
|
27 |
* @summary Test -apionly and -jdkinternals options |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27579
diff
changeset
|
28 |
* @modules java.base/sun.misc |
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27579
diff
changeset
|
29 |
* java.management |
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27579
diff
changeset
|
30 |
* jdk.compiler/com.sun.tools.classfile |
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27579
diff
changeset
|
31 |
* jdk.dev/com.sun.tools.jdeps |
21046 | 32 |
* @build m.Bar m.Foo m.Gee b.B c.C c.I d.D e.E f.F g.G |
33 |
* @run main APIDeps |
|
34 |
*/ |
|
35 |
||
36 |
import java.io.File; |
|
37 |
import java.io.IOException; |
|
38 |
import java.io.PrintWriter; |
|
39 |
import java.io.StringWriter; |
|
40 |
import java.nio.file.Path; |
|
41 |
import java.nio.file.Paths; |
|
42 |
import java.util.*; |
|
43 |
import java.util.regex.*; |
|
44 |
||
45 |
public class APIDeps { |
|
46 |
public static void main(String... args) throws Exception { |
|
47 |
int errors = 0; |
|
48 |
errors += new APIDeps().run(); |
|
49 |
if (errors > 0) |
|
50 |
throw new Exception(errors + " errors found"); |
|
51 |
} |
|
52 |
||
53 |
int run() throws IOException { |
|
54 |
File testDir = new File(System.getProperty("test.classes", ".")); |
|
55 |
String testDirBasename = testDir.toPath().getFileName().toString(); |
|
56 |
File mDir = new File(testDir, "m"); |
|
57 |
// all dependencies |
|
58 |
test(new File(mDir, "Bar.class"), |
|
59 |
new String[] {"java.lang.Object", "java.lang.String", |
|
60 |
"java.util.Set", "java.util.HashSet", |
|
61 |
"java.lang.management.ManagementFactory", |
|
62 |
"java.lang.management.RuntimeMXBean", |
|
63 |
"b.B", "c.C", "d.D", "f.F", "g.G"}, |
|
64 |
new String[] {"compact1", "compact3", testDirBasename}, |
|
65 |
new String[] {"-classpath", testDir.getPath(), "-verbose", "-P"}); |
|
66 |
test(new File(mDir, "Foo.class"), |
|
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
67 |
new String[] {"c.I", "e.E", "f.F"}, |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
68 |
new String[] {testDirBasename}, |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
69 |
new String[] {"-classpath", testDir.getPath(), "-verbose:class", "-P"}); |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
70 |
test(new File(mDir, "Foo.class"), |
21046 | 71 |
new String[] {"c.I", "e.E", "f.F", "m.Bar"}, |
72 |
new String[] {testDirBasename}, |
|
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
73 |
new String[] {"-classpath", testDir.getPath(), "-verbose:class", "-filter:none", "-P"}); |
21046 | 74 |
test(new File(mDir, "Gee.class"), |
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
75 |
new String[] {"g.G", "sun.misc.Lock", "com.sun.tools.classfile.ClassFile", |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
76 |
"com.sun.management.ThreadMXBean", "com.sun.source.tree.BinaryTree"}, |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
77 |
new String[] {testDirBasename, "JDK internal API", "compact3", ""}, |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
78 |
new String[] {"-classpath", testDir.getPath(), "-verbose", "-P"}); |
22007
907f7054db16
8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents:
21046
diff
changeset
|
79 |
|
907f7054db16
8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents:
21046
diff
changeset
|
80 |
// -jdkinternals |
907f7054db16
8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents:
21046
diff
changeset
|
81 |
test(new File(mDir, "Gee.class"), |
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
82 |
new String[] {"sun.misc.Lock", "com.sun.tools.classfile.ClassFile"}, |
22007
907f7054db16
8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents:
21046
diff
changeset
|
83 |
new String[] {"JDK internal API"}, |
907f7054db16
8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents:
21046
diff
changeset
|
84 |
new String[] {"-jdkinternals"}); |
907f7054db16
8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents:
21046
diff
changeset
|
85 |
// -jdkinternals parses all classes on -classpath and the input arguments |
907f7054db16
8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents:
21046
diff
changeset
|
86 |
test(new File(mDir, "Gee.class"), |
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
87 |
new String[] {"com.sun.tools.jdeps.Main", "com.sun.tools.classfile.ClassFile", |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
88 |
"sun.misc.Lock", "sun.misc.Unsafe"}, |
22007
907f7054db16
8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents:
21046
diff
changeset
|
89 |
new String[] {"JDK internal API"}, |
907f7054db16
8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents:
21046
diff
changeset
|
90 |
new String[] {"-classpath", testDir.getPath(), "-jdkinternals"}); |
907f7054db16
8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents:
21046
diff
changeset
|
91 |
|
907f7054db16
8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents:
21046
diff
changeset
|
92 |
// parse only APIs |
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
93 |
test(mDir, |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
94 |
new String[] {"java.lang.Object", "java.lang.String", |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
95 |
"java.util.Set", |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
96 |
"c.C", "d.D", "c.I", "e.E"}, |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
97 |
new String[] {"compact1", testDirBasename}, |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
98 |
new String[] {"-classpath", testDir.getPath(), "-verbose:class", "-P", "-apionly"}); |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
99 |
|
21046 | 100 |
test(mDir, |
101 |
new String[] {"java.lang.Object", "java.lang.String", |
|
102 |
"java.util.Set", |
|
103 |
"c.C", "d.D", "c.I", "e.E", "m.Bar"}, |
|
104 |
new String[] {"compact1", testDirBasename, mDir.getName()}, |
|
105 |
new String[] {"-classpath", testDir.getPath(), "-verbose", "-P", "-apionly"}); |
|
106 |
return errors; |
|
107 |
} |
|
108 |
||
109 |
void test(File file, String[] expect, String[] profiles) { |
|
110 |
test(file, expect, profiles, new String[0]); |
|
111 |
} |
|
112 |
||
113 |
void test(File file, String[] expect, String[] profiles, String[] options) { |
|
114 |
List<String> args = new ArrayList<>(Arrays.asList(options)); |
|
115 |
if (file != null) { |
|
116 |
args.add(file.getPath()); |
|
117 |
} |
|
118 |
checkResult("api-dependencies", expect, profiles, |
|
119 |
jdeps(args.toArray(new String[0]))); |
|
120 |
} |
|
121 |
||
122 |
Map<String,String> jdeps(String... args) { |
|
123 |
StringWriter sw = new StringWriter(); |
|
124 |
PrintWriter pw = new PrintWriter(sw); |
|
125 |
System.err.println("jdeps " + Arrays.toString(args)); |
|
126 |
int rc = com.sun.tools.jdeps.Main.run(args, pw); |
|
127 |
pw.close(); |
|
128 |
String out = sw.toString(); |
|
129 |
if (!out.isEmpty()) |
|
130 |
System.err.println(out); |
|
131 |
if (rc != 0) |
|
132 |
throw new Error("jdeps failed: rc=" + rc); |
|
133 |
return findDeps(out); |
|
134 |
} |
|
135 |
||
136 |
// Pattern used to parse lines |
|
137 |
private static Pattern linePattern = Pattern.compile(".*\r?\n"); |
|
138 |
private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)"); |
|
139 |
||
140 |
// Use the linePattern to break the given String into lines, applying |
|
141 |
// the pattern to each line to see if we have a match |
|
142 |
private static Map<String,String> findDeps(String out) { |
|
143 |
Map<String,String> result = new HashMap<>(); |
|
144 |
Matcher lm = linePattern.matcher(out); // Line matcher |
|
145 |
Matcher pm = null; // Pattern matcher |
|
146 |
int lines = 0; |
|
147 |
while (lm.find()) { |
|
148 |
lines++; |
|
149 |
CharSequence cs = lm.group(); // The current line |
|
150 |
if (pm == null) |
|
151 |
pm = pattern.matcher(cs); |
|
152 |
else |
|
153 |
pm.reset(cs); |
|
154 |
if (pm.find()) |
|
155 |
result.put(pm.group(1), pm.group(2).trim()); |
|
156 |
if (lm.end() == out.length()) |
|
157 |
break; |
|
158 |
} |
|
159 |
return result; |
|
160 |
} |
|
161 |
||
162 |
void checkResult(String label, String[] expect, Collection<String> found) { |
|
163 |
List<String> list = Arrays.asList(expect); |
|
164 |
if (!isEqual(list, found)) |
|
165 |
error("Unexpected " + label + " found: '" + found + "', expected: '" + list + "'"); |
|
166 |
} |
|
167 |
||
168 |
void checkResult(String label, String[] expect, String[] profiles, Map<String,String> result) { |
|
169 |
// check the dependencies |
|
170 |
checkResult(label, expect, result.keySet()); |
|
171 |
// check profile information |
|
172 |
Set<String> values = new TreeSet<>(); |
|
173 |
String internal = "JDK internal API"; |
|
174 |
for (String s: result.values()) { |
|
175 |
if (s.startsWith(internal)){ |
|
176 |
values.add(internal); |
|
177 |
} else { |
|
178 |
values.add(s); |
|
179 |
} |
|
180 |
} |
|
181 |
checkResult(label, profiles, values); |
|
182 |
} |
|
183 |
||
184 |
boolean isEqual(List<String> expected, Collection<String> found) { |
|
185 |
if (expected.size() != found.size()) |
|
186 |
return false; |
|
187 |
||
188 |
List<String> list = new ArrayList<>(found); |
|
189 |
list.removeAll(expected); |
|
190 |
return list.isEmpty(); |
|
191 |
} |
|
192 |
||
193 |
void error(String msg) { |
|
194 |
System.err.println("Error: " + msg); |
|
195 |
errors++; |
|
196 |
} |
|
197 |
||
198 |
int errors; |
|
199 |
} |