File: TestMarkdownFirstSentence.java

package info (click to toggle)
openjdk-24 24.0.2%2B12-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 831,900 kB
  • sloc: java: 5,677,020; cpp: 1,323,154; xml: 1,320,524; ansic: 486,889; asm: 405,131; objc: 21,025; sh: 15,221; javascript: 11,049; python: 8,222; makefile: 2,504; perl: 357; awk: 351; sed: 172; pascal: 103; exp: 54; jsp: 24; csh: 3
file content (274 lines) | stat: -rw-r--r-- 11,269 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (c) 2022, 2024, 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      8298405
 * @summary  Markdown support in the standard doclet
 * @library  /tools/lib ../../lib
 * @modules  jdk.javadoc/jdk.javadoc.internal.tool
 * @build    toolbox.ToolBox javadoc.tester.*
 * @run main TestMarkdownFirstSentence
 */

import javadoc.tester.JavadocTester;
import toolbox.ToolBox;

import java.nio.file.Path;
import java.util.List;

public class TestMarkdownFirstSentence extends JavadocTester {

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

    ToolBox tb = new ToolBox();

    @Test
    public void testFirstSentence(Path base) throws Exception {
        Path src = base.resolve("src");
        tb.writeJavaFiles(src,
                """
                    package p;
                    public class C {
                        /// This is the _first_ sentence.
                        /// This is the _second_ sentence.
                         public void m() { }
                    }
                    """);
        javadoc("-d", base.resolve("api").toString(),
                "-Xdoclint:none",
                "--source-path", src.toString(),
                "p");

        checkOrder("p/C.html",
                """
                    <section class="method-summary" id="method-summary">""",
                """
                    <div class="block">This is the <em>first</em> sentence.</div>""",
                """
                    <section class="method-details" id="method-detail">""",
                """
                    <div class="block">This is the <em>first</em> sentence.
                    This is the <em>second</em> sentence.</div>""");
    }

    // Test the ability to put links in the first sentence of a description.
    // Note that user-defined reference links cannot be used in the first
    // sentence, and so in that case we verify the behavior is "as expected".
    @Test
    public void testFirstSentenceLinks(Path base) throws Exception {
        Path src = base.resolve("src");

        // Apart from the (control) case, the other cases exercise
        // various kinds of links in the first sentence of a description.
        // Note the last case is an explicit test of a link that is
        // _not_ currently supported, since the link reference definition
        // is not part of the first sentence.
        tb.writeJavaFiles(src,
                """
                    package p;
                    import q.MyObject;
                    public class C {
                        /// First sentence.
                        /// Control: [MyObject]
                        public void m1() { }

                        /// Simple autoref in first sentence [MyObject].
                        /// More.
                        public void m2() { }

                        /// Qualified autoref in first sentence [q.MyObject].
                        /// More.
                        public void m3() { }

                        /// Standard link with periods [example.com](http://example.com).
                        /// More.
                        public void m4() { }

                        /// Manual ref link [foo].
                        /// More.
                        ///
                        /// [foo]: http:example.com
                        public void m5() { }
                    }""",
                // use a simple class in a different package, to avoid platform links to system classes
                """
                    package q;
                    public class MyObject { }""");

        javadoc("-d", base.resolve("api").toString(),
                "-Xdoclint:none",
                "--source-path", src.toString(),
                "p", "q");
        checkExit(Exit.OK);

        // use checkOrder and the delimiter comments to ensure that
        // we check the strings in the method summary table, and not
        // subsequently in the method details section.
        checkOrder("p/C.html",
                "<!-- ========== METHOD SUMMARY =========== -->",
                """
                    <div class="block">Simple autoref in first sentence <a href="../q/MyObject.html" \
                    title="class in q"><code>MyObject</code></a>.</div>""",
                """
                    <div class="block">Qualified autoref in first sentence <a href="../q/MyObject.html" \
                    title="class in q"><code>MyObject</code></a>.</div>""",
                """
                    <div class="block">Standard link with periods \
                    <a href="http://example.com">example.com</a>.</div>""",
                // The following is a test of the regrettably expected behavior,
                // because the link reference definition is not carried into
                // the first sentence.
                """
                    <div class="block">Manual ref link [foo].</div>""",
                "<!-- ============ METHOD DETAIL ========== -->"
        );
    }

    // Test that periods within certain constructs do not prematurely terminate
    // the first sentence.
    @Test
    public void testFirstSentencePeriods(Path base) throws Exception {
        testFirstSentencePeriods(base.resolve("no-bi"), false);
        testFirstSentencePeriods(base.resolve("bi"), true);
    }

    void testFirstSentencePeriods(Path base, boolean useBreakIterator) throws Exception {
        Path src = base.resolve("src");

        tb.writeJavaFiles(src,
                """
                        package p;
                        public class C {
                            /// Code span `1.0` end.
                            /// More.
                            public void m1() { }
                            /// Complex code span ``` `1.0` ``` end.
                            /// More.
                            public void m2() { }
                            /// Period space `1.  2.  3.` end.
                            /// More.
                            public void m3() { }
                            /// Link [example.com](http://example.com) end.
                            /// More.
                            public void m4() { }
                        }
                        """);

        javadoc("-d", base.resolve("api").toString(),
                "-Xdoclint:none",
                (useBreakIterator ? "-breakiterator" : "-XDdummy"),
                "--source-path", src.toString(),
                "p");
        checkExit(Exit.OK);

        // use checkOrder and the delimiter comments to ensure that
        // we check the strings in the method summary table, and not
        // subsequently in the method details section.
        checkOrder("p/C.html",
                "<!-- ========== METHOD SUMMARY =========== -->",
                """
                    <div class="block">Code span <code>1.0</code> end.</div>""",
                """
                    <div class="block">Complex code span <code>`1.0`</code> end.</div>""",
                """
                    <div class="block">Period space <code>1.  2.  3.</code> end.</div>""",
                """
                    <div class="block">Link <a href="http://example.com">example.com</a> end.</div>""",
                "<!-- ============ METHOD DETAIL ========== -->"
        );
    }

    @Test
    public void testIndentedInlineReturn(Path base) throws Exception {
        //this is a Markdown-specific test, because leading whitespace is ignored in HTML comments
        Path src = base.resolve("src");
        tb.writeJavaFiles(src,
                """
                    package p;
                    /// Class description.
                    public class C {
                        ///    {@return an int}
                        /// More description.
                        public int m() { return 0; }
                    }
                    """);
        javadoc("-d", base.resolve("api").toString(),
                "--source-path", src.toString(),
                "p");

        checkOutput("p/C.html", true,
                """
                    <section class="detail" id="m()">
                    <h3>m</h3>
                    <div class="horizontal-scroll">
                    <div class="member-signature"><span class="modifiers">public</span>&nbsp;<span class="return-type">int</span>&nbsp;<span class="element-name">m</span>()</div>
                    <div class="block">Returns an int.
                    More description.</div>
                    <dl class="notes">
                    <dt>Returns:</dt>
                    <dd>an int</dd>
                    </dl>
                    </div>
                    </section>""");
    }

    @Test
    public void testExtraPara(Path base) throws Exception {
        Path src = base.resolve("src");
        tb.writeJavaFiles(src,
                """
                    package p;
                    /// This is the class description.
                    ///
                    /// # Heading
                    /// Lorem ipsum
                    public class C { }
                    """);
        javadoc("-d", base.resolve("api").toString(),
                "--no-platform-links",
                "--source-path", src.toString(),
                "p");

        checkOutput("p/package-summary.html", true,
                """
                    <div class="col-first even-row-color class-summary class-summary-tab2"><a href="C.html" title="clas\
                    s in p">C</a></div>
                    <div class="col-last even-row-color class-summary class-summary-tab2">
                    <div class="block">This is the class description.</div>
                    </div>""");

        checkOutput("p/C.html", true,
                """
                        <span class="element-name type-name-label">C</span>
                        <span class="extends-implements">extends java.lang.Object</span></div>
                        <div class="block"><p>This is the class description.</p>
                        <h2 id="heading-heading">Heading</h2>
                        <p>Lorem ipsum</p>
                        </div>""");

    }
}