1 /*
2  * Copyright (c) 2013, 2017, 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
27  * @summary set default max errs and max warns
28  * @modules jdk.javadoc/com.sun.tools.doclets.standard
29  */
30 
31 import java.io.File;
32 import java.io.PrintWriter;
33 import java.io.StringWriter;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
36 
37 import com.sun.javadoc.DocErrorReporter;
38 import com.sun.javadoc.RootDoc;
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         File f = new File(System.getProperty("test.src"), "MaxWarns.java");
48         String out = javadoc(f);
49         check(out, defaultMaxWarns);
50 
51         if (errors > 0)
52             throw new Exception(errors + " errors occurred");
53     }
54 
javadoc(File f)55     String javadoc(File f) {
56         StringWriter sw = new StringWriter();
57         PrintWriter pw = new PrintWriter(sw);
58         String[] args = { f.getPath() };
59         int rc = com.sun.tools.javadoc.Main.execute("javadoc", pw, pw, pw,
60                 "MaxWarns$TestDoclet",
61                 getClass().getClassLoader(), args);
62         pw.flush();
63         return sw.toString();
64     }
65 
66     private static final String WARNING_TEXT = "count ";
67 
check(String out, int count)68     void check(String out, int count) {
69         System.err.println(out);
70         Pattern warn = Pattern.compile("warning - " + WARNING_TEXT + "[0-9]+");
71         Matcher m = warn.matcher(out);
72         int n = 0;
73         for (int start = 0; m.find(start); start = m.start() + 1) {
74             n++;
75         }
76         if (n != count)
77             error("unexpected number of warnings reported: " + n + "; expected: " + count);
78 
79         Pattern warnCount = Pattern.compile("(?ms).*^([0-9]+) warnings$.*");
80         m = warnCount.matcher(out);
81         if (m.matches()) {
82             n = Integer.parseInt(m.group(1));
83             if (n != count)
84                 error("unexpected number of warnings reported: " + n + "; expected: " + count);
85         } else
86             error("total count not found");
87     }
88 
error(String msg)89     void error(String msg) {
90         System.err.println("Error: " + msg);
91         errors++;
92     }
93 
94     int errors;
95 
96     public static class TestDoclet {
97 
start(RootDoc root)98         public static boolean start(RootDoc root) {
99             // generate 150 warnings
100             for (int i = 1 ; i <= 150 ; i++) {
101                 root.printWarning(WARNING_TEXT + i);
102             }
103             return true;
104         }
105 
optionLength(String option)106         public static int optionLength(String option) {
107             return 0;
108         }
109 
validOptions(String[][] options, DocErrorReporter reporter)110         public static boolean validOptions(String[][] options, DocErrorReporter reporter) {
111             return true;
112         }
113     }
114 }
115 
116