langtools/test/tools/javadoc/MaxWarns.java
author lana
Thu, 21 Jan 2016 09:46:01 -0800
changeset 35340 38f7386ed942
parent 30730 d3ce7619db2c
child 45742 36bf0f2436ad
permissions -rw-r--r--
Added tag jdk-9+102 for changeset 7e3feb2ba5b3
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
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 15354
diff changeset
     2
 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
15041
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
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 15354
diff changeset
    28
 * @modules jdk.javadoc/com.sun.tools.doclets.standard
15041
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
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    31
import java.io.File;
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    32
import java.io.FileWriter;
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    33
import java.io.IOException;
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    34
import java.io.PrintWriter;
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    35
import java.io.StringWriter;
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    36
import java.util.regex.Matcher;
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    37
import java.util.regex.Pattern;
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
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    40
public class MaxWarns {
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    41
    public static void main(String... args) throws Exception {
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    42
        new MaxWarns().run();
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
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    45
    void run() throws Exception {
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    46
        final int defaultMaxWarns = 100;
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    47
        final int genWarns = 150;
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    48
        File f = genSrc(genWarns);
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    49
        String out = javadoc(f);
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    50
        check(out, defaultMaxWarns);
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    51
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    52
        if (errors > 0)
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    53
            throw new Exception(errors + " errors occurred");
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
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    56
    File genSrc(int count) throws IOException {
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    57
        StringBuilder sb = new StringBuilder();
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    58
        sb.append("package p;\n")
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    59
            .append("public class C {\n")
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    60
            .append("    /**\n");
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    61
        for (int i = 0; i < count; i++)
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    62
            sb.append("     * @param i").append(i).append(" does not exist!\n");
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    63
        sb.append("     */\n")
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    64
            .append("    public void m() { }\n")
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    65
            .append("}\n");
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    66
        File srcDir = new File("src");
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    67
        srcDir.mkdirs();
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    68
        File f = new File(srcDir, "C.java");
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    69
        try (FileWriter fw = new FileWriter(f)) {
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    70
            fw.write(sb.toString());
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    71
        }
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    72
        return f;
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
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    75
    String javadoc(File f) {
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    76
        StringWriter sw = new StringWriter();
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    77
        PrintWriter pw = new PrintWriter(sw);
15354
52a04c670c05 8004834: Add doclint support into javadoc
jjg
parents: 15041
diff changeset
    78
        String[] args = { "-Xdoclint:none", "-d", "api", f.getPath() };
15041
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    79
        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
    80
                com.sun.tools.doclets.standard.Standard.class.getName(), args);
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    81
        pw.flush();
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    82
        return sw.toString();
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
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    85
    void check(String out, int count) {
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    86
        System.err.println(out);
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    87
        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
    88
        Matcher m = warn.matcher(out);
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    89
        int n = 0;
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    90
        for (int start = 0; m.find(start); start = m.start() + 1) {
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    91
            n++;
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    92
        }
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    93
        if (n != count)
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    94
            error("unexpected number of warnings reported: " + n + "; expected: " + count);
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    95
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    96
        Pattern warnCount = Pattern.compile("(?ms).*^([0-9]+) warnings$.*");
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    97
        m = warnCount.matcher(out);
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    98
        if (m.matches()) {
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
    99
            n = Integer.parseInt(m.group(1));
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
   100
            if (n != count)
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
   101
                error("unexpected number of warnings reported: " + n + "; expected: " + count);
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
   102
        } else
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
   103
            error("total count not found");
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
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
   106
    void error(String msg) {
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
   107
        System.err.println("Error: " + msg);
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
   108
        errors++;
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
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
   111
    int errors;
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
   112
}
0676d2a112ff 8005644: set default max errs and max warns
jjg
parents:
diff changeset
   113