File: TestReporterStreams.java

package info (click to toggle)
openjdk-23 23.0.2%2B7-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 815,324 kB
  • sloc: java: 5,632,909; cpp: 1,303,022; xml: 1,237,193; ansic: 419,177; asm: 404,932; objc: 20,978; sh: 15,486; javascript: 11,040; python: 6,802; makefile: 2,331; perl: 357; awk: 351; sed: 172; pascal: 103; exp: 26; jsp: 24; csh: 3
file content (220 lines) | stat: -rw-r--r-- 8,489 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

 /*
  * @test
  * @bug      8267204
  * @summary  Expose access to underlying streams in Reporter
  * @library  /tools/lib ../../lib
  * @modules  jdk.javadoc/jdk.javadoc.internal.tool
  * @build    toolbox.ToolBox javadoc.tester.*
  * @run main TestReporterStreams
  */

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Locale;
import java.util.Set;

import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;

import com.sun.source.doctree.DocCommentTree;
import com.sun.source.doctree.SinceTree;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.util.DocSourcePositions;
import com.sun.source.util.DocTreePath;
import com.sun.source.util.DocTrees;
import com.sun.source.util.TreePath;
import javadoc.tester.JavadocTester;
import jdk.javadoc.doclet.Doclet;
import jdk.javadoc.doclet.DocletEnvironment;
import jdk.javadoc.doclet.Reporter;
import toolbox.ToolBox;

public class TestReporterStreams extends JavadocTester {

    public static void main(String... args) throws Exception {
        var tester = new TestReporterStreams();
        tester.runTests();
    }

    ToolBox tb = new ToolBox();

    TestReporterStreams() throws IOException {
        tb.writeJavaFiles(Path.of("."), """
                    /**
                     * Comment.
                     * @since 0
                     */
                    public class C { }""");
    }

    /**
     * Tests the entry point used by the DocumentationTool API and JavadocTester, in which
     * all output is written to a single specified writer.
     */
    @Test
    public void testSingleStream(Path base) throws IOException {
        test(base, false, Output.OUT, Output.OUT);
    }

    /**
     * Tests the entry point used by the launcher, in which output is written to
     * writers that wrap {@code System.out} and {@code System.err}.
     */
    @Test
    public void testStandardStreams(Path base) throws IOException {
        test(base, true, Output.STDOUT, Output.STDERR);
    }

    void test(Path base, boolean useStdStreams, Output stdOut, Output stdErr) throws IOException {
        String testClasses = System.getProperty("test.classes");

        setOutputDirectoryCheck(DirectoryCheck.NONE);
        setUseStandardStreams(useStdStreams);
        javadoc("-docletpath", testClasses,
                "-doclet", MyDoclet.class.getName(),
                "C.java" // avoid using a directory, to avoid path separator issues in expected output
        );
        checkExit(Exit.ERROR);
        checkOutput(stdOut, true,
                "Writing to the standard writer");
        checkOutput(stdErr, true,
                "Writing to the diagnostic writer");
        checkOutput(stdErr, true,
                """
                    error: This is a ERROR with no position
                    C.java:5: error: This is a ERROR for an element
                    public class C { }
                           ^
                    C.java:2: error: This is a ERROR for a doc tree path
                     * Comment.
                       ^
                    C.java:3: error: This is a ERROR for a file position
                     * @since 0
                              ^
                    warning: This is a WARNING with no position
                    C.java:5: warning: This is a WARNING for an element
                    public class C { }
                           ^
                    C.java:2: warning: This is a WARNING for a doc tree path
                     * Comment.
                       ^
                    C.java:3: warning: This is a WARNING for a file position
                     * @since 0
                              ^
                    warning: This is a MANDATORY_WARNING with no position
                    C.java:5: warning: This is a MANDATORY_WARNING for an element
                    public class C { }
                           ^
                    C.java:2: warning: This is a MANDATORY_WARNING for a doc tree path
                     * Comment.
                       ^
                    C.java:3: warning: This is a MANDATORY_WARNING for a file position
                     * @since 0
                              ^
                    Note: This is a NOTE with no position
                    C.java:5: Note: This is a NOTE for an element
                    public class C { }
                           ^
                    C.java:2: Note: This is a NOTE for a doc tree path
                     * Comment.
                       ^
                    C.java:3: Note: This is a NOTE for a file position
                     * @since 0
                              ^
                    """);
    }

    public static class MyDoclet implements Doclet {
        private Locale locale;
        private Reporter reporter;

        @Override
        public void init(Locale locale, Reporter reporter) {
            this.locale = locale;
            this.reporter = reporter;
        }

        @Override
        public String getName() {
            return "MyDoclet";
        }

        @Override
        public Set<? extends Option> getSupportedOptions() {
            return Collections.emptySet();
        }

        @Override
        public SourceVersion getSupportedSourceVersion() {
            return SourceVersion.latestSupported();
        }

        @Override
        public boolean run(DocletEnvironment environment) {
            // Write directly to the given streams
            reporter.getStandardWriter().println("Writing to the standard writer");
            reporter.getDiagnosticWriter().println("Writing to the diagnostic writer");

            // the following is little more than a null check for the locale
            reporter.print(Diagnostic.Kind.NOTE, "The locale is " + locale.getDisplayName());

            // Write different kinds of diagnostics using the different overloads
            // for printing diagnostics
            Set<? extends Element> specElems = environment.getSpecifiedElements();
            Element e = specElems.iterator().next();

            DocTrees trees = environment.getDocTrees();
            TreePath tp = trees.getPath(e);
            DocCommentTree dct = trees.getDocCommentTree(e);
            DocTreePath dtp = new DocTreePath(tp, dct);

            CompilationUnitTree cut = tp.getCompilationUnit();
            JavaFileObject fo = cut.getSourceFile();
            SinceTree st = (SinceTree) dct.getBlockTags().get(0);
            DocSourcePositions sp = trees.getSourcePositions();
            int start = (int) sp.getStartPosition(cut, dct, st);
            int pos = (int) sp.getStartPosition(cut, dct, st.getBody().get(0));
            int end = (int) sp.getEndPosition(cut, dct, st);

            for (Diagnostic.Kind k : Diagnostic.Kind.values()) {
                if (k == Diagnostic.Kind.OTHER) {
                    continue;
                }

                reporter.print(k, "This is a " + k + " with no position");
                reporter.print(k, e, "This is a " + k + " for an element");
                reporter.print(k, dtp, "This is a " + k + " for a doc tree path");
                reporter.print(k, fo, start, pos, end, "This is a " + k + " for a file position");
            }

            return true;
        }
    }
}