Test.java revision 2933:49d207bf704d
1/* 2 * Copyright (c) 2009, 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 6813059 27 * @summary 28 * @modules jdk.compiler 29 */ 30 31import java.io.*; 32import java.util.*; 33 34// Simple test of -XDshouldStopPolicy. 35// For each of the permissable values, we compile a file with an error in it, 36// then using -XDverboseCompilePolicy we check that the compilation gets as 37// far as expected, but no further. 38 39public class Test { 40 enum ShouldStopPolicy { 41 BLANK(false, null, "attr"), 42 PROCESS(true, null, "attr"), 43 ATTR(true, "attr", "flow"), 44 FLOW(true, "flow", "desugar"), 45 TRANSTYPES(true, "desugar", "generate"), 46 LOWER(true, "desugar", "generate"), 47 GENERATE(true, "generate", null); 48 ShouldStopPolicy(boolean needOption, String expect, String dontExpect) { 49 this.needOption = needOption; 50 this.expect = expect; 51 this.dontExpect = dontExpect; 52 } 53 boolean needOption; 54 String expect; 55 String dontExpect; 56 } 57 58 enum CompilePolicy { 59 BYFILE, 60 BYTODO 61 } 62 63 public static void main(String... args) throws Exception { 64 new Test().run(); 65 } 66 67 public void run() throws Exception { 68 for (CompilePolicy cp: CompilePolicy.values()) { 69 for (ShouldStopPolicy ssp: ShouldStopPolicy.values()) { 70 test(cp, ssp); 71 } 72 } 73 74 if (errors > 0) 75 throw new Exception(errors + " errors occurred"); 76 } 77 78 public void test(CompilePolicy cp, ShouldStopPolicy ssp) { 79 System.err.println(); 80 System.err.println("test " + cp + " " + ssp); 81 List<String> args = new ArrayList<String>(); 82 args.add("-XDverboseCompilePolicy"); 83 args.add("-XDcompilePolicy=" + cp.toString().toLowerCase()); 84 args.add("-d"); 85 args.add("."); 86 if (ssp.needOption) 87 args.add("-XDshouldStopPolicy=" + ssp); 88 args.add(new File(System.getProperty("test.src", "."), "A.java").getPath()); 89 90 StringWriter sw = new StringWriter(); 91 PrintWriter pw = new PrintWriter(sw); 92 System.err.println("compile " + args); 93 int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw); 94 if (rc == 0) 95 throw new Error("compilation succeeded unexpectedly"); 96 //System.err.println(sw); 97 98 // The following is a workaround for the current javac implementation, 99 // that in bytodo mode, it will still attribute files after syntax errors. 100 // Changing that behavior may surprise existing users, so for now, we 101 // work around it. 102 if (cp == CompilePolicy.BYTODO && ssp == ShouldStopPolicy.PROCESS) 103 ssp = ShouldStopPolicy.ATTR; 104 105 boolean foundExpected = (ssp.expect == null); 106 String[] lines = sw.toString().split("\n"); 107 for (String line: lines) { 108 if (ssp.expect != null && line.startsWith("[" + ssp.expect)) 109 foundExpected = true; 110 if (ssp.dontExpect != null && line.startsWith("[" + ssp.dontExpect)) { 111 error("Unexpected output: " + ssp.dontExpect + "\n" + sw); 112 return; 113 } 114 } 115 116 if (!foundExpected) 117 error("Expected output not found: " + ssp.expect + "\n" + sw); 118 } 119 120 void error(String message) { 121 System.err.println(message); 122 errors++; 123 } 124 125 int errors; 126} 127