File: TestJavaFXCombo.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 (435 lines) | stat: -rw-r--r-- 18,295 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
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
/*
 * 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 8270195
 * @summary Add missing links between methods of JavaFX properties
 * @library /tools/lib ../../lib
 * @modules jdk.javadoc/jdk.javadoc.internal.tool
 * @build toolbox.ToolBox javadoc.tester.*
 * @run main TestJavaFXCombo
 */

import java.io.IOException;
import java.nio.file.Path;
import java.util.EnumSet;
import java.util.Set;

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

/**
 * Combo-test for JavaFX properties and related methods.
 * The test generates instances of a class with various combinations of
 * a property field, property method, getter method and setter method,
 * each in combinations of with and without doc comments.
 * For each instance, it runs javadoc and verifies the generated
 * code and any diagnostics are as expected.
 */
public class TestJavaFXCombo extends JavadocTester {
    public static void main(String... args) throws Exception {
        var tester = new TestJavaFXCombo(args);
        tester.runTests();
    }

    ToolBox tb = new ToolBox();
    enum Kind { NONE, NO_COMMENT, COMMENT }

    private final Set<Kind> fieldValues = EnumSet.allOf(Kind.class);
    private final Set<Kind> propertyMethodValues = EnumSet.allOf(Kind.class);
    private final Set<Kind> getterMethodValues = EnumSet.allOf(Kind.class);
    private final Set<Kind> setterMethodValues = EnumSet.allOf(Kind.class);

    TestJavaFXCombo(String... args) {
        // for testing, allow subsets of combinations to be specified
        for (int i = 0; i < args.length; i++) {
            String arg = args[1];
            switch (arg) {
                case "-f" -> set(fieldValues, args[++i]);
                case "-p" -> set(propertyMethodValues, args[++i]);
                case "-g" -> set(getterMethodValues, args[++i]);
                case "-s" -> set(setterMethodValues, args[++i]);
            }
        }

        // A property method is always required for any property,
        propertyMethodValues.remove(Kind.NONE);

    }

    private void set(Set<Kind> set, String values) {
        set.clear();
        for (String v : values.split("[, ]")) {
            set.add(Kind.valueOf(v));
        }
    }

    @Test
    public void test(Path base) throws IOException {
        for (Kind pk : propertyMethodValues) {
            for (Kind fk : fieldValues) {
                for (Kind gk : getterMethodValues) {
                    for (Kind sk: setterMethodValues) {
                        test(base, fk, pk, gk, sk);
                    }
                }
            }
        }
    }

    void test(Path base, Kind fk, Kind pk, Kind gk, Kind sk) throws IOException {
        String description = "Field:" + fk + " Property:" + pk + " Getter:" + gk + " Setter:" + sk;
        out.println("Test: " + description);
        Path sub = base.resolve(String.format("%s-%s-%s-%s", abbrev(fk), abbrev(pk), abbrev(gk), abbrev(sk)));
        Path src = sub.resolve("src");
        tb.writeJavaFiles(src, """
                package p;
                /** Dummy property class. */
                public class BooleanProperty { }
                """, """
                package p;
                /** Class comment. ## */
                public class C {
                """.replace("##", description)
                + getFieldText(fk)
                + getPropertyMethodText(pk)
                + getGetterMethodText(gk)
                + getSetterMethodText(sk)
                + """
                }
                """
        );

        javadoc("-d", sub.resolve("api").toString(),
                "-javafx",
                "--disable-javafx-strict-checks",
                "-Xdoclint:all,-missing",
                "-nohelp", "-noindex",
                "-sourcepath", src.toString(),
                "p");
        checkExit(Exit.OK);
        checkField(fk, pk, gk, sk);
        checkGetter(fk, pk, gk, sk);
        checkSetter(fk, pk, gk, sk);
        checkPropertyMethod(fk, pk, gk, sk);
        checkDiags(fk, pk, gk, sk);
    }

    void checkField(Kind fk, Kind pk, Kind gk, Kind sk) {
        // the field is private and so should never show up
        checkOutput("p/C.html", false,
                "field.detail");
    }

    void checkGetter(Kind fk, Kind pk, Kind gk, Kind sk) {
        switch (gk) {
            case NONE ->
                    checkOutput("p/C.html", false,
                            "getExample");

            case NO_COMMENT ->
                    // comment gets auto-created
                    checkOutput("p/C.html", true,
                            """
                                <section class="detail" id="getExample()">
                                <h3>getExample</h3>
                                <div class="horizontal-scroll">
                                <div class="member-signature"><span class="modifiers">public</span>&nbsp;<span class="return-type">boolean</span>&nbsp;<span class="element-name">getExample</span>()</div>
                                <div class="block">Gets the value of the <code>example</code> property.</div>
                                <dl class="notes">
                                <dt>Property description:</dt>
                                #DESC#
                                <dt>Returns:</dt>
                                <dd>the value of the <code>example</code> property</dd>
                                #SEE#
                                </dl>
                                </div>
                                </section>
                                """
                                .replace("#DESC#", getPropertyDescription(fk, pk))
                                .replace("#SEE#", getSee(pk, null, sk))
                                .replaceAll("\n\n", "\n")
                            );

            case COMMENT ->
                    // existing comments do not get modified
                    checkOutput("p/C.html", true,
                            """
                                <section class="detail" id="getExample()">
                                <h3>getExample</h3>
                                <div class="horizontal-scroll">
                                <div class="member-signature"><span class="modifiers">public</span>&nbsp;<span class="return-type">boolean</span>&nbsp;<span class="element-name">getExample</span>()</div>
                                <div class="block">Getter method description. More getter method description.</div>
                                <dl class="notes">
                                <dt>Returns:</dt>
                                <dd>the <code>example</code> property</dd>
                                </dl>
                                </div>
                                </section>
                                    """);
        }
    }

    void checkSetter(Kind fk, Kind pk, Kind gk, Kind sk) {
        switch (sk) {
            case NONE ->
                    checkOutput("p/C.html", false,
                            "setExample");

            case NO_COMMENT ->
                    // comment gets auto-created
                    checkOutput("p/C.html", true,
                            """
                                <section class="detail" id="setExample(boolean)">
                                <h3>setExample</h3>
                                <div class="horizontal-scroll">
                                <div class="member-signature"><span class="modifiers">public</span>&nbsp;<span class="return-type">void</span>&nbsp;<span class="element-name">setExample</span><wbr><span class="parameters">(boolean&nbsp;b)</span></div>
                                <div class="block">Sets the value of the <code>example</code> property.</div>
                                <dl class="notes">
                                <dt>Property description:</dt>
                                #DESC#
                                <dt>Parameters:</dt>
                                <dd><code>b</code> - the value for the <code>example</code> property</dd>
                                #SEE#
                                </dl>
                                </div>
                                </section>
                                """
                                .replace("#DESC#", getPropertyDescription(fk, pk))
                                .replace("#SEE#", getSee(pk, gk, null))
                                .replaceAll("\n\n", "\n"));

            case COMMENT ->
                    // existing comments do not get modified
                    checkOutput("p/C.html", true,
                            """
                                <section class="detail" id="setExample(boolean)">
                                <h3>setExample</h3>
                                <div class="horizontal-scroll">
                                <div class="member-signature"><span class="modifiers">public</span>&nbsp;<span class="return-type">void</span>&nbsp;<span class="element-name">setExample</span><wbr><span class="parameters">(boolean&nbsp;b)</span></div>
                                <div class="block">Setter method description. More setter method description.</div>
                                <dl class="notes">
                                <dt>Parameters:</dt>
                                <dd><code>b</code> - the new value for the property</dd>
                                </dl>
                                </div>
                                </section>
                                """);
        }
    }

    void checkPropertyMethod(Kind fk, Kind pk, Kind gk, Kind sk) {
        switch (pk) {
            case NONE ->
                    // should not happen; there is always a property method
                    throw new IllegalArgumentException();

            case NO_COMMENT ->
                    checkOutput("p/C.html", true,
                            """
                                <section class="detail" id="exampleProperty()">
                                <h3>exampleProperty</h3>
                                <div class="horizontal-scroll">
                                <div class="member-signature"><span class="modifiers">public</span>&nbsp;<span class="return-type"><a href="BooleanProperty.html" title="class in p">BooleanProperty</a></span>&nbsp;<span class="element-name">exampleProperty</span>()</div>
                                #PCOMM#
                                <dl class="notes">
                                <dt>Returns:</dt>
                                <dd>the <code>example</code> property</dd>
                                #SEE#
                                </dl>
                                </div>
                                </section>
                                """
                                .replace("#PCOMM#", getPropertyMethodComment(fk, pk))
                                .replace("#SEE#", getSee(null, gk, sk))
                                .replaceAll("\n\n", "\n"));

            case COMMENT ->
                    // @see tags are added to an existing method if it is the primary source of info
                    // for the property (i.e. there is no comment on a corresponding property field.
                    checkOutput("p/C.html", true,
                            """
                                <section class="detail" id="exampleProperty()">
                                <h3>exampleProperty</h3>
                                <div class="horizontal-scroll">
                                <div class="member-signature"><span class="modifiers">public</span>&nbsp;<span class="return-type"><a href="BooleanProperty.html" title="class in p">BooleanProperty</a></span>&nbsp;<span class="element-name">exampleProperty</span>()</div>
                                <div class="block">Property method description. More property method description.</div>
                                <dl class="notes">
                                <dt>Returns:</dt>
                                <dd>the <code>example</code> property</dd>
                                #SEE#
                                </dl>
                                </div>
                                </section>
                                """
                                .replace("#SEE#", (fk == Kind.COMMENT ? "" : getSee(null, gk, sk)))
                                .replaceAll("\n\n", "\n"));
        }
    }

    void checkDiags(Kind fk, Kind pk, Kind gk, Kind sk) {
        // A warning is generated if there is a comment on both the property field and property method
        checkOutput(Output.OUT, (fk == Kind.COMMENT && pk == Kind.COMMENT),
                "warning: Duplicate comment for property",
                "Remove the comment on the property field or on this method to suppress this warning.");
    }

    String getPropertyComment(Kind fk, Kind pk) {
        return switch (fk) {
            case NONE, NO_COMMENT ->
                    switch (pk) {
                        case NONE, NO_COMMENT ->
                                "";

                        case COMMENT ->
                                "Property method description. More property method description.";
                    };

            case COMMENT ->
                    "Field description. More field description.";
        };
    }

    String getPropertyDescription(Kind fk, Kind pk) {
        String s = getPropertyComment(fk, pk);
        return s.isEmpty() ? s : "<dd>" + s + "</dd>";
    }

    String getPropertyMethodComment(Kind fk, Kind pk) {
        String s = getPropertyComment(fk, pk);
        return s.isEmpty() ? s : "<div class=\"block\">" + s + "</div>";
    }

    String getSee(Kind pk, Kind gk, Kind sk) {
        StringBuilder sb = new StringBuilder();
        if (gk != null && gk != Kind.NONE) {
            sb.append("""
                <li><a href="#getExample()"><code>getExample()</code></a></li>
                """);
        }
        if (sk != null && sk != Kind.NONE) {
            sb.append("""
                <li><a href="#setExample(boolean)"><code>setExample(boolean)</code></a></li>
                """);
        }
        if (pk != null && pk != Kind.NONE) {
            sb.append("""
                <li><a href="#exampleProperty()"><code>exampleProperty()</code></a></li>
                """);
        }
        return sb.isEmpty() ? "" : """
                <dt>See Also:</dt>
                <dd>
                <ul class="tag-list">
                """ + sb + """
                </ul>
                </dd>""";
    }

    String abbrev(Kind k) {
        return k.name().substring(0, 4);
    }

    String getFieldText(Kind fk) {
        return switch (fk) {
            case NONE -> """
                    // no field declaration
                    """;

            case NO_COMMENT -> """
                    // no field comment
                    private BooleanProperty example;
                    """;

            case COMMENT -> """
                    /** Field description. More field description. */
                    private BooleanProperty example;
                    """;
        };
    }

    String getPropertyMethodText(Kind fk) {
        return switch (fk) {
            case NONE -> """
                    // no property method declaration
                    """;

            case NO_COMMENT -> """
                    // no property method comment
                    public BooleanProperty exampleProperty();
                    """;

            case COMMENT -> """
                    /**
                     * Property method description. More property method description.
                     * @return the {@code example} property
                     */
                    public BooleanProperty exampleProperty();
                    """;
        };
    }

    String getGetterMethodText(Kind fk) {
        return switch (fk) {
            case NONE -> """
                    // no getter method declaration
                    """;

            case NO_COMMENT -> """
                    // no getter method comment
                    public boolean getExample();
                    """;

            case COMMENT -> """
                    /**
                     * Getter method description. More getter method description.
                     * @return the {@code example} property
                     */
                    public boolean getExample();
                    """;
        };
    }

    String getSetterMethodText(Kind fk) {
        return switch (fk) {
            case NONE -> """
                    // no setter method declaration
                    """;

            case NO_COMMENT -> """
                    // no setter method comment
                    public void setExample(boolean b);
                    """;

            case COMMENT -> """
                    /**
                     * Setter method description. More setter method description.
                     * @param b the new value for the property
                     */
                    public void setExample(boolean b);
                    """;
        };
    }

}