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