897N/A/*
897N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
897N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
897N/A *
897N/A * This code is free software; you can redistribute it and/or modify it
897N/A * under the terms of the GNU General Public License version 2 only, as
897N/A * published by the Free Software Foundation.
897N/A *
897N/A * This code is distributed in the hope that it will be useful, but WITHOUT
897N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
897N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
897N/A * version 2 for more details (a copy is included in the LICENSE file that
897N/A * accompanied this code).
897N/A *
897N/A * You should have received a copy of the GNU General Public License version
897N/A * 2 along with this work; if not, write to the Free Software Foundation,
897N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
897N/A *
897N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
897N/A * or visit www.oracle.com if you need additional information or have any
897N/A * questions.
897N/A */
897N/A
897N/A
897N/A/*
897N/A * @test
897N/A * @bug 7022337
897N/A * @summary repeated warnings about bootclasspath not set
897N/A * @library ../lib
897N/A * @build JavacTestingAbstractProcessor TestWarnErrorCount
897N/A * @run main TestWarnErrorCount
897N/A */
897N/A
897N/Aimport java.io.*;
897N/Aimport java.util.*;
897N/Aimport javax.annotation.processing.*;
897N/Aimport javax.lang.model.element.*;
897N/Aimport javax.tools.*;
897N/A
897N/A@SupportedOptions({"errKind", "msgrWarnKind", "javaWarnKind"})
897N/Apublic class TestWarnErrorCount extends JavacTestingAbstractProcessor {
897N/A public static void main(String... args) throws Exception {
897N/A new TestWarnErrorCount().run(args);
897N/A }
897N/A
897N/A final int MAX_GEN = 10;
897N/A final int ERROR_ROUND = MAX_GEN / 2; // when to generate error
897N/A
897N/A /**
897N/A * Type of errors to generate in test case.
897N/A */
897N/A enum ErrorKind {
897N/A /** No errors. */
897N/A NONE,
897N/A /** Source code errors. */
897N/A JAVA,
897N/A /** Errors reported to Messager. */
897N/A MESSAGER,
897N/A /** Error as a result of using -Werror. */
897N/A WERROR,
897N/A }
897N/A
897N/A /**
897N/A * Frequency of warnings in test case.
897N/A */
897N/A enum WarnKind {
897N/A /** No warnings. */
897N/A NONE {
897N/A boolean warn(int round) { return false; }
897N/A int count(int start, int end) { return 0; }
897N/A },
897N/A /** Generate a warning if round count is a multiple of 2. */
897N/A EVERY_TWO {
897N/A boolean warn(int round) { return (round % 2) == 0; }
897N/A int count(int start, int end) { return (end / 2) - ((start - 1)/ 2); }
897N/A },
897N/A /** Generate a warning if round count is a multiple of 3. */
897N/A EVERY_THREE {
897N/A boolean warn(int round) { return (round % 3) == 0; }
897N/A int count(int start, int end) { return (end / 3) - ((start - 1)/ 3); }
897N/A },
897N/A /** Generate a warning every round. */
897N/A ALL {
897N/A boolean warn(int round) { return true; }
897N/A int count(int start, int end) { return (end - start + 1); }
897N/A };
897N/A
897N/A /** whether to generate a warning in round 'round'. */
897N/A abstract boolean warn(int round);
897N/A
897N/A /** number of warnings generated in a range of rounds, inclusive. */
897N/A abstract int count(int start, int end);
897N/A }
897N/A
897N/A
897N/A /**
897N/A * Run test.
897N/A * @param args provide ability to specify particular test cases for debugging.
897N/A */
897N/A void run(String... args) throws Exception {
897N/A for (String arg: args) {
897N/A if (arg.matches("[0-9]+")) {
897N/A if (testCases == null)
897N/A testCases = new HashSet<Integer>();
897N/A testCases.add(Integer.valueOf(arg));
897N/A } else if (arg.equals("-stopOnError")) {
897N/A stopOnError = true;
897N/A } else
897N/A throw new IllegalArgumentException(arg);
897N/A }
897N/A
897N/A run ();
897N/A
897N/A if (errors > 0)
897N/A throw new Exception(errors + " errors found");
897N/A }
897N/A
897N/A /**
897N/A * Run test.
897N/A */
897N/A void run() throws Exception {
897N/A for (ErrorKind ek: ErrorKind.values()) {
897N/A for (WarnKind mwk: WarnKind.values()) {
897N/A for (WarnKind jwk: WarnKind.values()) {
897N/A test(ek, mwk, jwk);
897N/A if (stopOnError && errors > 0)
897N/A throw new Exception(errors + " errors found");
897N/A }
897N/A }
897N/A }
897N/A }
897N/A
897N/A boolean stopOnError;
897N/A Set<Integer> testCases;
897N/A int testNum = 0;
897N/A
897N/A /**
897N/A * Run a test case.
897N/A * @param ek The type of errors to generate
897N/A * @param mwk The frequency of Messager warnings to generate
897N/A * @param jwk The frequency of Java warnings to generate
897N/A */
897N/A void test(ErrorKind ek, WarnKind mwk, WarnKind jwk) {
897N/A testNum++;
897N/A
897N/A if (testCases != null && !testCases.contains(testNum))
897N/A return;
897N/A
897N/A System.err.println("Test " + testNum + ": ek:" + ek + " mwk:" + mwk + " jwk:" + jwk);
897N/A
897N/A File testDir = new File("test" + testNum);
897N/A testDir.mkdirs();
897N/A
897N/A String myName = TestWarnErrorCount.class.getSimpleName();
897N/A File testSrc = new File(System.getProperty("test.src"));
897N/A File file = new File(testSrc, myName + ".java");
897N/A
897N/A List<String> args = new ArrayList<String>();
897N/A args.addAll(Arrays.asList(
897N/A "-XDrawDiagnostics",
897N/A "-d", testDir.getPath(),
897N/A "-processor", myName,
897N/A// "-XprintRounds",
907N/A "-Xlint:all,-path",
897N/A "-AerrKind=" + ek,
897N/A "-AmsgrWarnKind=" + mwk,
897N/A "-AjavaWarnKind=" + jwk));
897N/A if (ek == ErrorKind.WERROR)
897N/A args.add("-Werror");
897N/A args.add(file.getPath());
897N/A
897N/A String out = compile(args.toArray(new String[args.size()]));
897N/A
897N/A int errsFound = 0;
897N/A int errsReported = 0;
897N/A int warnsFound = 0;
897N/A int warnsReported = 0;
897N/A
897N/A // Scan the output looking for messages of interest.
897N/A
897N/A for (String line: out.split("[\r\n]+")) {
897N/A if (line.contains("compiler.err.")) {
897N/A errsFound++;
897N/A } else if (line.contains("compiler.warn.")) {
897N/A warnsFound++;
897N/A } else if (line.matches("[0-9]+ error(?:s?)")) {
897N/A errsReported = Integer.valueOf(line.substring(0, line.indexOf("error")).trim());
897N/A } else if (line.matches("[0-9]+ warning(?:s?)")) {
897N/A warnsReported = Integer.valueOf(line.substring(0, line.indexOf("warning")).trim());
897N/A }
897N/A }
897N/A
897N/A // Compute the expected number of errors and warnings, based on
897N/A // the test case parameters.
897N/A // This is highly specific to the annotation processor below, and to
897N/A // the files it generates.
897N/A // Generally, the rules are:
897N/A // -- errors stop annotation processing, allowing for one extra "last round"
897N/A // -- messager warnings are immediate
897N/A // -- javac warnings are not shown before the final compilation
897N/A // (FIXME? -Werror does not stop processing for java warnings)
897N/A int errsExpected;
897N/A int msgrWarnsExpected;
897N/A int javaWarnsExpected;
897N/A switch (ek) {
897N/A case NONE:
897N/A errsExpected = 0;
897N/A msgrWarnsExpected = mwk.count(1, 1 + MAX_GEN + 1);
897N/A javaWarnsExpected = jwk.count(2, 1 + MAX_GEN);
897N/A break;
897N/A case MESSAGER:
897N/A errsExpected = 1;
897N/A msgrWarnsExpected = mwk.count(1, ERROR_ROUND + 1);
897N/A javaWarnsExpected = 0;
897N/A break;
897N/A case JAVA:
897N/A errsExpected = 2;
897N/A msgrWarnsExpected = mwk.count(1, ERROR_ROUND + 1);
897N/A javaWarnsExpected = 0;
897N/A break;
897N/A case WERROR:
897N/A errsExpected = (mwk != WarnKind.NONE || jwk != WarnKind.NONE) ? 1 : 0;
897N/A switch (mwk) {
897N/A case NONE:
897N/A msgrWarnsExpected = 0;
897N/A javaWarnsExpected = (jwk == WarnKind.NONE)
897N/A ? 0
897N/A : 1; // this is surprising: javac only reports warning in first file
897N/A break;
897N/A case EVERY_TWO:
897N/A msgrWarnsExpected = mwk.count(1, 2 + 1);
897N/A javaWarnsExpected = 0;
897N/A break;
897N/A case EVERY_THREE:
897N/A msgrWarnsExpected = mwk.count(1, 3 + 1);
897N/A javaWarnsExpected = 0;
897N/A break;
897N/A case ALL:
897N/A msgrWarnsExpected = mwk.count(1, 1 + 1);
897N/A javaWarnsExpected = 0;
897N/A break;
897N/A default:
897N/A throw new IllegalStateException();
897N/A }
897N/A break;
897N/A default:
897N/A throw new IllegalStateException();
897N/A }
897N/A
897N/A int warnsExpected = msgrWarnsExpected + javaWarnsExpected;
897N/A System.err.println("mwk: " + msgrWarnsExpected
897N/A + ", jwk: " + javaWarnsExpected
897N/A + ", total: " + warnsExpected);
897N/A
897N/A boolean ok;
897N/A ok = checkEqual("errors", "reported", errsFound, errsReported);
897N/A ok &= checkEqual("errors", "expected", errsFound, errsExpected);
897N/A ok &= checkEqual("warnings", "reported", warnsFound, warnsReported);
897N/A ok &= checkEqual("warnings", "expected", warnsFound, warnsExpected);
897N/A if (ok)
897N/A System.err.println("OK");
897N/A
897N/A System.err.println();
897N/A }
897N/A
897N/A String compile(String... args) {
897N/A StringWriter sw = new StringWriter();
897N/A PrintWriter pw = new PrintWriter(sw);
897N/A int rc = com.sun.tools.javac.Main.compile(args, pw);
897N/A pw.close();
897N/A String out = sw.toString();
897N/A if (!out.isEmpty())
897N/A System.err.println(out);
897N/A if (rc != 0)
897N/A System.err.println("compilation failed: rc=" + rc);
897N/A return out;
897N/A }
897N/A
897N/A boolean checkEqual(String l1, String l2, int i1, int i2) {
897N/A if (i1 != i2)
897N/A error("number of " + l1 + " found, " + i1 + ", does not match number " + l2 + ", " + i2);
897N/A return (i1 == i2);
897N/A }
897N/A
897N/A void error(String msg) {
897N/A System.err.println("Error: " + msg);
897N/A errors++;
897N/A }
897N/A
897N/A int errors = 0;
897N/A
897N/A // ----- Annotation processor -----
897N/A
897N/A int round = 0;
897N/A
897N/A @Override
897N/A public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
897N/A round++;
897N/A
897N/A ErrorKind ek = ErrorKind.valueOf(options.get("errKind"));
897N/A WarnKind mwk = WarnKind.valueOf(options.get("msgrWarnKind"));
897N/A WarnKind jwk = WarnKind.valueOf(options.get("javaWarnKind"));
897N/A messager.printMessage(Diagnostic.Kind.NOTE,
897N/A "Round " + round
897N/A + " " + roundEnv.getRootElements()
897N/A + ", last round: " + roundEnv.processingOver());
897N/A messager.printMessage(Diagnostic.Kind.NOTE,
897N/A "ek: " + ek + ", mwk: " + mwk + ", jwk: " + jwk);
897N/A
897N/A if (round <= MAX_GEN && !roundEnv.processingOver())
897N/A generate("Gen" + round,
897N/A (ek == ErrorKind.JAVA) && (round == ERROR_ROUND),
897N/A jwk.warn(round));
897N/A
897N/A if (mwk.warn(round))
897N/A messager.printMessage(Diagnostic.Kind.WARNING, "round " + round);
897N/A
897N/A if ((ek == ErrorKind.MESSAGER) && (round == ERROR_ROUND))
897N/A messager.printMessage(Diagnostic.Kind.ERROR, "round " + round);
897N/A
897N/A return true;
897N/A }
897N/A
897N/A void generate(String name, boolean error, boolean warn) {
897N/A try {
897N/A JavaFileObject fo = filer.createSourceFile(name);
897N/A Writer out = fo.openWriter();
897N/A try {
897N/A out.write("class " + name + " {\n"
897N/A + (warn ? " int i = (int) 0;\n" : "")
897N/A + (error ? " ERROR\n" : "")
897N/A + "}\n");
897N/A } finally {
897N/A out.close();
897N/A }
897N/A } catch (IOException e) {
897N/A throw new Error(e);
897N/A }
897N/A }
897N/A}