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

changeset 257
af10262bd031
child 554
9d9f26857129
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/policy/test3/Test.java	Tue Mar 31 11:16:15 2009 -0700
     1.3 @@ -0,0 +1,169 @@
     1.4 +/*
     1.5 + * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + */
    1.26 +
    1.27 +
    1.28 +/* @test
    1.29 + * @bug 6813059
    1.30 + * @summary
    1.31 + */
    1.32 +
    1.33 +import java.io.*;
    1.34 +import java.util.*;
    1.35 +
    1.36 +// Simple test of -XDshouldStopPolicy.
    1.37 +// For each of the permissable values, we compile a file with an error in it,
    1.38 +// then using -XDverboseCompilePolicy we check that the compilation gets as
    1.39 +// far as expected, but no further.
    1.40 +
    1.41 +public class Test {
    1.42 +    enum ShouldStopPolicy {
    1.43 +        BLANK(false, null, "attr"),
    1.44 +        PROCESS(true, null, "attr"),
    1.45 +        ATTR(true, "attr", "flow"),
    1.46 +        FLOW(true, "flow", "desugar"),
    1.47 +        TRANSTYPES(true, "desugar", "generate"),
    1.48 +        LOWER(true, "desugar", "generate"),
    1.49 +        GENERATE(true, "generate", null);
    1.50 +        ShouldStopPolicy(boolean needOption, String expect, String dontExpect) {
    1.51 +            this.needOption = needOption;
    1.52 +            this.expect = expect;
    1.53 +            this.dontExpect = dontExpect;
    1.54 +        }
    1.55 +        boolean needOption;
    1.56 +        String expect;
    1.57 +        String dontExpect;
    1.58 +    }
    1.59 +
    1.60 +    enum CompilePolicy {
    1.61 +        BYFILE,
    1.62 +        BYTODO
    1.63 +    }
    1.64 +
    1.65 +    public static void main(String... args) throws Exception {
    1.66 +        new Test().run();
    1.67 +    }
    1.68 +
    1.69 +    public void run() throws Exception {
    1.70 +        for (CompilePolicy cp: CompilePolicy.values()) {
    1.71 +            for (ShouldStopPolicy ssp: ShouldStopPolicy.values()) {
    1.72 +                test(cp, ssp);
    1.73 +            }
    1.74 +        }
    1.75 +
    1.76 +        if (errors > 0)
    1.77 +            throw new Exception(errors + " errors occurred");
    1.78 +    }
    1.79 +
    1.80 +    public void test(CompilePolicy cp, ShouldStopPolicy ssp) {
    1.81 +        System.err.println();
    1.82 +        System.err.println("test " + cp + " " + ssp);
    1.83 +        List<String> args = new ArrayList<String>();
    1.84 +        args.add("-XDverboseCompilePolicy");
    1.85 +        args.add("-XDcompilePolicy=" + cp.toString().toLowerCase());
    1.86 +        args.add("-d");
    1.87 +        args.add(".");
    1.88 +        if (ssp.needOption)
    1.89 +            args.add("-XDshouldStopPolicy=" + ssp);
    1.90 +        args.add(new File(System.getProperty("test.src", "."), "A.java").getPath());
    1.91 +
    1.92 +        StringWriter sw = new StringWriter();
    1.93 +        PrintWriter pw = new PrintWriter(sw);
    1.94 +        System.err.println("compile " + args);
    1.95 +        int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw);
    1.96 +        if (rc == 0)
    1.97 +            throw new Error("compilation succeeded unexpectedly");
    1.98 +        //System.err.println(sw);
    1.99 +
   1.100 +        // The following is a workaround for the current javac implementation,
   1.101 +        // that in bytodo mode, it will still attribute files after syntax errors.
   1.102 +        // Changing that behavior may surprise existing users, so for now, we
   1.103 +        // work around it.
   1.104 +        if (cp == CompilePolicy.BYTODO && ssp == ShouldStopPolicy.PROCESS)
   1.105 +            ssp = ShouldStopPolicy.ATTR;
   1.106 +
   1.107 +        boolean foundExpected = (ssp.expect == null);
   1.108 +        String[] lines = sw.toString().split("\n");
   1.109 +        for (String line: lines) {
   1.110 +            if (ssp.expect != null && line.startsWith("[" + ssp.expect))
   1.111 +                foundExpected = true;
   1.112 +            if (ssp.dontExpect != null && line.startsWith("[" + ssp.dontExpect)) {
   1.113 +                error("Unexpected output: " + ssp.dontExpect + "\n" + sw);
   1.114 +                return;
   1.115 +            }
   1.116 +        }
   1.117 +
   1.118 +        if (!foundExpected)
   1.119 +            error("Expected output not found: " + ssp.expect + "\n" + sw);
   1.120 +    }
   1.121 +
   1.122 +    void error(String message) {
   1.123 +        System.err.println(message);
   1.124 +        errors++;
   1.125 +    }
   1.126 +
   1.127 +    int errors;
   1.128 +}
   1.129 +
   1.130 +
   1.131 +
   1.132 +
   1.133 +
   1.134 +
   1.135 +
   1.136 +
   1.137 +
   1.138 +
   1.139 +
   1.140 +
   1.141 +// These tests test the ability of the compiler to continue in the face of
   1.142 +// errors, accordining to the shouldStopPolicy
   1.143 +
   1.144 +/* @ test /nodynamiccopyright/
   1.145 + * @bug 6813059
   1.146 + * @summary
   1.147 + * @compile/fail/ref=flow.out       -XDrawDiagnostics -XDcompilePolicy=byfile -XDverboseCompilePolicy -XDshouldStopPolicy=FLOW       Test.java
   1.148 +
   1.149 + * @compile/fail/ref=default.out    -XDrawDiagnostics -XDcompilePolicy=byfile -XDverboseCompilePolicy                                Test.java
   1.150 + * @compile/fail/ref=enter.out      -XDrawDiagnostics -XDcompilePolicy=byfile -XDverboseCompilePolicy -XDshouldStopPolicy=ENTER      Test.java
   1.151 + * @compile/fail/ref=attr.out       -XDrawDiagnostics -XDcompilePolicy=byfile -XDverboseCompilePolicy -XDshouldStopPolicy=ATTR       Test.java
   1.152 + * @compile/fail/ref=transtypes.out -XDrawDiagnostics -XDcompilePolicy=byfile -XDverboseCompilePolicy -XDshouldStopPolicy=TRANSTYPES Test.java
   1.153 + * @compile/fail/ref=lower.out      -XDrawDiagnostics -XDcompilePolicy=byfile -XDverboseCompilePolicy -XDshouldStopPolicy=LOWER      Test.java
   1.154 + * @compile/fail/ref=generate.out   -XDrawDiagnostics -XDcompilePolicy=byfile -XDverboseCompilePolicy -XDshouldStopPolicy=GENERATE   Test.java
   1.155 + */
   1.156 +
   1.157 +/*
   1.158 +class Test {
   1.159 +    void m1() {
   1.160 +        System.err.println("hello");
   1.161 +        0 // syntax error
   1.162 +        System.err.println("world");
   1.163 +    }
   1.164 +
   1.165 +    void m2() {
   1.166 +    }
   1.167 +}
   1.168 +
   1.169 +class Test2 {
   1.170 +}
   1.171 +*/
   1.172 +

mercurial