1 /*
2  * Copyright (c) 2013, 2018, 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  */
23 
24 /*
25  * @test
26  * @bug 8005644 8182765
27  * @summary set default max errs and max warns
28  * @modules jdk.javadoc/jdk.javadoc.internal.tool
29  */
30 
31 import java.io.File;
32 import java.io.FileWriter;
33 import java.io.IOException;
34 import java.io.PrintWriter;
35 import java.io.StringWriter;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
38 
39 
40 public class MaxWarns {
main(String... args)41     public static void main(String... args) throws Exception {
42         new MaxWarns().run();
43     }
44 
run()45     void run() throws Exception {
46         final int defaultMaxWarns = 100;
47         final int genWarns = 150;
48         File f = genSrc(genWarns);
49         String out = javadoc(f);
50         check(out, defaultMaxWarns);
51 
52         if (errors > 0)
53             throw new Exception(errors + " errors occurred");
54     }
55 
genSrc(int count)56     File genSrc(int count) throws IOException {
57         StringBuilder sb = new StringBuilder();
58         sb.append("package p;\n")
59             .append("public class C {\n")
60             .append("    /**\n");
61         for (int i = 0; i < count; i++)
62             sb.append("     * @param i").append(i).append(" does not exist!\n");
63         sb.append("     */\n")
64             .append("    public void m() { }\n")
65             .append("}\n");
66         File srcDir = new File("src");
67         srcDir.mkdirs();
68         File f = new File(srcDir, "C.java");
69         try (FileWriter fw = new FileWriter(f)) {
70             fw.write(sb.toString());
71         }
72         return f;
73     }
74 
javadoc(File f)75     String javadoc(File f) {
76         StringWriter sw = new StringWriter();
77         PrintWriter pw = new PrintWriter(sw);
78         String[] args = { "-Xdoclint:none", "-d", "api", f.getPath() };
79         int rc = jdk.javadoc.internal.tool.Main.execute(args, pw);
80         pw.flush();
81         return sw.toString();
82     }
83 
check(String out, int count)84     void check(String out, int count) {
85         System.err.println(out);
86         Pattern warn = Pattern.compile("""
87             warning - @param argument "i[0-9]+" is not a parameter name""");
88         Matcher m = warn.matcher(out);
89         int n = 0;
90         for (int start = 0; m.find(start); start = m.start() + 1) {
91             n++;
92         }
93         if (n != count)
94             error("unexpected number of warnings reported: " + n + "; expected: " + count);
95 
96         Pattern warnCount = Pattern.compile("(?ms).*^([0-9]+) warnings$.*");
97         m = warnCount.matcher(out);
98         if (m.matches()) {
99             n = Integer.parseInt(m.group(1));
100             if (n != count)
101                 error("unexpected number of warnings reported: " + n + "; expected: " + count);
102         } else
103             error("total count not found");
104     }
105 
error(String msg)106     void error(String msg) {
107         System.err.println("Error: " + msg);
108         errors++;
109     }
110 
111     int errors;
112 }
113 
114