test/tools/javac/annotations/8218152/MalformedAnnotationProcessorTests.java

changeset 3814
12045c5cae33
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/annotations/8218152/MalformedAnnotationProcessorTests.java	Fri May 17 18:41:21 2019 +0100
     1.3 @@ -0,0 +1,158 @@
     1.4 +/*
     1.5 + * Copyright (c) 2019, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 8218152
    1.30 + * @summary A bad annotation processor class file should fail with an error
    1.31 + * @author Steven Groeger
    1.32 + *
    1.33 + * @library /tools/javac/lib
    1.34 + * @build ToolBox
    1.35 + * @run main MalformedAnnotationProcessorTests
    1.36 + */
    1.37 +
    1.38 +import java.io.*;
    1.39 +import java.util.*;
    1.40 +import java.io.RandomAccessFile;
    1.41 +import java.nio.ByteBuffer;
    1.42 +import java.nio.channels.FileChannel;
    1.43 +import java.nio.file.Files;
    1.44 +import java.nio.file.Path;
    1.45 +import java.nio.file.Paths;
    1.46 +import java.util.List;
    1.47 +import javax.annotation.processing.Processor;
    1.48 +
    1.49 +public class MalformedAnnotationProcessorTests {
    1.50 +    public static void main(String... args) throws Exception {
    1.51 +        new MalformedAnnotationProcessorTests().run();
    1.52 +    }
    1.53 +
    1.54 +    void run() throws Exception {
    1.55 +        testBadAnnotationProcessor(Paths.get("."));
    1.56 +        testMissingAnnotationProcessor(Paths.get("."));
    1.57 +        testWrongClassFileVersion(Paths.get("."));
    1.58 +    }
    1.59 +
    1.60 +    public void testBadAnnotationProcessor(Path base) throws Exception {
    1.61 +        Path apDir = base.resolve("annoprocessor");
    1.62 +        ToolBox.writeFile(apDir.resolve("META-INF").resolve("services")
    1.63 +                          .resolve(Processor.class.getCanonicalName()), "BadAnnoProcessor");
    1.64 +        ToolBox.writeFile(apDir.resolve("BadAnnoProcessor.class"), "badannoprocessor");
    1.65 +
    1.66 +        Path classes = base.resolve("classes");
    1.67 +        Files.createDirectories(classes);
    1.68 +
    1.69 +        List<String> actualErrors = new ArrayList<>();
    1.70 +        ToolBox.JavaToolArgs args = new ToolBox.JavaToolArgs();
    1.71 +        args.setSources("package test; public class Test {}")
    1.72 +            .appendArgs("-XDrawDiagnostics",
    1.73 +                        "-d", classes.toString(),
    1.74 +                        "-classpath", "",
    1.75 +                        "-processorpath", apDir.toString())
    1.76 +            .set(ToolBox.Expect.FAIL)
    1.77 +            .setErrOutput(actualErrors);
    1.78 +        ToolBox.javac(args);
    1.79 +
    1.80 +        System.out.println(actualErrors.get(0));
    1.81 +        if (!actualErrors.get(0).contains("- compiler.err.proc.cant.load.class: " +
    1.82 +                                          "Incompatible magic value")) {
    1.83 +            throw new AssertionError("Unexpected errors reported: " + actualErrors);
    1.84 +        }
    1.85 +    }
    1.86 +
    1.87 +    public void testMissingAnnotationProcessor(Path base) throws Exception {
    1.88 +        Path apDir = base.resolve("annoprocessor");
    1.89 +        ToolBox.writeFile(apDir.resolve("META-INF").resolve("services").resolve(Processor.class.getCanonicalName()),
    1.90 +                     "MissingAnnoProcessor");
    1.91 +
    1.92 +        Path classes = base.resolve("classes");
    1.93 +        Files.createDirectories(classes);
    1.94 +
    1.95 +        List<String> actualErrors = new ArrayList<>();
    1.96 +        ToolBox.JavaToolArgs args = new ToolBox.JavaToolArgs();
    1.97 +        args.setSources("package test; public class Test {}")
    1.98 +            .appendArgs("-XDrawDiagnostics",
    1.99 +                        "-d", classes.toString(),
   1.100 +                        "-classpath", "",
   1.101 +                        "-processorpath", apDir.toString())
   1.102 +            .set(ToolBox.Expect.FAIL)
   1.103 +            .setErrOutput(actualErrors);
   1.104 +        ToolBox.javac(args);
   1.105 +
   1.106 +        if (!actualErrors.get(0).contains("- compiler.err.proc.bad.config.file: " +
   1.107 +            "javax.annotation.processing.Processor: Provider MissingAnnoProcessor not found")) {
   1.108 +            throw new AssertionError("Unexpected errors reported: " + actualErrors);
   1.109 +        }
   1.110 +    }
   1.111 +
   1.112 +    public void testWrongClassFileVersion(Path base) throws Exception {
   1.113 +        Path apDir = base.resolve("annoprocessor");
   1.114 +        ToolBox.writeFile(apDir.resolve("META-INF").resolve("services").resolve(Processor.class.getCanonicalName()),
   1.115 +                     "WrongClassFileVersion");
   1.116 +
   1.117 +        ToolBox.JavaToolArgs args = new ToolBox.JavaToolArgs();
   1.118 +        args.setSources("class WrongClassFileVersion {}")
   1.119 +            .appendArgs("-d", apDir.toString())
   1.120 +            .set(ToolBox.Expect.SUCCESS);
   1.121 +        ToolBox.javac(args);
   1.122 +
   1.123 +        increaseMajor(apDir.resolve("WrongClassFileVersion.class"), 1);
   1.124 +
   1.125 +        Path classes = base.resolve("classes");
   1.126 +        Files.createDirectories(classes);
   1.127 +
   1.128 +        List<String> actualErrors = new ArrayList<>();
   1.129 +        args = new ToolBox.JavaToolArgs();
   1.130 +        args.setSources("package test; public class Test {}")
   1.131 +            .appendArgs("-XDrawDiagnostics",
   1.132 +                        "-d", classes.toString(),
   1.133 +                        "-classpath", "",
   1.134 +                        "-processorpath", apDir.toString())
   1.135 +            .set(ToolBox.Expect.FAIL)
   1.136 +            .setErrOutput(actualErrors);
   1.137 +        ToolBox.javac(args);
   1.138 +
   1.139 +        if (!actualErrors.get(0).contains("- compiler.err.proc.cant.load.class: " +
   1.140 +            "WrongClassFileVersion has been compiled by a more recent version")) {
   1.141 +            throw new AssertionError("Unexpected errors reported: " + actualErrors);
   1.142 +        }
   1.143 +    }
   1.144 +
   1.145 +    // Increase class file cfile's major version by delta
   1.146 +    // (note: based on test/langtools/tools/javac/6330997/T6330997.java)
   1.147 +    static void increaseMajor(Path cfile, int delta) {
   1.148 +        try (RandomAccessFile cls =
   1.149 +             new RandomAccessFile(cfile.toFile(), "rw");
   1.150 +             FileChannel fc = cls.getChannel()) {
   1.151 +            ByteBuffer rbuf = ByteBuffer.allocate(2);
   1.152 +            fc.read(rbuf, 6);
   1.153 +            ByteBuffer wbuf = ByteBuffer.allocate(2);
   1.154 +            wbuf.putShort(0, (short)(rbuf.getShort(0) + delta));
   1.155 +            fc.write(wbuf, 6);
   1.156 +            fc.force(false);
   1.157 +        } catch (Exception e){
   1.158 +            throw new RuntimeException("Failed: unexpected exception");
   1.159 +        }
   1.160 +     }
   1.161 +}

mercurial