author | jjg |
Thu, 31 Mar 2016 15:20:50 -0700 | |
changeset 36778 | e04318f39f92 |
parent 36526 | 3b41f1c69604 |
permissions | -rw-r--r-- |
15370 | 1 |
/* |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
2 |
* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved. |
15370 | 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 |
|
26 |
* @bug 8006263 |
|
27 |
* @summary Supplementary test cases needed for doclint |
|
27579 | 28 |
* @library /tools/lib |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27579
diff
changeset
|
29 |
* @modules jdk.compiler/com.sun.tools.doclint |
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27579
diff
changeset
|
30 |
* jdk.compiler/com.sun.tools.javac.api |
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27579
diff
changeset
|
31 |
* jdk.compiler/com.sun.tools.javac.main |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
32 |
* @build toolbox.ToolBox toolbox.JarTask |
27579 | 33 |
* @run main PathsTest |
15370 | 34 |
*/ |
35 |
||
36 |
import java.io.File; |
|
37 |
import java.io.FileWriter; |
|
38 |
import java.io.IOException; |
|
39 |
import java.io.PrintWriter; |
|
40 |
import java.io.StringWriter; |
|
41 |
import java.util.regex.Pattern; |
|
27579 | 42 |
import javax.tools.StandardLocation; |
43 |
import javax.tools.JavaFileManager; |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
44 |
import javax.tools.ToolProvider; |
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
45 |
|
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
46 |
import com.sun.tools.doclint.DocLint; |
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
47 |
import com.sun.tools.doclint.DocLint.BadArgs; |
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
48 |
|
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
49 |
import toolbox.JarTask; |
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
50 |
import toolbox.ToolBox; |
15370 | 51 |
|
52 |
public class PathsTest { |
|
53 |
public static void main(String... args) throws Exception { |
|
54 |
new PathsTest().run(); |
|
55 |
} |
|
56 |
||
57 |
void run() throws Exception { |
|
58 |
String PS = File.pathSeparator; |
|
59 |
writeFile("src1/p/A.java", |
|
60 |
"package p; public class A { }"); |
|
61 |
compile("-d", "classes1", "src1/p/A.java"); |
|
62 |
||
63 |
writeFile("src2/q/B.java", |
|
64 |
"package q; public class B extends p.A { }"); |
|
65 |
compile("-d", "classes2", "-classpath", "classes1", "src2/q/B.java"); |
|
66 |
||
67 |
writeFile("src/Test.java", |
|
68 |
"/** &0; */ class Test extends q.B { }"); |
|
69 |
||
70 |
test("src/Test.java", "-sourcepath", "src1" + PS + "src2"); |
|
71 |
test("src/Test.java", "-classpath", "classes1" + PS + "classes2"); |
|
27579 | 72 |
|
73 |
File testJar = createJar(); |
|
15370 | 74 |
test("src/Test.java", "-bootclasspath", |
27579 | 75 |
testJar + PS + "classes1" + PS + "classes2"); |
15370 | 76 |
|
77 |
if (errors > 0) |
|
78 |
throw new Exception(errors + " errors found"); |
|
79 |
} |
|
80 |
||
81 |
Pattern pkgNotFound = Pattern.compile("package [a-z]+ does not exist"); |
|
82 |
Pattern badHtmlEntity = Pattern.compile("bad HTML entity"); |
|
83 |
||
84 |
void test(String file, String pathOpt, String path) throws BadArgs, IOException { |
|
85 |
System.err.println("test " + pathOpt); |
|
86 |
String out1 = doclint("-Xmsgs", file); |
|
87 |
if (!pkgNotFound.matcher(out1).find()) |
|
88 |
error("message not found: " + pkgNotFound); |
|
89 |
||
36526 | 90 |
String out2; |
91 |
if (needTarget8(pathOpt)) { |
|
92 |
out2 = doclint("-Xmsgs", "-source", "8", "-target", "8", pathOpt, path, file); |
|
93 |
} else { |
|
94 |
out2 = doclint("-Xmsgs", pathOpt, path, file); |
|
95 |
} |
|
15370 | 96 |
if (pkgNotFound.matcher(out2).find()) |
97 |
error("unexpected message found: " + pkgNotFound); |
|
98 |
if (!badHtmlEntity.matcher(out1).find()) |
|
99 |
error("message not found: " + badHtmlEntity); |
|
100 |
||
101 |
try { |
|
102 |
doclint("-Xmsgs", pathOpt); |
|
103 |
error("expected exception not thrown"); |
|
104 |
} catch (BadArgs e) { |
|
105 |
System.err.println(e); |
|
106 |
} |
|
107 |
} |
|
108 |
||
36526 | 109 |
boolean needTarget8(String opt) { |
110 |
switch (opt) { |
|
111 |
case "-bootclasspath": |
|
112 |
return true; |
|
113 |
default: |
|
114 |
return false; |
|
115 |
} |
|
116 |
} |
|
117 |
||
27579 | 118 |
File createJar() throws IOException { |
119 |
File f = new File("test.jar"); |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
120 |
try (JavaFileManager fm = ToolProvider.getSystemJavaCompiler() |
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
121 |
.getStandardFileManager(null, null, null)) { |
27579 | 122 |
ToolBox tb = new ToolBox(); |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
123 |
new JarTask(tb, f.getPath()) |
27579 | 124 |
.files(fm, StandardLocation.PLATFORM_CLASS_PATH, "java.lang.*") |
125 |
.run(); |
|
126 |
} |
|
127 |
return f; |
|
128 |
} |
|
129 |
||
15370 | 130 |
void compile(String... args) { |
131 |
for (int i = 0; i < args.length; i++) { |
|
132 |
if (args[i].equals("-d")) { |
|
133 |
new File(args[++i]).mkdirs(); |
|
134 |
break; |
|
135 |
} |
|
136 |
} |
|
137 |
||
138 |
StringWriter sw = new StringWriter(); |
|
139 |
PrintWriter pw = new PrintWriter(sw); |
|
140 |
int rc = com.sun.tools.javac.Main.compile(args, pw); |
|
141 |
pw.close(); |
|
142 |
String out = sw.toString(); |
|
143 |
if (!out.isEmpty()) |
|
144 |
System.err.println(out); |
|
145 |
if (rc != 0) |
|
146 |
error("compilation failed: rc=" + rc); |
|
147 |
} |
|
148 |
||
149 |
String doclint(String... args) throws BadArgs, IOException { |
|
150 |
StringWriter sw = new StringWriter(); |
|
151 |
PrintWriter pw = new PrintWriter(sw); |
|
152 |
DocLint dl = new DocLint(); |
|
153 |
dl.run(pw, args); |
|
154 |
pw.close(); |
|
155 |
String out = sw.toString(); |
|
156 |
if (!out.isEmpty()) |
|
157 |
System.err.println(out); |
|
158 |
return out; |
|
159 |
} |
|
160 |
||
161 |
File writeFile(String path, String body) throws IOException { |
|
162 |
File f = new File(path); |
|
163 |
f.getParentFile().mkdirs(); |
|
164 |
try (FileWriter fw = new FileWriter(path)) { |
|
165 |
fw.write(body); |
|
166 |
} |
|
167 |
return f; |
|
168 |
} |
|
169 |
||
170 |
void error(String msg) { |
|
171 |
System.err.println("Error: " + msg); |
|
172 |
errors++; |
|
173 |
} |
|
174 |
||
175 |
int errors; |
|
176 |
} |