author | darcy |
Tue, 11 Jun 2019 16:45:20 -0700 | |
changeset 55382 | 30b1b7b4dd86 |
parent 53023 | 6879069d9d94 |
permissions | -rw-r--r-- |
50892 | 1 |
/* |
55382 | 2 |
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved. |
15724 | 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 |
|
22151 | 26 |
* @bug 8004182 8028545 |
15724 | 27 |
* @summary Add support for profiles in javac |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27579
diff
changeset
|
28 |
* @modules java.desktop |
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27579
diff
changeset
|
29 |
* java.sql.rowset |
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.jvm |
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27579
diff
changeset
|
32 |
* jdk.security.auth |
15724 | 33 |
*/ |
34 |
||
35 |
import java.io.PrintWriter; |
|
36 |
import java.io.StringWriter; |
|
37 |
import java.lang.annotation.Annotation; |
|
38 |
import java.lang.annotation.Retention; |
|
39 |
import java.lang.annotation.RetentionPolicy; |
|
40 |
import java.lang.reflect.InvocationTargetException; |
|
41 |
import java.lang.reflect.Method; |
|
42 |
import java.net.URI; |
|
43 |
import java.util.ArrayList; |
|
44 |
import java.util.Arrays; |
|
45 |
import java.util.Collections; |
|
46 |
import java.util.EnumMap; |
|
47 |
import java.util.List; |
|
48 |
import java.util.Map; |
|
49 |
||
50 |
import javax.tools.Diagnostic; |
|
51 |
import javax.tools.DiagnosticCollector; |
|
52 |
import javax.tools.JavaCompiler; |
|
53 |
import javax.tools.JavaFileObject; |
|
54 |
import javax.tools.SimpleJavaFileObject; |
|
55 |
import javax.tools.StandardJavaFileManager; |
|
56 |
||
57 |
import com.sun.source.util.JavacTask; |
|
58 |
import com.sun.tools.javac.api.JavacTool; |
|
59 |
import com.sun.tools.javac.jvm.Profile; |
|
60 |
import com.sun.tools.javac.jvm.Target; |
|
61 |
||
62 |
||
63 |
public class ProfileOptionTest { |
|
64 |
public static void main(String... args) throws Exception { |
|
65 |
new ProfileOptionTest().run(); |
|
66 |
} |
|
67 |
||
68 |
private final JavaCompiler javac = JavacTool.create(); |
|
69 |
private final StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null); |
|
70 |
||
71 |
||
72 |
// ---------- Test cases, invoked reflectively via run. ---------- |
|
73 |
||
74 |
@Test |
|
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
23104
diff
changeset
|
75 |
void testInvalidProfile_API() throws Exception { |
15724 | 76 |
JavaFileObject fo = new StringJavaFileObject("Test.java", "class Test { }"); |
77 |
String badName = "foo"; |
|
42822
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41637
diff
changeset
|
78 |
List<String> opts = Arrays.asList("--release", "8", "-profile", badName); |
15724 | 79 |
StringWriter sw = new StringWriter(); |
80 |
try { |
|
81 |
JavacTask task = (JavacTask) javac.getTask(sw, fm, null, opts, null, |
|
82 |
Arrays.asList(fo)); |
|
83 |
throw new Exception("expected exception not thrown"); |
|
84 |
} catch (IllegalArgumentException e) { |
|
85 |
// expected |
|
86 |
} |
|
87 |
} |
|
88 |
||
89 |
@Test |
|
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
23104
diff
changeset
|
90 |
void testInvalidProfile_CommandLine() throws Exception { |
15724 | 91 |
String badName = "foo"; |
42822
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41637
diff
changeset
|
92 |
String[] opts = { "--release", "8", "-profile", badName }; |
15724 | 93 |
StringWriter sw = new StringWriter(); |
94 |
PrintWriter pw = new PrintWriter(sw); |
|
95 |
int rc = com.sun.tools.javac.Main.compile(opts, pw); |
|
96 |
||
97 |
// sadly, command line errors are not (yet?) reported to |
|
98 |
// the diag listener |
|
99 |
String out = sw.toString(); |
|
100 |
if (!out.isEmpty()) |
|
101 |
System.err.println(out.trim()); |
|
102 |
||
103 |
if (!out.contains("invalid profile: " + badName)) { |
|
104 |
error("expected message not found"); |
|
105 |
} |
|
106 |
} |
|
107 |
||
108 |
@Test |
|
109 |
void testTargetProfileCombinations() throws Exception { |
|
110 |
JavaFileObject fo = new StringJavaFileObject("Test.java", "class Test { }"); |
|
111 |
for (Target t: Target.values()) { |
|
112 |
switch (t) { |
|
27579 | 113 |
case JDK1_1: |
114 |
case JDK1_2: |
|
115 |
case JDK1_3: |
|
116 |
case JDK1_4: |
|
117 |
case JDK1_5: // not supported |
|
15724 | 118 |
continue; |
119 |
} |
|
120 |
||
121 |
for (Profile p: Profile.values()) { |
|
22151 | 122 |
List<String> opts = new ArrayList<>(); |
15724 | 123 |
opts.addAll(Arrays.asList("-source", t.name, "-target", t.name)); |
27579 | 124 |
opts.add("-Xlint:-options"); // don't warn about no -bootclasspath |
15724 | 125 |
if (p != Profile.DEFAULT) |
126 |
opts.addAll(Arrays.asList("-profile", p.name)); |
|
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
23104
diff
changeset
|
127 |
|
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
23104
diff
changeset
|
128 |
IllegalStateException ise; |
15724 | 129 |
StringWriter sw = new StringWriter(); |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
23104
diff
changeset
|
130 |
try { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
23104
diff
changeset
|
131 |
JavacTask task = (JavacTask) javac.getTask(sw, fm, null, opts, null, |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
23104
diff
changeset
|
132 |
Arrays.asList(fo)); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
23104
diff
changeset
|
133 |
task.analyze(); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
23104
diff
changeset
|
134 |
ise = null; |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
23104
diff
changeset
|
135 |
} catch (IllegalStateException e) { |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
23104
diff
changeset
|
136 |
ise = e; |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
23104
diff
changeset
|
137 |
} |
15724 | 138 |
|
139 |
// sadly, command line errors are not (yet?) reported to |
|
140 |
// the diag listener |
|
141 |
String out = sw.toString(); |
|
142 |
if (!out.isEmpty()) |
|
143 |
System.err.println(out.trim()); |
|
144 |
||
145 |
switch (t) { |
|
146 |
case JDK1_8: |
|
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
23104
diff
changeset
|
147 |
if (ise != null) |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
23104
diff
changeset
|
148 |
error("unexpected exception from compiler: " + ise); |
15724 | 149 |
break; |
42822
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41637
diff
changeset
|
150 |
case JDK1_9: |
43646 | 151 |
case JDK1_10: |
48343 | 152 |
case JDK1_11: |
50892 | 153 |
case JDK1_12: |
53023 | 154 |
case JDK1_13: |
55382 | 155 |
case JDK1_14: |
42822
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41637
diff
changeset
|
156 |
if (p == Profile.DEFAULT) |
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41637
diff
changeset
|
157 |
break; |
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41637
diff
changeset
|
158 |
if (ise == null) |
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41637
diff
changeset
|
159 |
error("IllegalStateException not thrown as expected"); |
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41637
diff
changeset
|
160 |
else if (!ise.getMessage().contains("option -profile " + |
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41637
diff
changeset
|
161 |
"not allowed with target " + t.name)) { |
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41637
diff
changeset
|
162 |
error("exception not thrown as expected: " + ise); |
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41637
diff
changeset
|
163 |
} |
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41637
diff
changeset
|
164 |
break; |
15724 | 165 |
default: |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
23104
diff
changeset
|
166 |
if (p == Profile.DEFAULT) |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
23104
diff
changeset
|
167 |
break; |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
23104
diff
changeset
|
168 |
if (ise == null) |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
23104
diff
changeset
|
169 |
error("IllegalStateException not thrown as expected"); |
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
23104
diff
changeset
|
170 |
else if (!ise.getMessage().contains("profile " + p.name |
15724 | 171 |
+ " is not valid for target release " + t.name)) { |
26264
a09fedde76be
8044859: javac duplicates option processing when using Compiler API
jjg
parents:
23104
diff
changeset
|
172 |
error("exception not thrown as expected: " + ise); |
15724 | 173 |
} |
42822
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41637
diff
changeset
|
174 |
break; |
15724 | 175 |
} |
176 |
} |
|
177 |
} |
|
178 |
} |
|
179 |
||
180 |
@Test |
|
181 |
void testClassesInProfiles() throws Exception { |
|
182 |
for (Profile p: Profile.values()) { |
|
183 |
for (Map.Entry<Profile, List<JavaFileObject>> e: testClasses.entrySet()) { |
|
184 |
for (JavaFileObject fo: e.getValue()) { |
|
185 |
DiagnosticCollector<JavaFileObject> dl = |
|
186 |
new DiagnosticCollector<JavaFileObject>(); |
|
187 |
List<String> opts = (p == Profile.DEFAULT) |
|
188 |
? Collections.<String>emptyList() |
|
42822
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41637
diff
changeset
|
189 |
: Arrays.asList("--release", "8", "-profile", p.name); |
15724 | 190 |
JavacTask task = (JavacTask) javac.getTask(null, fm, dl, opts, null, |
191 |
Arrays.asList(fo)); |
|
192 |
task.analyze(); |
|
193 |
||
41637 | 194 |
List<String> expectDiagCodes = new ArrayList<>(); |
195 |
if (fo.getName().equals("TPolicyFile.java")) { |
|
196 |
expectDiagCodes.add("compiler.warn.has.been.deprecated.for.removal"); |
|
197 |
} |
|
198 |
||
199 |
if (p.value < e.getKey().value) { |
|
200 |
expectDiagCodes.add("compiler.err.not.in.profile"); |
|
201 |
} |
|
15724 | 202 |
|
203 |
checkDiags(opts + " " + fo.getName(), dl.getDiagnostics(), expectDiagCodes); |
|
204 |
} |
|
205 |
} |
|
206 |
} |
|
207 |
} |
|
208 |
||
209 |
Map<Profile, List<JavaFileObject>> testClasses = |
|
210 |
new EnumMap<Profile, List<JavaFileObject>>(Profile.class); |
|
211 |
||
212 |
void initTestClasses() { |
|
213 |
// The following table assumes the existence of specific classes |
|
214 |
// in specific profiles, as defined in the Java SE 8 spec. |
|
215 |
init(Profile.COMPACT1, |
|
216 |
java.lang.String.class); |
|
217 |
||
218 |
init(Profile.COMPACT2, |
|
219 |
javax.xml.XMLConstants.class); |
|
220 |
||
42822
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41637
diff
changeset
|
221 |
//init(Profile.COMPACT3, |
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41637
diff
changeset
|
222 |
// javax.sql.rowset.Predicate.class, |
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41637
diff
changeset
|
223 |
// com.sun.security.auth.PolicyFile.class); // specifically included in 3 |
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41637
diff
changeset
|
224 |
|
15724 | 225 |
init(Profile.COMPACT3, |
42822
a84956e7ee4d
8170987: Module system implementation refresh (12/2016)
alanb
parents:
41637
diff
changeset
|
226 |
javax.sql.rowset.Predicate.class); |
15724 | 227 |
|
228 |
init(Profile.DEFAULT, |
|
23104
f2504b690dc5
8033366: Add configure option to allow RMIConnector IIOP transport be selected compiled in or not
alanb
parents:
22151
diff
changeset
|
229 |
java.beans.BeanInfo.class); |
15724 | 230 |
} |
231 |
||
232 |
void init(Profile p, Class<?>... classes) { |
|
233 |
List<JavaFileObject> srcs = new ArrayList<JavaFileObject>(); |
|
234 |
for (Class<?> c: classes) { |
|
235 |
String name = "T" + c.getSimpleName(); |
|
236 |
String src = |
|
237 |
"class T" + name + "{" + "\n" + |
|
238 |
" Class<?> c = " + c.getName() + ".class;\n" + |
|
239 |
"}"; |
|
240 |
srcs.add(new StringJavaFileObject(name + ".java", src)); |
|
241 |
} |
|
242 |
testClasses.put(p, srcs); |
|
243 |
} |
|
244 |
||
245 |
void checkDiags(String msg, List<Diagnostic<? extends JavaFileObject>> diags, List<String> expectDiagCodes) { |
|
246 |
System.err.print(msg); |
|
247 |
if (diags.isEmpty()) |
|
248 |
System.err.println(" OK"); |
|
249 |
else { |
|
250 |
System.err.println(); |
|
251 |
System.err.println(diags); |
|
252 |
} |
|
253 |
||
254 |
List<String> foundDiagCodes = new ArrayList<String>(); |
|
255 |
for (Diagnostic<? extends JavaFileObject> d: diags) |
|
256 |
foundDiagCodes.add(d.getCode()); |
|
257 |
||
258 |
if (!foundDiagCodes.equals(expectDiagCodes)) { |
|
259 |
System.err.println("Found diag codes: " + foundDiagCodes); |
|
260 |
System.err.println("Expected diag codes: " + expectDiagCodes); |
|
261 |
error("expected diagnostics not found"); |
|
262 |
} |
|
263 |
} |
|
264 |
||
265 |
/** Marker annotation for test cases. */ |
|
266 |
@Retention(RetentionPolicy.RUNTIME) |
|
267 |
@interface Test { } |
|
268 |
||
269 |
/** Run all test cases. */ |
|
270 |
void run() throws Exception { |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
26264
diff
changeset
|
271 |
try { |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
26264
diff
changeset
|
272 |
initTestClasses(); |
15724 | 273 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
26264
diff
changeset
|
274 |
for (Method m: getClass().getDeclaredMethods()) { |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
26264
diff
changeset
|
275 |
Annotation a = m.getAnnotation(Test.class); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
26264
diff
changeset
|
276 |
if (a != null) { |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
26264
diff
changeset
|
277 |
System.err.println(m.getName()); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
26264
diff
changeset
|
278 |
try { |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
26264
diff
changeset
|
279 |
m.invoke(this, new Object[] { }); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
26264
diff
changeset
|
280 |
} catch (InvocationTargetException e) { |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
26264
diff
changeset
|
281 |
Throwable cause = e.getCause(); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
26264
diff
changeset
|
282 |
throw (cause instanceof Exception) ? ((Exception) cause) : e; |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
26264
diff
changeset
|
283 |
} |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
26264
diff
changeset
|
284 |
System.err.println(); |
15724 | 285 |
} |
286 |
} |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
26264
diff
changeset
|
287 |
|
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
26264
diff
changeset
|
288 |
if (errors > 0) |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
26264
diff
changeset
|
289 |
throw new Exception(errors + " errors occurred"); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
26264
diff
changeset
|
290 |
} finally { |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
26264
diff
changeset
|
291 |
fm.close(); |
15724 | 292 |
} |
293 |
} |
|
294 |
||
295 |
void error(String msg) { |
|
296 |
System.err.println("Error: " + msg); |
|
297 |
errors++; |
|
298 |
} |
|
299 |
||
300 |
int errors; |
|
301 |
||
302 |
private static class StringJavaFileObject extends SimpleJavaFileObject { |
|
303 |
StringJavaFileObject(String name, String text) { |
|
304 |
super(URI.create(name), JavaFileObject.Kind.SOURCE); |
|
305 |
this.text = text; |
|
306 |
} |
|
307 |
@Override |
|
308 |
public CharSequence getCharContent(boolean b) { |
|
309 |
return text; |
|
310 |
} |
|
311 |
private String text; |
|
312 |
} |
|
313 |
} |