T6597678.java revision 3376:4c740bddc648
138451Smsmith/*
238451Smsmith * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
338451Smsmith * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
438451Smsmith *
538451Smsmith * This code is free software; you can redistribute it and/or modify it
638451Smsmith * under the terms of the GNU General Public License version 2 only, as
738451Smsmith * published by the Free Software Foundation.
838451Smsmith *
938451Smsmith * This code is distributed in the hope that it will be useful, but WITHOUT
1038451Smsmith * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1138451Smsmith * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1238451Smsmith * version 2 for more details (a copy is included in the LICENSE file that
1338451Smsmith * accompanied this code).
1438451Smsmith *
1538451Smsmith * You should have received a copy of the GNU General Public License version
1638451Smsmith * 2 along with this work; if not, write to the Free Software Foundation,
1738451Smsmith * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1838451Smsmith *
1938451Smsmith * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2038451Smsmith * or visit www.oracle.com if you need additional information or have any
2138451Smsmith * questions.
2238451Smsmith */
2338451Smsmith
2438451Smsmith/**
2538451Smsmith * @test
2638451Smsmith * @bug 6597678 6449184
2738451Smsmith * @summary Ensure Messages propogated between rounds
2838451Smsmith * @library /tools/javac/lib
2938451Smsmith * @modules jdk.compiler/com.sun.tools.javac.processing
3038451Smsmith *          jdk.compiler/com.sun.tools.javac.util
3184221Sdillon * @build JavacTestingAbstractProcessor T6597678
3284221Sdillon * @run main T6597678
3384221Sdillon */
3438451Smsmith
3538451Smsmithimport java.io.*;
3638451Smsmithimport java.util.*;
3738451Smsmithimport javax.annotation.processing.RoundEnvironment;
3838451Smsmithimport javax.annotation.processing.SupportedOptions;
3938451Smsmithimport javax.lang.model.element.TypeElement;
4038451Smsmithimport javax.tools.Diagnostic;
4138451Smsmith
4238451Smsmithimport com.sun.tools.javac.processing.JavacProcessingEnvironment;
4338451Smsmithimport com.sun.tools.javac.util.Context;
4438451Smsmithimport com.sun.tools.javac.util.JavacMessages;
4538451Smsmithimport com.sun.tools.javac.util.Log;
4638451Smsmith
4738451Smsmith@SupportedOptions("WriterString")
4838451Smsmithpublic class T6597678 extends JavacTestingAbstractProcessor {
4938451Smsmith    public static void main(String... args) throws Exception {
5038451Smsmith        new T6597678().run();
5138451Smsmith    }
5238451Smsmith
5338451Smsmith    void run() throws Exception {
5438451Smsmith        String myName = T6597678.class.getSimpleName();
5538451Smsmith        File testSrc = new File(System.getProperty("test.src"));
5638451Smsmith        File file = new File(testSrc, myName + ".java");
5738451Smsmith
5838451Smsmith        StringWriter sw = new StringWriter();
5938451Smsmith        PrintWriter pw = new PrintWriter(sw);
6038451Smsmith
6138451Smsmith        compile(sw, pw,
6238451Smsmith            "-XaddExports:jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED",
6338451Smsmith            "-XaddExports:jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
6438451Smsmith            "-XDaccessInternalAPI",
6538451Smsmith            "-proc:only",
6638451Smsmith            "-processor", myName,
6738451Smsmith            "-AWriterString=" + pw.toString(),
6838451Smsmith            file.getPath());
6938451Smsmith    }
7038451Smsmith
7138451Smsmith    void compile(StringWriter sw, PrintWriter pw, String... args) throws Exception {
7238451Smsmith        int rc = com.sun.tools.javac.Main.compile(args, pw);
7338451Smsmith        pw.close();
7438451Smsmith        String out = sw.toString();
7538451Smsmith        if (!out.isEmpty())
7638451Smsmith            System.err.println(out);
7738451Smsmith        if (rc != 0)
7838451Smsmith            throw new Exception("compilation failed unexpectedly: rc=" + rc);
7938451Smsmith    }
8038451Smsmith
8138451Smsmith    //---------------
8238451Smsmith
8338451Smsmith    @Override
8438451Smsmith    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
8538451Smsmith        Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
8638451Smsmith        Log log = Log.instance(context);
8738451Smsmith        PrintWriter noteOut = log.getWriter(Log.WriterKind.NOTICE);
8838451Smsmith        PrintWriter warnOut = log.getWriter(Log.WriterKind.WARNING);
8938451Smsmith        PrintWriter errOut  = log.getWriter(Log.WriterKind.ERROR);
9038451Smsmith        Locale locale = context.get(Locale.class);
9138451Smsmith        JavacMessages messages = context.get(JavacMessages.messagesKey);
9238451Smsmith
9338451Smsmith        round++;
9438451Smsmith        if (round == 1) {
9538451Smsmith            initialLocale = locale;
9659853Sps            initialMessages = messages;
9759853Sps            initialNoteWriter = noteOut;
9859853Sps            initialWarnWriter = warnOut;
9959853Sps            initialErrWriter  = errOut;
10059853Sps
10159853Sps            String writerStringOpt = options.get("WriterString").intern();
10259853Sps            checkEqual("noteWriterString", noteOut.toString().intern(), writerStringOpt);
10359853Sps            checkEqual("warnWriterString", warnOut.toString().intern(), writerStringOpt);
10459853Sps            checkEqual("errWriterString",  errOut.toString().intern(),  writerStringOpt);
10559853Sps        } else {
10659853Sps            checkEqual("locale", locale, initialLocale);
10759853Sps            checkEqual("messages", messages, initialMessages);
10859853Sps            checkEqual("noteWriter", noteOut, initialNoteWriter);
10959853Sps            checkEqual("warnWriter", warnOut, initialWarnWriter);
11059853Sps            checkEqual("errWriter",  errOut,  initialErrWriter);
11159853Sps        }
11259853Sps
11338451Smsmith        return true;
11438451Smsmith    }
11538451Smsmith
11638451Smsmith    <T> void checkEqual(String label, T actual, T expected) {
11738451Smsmith        if (actual != expected)
11838451Smsmith            messager.printMessage(Diagnostic.Kind.ERROR,
11938451Smsmith                    "Unexpected value for " + label
12038451Smsmith                    + "; expected: " + expected
12138451Smsmith                    + "; found: " + actual);
12238451Smsmith    }
12338451Smsmith
12439468Smsmith    int round = 0;
12538451Smsmith    Locale initialLocale;
12638451Smsmith    JavacMessages initialMessages;
12738451Smsmith    PrintWriter initialNoteWriter;
12838451Smsmith    PrintWriter initialWarnWriter;
12938451Smsmith    PrintWriter initialErrWriter;
13059853Sps}
13138451Smsmith