< prev index next >

test/tools/javac/profiles/ProfileOptionTest.java

Print this page




  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
  75     void testInvalidProfile_API() throws Exception {
  76         JavaFileObject fo = new StringJavaFileObject("Test.java", "class Test { }");
  77         String badName = "foo";
  78         List<String> opts = Arrays.asList("-profile", badName);
  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
  90     void testInvalidProfile_CommandLine() throws Exception {
  91         String badName = "foo";
  92         String[] opts = { "-profile", badName };
  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) {


 127 
 128                 IllegalStateException ise;
 129                 StringWriter sw = new StringWriter();
 130                 try {
 131                     JavacTask task = (JavacTask) javac.getTask(sw, fm, null, opts, null,
 132                             Arrays.asList(fo));
 133                     task.analyze();
 134                     ise = null;
 135                 } catch (IllegalStateException e) {
 136                     ise = e;
 137                 }
 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:
 147                     case JDK1_9:
 148                         if (ise != null)
 149                             error("unexpected exception from compiler: " + ise);
 150                         break;










 151                     default:
 152                         if (p == Profile.DEFAULT)
 153                             break;
 154                         if (ise == null)
 155                             error("IllegalStateException not thrown as expected");
 156                         else if (!ise.getMessage().contains("profile " + p.name
 157                                     + " is not valid for target release " + t.name)) {
 158                             error("exception not thrown as expected: " + ise);
 159                         }

 160                 }
 161             }
 162         }
 163     }
 164 
 165     @Test
 166     void testClassesInProfiles() throws Exception {
 167         for (Profile p: Profile.values()) {
 168             for (Map.Entry<Profile, List<JavaFileObject>> e: testClasses.entrySet()) {
 169                 for (JavaFileObject fo: e.getValue()) {
 170                     DiagnosticCollector<JavaFileObject> dl =
 171                             new DiagnosticCollector<JavaFileObject>();
 172                     List<String> opts = (p == Profile.DEFAULT)
 173                             ? Collections.<String>emptyList()
 174                             : Arrays.asList("-profile", p.name);
 175                     JavacTask task = (JavacTask) javac.getTask(null, fm, dl, opts, null,
 176                             Arrays.asList(fo));
 177                     task.analyze();
 178 
 179                     List<String> expectDiagCodes = new ArrayList<>();
 180                     if (fo.getName().equals("TPolicyFile.java")) {
 181                         expectDiagCodes.add("compiler.warn.has.been.deprecated.for.removal");
 182                     }
 183 
 184                     if (p.value < e.getKey().value) {
 185                         expectDiagCodes.add("compiler.err.not.in.profile");
 186                     }
 187 
 188                     checkDiags(opts + " " + fo.getName(), dl.getDiagnostics(), expectDiagCodes);
 189                 }
 190             }
 191         }
 192     }
 193 
 194     Map<Profile, List<JavaFileObject>> testClasses =
 195             new EnumMap<Profile, List<JavaFileObject>>(Profile.class);
 196 
 197     void initTestClasses() {
 198         // The following table assumes the existence of specific classes
 199         // in specific profiles, as defined in the Java SE 8 spec.
 200         init(Profile.COMPACT1,
 201                 java.lang.String.class);
 202 
 203         init(Profile.COMPACT2,
 204                 javax.xml.XMLConstants.class);
 205 




 206         init(Profile.COMPACT3,
 207                 javax.sql.rowset.Predicate.class,
 208                 com.sun.security.auth.PolicyFile.class); // specifically included in 3
 209 
 210         init(Profile.DEFAULT,
 211                 java.beans.BeanInfo.class);
 212     }
 213 
 214     void init(Profile p, Class<?>... classes) {
 215         List<JavaFileObject> srcs = new ArrayList<JavaFileObject>();
 216         for (Class<?> c: classes) {
 217             String name = "T" + c.getSimpleName();
 218             String src =
 219                     "class T" + name + "{" + "\n" +
 220                     "    Class<?> c = " + c.getName() + ".class;\n" +
 221                     "}";
 222             srcs.add(new StringJavaFileObject(name + ".java", src));
 223         }
 224         testClasses.put(p, srcs);
 225     }
 226 
 227     void checkDiags(String msg, List<Diagnostic<? extends JavaFileObject>> diags, List<String> expectDiagCodes) {
 228         System.err.print(msg);




  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
  75     void testInvalidProfile_API() throws Exception {
  76         JavaFileObject fo = new StringJavaFileObject("Test.java", "class Test { }");
  77         String badName = "foo";
  78         List<String> opts = Arrays.asList("--release", "8", "-profile", badName);
  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
  90     void testInvalidProfile_CommandLine() throws Exception {
  91         String badName = "foo";
  92         String[] opts = { "--release", "8", "-profile", badName };
  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) {


 127 
 128                 IllegalStateException ise;
 129                 StringWriter sw = new StringWriter();
 130                 try {
 131                     JavacTask task = (JavacTask) javac.getTask(sw, fm, null, opts, null,
 132                             Arrays.asList(fo));
 133                     task.analyze();
 134                     ise = null;
 135                 } catch (IllegalStateException e) {
 136                     ise = e;
 137                 }
 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:

 147                         if (ise != null)
 148                             error("unexpected exception from compiler: " + ise);
 149                         break;
 150                     case JDK1_9:
 151                         if (p == Profile.DEFAULT)
 152                             break;
 153                         if (ise == null)
 154                             error("IllegalStateException not thrown as expected");
 155                         else if (!ise.getMessage().contains("option -profile " +
 156                                 "not allowed with target " + t.name)) {
 157                             error("exception not thrown as expected: " + ise);
 158                         }
 159                         break;
 160                     default:
 161                         if (p == Profile.DEFAULT)
 162                             break;
 163                         if (ise == null)
 164                             error("IllegalStateException not thrown as expected");
 165                         else if (!ise.getMessage().contains("profile " + p.name
 166                                     + " is not valid for target release " + t.name)) {
 167                             error("exception not thrown as expected: " + ise);
 168                         }
 169                         break;
 170                 }
 171             }
 172         }
 173     }
 174 
 175     @Test
 176     void testClassesInProfiles() throws Exception {
 177         for (Profile p: Profile.values()) {
 178             for (Map.Entry<Profile, List<JavaFileObject>> e: testClasses.entrySet()) {
 179                 for (JavaFileObject fo: e.getValue()) {
 180                     DiagnosticCollector<JavaFileObject> dl =
 181                             new DiagnosticCollector<JavaFileObject>();
 182                     List<String> opts = (p == Profile.DEFAULT)
 183                             ? Collections.<String>emptyList()
 184                             : Arrays.asList("--release", "8", "-profile", p.name);
 185                     JavacTask task = (JavacTask) javac.getTask(null, fm, dl, opts, null,
 186                             Arrays.asList(fo));
 187                     task.analyze();
 188 
 189                     List<String> expectDiagCodes = new ArrayList<>();
 190                     if (fo.getName().equals("TPolicyFile.java")) {
 191                         expectDiagCodes.add("compiler.warn.has.been.deprecated.for.removal");
 192                     }
 193 
 194                     if (p.value < e.getKey().value) {
 195                         expectDiagCodes.add("compiler.err.not.in.profile");
 196                     }
 197 
 198                     checkDiags(opts + " " + fo.getName(), dl.getDiagnostics(), expectDiagCodes);
 199                 }
 200             }
 201         }
 202     }
 203 
 204     Map<Profile, List<JavaFileObject>> testClasses =
 205             new EnumMap<Profile, List<JavaFileObject>>(Profile.class);
 206 
 207     void initTestClasses() {
 208         // The following table assumes the existence of specific classes
 209         // in specific profiles, as defined in the Java SE 8 spec.
 210         init(Profile.COMPACT1,
 211                 java.lang.String.class);
 212 
 213         init(Profile.COMPACT2,
 214                 javax.xml.XMLConstants.class);
 215 
 216         //init(Profile.COMPACT3,
 217         //        javax.sql.rowset.Predicate.class,
 218         //        com.sun.security.auth.PolicyFile.class); // specifically included in 3
 219 
 220         init(Profile.COMPACT3,
 221                 javax.sql.rowset.Predicate.class);

 222 
 223         init(Profile.DEFAULT,
 224                 java.beans.BeanInfo.class);
 225     }
 226 
 227     void init(Profile p, Class<?>... classes) {
 228         List<JavaFileObject> srcs = new ArrayList<JavaFileObject>();
 229         for (Class<?> c: classes) {
 230             String name = "T" + c.getSimpleName();
 231             String src =
 232                     "class T" + name + "{" + "\n" +
 233                     "    Class<?> c = " + c.getName() + ".class;\n" +
 234                     "}";
 235             srcs.add(new StringJavaFileObject(name + ".java", src));
 236         }
 237         testClasses.put(p, srcs);
 238     }
 239 
 240     void checkDiags(String msg, List<Diagnostic<? extends JavaFileObject>> diags, List<String> expectDiagCodes) {
 241         System.err.print(msg);


< prev index next >