test/tools/javadoc/MaxWarns.java

Wed, 14 Nov 2018 10:18:25 -0800

author
diazhou
date
Wed, 14 Nov 2018 10:18:25 -0800
changeset 3762
7909abb85562
parent 1490
fc4cb1577ad6
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8u201-b04 for changeset a7f48b9dfb82

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

mercurial