1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/profiles/ProfileOptionTest.java Mon Jan 21 01:27:42 2013 -0500 1.3 @@ -0,0 +1,268 @@ 1.4 +/* 1.5 + * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 8004182 1.30 + * @summary Add support for profiles in javac 1.31 + */ 1.32 + 1.33 +import java.io.PrintWriter; 1.34 +import java.io.StringWriter; 1.35 +import java.lang.annotation.Annotation; 1.36 +import java.lang.annotation.Retention; 1.37 +import java.lang.annotation.RetentionPolicy; 1.38 +import java.lang.reflect.InvocationTargetException; 1.39 +import java.lang.reflect.Method; 1.40 +import java.net.URI; 1.41 +import java.util.ArrayList; 1.42 +import java.util.Arrays; 1.43 +import java.util.Collections; 1.44 +import java.util.EnumMap; 1.45 +import java.util.List; 1.46 +import java.util.Map; 1.47 + 1.48 +import javax.tools.Diagnostic; 1.49 +import javax.tools.DiagnosticCollector; 1.50 +import javax.tools.JavaCompiler; 1.51 +import javax.tools.JavaFileObject; 1.52 +import javax.tools.SimpleJavaFileObject; 1.53 +import javax.tools.StandardJavaFileManager; 1.54 + 1.55 +import com.sun.source.util.JavacTask; 1.56 +import com.sun.tools.javac.api.JavacTool; 1.57 +import com.sun.tools.javac.jvm.Profile; 1.58 +import com.sun.tools.javac.jvm.Target; 1.59 + 1.60 + 1.61 +public class ProfileOptionTest { 1.62 + public static void main(String... args) throws Exception { 1.63 + new ProfileOptionTest().run(); 1.64 + } 1.65 + 1.66 + private final JavaCompiler javac = JavacTool.create(); 1.67 + private final StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null); 1.68 + 1.69 + 1.70 + // ---------- Test cases, invoked reflectively via run. ---------- 1.71 + 1.72 + @Test 1.73 + void testInvalidProfile_CommandLine() throws Exception { 1.74 + JavaFileObject fo = new StringJavaFileObject("Test.java", "class Test { }"); 1.75 + String badName = "foo"; 1.76 + List<String> opts = Arrays.asList("-profile", badName); 1.77 + StringWriter sw = new StringWriter(); 1.78 + try { 1.79 + JavacTask task = (JavacTask) javac.getTask(sw, fm, null, opts, null, 1.80 + Arrays.asList(fo)); 1.81 + throw new Exception("expected exception not thrown"); 1.82 + } catch (IllegalArgumentException e) { 1.83 + // expected 1.84 + } 1.85 + } 1.86 + 1.87 + @Test 1.88 + void testInvalidProfile_API() throws Exception { 1.89 + String badName = "foo"; 1.90 + String[] opts = { "-profile", badName }; 1.91 + StringWriter sw = new StringWriter(); 1.92 + PrintWriter pw = new PrintWriter(sw); 1.93 + int rc = com.sun.tools.javac.Main.compile(opts, pw); 1.94 + 1.95 + // sadly, command line errors are not (yet?) reported to 1.96 + // the diag listener 1.97 + String out = sw.toString(); 1.98 + if (!out.isEmpty()) 1.99 + System.err.println(out.trim()); 1.100 + 1.101 + if (!out.contains("invalid profile: " + badName)) { 1.102 + error("expected message not found"); 1.103 + } 1.104 + } 1.105 + 1.106 + @Test 1.107 + void testTargetProfileCombinations() throws Exception { 1.108 + JavaFileObject fo = new StringJavaFileObject("Test.java", "class Test { }"); 1.109 + for (Target t: Target.values()) { 1.110 + switch (t) { 1.111 + case JDK1_1: case JDK1_2: // no equivalent -source 1.112 + case JDK1_4_1: case JDK1_4_2: case JSR14: // transitional values 1.113 + continue; 1.114 + } 1.115 + 1.116 + for (Profile p: Profile.values()) { 1.117 + List<String> opts = new ArrayList<String>(); 1.118 + opts.addAll(Arrays.asList("-source", t.name, "-target", t.name)); 1.119 + opts.add("-Xlint:-options"); // dont warn about no -bootclasspath 1.120 + if (p != Profile.DEFAULT) 1.121 + opts.addAll(Arrays.asList("-profile", p.name)); 1.122 + StringWriter sw = new StringWriter(); 1.123 + JavacTask task = (JavacTask) javac.getTask(sw, fm, null, opts, null, 1.124 + Arrays.asList(fo)); 1.125 + task.analyze(); 1.126 + 1.127 + // sadly, command line errors are not (yet?) reported to 1.128 + // the diag listener 1.129 + String out = sw.toString(); 1.130 + if (!out.isEmpty()) 1.131 + System.err.println(out.trim()); 1.132 + 1.133 + switch (t) { 1.134 + case JDK1_8: 1.135 + if (!out.isEmpty()) 1.136 + error("unexpected output from compiler"); 1.137 + break; 1.138 + default: 1.139 + if (p != Profile.DEFAULT 1.140 + && !out.contains("profile " + p.name 1.141 + + " is not valid for target release " + t.name)) { 1.142 + error("expected message not found"); 1.143 + } 1.144 + } 1.145 + } 1.146 + } 1.147 + } 1.148 + 1.149 + @Test 1.150 + void testClassesInProfiles() throws Exception { 1.151 + for (Profile p: Profile.values()) { 1.152 + for (Map.Entry<Profile, List<JavaFileObject>> e: testClasses.entrySet()) { 1.153 + for (JavaFileObject fo: e.getValue()) { 1.154 + DiagnosticCollector<JavaFileObject> dl = 1.155 + new DiagnosticCollector<JavaFileObject>(); 1.156 + List<String> opts = (p == Profile.DEFAULT) 1.157 + ? Collections.<String>emptyList() 1.158 + : Arrays.asList("-profile", p.name); 1.159 + JavacTask task = (JavacTask) javac.getTask(null, fm, dl, opts, null, 1.160 + Arrays.asList(fo)); 1.161 + task.analyze(); 1.162 + 1.163 + List<String> expectDiagCodes = (p.value >= e.getKey().value) 1.164 + ? Collections.<String>emptyList() 1.165 + : Arrays.asList("compiler.err.not.in.profile"); 1.166 + 1.167 + checkDiags(opts + " " + fo.getName(), dl.getDiagnostics(), expectDiagCodes); 1.168 + } 1.169 + } 1.170 + } 1.171 + } 1.172 + 1.173 + Map<Profile, List<JavaFileObject>> testClasses = 1.174 + new EnumMap<Profile, List<JavaFileObject>>(Profile.class); 1.175 + 1.176 + void initTestClasses() { 1.177 + // The following table assumes the existence of specific classes 1.178 + // in specific profiles, as defined in the Java SE 8 spec. 1.179 + init(Profile.COMPACT1, 1.180 + java.lang.String.class); 1.181 + 1.182 + init(Profile.COMPACT2, 1.183 + javax.xml.XMLConstants.class); 1.184 + 1.185 + init(Profile.COMPACT3, 1.186 + javax.script.Bindings.class, 1.187 + com.sun.security.auth.PolicyFile.class); // specifically included in 3 1.188 + 1.189 + init(Profile.DEFAULT, 1.190 + java.beans.BeanInfo.class, 1.191 + javax.management.remote.rmi._RMIServer_Stub.class); // specifically excluded in 3 1.192 + } 1.193 + 1.194 + void init(Profile p, Class<?>... classes) { 1.195 + List<JavaFileObject> srcs = new ArrayList<JavaFileObject>(); 1.196 + for (Class<?> c: classes) { 1.197 + String name = "T" + c.getSimpleName(); 1.198 + String src = 1.199 + "class T" + name + "{" + "\n" + 1.200 + " Class<?> c = " + c.getName() + ".class;\n" + 1.201 + "}"; 1.202 + srcs.add(new StringJavaFileObject(name + ".java", src)); 1.203 + } 1.204 + testClasses.put(p, srcs); 1.205 + } 1.206 + 1.207 + void checkDiags(String msg, List<Diagnostic<? extends JavaFileObject>> diags, List<String> expectDiagCodes) { 1.208 + System.err.print(msg); 1.209 + if (diags.isEmpty()) 1.210 + System.err.println(" OK"); 1.211 + else { 1.212 + System.err.println(); 1.213 + System.err.println(diags); 1.214 + } 1.215 + 1.216 + List<String> foundDiagCodes = new ArrayList<String>(); 1.217 + for (Diagnostic<? extends JavaFileObject> d: diags) 1.218 + foundDiagCodes.add(d.getCode()); 1.219 + 1.220 + if (!foundDiagCodes.equals(expectDiagCodes)) { 1.221 + System.err.println("Found diag codes: " + foundDiagCodes); 1.222 + System.err.println("Expected diag codes: " + expectDiagCodes); 1.223 + error("expected diagnostics not found"); 1.224 + } 1.225 + } 1.226 + 1.227 + /** Marker annotation for test cases. */ 1.228 + @Retention(RetentionPolicy.RUNTIME) 1.229 + @interface Test { } 1.230 + 1.231 + /** Run all test cases. */ 1.232 + void run() throws Exception { 1.233 + initTestClasses(); 1.234 + 1.235 + for (Method m: getClass().getDeclaredMethods()) { 1.236 + Annotation a = m.getAnnotation(Test.class); 1.237 + if (a != null) { 1.238 + System.err.println(m.getName()); 1.239 + try { 1.240 + m.invoke(this, new Object[] { }); 1.241 + } catch (InvocationTargetException e) { 1.242 + Throwable cause = e.getCause(); 1.243 + throw (cause instanceof Exception) ? ((Exception) cause) : e; 1.244 + } 1.245 + System.err.println(); 1.246 + } 1.247 + } 1.248 + 1.249 + if (errors > 0) 1.250 + throw new Exception(errors + " errors occurred"); 1.251 + } 1.252 + 1.253 + void error(String msg) { 1.254 + System.err.println("Error: " + msg); 1.255 + errors++; 1.256 + } 1.257 + 1.258 + int errors; 1.259 + 1.260 + private static class StringJavaFileObject extends SimpleJavaFileObject { 1.261 + StringJavaFileObject(String name, String text) { 1.262 + super(URI.create(name), JavaFileObject.Kind.SOURCE); 1.263 + this.text = text; 1.264 + } 1.265 + @Override 1.266 + public CharSequence getCharContent(boolean b) { 1.267 + return text; 1.268 + } 1.269 + private String text; 1.270 + } 1.271 +}