langtools/test/tools/javac/processing/errors/TestBadProcessor.java
changeset 20615 77f2d47729b0
child 23810 b92eb80925f0
equal deleted inserted replaced
20614:994200acf1e4 20615:77f2d47729b0
       
     1 /*
       
     2  * Copyright (c) 2013, 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 /*
       
    25  * @test
       
    26  * @bug 8022163
       
    27  * @summary javac exits with 0 status and no messages on error to construct an ann-procesor
       
    28  */
       
    29 
       
    30 import java.io.*;
       
    31 
       
    32 public class TestBadProcessor {
       
    33     public static void main(String... args) throws Exception {
       
    34         new TestBadProcessor().run();
       
    35     }
       
    36 
       
    37     public static final String badAnnoProcSrc =
       
    38         "import java.util.*;\n" +
       
    39         "import javax.annotation.processing.*;\n" +
       
    40         "import javax.lang.model.element.*;\n" +
       
    41 
       
    42         "public class AnnoProc extends AbstractProcessor {\n" +
       
    43         "    public AnnoProc() {\n" +
       
    44         "        throw new Error();\n" +
       
    45         "    }\n" +
       
    46 
       
    47         "    public boolean process(Set<? extends TypeElement> elems, \n" +
       
    48         "                        RoundEnvironment rEnv) {\n" +
       
    49         "        return false;\n" +
       
    50         "    }\n" +
       
    51         "}\n";
       
    52 
       
    53     public void run() throws Exception {
       
    54         // setup
       
    55         File srcDir = new File("src");
       
    56         File classesDir = new File("classes");
       
    57         classesDir.mkdirs();
       
    58         File srcFile = writeFile(srcDir, "AnnoProc.java", badAnnoProcSrc);
       
    59         compile("-d", classesDir.getPath(), srcFile.getPath());
       
    60         writeFile(classesDir, "META-INF/services/javax.annotation.processing.Processor", "AnnoProc");
       
    61 
       
    62         // run the primary compilation
       
    63         int rc;
       
    64         StringWriter sw = new StringWriter();
       
    65         try (PrintWriter pw = new PrintWriter(sw)) {
       
    66             String[] args = { "-processorpath", classesDir.getPath(), srcFile.getPath() };
       
    67             rc = com.sun.tools.javac.Main.compile(args, pw);
       
    68         }
       
    69 
       
    70         // verify that it failed as expected, with the expected message
       
    71         String out = sw.toString();
       
    72         System.err.println(out);
       
    73         String expect = "error: Bad service configuration file, " +
       
    74                 "or exception thrown while constructing Processor object: " +
       
    75                 "javax.annotation.processing.Processor: " +
       
    76                 "Provider AnnoProc could not be instantiated: java.lang.Error";
       
    77         if (!out.trim().equals(expect)) {
       
    78             System.err.println("expected: " + expect);
       
    79             error("output not as expected");
       
    80         }
       
    81 
       
    82         if (rc == 0) {
       
    83             error("unexpected exit code: " + rc + "; expected: not zero");
       
    84         }
       
    85 
       
    86         // summary
       
    87         if (errors > 0)
       
    88             throw new Exception(errors + " errors found");
       
    89     }
       
    90 
       
    91     void compile(String... args) throws Exception {
       
    92         int rc;
       
    93         StringWriter sw = new StringWriter();
       
    94         try (PrintWriter pw = new PrintWriter(sw)) {
       
    95             rc = com.sun.tools.javac.Main.compile(args, pw);
       
    96         }
       
    97         String out = sw.toString();
       
    98         if (!out.isEmpty())
       
    99             System.err.println(out);
       
   100         if (rc != 0)
       
   101             throw new Exception("compilation failed");
       
   102     }
       
   103 
       
   104     File writeFile(File dir, String path, String body) throws IOException {
       
   105         File f = new File(dir, path);
       
   106         f.getParentFile().mkdirs();
       
   107         try (FileWriter out = new FileWriter(f)) {
       
   108             out.write(body);
       
   109         }
       
   110         return f;
       
   111     }
       
   112 
       
   113     void error(String msg) {
       
   114         System.err.println("Error: " + msg);
       
   115         errors++;
       
   116     }
       
   117 
       
   118     int errors;
       
   119 }