test/tools/javac/policy/test3/Test.java

Tue, 31 Mar 2009 11:16:15 -0700

author
jjg
date
Tue, 31 Mar 2009 11:16:15 -0700
changeset 257
af10262bd031
child 554
9d9f26857129
permissions
-rw-r--r--

6813059: replace use of JavaCompiler.errorCount with shouldContinue
Reviewed-by: mcimadamore

jjg@257 1 /*
jjg@257 2 * Copyright 2009 Sun Microsystems, Inc. All Rights Reserved.
jjg@257 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@257 4 *
jjg@257 5 * This code is free software; you can redistribute it and/or modify it
jjg@257 6 * under the terms of the GNU General Public License version 2 only, as
jjg@257 7 * published by the Free Software Foundation.
jjg@257 8 *
jjg@257 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@257 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@257 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@257 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@257 13 * accompanied this code).
jjg@257 14 *
jjg@257 15 * You should have received a copy of the GNU General Public License version
jjg@257 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@257 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@257 18 *
jjg@257 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
jjg@257 20 * CA 95054 USA or visit www.sun.com if you need additional information or
jjg@257 21 * have any questions.
jjg@257 22 */
jjg@257 23
jjg@257 24
jjg@257 25 /* @test
jjg@257 26 * @bug 6813059
jjg@257 27 * @summary
jjg@257 28 */
jjg@257 29
jjg@257 30 import java.io.*;
jjg@257 31 import java.util.*;
jjg@257 32
jjg@257 33 // Simple test of -XDshouldStopPolicy.
jjg@257 34 // For each of the permissable values, we compile a file with an error in it,
jjg@257 35 // then using -XDverboseCompilePolicy we check that the compilation gets as
jjg@257 36 // far as expected, but no further.
jjg@257 37
jjg@257 38 public class Test {
jjg@257 39 enum ShouldStopPolicy {
jjg@257 40 BLANK(false, null, "attr"),
jjg@257 41 PROCESS(true, null, "attr"),
jjg@257 42 ATTR(true, "attr", "flow"),
jjg@257 43 FLOW(true, "flow", "desugar"),
jjg@257 44 TRANSTYPES(true, "desugar", "generate"),
jjg@257 45 LOWER(true, "desugar", "generate"),
jjg@257 46 GENERATE(true, "generate", null);
jjg@257 47 ShouldStopPolicy(boolean needOption, String expect, String dontExpect) {
jjg@257 48 this.needOption = needOption;
jjg@257 49 this.expect = expect;
jjg@257 50 this.dontExpect = dontExpect;
jjg@257 51 }
jjg@257 52 boolean needOption;
jjg@257 53 String expect;
jjg@257 54 String dontExpect;
jjg@257 55 }
jjg@257 56
jjg@257 57 enum CompilePolicy {
jjg@257 58 BYFILE,
jjg@257 59 BYTODO
jjg@257 60 }
jjg@257 61
jjg@257 62 public static void main(String... args) throws Exception {
jjg@257 63 new Test().run();
jjg@257 64 }
jjg@257 65
jjg@257 66 public void run() throws Exception {
jjg@257 67 for (CompilePolicy cp: CompilePolicy.values()) {
jjg@257 68 for (ShouldStopPolicy ssp: ShouldStopPolicy.values()) {
jjg@257 69 test(cp, ssp);
jjg@257 70 }
jjg@257 71 }
jjg@257 72
jjg@257 73 if (errors > 0)
jjg@257 74 throw new Exception(errors + " errors occurred");
jjg@257 75 }
jjg@257 76
jjg@257 77 public void test(CompilePolicy cp, ShouldStopPolicy ssp) {
jjg@257 78 System.err.println();
jjg@257 79 System.err.println("test " + cp + " " + ssp);
jjg@257 80 List<String> args = new ArrayList<String>();
jjg@257 81 args.add("-XDverboseCompilePolicy");
jjg@257 82 args.add("-XDcompilePolicy=" + cp.toString().toLowerCase());
jjg@257 83 args.add("-d");
jjg@257 84 args.add(".");
jjg@257 85 if (ssp.needOption)
jjg@257 86 args.add("-XDshouldStopPolicy=" + ssp);
jjg@257 87 args.add(new File(System.getProperty("test.src", "."), "A.java").getPath());
jjg@257 88
jjg@257 89 StringWriter sw = new StringWriter();
jjg@257 90 PrintWriter pw = new PrintWriter(sw);
jjg@257 91 System.err.println("compile " + args);
jjg@257 92 int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw);
jjg@257 93 if (rc == 0)
jjg@257 94 throw new Error("compilation succeeded unexpectedly");
jjg@257 95 //System.err.println(sw);
jjg@257 96
jjg@257 97 // The following is a workaround for the current javac implementation,
jjg@257 98 // that in bytodo mode, it will still attribute files after syntax errors.
jjg@257 99 // Changing that behavior may surprise existing users, so for now, we
jjg@257 100 // work around it.
jjg@257 101 if (cp == CompilePolicy.BYTODO && ssp == ShouldStopPolicy.PROCESS)
jjg@257 102 ssp = ShouldStopPolicy.ATTR;
jjg@257 103
jjg@257 104 boolean foundExpected = (ssp.expect == null);
jjg@257 105 String[] lines = sw.toString().split("\n");
jjg@257 106 for (String line: lines) {
jjg@257 107 if (ssp.expect != null && line.startsWith("[" + ssp.expect))
jjg@257 108 foundExpected = true;
jjg@257 109 if (ssp.dontExpect != null && line.startsWith("[" + ssp.dontExpect)) {
jjg@257 110 error("Unexpected output: " + ssp.dontExpect + "\n" + sw);
jjg@257 111 return;
jjg@257 112 }
jjg@257 113 }
jjg@257 114
jjg@257 115 if (!foundExpected)
jjg@257 116 error("Expected output not found: " + ssp.expect + "\n" + sw);
jjg@257 117 }
jjg@257 118
jjg@257 119 void error(String message) {
jjg@257 120 System.err.println(message);
jjg@257 121 errors++;
jjg@257 122 }
jjg@257 123
jjg@257 124 int errors;
jjg@257 125 }
jjg@257 126
jjg@257 127
jjg@257 128
jjg@257 129
jjg@257 130
jjg@257 131
jjg@257 132
jjg@257 133
jjg@257 134
jjg@257 135
jjg@257 136
jjg@257 137
jjg@257 138 // These tests test the ability of the compiler to continue in the face of
jjg@257 139 // errors, accordining to the shouldStopPolicy
jjg@257 140
jjg@257 141 /* @ test /nodynamiccopyright/
jjg@257 142 * @bug 6813059
jjg@257 143 * @summary
jjg@257 144 * @compile/fail/ref=flow.out -XDrawDiagnostics -XDcompilePolicy=byfile -XDverboseCompilePolicy -XDshouldStopPolicy=FLOW Test.java
jjg@257 145
jjg@257 146 * @compile/fail/ref=default.out -XDrawDiagnostics -XDcompilePolicy=byfile -XDverboseCompilePolicy Test.java
jjg@257 147 * @compile/fail/ref=enter.out -XDrawDiagnostics -XDcompilePolicy=byfile -XDverboseCompilePolicy -XDshouldStopPolicy=ENTER Test.java
jjg@257 148 * @compile/fail/ref=attr.out -XDrawDiagnostics -XDcompilePolicy=byfile -XDverboseCompilePolicy -XDshouldStopPolicy=ATTR Test.java
jjg@257 149 * @compile/fail/ref=transtypes.out -XDrawDiagnostics -XDcompilePolicy=byfile -XDverboseCompilePolicy -XDshouldStopPolicy=TRANSTYPES Test.java
jjg@257 150 * @compile/fail/ref=lower.out -XDrawDiagnostics -XDcompilePolicy=byfile -XDverboseCompilePolicy -XDshouldStopPolicy=LOWER Test.java
jjg@257 151 * @compile/fail/ref=generate.out -XDrawDiagnostics -XDcompilePolicy=byfile -XDverboseCompilePolicy -XDshouldStopPolicy=GENERATE Test.java
jjg@257 152 */
jjg@257 153
jjg@257 154 /*
jjg@257 155 class Test {
jjg@257 156 void m1() {
jjg@257 157 System.err.println("hello");
jjg@257 158 0 // syntax error
jjg@257 159 System.err.println("world");
jjg@257 160 }
jjg@257 161
jjg@257 162 void m2() {
jjg@257 163 }
jjg@257 164 }
jjg@257 165
jjg@257 166 class Test2 {
jjg@257 167 }
jjg@257 168 */
jjg@257 169

mercurial