File: TestJavadocTester.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (438 lines) | stat: -rw-r--r-- 16,374 bytes parent folder | download | duplicates (2)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
 * Copyright (c) 2021, 2022, 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 8273154
 * @summary Provide a JavadocTester method for non-overlapping, unordered output matching
 * @library /tools/lib/ ../lib
 * @modules jdk.javadoc/jdk.javadoc.internal.tool
 * @build toolbox.ToolBox javadoc.tester.*
 * @run main TestJavadocTester
 */

import javadoc.tester.JavadocTester;

import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import toolbox.ToolBox;

/**
 * Tests basic mechanisms in the {@code JavadocTester} class.
 *
 * It is not a direct test of the javadoc tool or the output generated by the
 * Standard Doclet, although both are indirectly used as part of this test.
 *
 * The test works by exercising the {@code JavadocTester} API with a series of
 * positive and negative tests.  The {@code passed} and {@code failed} methods
 * are overridden to record the messages reported by the underlying instance, so
 * that the messages can subsequently be verified. Also, {@code printSummary}
 * is overridden to suppress the default action to throw {@code Error} when
 * tests have failed.
 */
public class TestJavadocTester extends JavadocTester {
    public static void main(String... args) throws Exception {
        var tester = new TestJavadocTester();
        tester.setup().runTests();
    }

    protected final List<String> messages = new ArrayList<>();
    private int testErrors = 0;

    /**
     * Overrides the default implementation of {@code passed} to record the argument.
     * {@inheritDoc}
     *
     * @param message a short description of the outcome
     */
    @Override
    public void passed(String message) {
        super.passed(message);
        messages.add("Passed: " + message);
    }

    /**
     * Overrides the default implementation of {@code failed} to record the argument.
     * {@inheritDoc}
     *
     * @param message a short description of the outcome
     */
    @Override
    public void failed(String message) {
        super.failed(message);
        messages.add("FAILED: " + message);
    }

    /**
     * Overrides the default implementation of {@code printSummary} to suppress
     * the error thrown as a result of errors reported by {@code JavadocTester}.
     * Instead, an error is thrown if any errors are found by the tests in this class.
     */
    @Override
    public void printSummary() {
        try {
            super.printSummary();
        } catch (Error e) {
            if (e.getClass() != Error.class) {
                throw e;
            }
            report("Suppressed: " + e);
        }

        if (testErrors > 0) {
            report(testErrors + " errors found");
            throw new Error(testErrors + " errors found");
        }
    }

    /**
     * Checks the content of messages reported by the {@code passed} and {@code failed}
     * methods in {@code JavadocTester}.  The messages are saved by the local overloads
     * of those methods in this class.
     *
     * Because some of the messages are <em>very</em> long, it is enough to pass in
     * initial substrings of the expected messages.
     *
     * Note that messages reported by {@code JavadocTester} use filenames as given
     * to the various {@code check...} calls. By convention, these always use {@code /}
     * as the file separator, and not the platform file separator.
     *
     * @param expect initial substrings of expected messages
     */
    void checkMessages(String... expect) {
        for (String e : expect) {
            Optional<String> match = messages.stream()
                    .filter(m -> m.startsWith(e))
                    .findFirst();
            if (match.isPresent()) {
                report("found '" + e + "'");
            } else {
                report("ERROR: no message found for '" + e + "'");
                testErrors++;
            }
        }
    }

    /**
     * Reports a message, preceded by {@code >>> }.
     *
     * It is helpful/important to distinguish the messages written as a side-effect
     * of the underlying tests from the messages used to report the outcome of the
     * tests that verify those messages.  Instead of interposing to mark the messages
     * written as a side effect of the underlying tests, we leave those messages
     * unchanged, and instead, mark the messages reporting whether those messages
     * are as expected or not.
     *
     * @param message the message to be reported.
     */
    private void report(String message) {
        message.lines().forEachOrdered(l -> out.println(">>> " + l));
    }

    //-------------------------------------------------

    protected final ToolBox tb = new ToolBox();

    TestJavadocTester setup() throws IOException {
        Path src = Path.of("src");
        tb.writeJavaFiles(src, """
                package p;
                /**
                 * First sentence abc.
                 * Second sentence.
                 * abc123
                 * def456
                 * ghi789
                 * abc123
                 * def456
                 * ghi789
                 */
                public class C {
                    private C() { }
                    /** m3 comment. */
                    public void m3() { }
                    /** m2 comment. */
                    public void m2() { }
                    /** m1 comment. */
                    public void m1() { }
                }
                """);

        javadoc("-d", "out",
                "-sourcepath", src.toString(),
                "-noindex", "-nohelp",
                "p");
        return this;
    }

    @Test
    public void testSimpleStringCheck() {
        messages.clear();
        new OutputChecker("p/C.html")
                .check("Second sentence",
                        "abc123",
                        "def456");
        messages.forEach(this::report);
        checkMessages(
                """
                    Passed: out/p/C.html: following text found:
                    Second sentence""",
                """
                    Passed: out/p/C.html: following text found:
                    abc123""",
                """
                    Passed: out/p/C.html: following text found:
                    def456""");
    }

    @Test
    public void testSimpleNegativeStringCheck_expected() {
        messages.clear();
        new OutputChecker("p/C.html")
                .setExpectFound(false)
                .check("Third sentence.");
        checkMessages(
                """
                    Passed: out/p/C.html: following text not found:
                    Third sentence""");
    }

    @Test
    public void testSimpleNegativeStringCheck_unexpected() {
        messages.clear();
        new OutputChecker("p/C.html")
                .check("Third sentence.");
        checkMessages(
                """
                    FAILED: out/p/C.html: following text not found:
                    Third sentence""");
    }

    @Test
    public void testSimpleRegexCheck() {
        messages.clear();
        new OutputChecker("p/C.html")
                .check(Pattern.compile("S.cond s.nt.nc."),
                        Pattern.compile("[abc]{3}[123]{3}"),
                        Pattern.compile("d.f4.6"));
        checkMessages(
                """
                    Passed: out/p/C.html: following pattern found:
                    S.cond s.nt.nc.""",
                """
                    Passed: out/p/C.html: following pattern found:
                    [abc]{3}[123]{3}""",
                """
                    Passed: out/p/C.html: following pattern found:
                    d.f4.6""");
    }

    @Test
    public void testOrdered() {
        messages.clear();
        // methods are listed alphabetically in the Summary table,
        // but in source-code order in the Details section.
        new OutputChecker("p/C.html")
                .check("<h2>Method Summary</h2>",
                        "<a href=\"#m1()\" class=\"member-name-link\">m1</a>",
                        "<a href=\"#m2()\" class=\"member-name-link\">m2</a>",
                        "<a href=\"#m3()\" class=\"member-name-link\">m3</a>")
                .check("<h2>Method Details</h2>",
                        "<section class=\"detail\" id=\"m3()\">\n",
                        "<section class=\"detail\" id=\"m2()\">\n",
                        "<section class=\"detail\" id=\"m1()\">\n");

        checkMessages(
                """
                    Passed: out/p/C.html: following text found:
                    <h2>Method Summary</h2>""",
                """
                    Passed: out/p/C.html: following text found:
                    <a href="#m1()" class="member-name-link">m1</a>""",
                """
                    Passed: out/p/C.html: following text found:
                    <a href="#m2()" class="member-name-link">m2</a>""",
                """
                    Passed: out/p/C.html: following text found:
                    <a href="#m3()" class="member-name-link">m3</a>""",
                """
                    Passed: out/p/C.html: following text found:
                    <h2>Method Details</h2>""",
                """
                    Passed: out/p/C.html: following text found:
                    <section class="detail" id="m3()">""",
                """
                    Passed: out/p/C.html: following text found:
                    <section class="detail" id="m2()">""",
                """
                    Passed: out/p/C.html: following text found:
                    <section class="detail" id="m1()">"""
        );
    }

    @Test
    public void testUnordered_expected() {
        messages.clear();
        new OutputChecker("p/C.html")
                .setExpectOrdered(false)
                .check("Second sentence",
                        "First sentence");
        checkMessages(
                """
                    Passed: out/p/C.html: following text found:
                    Second sentence""",
                """
                    Passed: out/p/C.html: following text found:
                    First sentence""");
    }

    @Test
    public void testUnordered_unexpected() {
        messages.clear();
        new OutputChecker("p/C.html")
                .check("Second sentence",
                        "First sentence");
        checkMessages(
                """
                    Passed: out/p/C.html: following text found:
                    Second sentence""",
                """
                    FAILED: out/p/C.html: following text was found on line""");
    }

    @Test
    public void testComplete_Ordered() {
        messages.clear();
        // In the following calls, the strings are specified in the expected order.
        // File separators are made platform-specific by calling 'fix'.
        // Newlines are handled automatically by the 'check' method.
        new OutputChecker(Output.OUT)
                .check("Loading source files for package p...\n",
                        "Constructing Javadoc information...\n",
                        fix("Creating destination directory: \"out/\"\n"))
                .check(Pattern.compile("Standard Doclet .*\\R"))
                .check("Building tree for all the packages and classes...\n",
                        fix("Generating out/p/C.html...\n"),
                        fix("Generating out/p/package-summary.html...\n"),
                        fix("Generating out/p/package-tree.html...\n"),
                        fix("Generating out/overview-tree.html...\n"),
                        fix("Generating out/index.html...\n"))
                .checkComplete();
        checkMessages("Passed: All output matched");
    }

    @Test
    public void testComplete_Unordered() {
        messages.clear();
        // In the following calls, the strings are deliberately specified out of the expected order.
        // File separators are made platform-specific by calling 'fix'.
        // Newlines are handled automatically by the 'check' method.
        new OutputChecker(Output.OUT)
                .setExpectOrdered(false)
                .check("Loading source files for package p...\n",
                        "Constructing Javadoc information...\n",
                        "Building tree for all the packages and classes...\n")
                .check(fix("Creating destination directory: \"out/\"\n",
                        "Generating out/index.html...\n",
                        "Generating out/overview-tree.html...\n",
                        "Generating out/p/package-tree.html...\n",
                        "Generating out/p/package-summary.html...\n",
                        "Generating out/p/C.html...\n"))
                .check(Pattern.compile("Standard Doclet .*\\R"))
                .checkComplete();
        checkMessages("Passed: All output matched");
    }

    @Test
    public void testEmpty() {
        messages.clear();
        new OutputChecker(Output.STDERR)
                .checkEmpty();
        checkMessages("Passed: STDERR is empty, as expected");
    }

    @Test
    public void testBadFile() {
        messages.clear();
        new OutputChecker("does-not-exist.html")
                .check("abcdef",
                        "very long string ".repeat(10))
                .check(Pattern.quote("abcdef"),
                        Pattern.quote("very long string".repeat(10)));
        checkMessages("FAILED: File not found: does-not-exist.html");
    }

    @Test
    public void testAnyOf() {
        messages.clear();
        new OutputChecker("p/C.html")
                .checkAnyOf("m1()", "m2()", "m3()")    // expect all found
                .checkAnyOf("m1()", "m2()", "M3()")    // expect some found
                .checkAnyOf("M1()", "M2()", "M3()");   // expect none found
        checkMessages("Passed: 3 matches found",
                "Passed: 2 matches found",
                "FAILED: no match found for any text");
    }

    @Test
    public void testUnique() {
        messages.clear();
        new OutputChecker("p/C.html")
                .setExpectOrdered(false)
                .checkUnique("id=\"m1()\"", "id=\"m2()\"", "id=\"m3()\"")   // expect unique
                .checkUnique("m1()", "m2()", "m3()");                       // expect not unique
        checkMessages("Passed: out/p/C.html: id=\"m1()\" is unique",
                "Passed: out/p/C.html: id=\"m2()\" is unique",
                "Passed: out/p/C.html: id=\"m3()\" is unique",
                "FAILED: out/p/C.html: m1() is not unique",
                "FAILED: out/p/C.html: m2() is not unique",
                "FAILED: out/p/C.html: m3() is not unique");
    }

    /**
     * {@return a string with {@code /} replaced by the platform file separator}
     *
     * @param item the string
     */
    private String fix(String item) {
        return item.replace("/", FS);
    }

    /**
     * {@return an array of strings with {@code /} replaced by the platform file separator}
     *
     * @param items the strings
     */
    private String[] fix(String... items) {
        return Stream.of(items)
                .map(this::fix)
                .toArray(String[]::new);
    }
}