test/langtools/tools/javac/annotations/8218152/MalformedAnnotationProcessorTests.java
changeset 54650 d9208a660094
equal deleted inserted replaced
54649:73a87b1aacc2 54650:d9208a660094
       
     1 /*
       
     2  * Copyright (c) 2019, 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 8218152
       
    27  * @summary A bad annotation processor class file should fail with an error
       
    28  * @author Steven Groeger
       
    29  *
       
    30  * @library /tools/lib /tools/javac/lib
       
    31  * @modules
       
    32  *      jdk.compiler/com.sun.tools.javac.api
       
    33  *      jdk.compiler/com.sun.tools.javac.main
       
    34  * @build toolbox.ToolBox toolbox.JavacTask JavacTestingAbstractProcessor
       
    35  * @run main MalformedAnnotationProcessorTests
       
    36  */
       
    37 
       
    38 import java.io.RandomAccessFile;
       
    39 import java.nio.ByteBuffer;
       
    40 import java.nio.channels.FileChannel;
       
    41 import java.nio.file.Files;
       
    42 import java.nio.file.Path;
       
    43 import java.nio.file.Paths;
       
    44 import java.util.List;
       
    45 
       
    46 import javax.annotation.processing.Processor;
       
    47 
       
    48 import toolbox.JavacTask;
       
    49 import toolbox.Task;
       
    50 import toolbox.Task.Expect;
       
    51 import toolbox.TestRunner;
       
    52 import toolbox.ToolBox;
       
    53 
       
    54 public class MalformedAnnotationProcessorTests extends TestRunner{
       
    55     public static void main(String... args) throws Exception {
       
    56         new MalformedAnnotationProcessorTests().runTests(
       
    57                 m -> new Object[] { Paths.get(m.getName()) }
       
    58         );
       
    59     }
       
    60 
       
    61     private ToolBox tb = new ToolBox();
       
    62 
       
    63     public MalformedAnnotationProcessorTests() {
       
    64         super(System.err);
       
    65     }
       
    66 
       
    67     @Test
       
    68     public void testBadAnnotationProcessor(Path base) throws Exception {
       
    69         Path apDir = base.resolve("annoprocessor");
       
    70         tb.writeFile(apDir.resolve("META-INF").resolve("services")
       
    71                           .resolve(Processor.class.getCanonicalName()), "BadAnnoProcessor");
       
    72         tb.writeFile(apDir.resolve("BadAnnoProcessor.class"), "badannoprocessor");
       
    73 
       
    74         Path src = base.resolve("src");
       
    75         Path classes = base.resolve("classes");
       
    76 
       
    77         Files.createDirectories(classes);
       
    78 
       
    79         tb.writeJavaFiles(src, "package test; public class Test {}");
       
    80 
       
    81         List<String> actualErrors =
       
    82                 new JavacTask(tb)
       
    83                     .options("-XDrawDiagnostics",
       
    84                              "-classpath", "",
       
    85                              "-sourcepath", src.toString(),
       
    86                              "-processorpath", apDir.toString())
       
    87                     .outdir(classes)
       
    88                     .files(tb.findJavaFiles(src))
       
    89                     .run(Expect.FAIL)
       
    90                     .writeAll()
       
    91                     .getOutputLines(Task.OutputKind.DIRECT);
       
    92 
       
    93         System.out.println(actualErrors.get(0));
       
    94         if (!actualErrors.get(0).contains("- compiler.err.proc.cant.load.class: " +
       
    95                                           "Incompatible magic value")) {
       
    96             throw new AssertionError("Unexpected errors reported: " + actualErrors);
       
    97         }
       
    98     }
       
    99 
       
   100     @Test
       
   101     public void testMissingAnnotationProcessor(Path base) throws Exception {
       
   102         Path apDir = base.resolve("annoprocessor");
       
   103         tb.writeFile(apDir.resolve("META-INF").resolve("services").resolve(Processor.class.getCanonicalName()),
       
   104                      "MissingAnnoProcessor");
       
   105 
       
   106         Path src = base.resolve("src");
       
   107         Path classes = base.resolve("classes");
       
   108 
       
   109         Files.createDirectories(classes);
       
   110 
       
   111         tb.writeJavaFiles(src, "package test; public class Test {}");
       
   112 
       
   113         List<String> actualErrors =
       
   114                 new JavacTask(tb)
       
   115                     .options("-XDrawDiagnostics",
       
   116                              "-classpath", "",
       
   117                              "-sourcepath", src.toString(),
       
   118                              "-processorpath", apDir.toString())
       
   119                     .outdir(classes)
       
   120                     .files(tb.findJavaFiles(src))
       
   121                     .run(Expect.FAIL)
       
   122                     .writeAll()
       
   123                     .getOutputLines(Task.OutputKind.DIRECT);
       
   124 
       
   125         if (!actualErrors.get(0).contains("- compiler.err.proc.bad.config.file: " +
       
   126             "javax.annotation.processing.Processor: Provider MissingAnnoProcessor not found")) {
       
   127             throw new AssertionError("Unexpected errors reported: " + actualErrors);
       
   128         }
       
   129     }
       
   130 
       
   131     @Test
       
   132     public void testWrongClassFileVersion(Path base) throws Exception {
       
   133         Path apDir = base.resolve("ap");
       
   134         tb.writeFile(apDir.resolve("META-INF").resolve("services").resolve(Processor.class.getCanonicalName()),
       
   135                      "WrongClassFileVersion");
       
   136 
       
   137         new JavacTask(tb)
       
   138                 .outdir(apDir)
       
   139                 .sources("class WrongClassFileVersion {}")
       
   140                 .run()
       
   141                 .writeAll();
       
   142 
       
   143         increaseMajor(apDir.resolve("WrongClassFileVersion.class"), 1);
       
   144 
       
   145         Path src = base.resolve("src");
       
   146         Path classes = base.resolve("classes");
       
   147 
       
   148         Files.createDirectories(classes);
       
   149 
       
   150         tb.writeJavaFiles(src, "package test; public class Test {}");
       
   151 
       
   152         List<String> actualErrors =
       
   153                 new JavacTask(tb)
       
   154                     .options("-XDrawDiagnostics",
       
   155                              "-classpath", "",
       
   156                              "-sourcepath", src.toString(),
       
   157                              "-processorpath", apDir.toString())
       
   158                     .outdir(classes)
       
   159                     .files(tb.findJavaFiles(src))
       
   160                     .run(Expect.FAIL)
       
   161                     .writeAll()
       
   162                     .getOutputLines(Task.OutputKind.DIRECT);
       
   163 
       
   164         if (!actualErrors.get(0).contains("- compiler.err.proc.cant.load.class: " +
       
   165             "WrongClassFileVersion has been compiled by a more recent version")) {
       
   166             throw new AssertionError("Unexpected errors reported: " + actualErrors);
       
   167         }
       
   168     }
       
   169 
       
   170     // Increase class file cfile's major version by delta
       
   171     // (note: based on test/langtools/tools/javac/6330997/T6330997.java)
       
   172     static void increaseMajor(Path cfile, int delta) {
       
   173         try (RandomAccessFile cls =
       
   174              new RandomAccessFile(cfile.toFile(), "rw");
       
   175              FileChannel fc = cls.getChannel()) {
       
   176             ByteBuffer rbuf = ByteBuffer.allocate(2);
       
   177             fc.read(rbuf, 6);
       
   178             ByteBuffer wbuf = ByteBuffer.allocate(2);
       
   179             wbuf.putShort(0, (short)(rbuf.getShort(0) + delta));
       
   180             fc.write(wbuf, 6);
       
   181             fc.force(false);
       
   182         } catch (Exception e){
       
   183             throw new RuntimeException("Failed: unexpected exception");
       
   184          }
       
   185      }
       
   186 }