Test.java revision 4212:ead6d6c18bcc
1/*
2 * Copyright (c) 2010, 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 6958836 8002168
27 * @summary javadoc should support -Xmaxerrs and -Xmaxwarns
28 * @modules jdk.javadoc
29 */
30
31import java.io.*;
32import java.util.*;
33
34import com.sun.javadoc.DocErrorReporter;
35import com.sun.javadoc.RootDoc;
36
37
38
39public class Test {
40    private static final String ERROR_MARKER = "Error-count";
41    private static final String WARNING_MARKER = "Warning-count";
42
43    public static void main(String... args) throws Exception {
44        new Test().run();
45    }
46
47    void run() throws Exception {
48        javadoc("Errors",  list(),                   10,  0);
49        javadoc("Errors",  list("-Xmaxerrs",   "0"), 10,  0);
50        javadoc("Errors",  list("-Xmaxerrs",   "2"),  2,  0);
51        javadoc("Errors",  list("-Xmaxerrs",   "4"),  4,  0);
52        javadoc("Errors",  list("-Xmaxerrs",  "20"), 10,  0);
53
54        javadoc("Warnings", list(),                    0, 10);
55        javadoc("Warnings", list("-Xmaxwarns",  "0"),  0, 10);
56        javadoc("Warnings", list("-Xmaxwarns",  "2"),  0,  2);
57        javadoc("Warnings", list("-Xmaxwarns",  "4"),  0,  4);
58        javadoc("Warnings", list("-Xmaxwarns", "20"),  0, 10);
59
60        if (errors > 0)
61            throw new Exception(errors + " errors occurred.");
62    }
63
64    void javadoc(String selector, List<String> testOpts,
65                int expectErrs, int expectWarns) {
66        System.err.println("Test " + (++count) + ": " + selector + " " + testOpts);
67        File testOutDir = new File("test" + count);
68
69        List<String> opts = new ArrayList<String>();
70        // Force en_US locale in lieu of something like -XDrawDiagnostics.
71        // For some reason, this must be the first option when used.
72        opts.addAll(list("-locale", "en_US"));
73        opts.add(new File(System.getProperty("test.src"),
74                Test.class.getSimpleName() + ".java").getPath());
75        opts.addAll(testOpts);
76        opts.add("-gen" + selector);
77
78        StringWriter errSW = new StringWriter();
79        PrintWriter errPW = new PrintWriter(errSW);
80        StringWriter warnSW = new StringWriter();
81        PrintWriter warnPW = new PrintWriter(warnSW);
82        StringWriter noteSW = new StringWriter();
83        PrintWriter notePW = new PrintWriter(noteSW);
84
85        int rc = com.sun.tools.javadoc.Main.execute("javadoc",
86                              errPW, warnPW, notePW,
87                              "Test$TestDoclet",
88                              getClass().getClassLoader(),
89                              opts.toArray(new String[opts.size()]));
90        System.err.println("rc: " + rc);
91
92        errPW.close();
93        String errOut = errSW.toString();
94        System.err.println("Errors:\n" + errOut);
95        warnPW.close();
96        String warnOut = warnSW.toString();
97        System.err.println("Warnings:\n" + warnOut);
98        notePW.close();
99        String noteOut = noteSW.toString();
100        System.err.println("Notes:\n" + noteOut);
101
102        check(errOut, ERROR_MARKER, expectErrs);
103        check(warnOut, WARNING_MARKER, expectWarns); // requires -locale en_US
104    }
105
106    void check(String text, String expectText, int expectCount) {
107        int foundCount = 0;
108        for (String line: text.split("[\r\n]+")) {
109            if (line.contains(expectText))
110                foundCount++;
111        }
112        if (foundCount != expectCount) {
113            error("incorrect number of matches found: " + foundCount
114                  + ", expected: " + expectCount);
115        }
116    }
117
118    private List<String> list(String... args) {
119        return Arrays.asList(args);
120    }
121
122    void error(String msg) {
123        System.err.println(msg);
124        errors++;
125    }
126
127    int count;
128    int errors;
129
130    public static class TestDoclet {
131        static boolean genErrors = false;
132        static boolean genWarnings = false;
133
134        public static boolean start(RootDoc root) {
135            // generate 10 errors or warnings
136            for (int i = 1 ; i <= 10 ; i++) {
137                if (genErrors)
138                    root.printError(ERROR_MARKER + " " + i);
139                if (genWarnings)
140                    root.printWarning(WARNING_MARKER + " " + i);
141            }
142            return true;
143        }
144
145        public static int optionLength(String option) {
146            if (option == null) {
147                throw new Error("invalid usage: ");
148            }
149            System.out.println("option: " + option);
150            switch (option.trim()) {
151                case "-genErrors":
152                    return 1;
153                case "-genWarnings":
154                    return 1;
155                default:
156                    return 0;
157            }
158        }
159
160        public static boolean validOptions(String[][] options, DocErrorReporter reporter) {
161            for (int i = 0 ; i < options.length; i++) {
162               String opt = options[i][0].trim();
163               switch (opt) {
164                   case "-genErrors":
165                       genErrors = true;
166                       genWarnings = false;
167                       break;
168                   case "-genWarnings":
169                       genErrors = false;
170                       genWarnings = true;
171                       break;
172               }
173            }
174            return true;
175        }
176    }
177}
178