Mon, 13 Dec 2010 14:56:50 +0000
7006109: Add test library to simplify the task of writing automated type-system tests
Summary: Types.java needs to be more stress-tested
Reviewed-by: jjg
1 /*
2 * Copyright (c) 2009, 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 */
25 /* @test
26 * @bug 6813059
27 * @summary
28 */
30 import java.io.*;
31 import java.util.*;
33 // Simple test of -XDshouldStopPolicy.
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.
38 public 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 }
57 enum CompilePolicy {
58 BYFILE,
59 BYTODO
60 }
62 public static void main(String... args) throws Exception {
63 new Test().run();
64 }
66 public void run() throws Exception {
67 for (CompilePolicy cp: CompilePolicy.values()) {
68 for (ShouldStopPolicy ssp: ShouldStopPolicy.values()) {
69 test(cp, ssp);
70 }
71 }
73 if (errors > 0)
74 throw new Exception(errors + " errors occurred");
75 }
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("-XDshouldStopPolicy=" + ssp);
87 args.add(new File(System.getProperty("test.src", "."), "A.java").getPath());
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);
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;
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 }
115 if (!foundExpected)
116 error("Expected output not found: " + ssp.expect + "\n" + sw);
117 }
119 void error(String message) {
120 System.err.println(message);
121 errors++;
122 }
124 int errors;
125 }
138 // These tests test the ability of the compiler to continue in the face of
139 // errors, accordining to the shouldStopPolicy
141 /* @ test /nodynamiccopyright/
142 * @bug 6813059
143 * @summary
144 * @compile/fail/ref=flow.out -XDrawDiagnostics -XDcompilePolicy=byfile -XDverboseCompilePolicy -XDshouldStopPolicy=FLOW Test.java
146 * @compile/fail/ref=default.out -XDrawDiagnostics -XDcompilePolicy=byfile -XDverboseCompilePolicy Test.java
147 * @compile/fail/ref=enter.out -XDrawDiagnostics -XDcompilePolicy=byfile -XDverboseCompilePolicy -XDshouldStopPolicy=ENTER Test.java
148 * @compile/fail/ref=attr.out -XDrawDiagnostics -XDcompilePolicy=byfile -XDverboseCompilePolicy -XDshouldStopPolicy=ATTR Test.java
149 * @compile/fail/ref=transtypes.out -XDrawDiagnostics -XDcompilePolicy=byfile -XDverboseCompilePolicy -XDshouldStopPolicy=TRANSTYPES Test.java
150 * @compile/fail/ref=lower.out -XDrawDiagnostics -XDcompilePolicy=byfile -XDverboseCompilePolicy -XDshouldStopPolicy=LOWER Test.java
151 * @compile/fail/ref=generate.out -XDrawDiagnostics -XDcompilePolicy=byfile -XDverboseCompilePolicy -XDshouldStopPolicy=GENERATE Test.java
152 */
154 /*
155 class Test {
156 void m1() {
157 System.err.println("hello");
158 0 // syntax error
159 System.err.println("world");
160 }
162 void m2() {
163 }
164 }
166 class Test2 {
167 }
168 */