langtools/test/tools/javac/processing/errors/TestBadProcessor.java
author jlahoda
Tue, 08 Apr 2014 23:33:50 +0200
changeset 23811 4bbbcaad653f
parent 23810 b92eb80925f0
child 30730 d3ce7619db2c
permissions -rw-r--r--
8039466: Tests failing in langtools repository. Summary: Fixing tests broken by JDK-8038455. Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20615
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
     1
/*
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 20615
diff changeset
     2
 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
20615
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
     4
 *
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
     8
 *
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    13
 * accompanied this code).
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    14
 *
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    18
 *
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    21
 * questions.
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    22
 */
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    23
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    24
/*
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    25
 * @test
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    26
 * @bug 8022163
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    27
 * @summary javac exits with 0 status and no messages on error to construct an ann-procesor
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    28
 */
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    29
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    30
import java.io.*;
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    31
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    32
public class TestBadProcessor {
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    33
    public static void main(String... args) throws Exception {
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    34
        new TestBadProcessor().run();
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    35
    }
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    36
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    37
    public static final String badAnnoProcSrc =
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    38
        "import java.util.*;\n" +
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    39
        "import javax.annotation.processing.*;\n" +
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    40
        "import javax.lang.model.element.*;\n" +
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    41
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    42
        "public class AnnoProc extends AbstractProcessor {\n" +
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    43
        "    public AnnoProc() {\n" +
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    44
        "        throw new Error();\n" +
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    45
        "    }\n" +
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    46
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    47
        "    public boolean process(Set<? extends TypeElement> elems, \n" +
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    48
        "                        RoundEnvironment rEnv) {\n" +
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    49
        "        return false;\n" +
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    50
        "    }\n" +
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    51
        "}\n";
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    52
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    53
    public void run() throws Exception {
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    54
        // setup
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    55
        File srcDir = new File("src");
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    56
        File classesDir = new File("classes");
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    57
        classesDir.mkdirs();
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    58
        File srcFile = writeFile(srcDir, "AnnoProc.java", badAnnoProcSrc);
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    59
        compile("-d", classesDir.getPath(), srcFile.getPath());
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    60
        writeFile(classesDir, "META-INF/services/javax.annotation.processing.Processor", "AnnoProc");
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    61
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    62
        // run the primary compilation
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    63
        int rc;
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    64
        StringWriter sw = new StringWriter();
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    65
        try (PrintWriter pw = new PrintWriter(sw)) {
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    66
            String[] args = { "-processorpath", classesDir.getPath(), srcFile.getPath() };
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    67
            rc = com.sun.tools.javac.Main.compile(args, pw);
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    68
        }
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    69
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    70
        // verify that it failed as expected, with the expected message
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    71
        String out = sw.toString();
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    72
        System.err.println(out);
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    73
        String expect = "error: Bad service configuration file, " +
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    74
                "or exception thrown while constructing Processor object: " +
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    75
                "javax.annotation.processing.Processor: " +
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 20615
diff changeset
    76
                "Provider AnnoProc could not be instantiated: java.lang.Error\n" +
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 20615
diff changeset
    77
                "1 error";
23811
4bbbcaad653f 8039466: Tests failing in langtools repository.
jlahoda
parents: 23810
diff changeset
    78
        String lineSeparator = System.getProperty("line.separator");
4bbbcaad653f 8039466: Tests failing in langtools repository.
jlahoda
parents: 23810
diff changeset
    79
        if (!out.trim().replace(lineSeparator, "\n").equals(expect)) {
20615
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    80
            System.err.println("expected: " + expect);
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    81
            error("output not as expected");
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    82
        }
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    83
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    84
        if (rc == 0) {
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    85
            error("unexpected exit code: " + rc + "; expected: not zero");
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    86
        }
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    87
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    88
        // summary
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    89
        if (errors > 0)
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    90
            throw new Exception(errors + " errors found");
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    91
    }
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    92
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    93
    void compile(String... args) throws Exception {
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    94
        int rc;
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    95
        StringWriter sw = new StringWriter();
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    96
        try (PrintWriter pw = new PrintWriter(sw)) {
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    97
            rc = com.sun.tools.javac.Main.compile(args, pw);
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    98
        }
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
    99
        String out = sw.toString();
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
   100
        if (!out.isEmpty())
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
   101
            System.err.println(out);
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
   102
        if (rc != 0)
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
   103
            throw new Exception("compilation failed");
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
   104
    }
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
   105
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
   106
    File writeFile(File dir, String path, String body) throws IOException {
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
   107
        File f = new File(dir, path);
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
   108
        f.getParentFile().mkdirs();
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
   109
        try (FileWriter out = new FileWriter(f)) {
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
   110
            out.write(body);
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
   111
        }
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
   112
        return f;
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
   113
    }
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
   114
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
   115
    void error(String msg) {
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
   116
        System.err.println("Error: " + msg);
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
   117
        errors++;
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
   118
    }
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
   119
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
   120
    int errors;
77f2d47729b0 8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
jjg
parents:
diff changeset
   121
}