test/tools/javac/profiles/ProfileOptionTest.java

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 3938
93012e2a5d1d
parent 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset eb6ee6a5f2fe

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 8004182
aoqi@0 27 * @summary Add support for profiles in javac
aoqi@0 28 */
aoqi@0 29
aoqi@0 30 import java.io.PrintWriter;
aoqi@0 31 import java.io.StringWriter;
aoqi@0 32 import java.lang.annotation.Annotation;
aoqi@0 33 import java.lang.annotation.Retention;
aoqi@0 34 import java.lang.annotation.RetentionPolicy;
aoqi@0 35 import java.lang.reflect.InvocationTargetException;
aoqi@0 36 import java.lang.reflect.Method;
aoqi@0 37 import java.net.URI;
aoqi@0 38 import java.util.ArrayList;
aoqi@0 39 import java.util.Arrays;
aoqi@0 40 import java.util.Collections;
aoqi@0 41 import java.util.EnumMap;
aoqi@0 42 import java.util.List;
aoqi@0 43 import java.util.Map;
aoqi@0 44
aoqi@0 45 import javax.tools.Diagnostic;
aoqi@0 46 import javax.tools.DiagnosticCollector;
aoqi@0 47 import javax.tools.JavaCompiler;
aoqi@0 48 import javax.tools.JavaFileObject;
aoqi@0 49 import javax.tools.SimpleJavaFileObject;
aoqi@0 50 import javax.tools.StandardJavaFileManager;
aoqi@0 51
aoqi@0 52 import com.sun.source.util.JavacTask;
aoqi@0 53 import com.sun.tools.javac.api.JavacTool;
aoqi@0 54 import com.sun.tools.javac.jvm.Profile;
aoqi@0 55 import com.sun.tools.javac.jvm.Target;
aoqi@0 56
aoqi@0 57
aoqi@0 58 public class ProfileOptionTest {
aoqi@0 59 public static void main(String... args) throws Exception {
aoqi@0 60 new ProfileOptionTest().run();
aoqi@0 61 }
aoqi@0 62
aoqi@0 63 private final JavaCompiler javac = JavacTool.create();
aoqi@0 64 private final StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null);
aoqi@0 65
aoqi@0 66
aoqi@0 67 // ---------- Test cases, invoked reflectively via run. ----------
aoqi@0 68
aoqi@0 69 @Test
aoqi@0 70 void testInvalidProfile_CommandLine() throws Exception {
aoqi@0 71 JavaFileObject fo = new StringJavaFileObject("Test.java", "class Test { }");
aoqi@0 72 String badName = "foo";
aoqi@0 73 List<String> opts = Arrays.asList("-profile", badName);
aoqi@0 74 StringWriter sw = new StringWriter();
aoqi@0 75 try {
aoqi@0 76 JavacTask task = (JavacTask) javac.getTask(sw, fm, null, opts, null,
aoqi@0 77 Arrays.asList(fo));
aoqi@0 78 throw new Exception("expected exception not thrown");
aoqi@0 79 } catch (IllegalArgumentException e) {
aoqi@0 80 // expected
aoqi@0 81 }
aoqi@0 82 }
aoqi@0 83
aoqi@0 84 @Test
aoqi@0 85 void testInvalidProfile_API() throws Exception {
aoqi@0 86 String badName = "foo";
aoqi@0 87 String[] opts = { "-profile", badName };
aoqi@0 88 StringWriter sw = new StringWriter();
aoqi@0 89 PrintWriter pw = new PrintWriter(sw);
aoqi@0 90 int rc = com.sun.tools.javac.Main.compile(opts, pw);
aoqi@0 91
aoqi@0 92 // sadly, command line errors are not (yet?) reported to
aoqi@0 93 // the diag listener
aoqi@0 94 String out = sw.toString();
aoqi@0 95 if (!out.isEmpty())
aoqi@0 96 System.err.println(out.trim());
aoqi@0 97
aoqi@0 98 if (!out.contains("invalid profile: " + badName)) {
aoqi@0 99 error("expected message not found");
aoqi@0 100 }
aoqi@0 101 }
aoqi@0 102
aoqi@0 103 @Test
aoqi@0 104 void testTargetProfileCombinations() throws Exception {
aoqi@0 105 JavaFileObject fo = new StringJavaFileObject("Test.java", "class Test { }");
aoqi@0 106 for (Target t: Target.values()) {
aoqi@0 107 switch (t) {
aoqi@0 108 case JDK1_1: case JDK1_2: // no equivalent -source
aoqi@0 109 continue;
aoqi@0 110 }
aoqi@0 111
aoqi@0 112 for (Profile p: Profile.values()) {
aoqi@0 113 List<String> opts = new ArrayList<String>();
aoqi@0 114 opts.addAll(Arrays.asList("-source", t.name, "-target", t.name));
aoqi@0 115 opts.add("-Xlint:-options"); // dont warn about no -bootclasspath
aoqi@0 116 if (p != Profile.DEFAULT)
aoqi@0 117 opts.addAll(Arrays.asList("-profile", p.name));
aoqi@0 118 StringWriter sw = new StringWriter();
aoqi@0 119 JavacTask task = (JavacTask) javac.getTask(sw, fm, null, opts, null,
aoqi@0 120 Arrays.asList(fo));
aoqi@0 121 task.analyze();
aoqi@0 122
aoqi@0 123 // sadly, command line errors are not (yet?) reported to
aoqi@0 124 // the diag listener
aoqi@0 125 String out = sw.toString();
aoqi@0 126 if (!out.isEmpty())
aoqi@0 127 System.err.println(out.trim());
aoqi@0 128
aoqi@0 129 switch (t) {
aoqi@0 130 case JDK1_8:
aoqi@0 131 if (!out.isEmpty())
aoqi@0 132 error("unexpected output from compiler");
aoqi@0 133 break;
aoqi@0 134 default:
aoqi@0 135 if (p != Profile.DEFAULT
aoqi@0 136 && !out.contains("profile " + p.name
aoqi@0 137 + " is not valid for target release " + t.name)) {
aoqi@0 138 error("expected message not found");
aoqi@0 139 }
aoqi@0 140 }
aoqi@0 141 }
aoqi@0 142 }
aoqi@0 143 }
aoqi@0 144
aoqi@0 145 @Test
aoqi@0 146 void testClassesInProfiles() throws Exception {
aoqi@0 147 for (Profile p: Profile.values()) {
aoqi@0 148 for (Map.Entry<Profile, List<JavaFileObject>> e: testClasses.entrySet()) {
aoqi@0 149 for (JavaFileObject fo: e.getValue()) {
aoqi@0 150 DiagnosticCollector<JavaFileObject> dl =
aoqi@0 151 new DiagnosticCollector<JavaFileObject>();
aoqi@0 152 List<String> opts = (p == Profile.DEFAULT)
aoqi@0 153 ? Collections.<String>emptyList()
aoqi@0 154 : Arrays.asList("-profile", p.name);
aoqi@0 155 JavacTask task = (JavacTask) javac.getTask(null, fm, dl, opts, null,
aoqi@0 156 Arrays.asList(fo));
aoqi@0 157 task.analyze();
aoqi@0 158
aoqi@0 159 List<String> expectDiagCodes = (p.value >= e.getKey().value)
aoqi@0 160 ? Collections.<String>emptyList()
aoqi@0 161 : Arrays.asList("compiler.err.not.in.profile");
aoqi@0 162
aoqi@0 163 checkDiags(opts + " " + fo.getName(), dl.getDiagnostics(), expectDiagCodes);
aoqi@0 164 }
aoqi@0 165 }
aoqi@0 166 }
aoqi@0 167 }
aoqi@0 168
aoqi@0 169 Map<Profile, List<JavaFileObject>> testClasses =
aoqi@0 170 new EnumMap<Profile, List<JavaFileObject>>(Profile.class);
aoqi@0 171
aoqi@0 172 void initTestClasses() {
aoqi@0 173 // The following table assumes the existence of specific classes
aoqi@0 174 // in specific profiles, as defined in the Java SE 8 spec.
aoqi@0 175 init(Profile.COMPACT1,
aoqi@0 176 java.lang.String.class);
aoqi@0 177
aoqi@0 178 init(Profile.COMPACT2,
aoqi@0 179 javax.xml.XMLConstants.class);
aoqi@0 180
aoqi@0 181 init(Profile.COMPACT3,
aoqi@0 182 javax.sql.rowset.Predicate.class,
aoqi@0 183 com.sun.security.auth.PolicyFile.class); // specifically included in 3
aoqi@0 184
aoqi@0 185 init(Profile.DEFAULT,
aoqi@0 186 java.beans.BeanInfo.class,
aoqi@0 187 javax.management.remote.rmi._RMIServer_Stub.class); // specifically excluded in 3
aoqi@0 188 }
aoqi@0 189
aoqi@0 190 void init(Profile p, Class<?>... classes) {
aoqi@0 191 List<JavaFileObject> srcs = new ArrayList<JavaFileObject>();
aoqi@0 192 for (Class<?> c: classes) {
aoqi@0 193 String name = "T" + c.getSimpleName();
aoqi@0 194 String src =
aoqi@0 195 "class T" + name + "{" + "\n" +
aoqi@0 196 " Class<?> c = " + c.getName() + ".class;\n" +
aoqi@0 197 "}";
aoqi@0 198 srcs.add(new StringJavaFileObject(name + ".java", src));
aoqi@0 199 }
aoqi@0 200 testClasses.put(p, srcs);
aoqi@0 201 }
aoqi@0 202
aoqi@0 203 void checkDiags(String msg, List<Diagnostic<? extends JavaFileObject>> diags, List<String> expectDiagCodes) {
aoqi@0 204 System.err.print(msg);
aoqi@0 205 if (diags.isEmpty())
aoqi@0 206 System.err.println(" OK");
aoqi@0 207 else {
aoqi@0 208 System.err.println();
aoqi@0 209 System.err.println(diags);
aoqi@0 210 }
aoqi@0 211
aoqi@0 212 List<String> foundDiagCodes = new ArrayList<String>();
aoqi@0 213 for (Diagnostic<? extends JavaFileObject> d: diags)
aoqi@0 214 foundDiagCodes.add(d.getCode());
aoqi@0 215
aoqi@0 216 if (!foundDiagCodes.equals(expectDiagCodes)) {
aoqi@0 217 System.err.println("Found diag codes: " + foundDiagCodes);
aoqi@0 218 System.err.println("Expected diag codes: " + expectDiagCodes);
aoqi@0 219 error("expected diagnostics not found");
aoqi@0 220 }
aoqi@0 221 }
aoqi@0 222
aoqi@0 223 /** Marker annotation for test cases. */
aoqi@0 224 @Retention(RetentionPolicy.RUNTIME)
aoqi@0 225 @interface Test { }
aoqi@0 226
aoqi@0 227 /** Run all test cases. */
aoqi@0 228 void run() throws Exception {
aoqi@0 229 initTestClasses();
aoqi@0 230
aoqi@0 231 for (Method m: getClass().getDeclaredMethods()) {
aoqi@0 232 Annotation a = m.getAnnotation(Test.class);
aoqi@0 233 if (a != null) {
aoqi@0 234 System.err.println(m.getName());
aoqi@0 235 try {
aoqi@0 236 m.invoke(this, new Object[] { });
aoqi@0 237 } catch (InvocationTargetException e) {
aoqi@0 238 Throwable cause = e.getCause();
aoqi@0 239 throw (cause instanceof Exception) ? ((Exception) cause) : e;
aoqi@0 240 }
aoqi@0 241 System.err.println();
aoqi@0 242 }
aoqi@0 243 }
aoqi@0 244
aoqi@0 245 if (errors > 0)
aoqi@0 246 throw new Exception(errors + " errors occurred");
aoqi@0 247 }
aoqi@0 248
aoqi@0 249 void error(String msg) {
aoqi@0 250 System.err.println("Error: " + msg);
aoqi@0 251 errors++;
aoqi@0 252 }
aoqi@0 253
aoqi@0 254 int errors;
aoqi@0 255
aoqi@0 256 private static class StringJavaFileObject extends SimpleJavaFileObject {
aoqi@0 257 StringJavaFileObject(String name, String text) {
aoqi@0 258 super(URI.create(name), JavaFileObject.Kind.SOURCE);
aoqi@0 259 this.text = text;
aoqi@0 260 }
aoqi@0 261 @Override
aoqi@0 262 public CharSequence getCharContent(boolean b) {
aoqi@0 263 return text;
aoqi@0 264 }
aoqi@0 265 private String text;
aoqi@0 266 }
aoqi@0 267 }

mercurial