author | jjg |
Wed, 29 Oct 2014 17:25:23 -0700 | |
changeset 27319 | 030080f03e4f |
parent 24069 | dfb8f11542fc |
child 30730 | d3ce7619db2c |
permissions | -rw-r--r-- |
10815 | 1 |
/* |
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
24069
diff
changeset
|
2 |
* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. |
10815 | 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 7096014 |
|
27 |
* @summary Javac tokens should retain state |
|
28 |
*/ |
|
29 |
||
30 |
import com.sun.source.tree.*; |
|
31 |
import com.sun.source.util.*; |
|
13077 | 32 |
import com.sun.tools.javac.tree.DocCommentTable; |
10815 | 33 |
import com.sun.tools.javac.tree.JCTree; |
34 |
||
35 |
import java.net.URI; |
|
36 |
import java.util.*; |
|
37 |
import javax.tools.*; |
|
38 |
||
39 |
||
40 |
public class DocCommentToplevelTest { |
|
41 |
||
42 |
enum PackageKind { |
|
43 |
HAS_PKG("package pkg;"), |
|
44 |
NO_PKG(""); |
|
45 |
||
46 |
String pkgStr; |
|
47 |
||
48 |
PackageKind(String pkgStr) { |
|
49 |
this.pkgStr = pkgStr; |
|
50 |
} |
|
51 |
} |
|
52 |
||
53 |
enum ImportKind { |
|
54 |
ZERO(""), |
|
55 |
ONE("import java.lang.*;"), |
|
56 |
TWO("import java.lang.*; import java.util.*;"); |
|
57 |
||
58 |
String importStr; |
|
59 |
||
60 |
ImportKind(String importStr) { |
|
61 |
this.importStr = importStr; |
|
62 |
} |
|
63 |
} |
|
64 |
||
65 |
enum ModifierKind { |
|
66 |
DEFAULT(""), |
|
67 |
PUBLIC("public"); |
|
68 |
||
69 |
String modStr; |
|
70 |
||
71 |
ModifierKind(String modStr) { |
|
72 |
this.modStr = modStr; |
|
73 |
} |
|
74 |
} |
|
75 |
||
76 |
enum ToplevelDocKind { |
|
77 |
HAS_DOC("/** Toplevel! */"), |
|
78 |
NO_DOC(""); |
|
79 |
||
80 |
String docStr; |
|
81 |
||
82 |
ToplevelDocKind(String docStr) { |
|
83 |
this.docStr = docStr; |
|
84 |
} |
|
85 |
} |
|
86 |
||
87 |
static int errors; |
|
88 |
static int checks; |
|
89 |
||
90 |
public static void main(String... args) throws Exception { |
|
91 |
//create default shared JavaCompiler - reused across multiple compilations |
|
92 |
JavaCompiler comp = ToolProvider.getSystemJavaCompiler(); |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
24069
diff
changeset
|
93 |
try (StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null)) { |
10815 | 94 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
24069
diff
changeset
|
95 |
for (PackageKind pk : PackageKind.values()) { |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
24069
diff
changeset
|
96 |
for (ImportKind ik : ImportKind.values()) { |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
24069
diff
changeset
|
97 |
for (ModifierKind mk1 : ModifierKind.values()) { |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
24069
diff
changeset
|
98 |
for (ModifierKind mk2 : ModifierKind.values()) { |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
24069
diff
changeset
|
99 |
for (ToplevelDocKind tdk : ToplevelDocKind.values()) { |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
24069
diff
changeset
|
100 |
new DocCommentToplevelTest(pk, ik, mk1, mk2, tdk).run(comp, fm); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
24069
diff
changeset
|
101 |
} |
10815 | 102 |
} |
103 |
} |
|
104 |
} |
|
105 |
} |
|
106 |
||
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
24069
diff
changeset
|
107 |
if (errors > 0) |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
24069
diff
changeset
|
108 |
throw new AssertionError(errors + " errors found"); |
10815 | 109 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
24069
diff
changeset
|
110 |
System.out.println(checks + " checks were made"); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
24069
diff
changeset
|
111 |
} |
10815 | 112 |
} |
113 |
||
114 |
PackageKind pk; |
|
115 |
ImportKind ik; |
|
116 |
ModifierKind mk1; |
|
117 |
ModifierKind mk2; |
|
118 |
ToplevelDocKind tdk; |
|
119 |
JavaSource source; |
|
120 |
||
121 |
DocCommentToplevelTest(PackageKind pk, ImportKind ik, ModifierKind mk1, ModifierKind mk2, ToplevelDocKind tdk) { |
|
122 |
this.pk = pk; |
|
123 |
this.ik = ik; |
|
124 |
this.mk1 = mk1; |
|
125 |
this.mk2 = mk2; |
|
126 |
this.tdk = tdk; |
|
127 |
source = new JavaSource(); |
|
128 |
} |
|
129 |
||
130 |
void run(JavaCompiler comp, JavaFileManager fm) throws Exception { |
|
131 |
JavacTask task = (JavacTask)comp.getTask(null, fm, null, Arrays.asList("-printsource"), null, Arrays.asList(source)); |
|
132 |
for (CompilationUnitTree cu: task.parse()) { |
|
133 |
check(cu); |
|
134 |
} |
|
135 |
} |
|
136 |
||
137 |
void check(CompilationUnitTree cu) { |
|
138 |
checks++; |
|
139 |
||
140 |
new TreeScanner<ClassTree,Void>() { |
|
141 |
||
13077 | 142 |
DocCommentTable docComments; |
10815 | 143 |
|
144 |
@Override |
|
145 |
public ClassTree visitCompilationUnit(CompilationUnitTree node, Void unused) { |
|
146 |
docComments = ((JCTree.JCCompilationUnit)node).docComments; |
|
147 |
boolean expectedComment = tdk == ToplevelDocKind.HAS_DOC && |
|
24069 | 148 |
pk == PackageKind.NO_PKG && |
149 |
ik != ImportKind.ZERO; |
|
13077 | 150 |
boolean foundComment = docComments.hasComment((JCTree) node); |
10815 | 151 |
if (expectedComment != foundComment) { |
13077 | 152 |
error("Unexpected comment " + docComments.getComment((JCTree) node) + " on toplevel"); |
10815 | 153 |
} |
154 |
return super.visitCompilationUnit(node, null); |
|
155 |
} |
|
156 |
||
157 |
@Override |
|
24069 | 158 |
public ClassTree visitPackage(PackageTree node, Void unused) { |
159 |
boolean expectedComment = tdk == ToplevelDocKind.HAS_DOC && |
|
160 |
pk != PackageKind.NO_PKG; |
|
161 |
boolean foundComment = docComments.hasComment((JCTree) node); |
|
162 |
if (expectedComment != foundComment) { |
|
163 |
error("Unexpected comment " + docComments.getComment((JCTree) node) + " on toplevel"); |
|
164 |
} |
|
165 |
return super.visitPackage(node, null); |
|
166 |
} |
|
167 |
||
168 |
@Override |
|
10815 | 169 |
public ClassTree visitClass(ClassTree node, Void unused) { |
170 |
boolean expectedComment = tdk == ToplevelDocKind.HAS_DOC && |
|
171 |
pk == PackageKind.NO_PKG && ik == ImportKind.ZERO && |
|
172 |
node.getSimpleName().toString().equals("First"); |
|
13077 | 173 |
boolean foundComment = docComments.hasComment((JCTree) node); |
10815 | 174 |
if (expectedComment != foundComment) { |
13077 | 175 |
error("Unexpected comment " + docComments.getComment((JCTree) node) + " on class " + node.getSimpleName()); |
10815 | 176 |
} |
177 |
return super.visitClass(node, unused); |
|
178 |
} |
|
179 |
}.scan(cu, null); |
|
180 |
} |
|
181 |
||
182 |
void error(String msg) { |
|
183 |
System.err.println("Error: " + msg); |
|
184 |
System.err.println("Source: " + source.source); |
|
185 |
errors++; |
|
186 |
} |
|
187 |
||
188 |
class JavaSource extends SimpleJavaFileObject { |
|
189 |
||
190 |
String template = "#D\n#P\n#I\n" + |
|
191 |
"#M1 class First { }\n" + |
|
192 |
"#M2 class Second { }\n"; |
|
193 |
||
194 |
String source; |
|
195 |
||
196 |
public JavaSource() { |
|
197 |
super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
|
198 |
source = template.replace("#P", pk.pkgStr) |
|
199 |
.replace("#I", ik.importStr) |
|
200 |
.replace("#M1", mk1.modStr) |
|
201 |
.replace("#M2", mk2.modStr) |
|
202 |
.replace("#D", tdk.docStr); |
|
203 |
} |
|
204 |
||
205 |
@Override |
|
206 |
public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
|
207 |
return source; |
|
208 |
} |
|
209 |
} |
|
210 |
} |