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