langtools/test/tools/javac/versions/SourceTargetTest.java
changeset 25453 be80cf0463b3
child 30730 d3ce7619db2c
equal deleted inserted replaced
25452:581d57f5c7d8 25453:be80cf0463b3
       
     1 /*
       
     2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
       
     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 8050106
       
    27  * @summary JavaCompiler relies on inappropriate result from comparison
       
    28  */
       
    29 
       
    30 import java.io.*;
       
    31 import java.util.*;
       
    32 import com.sun.tools.javac.code.Source;
       
    33 import com.sun.tools.javac.jvm.Target;
       
    34 
       
    35 public class SourceTargetTest {
       
    36     public static void main(String... args) throws Exception {
       
    37         SourceTargetTest t = new SourceTargetTest();
       
    38         t.run();
       
    39     }
       
    40 
       
    41     public void run() throws Exception {
       
    42         try (FileWriter out = new FileWriter("C.java")) {
       
    43             out.write("class C { }");
       
    44         }
       
    45 
       
    46         for (Source s: Source.values()) {
       
    47             test(s, null, "source", getKind(s, Source.MIN));
       
    48         }
       
    49 
       
    50         for (Target t: Target.values()) {
       
    51             test(Source.values()[0], t, "target", getKind(t, Target.MIN));
       
    52         }
       
    53 
       
    54         if (errors > 0)
       
    55             throw new Exception(errors + " errors occurred");
       
    56     }
       
    57 
       
    58     void test(Source s, Target t, String select, Kind kind) {
       
    59         System.err.println("Test: source:" + s + ", target:" + t + " " + select + " " + kind);
       
    60         List<String> args = new ArrayList<>();
       
    61         args.add("-XDrawDiagnostics");
       
    62         args.add("-source");
       
    63         args.add(s.name);
       
    64         if (t != null) {
       
    65             args.add("-target");
       
    66             args.add(t.name);
       
    67         }
       
    68         args.add("C.java");
       
    69 
       
    70         StringWriter sw = new StringWriter();
       
    71         PrintWriter pw = new PrintWriter(sw);
       
    72         int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw);
       
    73         pw.close();
       
    74         String out = sw.toString();
       
    75         System.err.print(out);
       
    76 
       
    77         switch (kind) {
       
    78             case INVALID:
       
    79                 check(out, "removed." + select, true);
       
    80                 check(out, "obsolete." + select, false);
       
    81                 break;
       
    82 
       
    83             case OBSOLETE:
       
    84                 check(out, "removed." + select, false);
       
    85                 check(out, "obsolete." + select, true);
       
    86                 break;
       
    87 
       
    88             case VALID:
       
    89                 check(out, "removed." + select, false);
       
    90                 check(out, "obsolete." + select, false);
       
    91                 break;
       
    92         }
       
    93 
       
    94         System.err.println();
       
    95     }
       
    96 
       
    97     enum Kind { INVALID, OBSOLETE, VALID };
       
    98 
       
    99     <T extends Comparable<T>> Kind getKind(T t1, T t2) {
       
   100         if (t1.compareTo(t2) < 0)
       
   101             return Kind.INVALID;
       
   102         else if (t1 == t2)
       
   103             return Kind.OBSOLETE;
       
   104         else
       
   105             return Kind.VALID;
       
   106     }
       
   107 
       
   108     void check(String out, String text, boolean expect) {
       
   109         if (out.contains(text) == expect) {
       
   110             if (expect)
       
   111                 System.err.println("expected string found: " + text);
       
   112             else
       
   113                 System.err.println("string not found, as expected: " + text);
       
   114         } else {
       
   115             if (expect)
       
   116                 error("expected string not found: " + text);
       
   117             else
       
   118                 error("unexpected string found: " + text);
       
   119         }
       
   120     }
       
   121 
       
   122     void error(String msg) {
       
   123         System.err.println("error: " + msg);
       
   124         errors++;
       
   125     }
       
   126 
       
   127     int errors;
       
   128 }