langtools/test/tools/javadoc/MaxWarns.java
author katleman
Tue, 21 Jan 2014 18:17:23 -0800
changeset 22365 895bfde469d4
parent 15354 52a04c670c05
child 30730 d3ce7619db2c
permissions -rw-r--r--
Added tag jdk9-b01 for changeset ae2d94255f32
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15041
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
     1
/*
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
     4
 *
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
     8
 *
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    13
 * accompanied this code).
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    14
 *
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    18
 *
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    21
 * questions.
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    22
 */
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    23
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    24
/*
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    25
 * @test
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    26
 * @bug 8005644
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    27
 * @summary set default max errs and max warns
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    28
 */
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    29
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    30
import java.io.File;
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    31
import java.io.FileWriter;
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    32
import java.io.IOException;
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    33
import java.io.PrintWriter;
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    34
import java.io.StringWriter;
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    35
import java.util.regex.Matcher;
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    36
import java.util.regex.Pattern;
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    37
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    38
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    39
public class MaxWarns {
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    40
    public static void main(String... args) throws Exception {
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    41
        new MaxWarns().run();
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    42
    }
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    43
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    44
    void run() throws Exception {
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    45
        final int defaultMaxWarns = 100;
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    46
        final int genWarns = 150;
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    47
        File f = genSrc(genWarns);
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    48
        String out = javadoc(f);
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    49
        check(out, defaultMaxWarns);
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    50
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    51
        if (errors > 0)
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    52
            throw new Exception(errors + " errors occurred");
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    53
    }
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    54
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    55
    File genSrc(int count) throws IOException {
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    56
        StringBuilder sb = new StringBuilder();
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    57
        sb.append("package p;\n")
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    58
            .append("public class C {\n")
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    59
            .append("    /**\n");
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    60
        for (int i = 0; i < count; i++)
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    61
            sb.append("     * @param i").append(i).append(" does not exist!\n");
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    62
        sb.append("     */\n")
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    63
            .append("    public void m() { }\n")
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    64
            .append("}\n");
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    65
        File srcDir = new File("src");
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    66
        srcDir.mkdirs();
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    67
        File f = new File(srcDir, "C.java");
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    68
        try (FileWriter fw = new FileWriter(f)) {
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    69
            fw.write(sb.toString());
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    70
        }
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    71
        return f;
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    72
    }
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    73
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    74
    String javadoc(File f) {
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    75
        StringWriter sw = new StringWriter();
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    76
        PrintWriter pw = new PrintWriter(sw);
15354
52a04c670c05 8004834: Add doclint support into javadoc
jjg
parents: 15041
diff changeset
    77
        String[] args = { "-Xdoclint:none", "-d", "api", f.getPath() };
15041
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    78
        int rc = com.sun.tools.javadoc.Main.execute("javadoc", pw, pw, pw,
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    79
                com.sun.tools.doclets.standard.Standard.class.getName(), args);
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    80
        pw.flush();
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    81
        return sw.toString();
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    82
    }
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    83
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    84
    void check(String out, int count) {
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    85
        System.err.println(out);
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    86
        Pattern warn = Pattern.compile("warning - @param argument \"i[0-9]+\" is not a parameter name");
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    87
        Matcher m = warn.matcher(out);
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    88
        int n = 0;
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    89
        for (int start = 0; m.find(start); start = m.start() + 1) {
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    90
            n++;
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    91
        }
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    92
        if (n != count)
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    93
            error("unexpected number of warnings reported: " + n + "; expected: " + count);
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    94
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    95
        Pattern warnCount = Pattern.compile("(?ms).*^([0-9]+) warnings$.*");
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    96
        m = warnCount.matcher(out);
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    97
        if (m.matches()) {
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    98
            n = Integer.parseInt(m.group(1));
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    99
            if (n != count)
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
   100
                error("unexpected number of warnings reported: " + n + "; expected: " + count);
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
   101
        } else
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
   102
            error("total count not found");
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
   103
    }
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
   104
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
   105
    void error(String msg) {
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
   106
        System.err.println("Error: " + msg);
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
   107
        errors++;
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
   108
    }
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
   109
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
   110
    int errors;
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
   111
}
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
   112