author | naoto |
Tue, 29 Mar 2016 17:06:33 -0700 | |
changeset 36739 | 145210aba850 |
parent 36526 | 3b41f1c69604 |
child 37848 | 3c8ff4204d2d |
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 |
33021
8a0d5889acea
8139371: Three langtools test failures after the removal of sun.misc.Lock
chegar
parents:
30846
diff
changeset
|
28 |
* @modules java.base/sun.security.x509 |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27579
diff
changeset
|
29 |
* java.management |
30846 | 30 |
* jdk.jdeps/com.sun.tools.classfile |
31 |
* jdk.jdeps/com.sun.tools.jdeps |
|
21046 | 32 |
* @run main APIDeps |
33 |
*/ |
|
34 |
||
35 |
import java.io.File; |
|
36 |
import java.io.IOException; |
|
37 |
import java.io.PrintWriter; |
|
38 |
import java.io.StringWriter; |
|
36526 | 39 |
import java.nio.file.*; |
21046 | 40 |
import java.util.*; |
41 |
import java.util.regex.*; |
|
36526 | 42 |
import java.util.stream.Collectors; |
21046 | 43 |
|
44 |
public class APIDeps { |
|
45 |
public static void main(String... args) throws Exception { |
|
46 |
int errors = 0; |
|
47 |
errors += new APIDeps().run(); |
|
48 |
if (errors > 0) |
|
49 |
throw new Exception(errors + " errors found"); |
|
50 |
} |
|
51 |
||
36526 | 52 |
private static final Path dest = Paths.get(System.getProperty("test.classes", "."), "tmp"); |
53 |
private static final String[] srcDirs = new String[] { |
|
54 |
"m", "b", "c", "d", "e", "f", "g" |
|
55 |
}; |
|
56 |
void setup(Path dest) throws IOException { |
|
57 |
CompilerUtils.cleanDir(dest); |
|
58 |
Files.createDirectories(dest); |
|
59 |
Path testsrc = Paths.get(System.getProperty("test.src")); |
|
60 |
List<String> options = new ArrayList<>(); |
|
61 |
||
62 |
// add -XaddExports |
|
63 |
String testModules = System.getProperty("test.modules", ""); |
|
64 |
List<String> addExports = new ArrayList<>(); |
|
65 |
for (String s : testModules.split("\\s+")) { |
|
66 |
if (s.isEmpty()) continue; |
|
67 |
if (s.indexOf('/') != -1) |
|
68 |
addExports.add(s.trim() + "=ALL-UNNAMED"); |
|
69 |
} |
|
70 |
if (addExports.size() > 0) { |
|
71 |
options.add(addExports.stream().collect(Collectors.joining(",", "-XaddExports:", ""))); |
|
72 |
} |
|
73 |
||
74 |
for (String dir : srcDirs) { |
|
75 |
Path source = testsrc.resolve(dir); |
|
76 |
boolean ok = CompilerUtils.compile(source, dest, options.toArray(new String[0])); |
|
77 |
if (!ok) { |
|
78 |
throw new RuntimeException("compilation fails"); |
|
79 |
} |
|
80 |
} |
|
81 |
} |
|
82 |
||
21046 | 83 |
int run() throws IOException { |
36526 | 84 |
// compile classes in a separate directory for analysis |
85 |
setup(dest); |
|
86 |
||
87 |
File testDir = dest.toFile(); |
|
21046 | 88 |
String testDirBasename = testDir.toPath().getFileName().toString(); |
89 |
File mDir = new File(testDir, "m"); |
|
90 |
// all dependencies |
|
91 |
test(new File(mDir, "Bar.class"), |
|
92 |
new String[] {"java.lang.Object", "java.lang.String", |
|
93 |
"java.util.Set", "java.util.HashSet", |
|
94 |
"java.lang.management.ManagementFactory", |
|
95 |
"java.lang.management.RuntimeMXBean", |
|
96 |
"b.B", "c.C", "d.D", "f.F", "g.G"}, |
|
97 |
new String[] {"compact1", "compact3", testDirBasename}, |
|
98 |
new String[] {"-classpath", testDir.getPath(), "-verbose", "-P"}); |
|
99 |
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
|
100 |
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
|
101 |
new String[] {testDirBasename}, |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
102 |
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
|
103 |
test(new File(mDir, "Foo.class"), |
21046 | 104 |
new String[] {"c.I", "e.E", "f.F", "m.Bar"}, |
105 |
new String[] {testDirBasename}, |
|
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
106 |
new String[] {"-classpath", testDir.getPath(), "-verbose:class", "-filter:none", "-P"}); |
21046 | 107 |
test(new File(mDir, "Gee.class"), |
33021
8a0d5889acea
8139371: Three langtools test failures after the removal of sun.misc.Lock
chegar
parents:
30846
diff
changeset
|
108 |
new String[] {"g.G", "sun.security.x509.X509CertInfo", "com.sun.tools.classfile.ClassFile", |
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
109 |
"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
|
110 |
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
|
111 |
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
|
112 |
|
907f7054db16
8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents:
21046
diff
changeset
|
113 |
// -jdkinternals |
907f7054db16
8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents:
21046
diff
changeset
|
114 |
test(new File(mDir, "Gee.class"), |
33021
8a0d5889acea
8139371: Three langtools test failures after the removal of sun.misc.Lock
chegar
parents:
30846
diff
changeset
|
115 |
new String[] {"sun.security.x509.X509CertInfo", "com.sun.tools.classfile.ClassFile"}, |
22007
907f7054db16
8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents:
21046
diff
changeset
|
116 |
new String[] {"JDK internal API"}, |
36526 | 117 |
new String[] {"-jdkinternals", "-quiet"}); |
22007
907f7054db16
8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents:
21046
diff
changeset
|
118 |
// -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
|
119 |
test(new File(mDir, "Gee.class"), |
36526 | 120 |
new String[] {"com.sun.tools.classfile.ClassFile", |
121 |
"sun.security.x509.X509CertInfo"}, |
|
22007
907f7054db16
8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents:
21046
diff
changeset
|
122 |
new String[] {"JDK internal API"}, |
36526 | 123 |
// use -classpath tmp/a with no use of JDK internal API |
124 |
new String[] {"-classpath", dest.resolve("a").toString(), "-jdkinternals", "-quiet"}); |
|
22007
907f7054db16
8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents:
21046
diff
changeset
|
125 |
|
907f7054db16
8029216: (jdeps) Provide a specific option to report JDK internal APIs
mchung
parents:
21046
diff
changeset
|
126 |
// parse only APIs |
25442
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
127 |
test(mDir, |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
128 |
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
|
129 |
"java.util.Set", |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
130 |
"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
|
131 |
new String[] {"compact1", testDirBasename}, |
755ff386d1ac
8029548: (jdeps) use @jdk.Exported to determine supported vs JDK internal API
mchung
parents:
22448
diff
changeset
|
132 |
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
|
133 |
|
21046 | 134 |
test(mDir, |
135 |
new String[] {"java.lang.Object", "java.lang.String", |
|
136 |
"java.util.Set", |
|
137 |
"c.C", "d.D", "c.I", "e.E", "m.Bar"}, |
|
138 |
new String[] {"compact1", testDirBasename, mDir.getName()}, |
|
139 |
new String[] {"-classpath", testDir.getPath(), "-verbose", "-P", "-apionly"}); |
|
140 |
return errors; |
|
141 |
} |
|
142 |
||
143 |
void test(File file, String[] expect, String[] profiles) { |
|
144 |
test(file, expect, profiles, new String[0]); |
|
145 |
} |
|
146 |
||
147 |
void test(File file, String[] expect, String[] profiles, String[] options) { |
|
148 |
List<String> args = new ArrayList<>(Arrays.asList(options)); |
|
149 |
if (file != null) { |
|
150 |
args.add(file.getPath()); |
|
151 |
} |
|
152 |
checkResult("api-dependencies", expect, profiles, |
|
153 |
jdeps(args.toArray(new String[0]))); |
|
154 |
} |
|
155 |
||
156 |
Map<String,String> jdeps(String... args) { |
|
157 |
StringWriter sw = new StringWriter(); |
|
158 |
PrintWriter pw = new PrintWriter(sw); |
|
159 |
System.err.println("jdeps " + Arrays.toString(args)); |
|
160 |
int rc = com.sun.tools.jdeps.Main.run(args, pw); |
|
161 |
pw.close(); |
|
162 |
String out = sw.toString(); |
|
163 |
if (!out.isEmpty()) |
|
164 |
System.err.println(out); |
|
165 |
if (rc != 0) |
|
166 |
throw new Error("jdeps failed: rc=" + rc); |
|
167 |
return findDeps(out); |
|
168 |
} |
|
169 |
||
170 |
// Pattern used to parse lines |
|
171 |
private static Pattern linePattern = Pattern.compile(".*\r?\n"); |
|
172 |
private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)"); |
|
173 |
||
174 |
// Use the linePattern to break the given String into lines, applying |
|
175 |
// the pattern to each line to see if we have a match |
|
176 |
private static Map<String,String> findDeps(String out) { |
|
177 |
Map<String,String> result = new HashMap<>(); |
|
178 |
Matcher lm = linePattern.matcher(out); // Line matcher |
|
179 |
Matcher pm = null; // Pattern matcher |
|
180 |
int lines = 0; |
|
181 |
while (lm.find()) { |
|
182 |
lines++; |
|
183 |
CharSequence cs = lm.group(); // The current line |
|
184 |
if (pm == null) |
|
185 |
pm = pattern.matcher(cs); |
|
186 |
else |
|
187 |
pm.reset(cs); |
|
188 |
if (pm.find()) |
|
189 |
result.put(pm.group(1), pm.group(2).trim()); |
|
190 |
if (lm.end() == out.length()) |
|
191 |
break; |
|
192 |
} |
|
193 |
return result; |
|
194 |
} |
|
195 |
||
196 |
void checkResult(String label, String[] expect, Collection<String> found) { |
|
197 |
List<String> list = Arrays.asList(expect); |
|
198 |
if (!isEqual(list, found)) |
|
199 |
error("Unexpected " + label + " found: '" + found + "', expected: '" + list + "'"); |
|
200 |
} |
|
201 |
||
202 |
void checkResult(String label, String[] expect, String[] profiles, Map<String,String> result) { |
|
203 |
// check the dependencies |
|
204 |
checkResult(label, expect, result.keySet()); |
|
205 |
// check profile information |
|
206 |
Set<String> values = new TreeSet<>(); |
|
207 |
String internal = "JDK internal API"; |
|
208 |
for (String s: result.values()) { |
|
209 |
if (s.startsWith(internal)){ |
|
210 |
values.add(internal); |
|
211 |
} else { |
|
212 |
values.add(s); |
|
213 |
} |
|
214 |
} |
|
215 |
checkResult(label, profiles, values); |
|
216 |
} |
|
217 |
||
218 |
boolean isEqual(List<String> expected, Collection<String> found) { |
|
219 |
if (expected.size() != found.size()) |
|
220 |
return false; |
|
221 |
||
222 |
List<String> list = new ArrayList<>(found); |
|
223 |
list.removeAll(expected); |
|
224 |
return list.isEmpty(); |
|
225 |
} |
|
226 |
||
227 |
void error(String msg) { |
|
228 |
System.err.println("Error: " + msg); |
|
229 |
errors++; |
|
230 |
} |
|
231 |
||
232 |
int errors; |
|
36526 | 233 |
|
21046 | 234 |
} |