TestUserDoclet.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (c) 2011, 2016, 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 6964914
27 * @summary javadoc does not output number of warnings using user written doclet
28 * @modules jdk.javadoc
29 */
30
31import java.io.*;
32
33import java.util.Collections;
34import java.util.Locale;
35import java.util.Set;
36
37import javax.lang.model.SourceVersion;
38
39import jdk.javadoc.doclet.Doclet;
40import jdk.javadoc.doclet.Reporter;
41import jdk.javadoc.doclet.DocletEnvironment;
42
43public class TestUserDoclet implements Doclet {
44    public static void main(String... args) throws Exception {
45        new TestUserDoclet().run();
46    }
47
48    static final String docletWarning = "warning from test doclet";
49
50    /** Main doclet method. */
51    public boolean run(DocletEnvironment root) {
52        reporter.print(javax.tools.Diagnostic.Kind.WARNING, docletWarning);
53        return true;
54    }
55
56    /** Main test method. */
57    void run() throws Exception {
58        File javaHome = new File(System.getProperty("java.home"));
59        File javadoc = new File(new File(javaHome, "bin"), "javadoc");
60        File testSrc = new File(System.getProperty("test.src"));
61        File testClasses = new File(System.getProperty("test.classes"));
62
63        // run javadoc in separate process to ensure doclet executed under
64        // normal user conditions w.r.t. classloader
65        String thisClassName = TestUserDoclet.class.getName();
66        Process p = new ProcessBuilder()
67            .command(javadoc.getPath(),
68                "-J-Xpatch:" + System.getProperty("jdk.launcher.patch.0", ""),
69                "-doclet", thisClassName,
70                "-docletpath", testClasses.getPath(),
71                new File(testSrc, thisClassName + ".java").getPath())
72            .redirectErrorStream(true)
73            .start();
74
75        int actualDocletWarnCount = 0;
76        int reportedDocletWarnCount = 0;
77        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
78        try {
79            String line;
80            while ((line = in.readLine()) != null) {
81                System.err.println(line);
82                if (line.contains(docletWarning))
83                    actualDocletWarnCount++;
84                if (line.matches("[0-9]+ warning(s)?"))
85                    reportedDocletWarnCount =
86                            Integer.valueOf(line.substring(0, line.indexOf(" ")));
87            }
88        } finally {
89            in.close();
90        }
91        int rc = p.waitFor();
92        if (rc != 0)
93            System.err.println("javadoc failed, rc:" + rc);
94
95        int expectedDocletWarnCount = 1;
96        checkEqual("actual", actualDocletWarnCount, "expected", expectedDocletWarnCount);
97        checkEqual("actual", actualDocletWarnCount, "reported", reportedDocletWarnCount);
98    }
99
100    void checkEqual(String l1, int i1, String l2, int i2) throws Exception {
101        if (i1 != i2)
102            throw new Exception(l1 + " warn count, " + i1 + ", does not match "
103                        + l2 + " warn count, " + i2);
104    }
105
106    @Override
107    public String getName() {
108        return "Test";
109    }
110
111    @Override
112    public Set<Option> getSupportedOptions() {
113        return Collections.emptySet();
114    }
115
116    @Override
117    public SourceVersion getSupportedSourceVersion() {
118        return SourceVersion.latest();
119    }
120
121    Reporter reporter;
122    Locale locale;
123    public void init(Locale locale, Reporter reporter) {
124        this.locale = locale;
125        this.reporter = reporter;
126    }
127}
128