File: ModuleTestBase.java

package info (click to toggle)
openjdk-17 17.0.17~5ea-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 764,780 kB
  • sloc: java: 5,318,220; xml: 1,291,711; cpp: 1,202,338; ansic: 428,731; asm: 404,978; objc: 20,861; sh: 14,753; javascript: 10,743; python: 6,402; makefile: 2,405; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (487 lines) | stat: -rw-r--r-- 17,061 bytes parent folder | download | duplicates (11)
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*
 * Copyright (c) 2016, 2019, 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.
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;

import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ModuleElement;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.ElementFilter;
import javax.lang.model.util.SimpleElementVisitor14;

import jdk.javadoc.doclet.Doclet;
import jdk.javadoc.doclet.DocletEnvironment;
import jdk.javadoc.doclet.Reporter;

import toolbox.JavadocTask;
import toolbox.Task;
import toolbox.Task.Expect;
import toolbox.TestRunner;
import toolbox.ToolBox;

import static toolbox.Task.OutputKind.*;

/**
 * Base class for module tests.
 */
public class ModuleTestBase extends TestRunner {

    // Field Separator
    private static final String FS = " ";

    protected ToolBox tb;
    private final Class<?> docletClass;

    private Task.Result currentTask = null;

    ModuleTestBase() {
        super(System.err);
        tb = new ToolBox();
        ClassLoader cl = ModuleTestBase.class.getClassLoader();
        try {
            docletClass = cl.loadClass("ModuleTestBase$ModulesTesterDoclet");
        } catch (ClassNotFoundException cfe) {
            throw new Error(cfe);
        }
    }

    /**
     * Execute methods annotated with @Test, and throw an exception if any
     * errors are reported..
     *
     * @throws Exception if any errors occurred
     */
    protected void runTests() throws Exception {
        runTests(m -> new Object[] { Paths.get(m.getName()) });
    }

    Task.Result execTask(String... args) {
        return execTask0(false, args);
    }

    Task.Result execNegativeTask(String... args) {
        return execTask0(true, args);
    }

    private Task.Result execTask0(boolean isNegative, String... args) {
        JavadocTask et = new JavadocTask(tb, Task.Mode.API);
        et.docletClass(docletClass);
        //Arrays.asList(args).forEach((a -> System.err.println("arg: " + a)));
        System.err.println(Arrays.asList(args));
        currentTask = isNegative
                ? et.options(args).run(Expect.FAIL)
                : et.options(args).run();
        return currentTask;
    }

    Path[] findHtmlFiles(Path... paths) throws IOException {
        return tb.findFiles(".html", paths);
    }

    boolean grep(String regex, Path file) throws Exception {
        List<String> lines = tb.readAllLines(file);
        List<String> foundList = tb.grep(regex, lines);
        return !foundList.isEmpty();
    }

    String normalize(String in) {
        return in.replace('\\', '/');
    }

    void checkModulesSpecified(String... args) throws Exception {
        for (String arg : args) {
            checkDocletOutputPresent("Specified", ElementKind.MODULE, arg);
        }
    }

    void checkPackagesSpecified(String... args) throws Exception {
        for (String arg : args) {
            checkDocletOutputPresent("Specified", ElementKind.PACKAGE, arg);
        }
    }

    void checkTypesSpecified(String... args) throws Exception {
        for (String arg : args) {
            checkDocletOutputPresent("Specified", ElementKind.CLASS, arg);
        }
    }

    void checkModulesIncluded(String... args) throws Exception {
        for (String arg : args) {
            checkDocletOutputPresent("Included", ElementKind.MODULE, arg);
        }
    }

    void checkPackagesIncluded(String... args) throws Exception {
        for (String arg : args) {
            checkDocletOutputPresent("Included", ElementKind.PACKAGE, arg);
        }
    }

    void checkTypesIncluded(String... args) throws Exception {
        for (String arg : args) {
            checkDocletOutputPresent("Included", ElementKind.CLASS, arg);
        }
    }

    void checkTypesSelected(String... args) throws Exception {
        for (String arg : args) {
            checkDocletOutputPresent("Selected", ElementKind.CLASS, arg);
        }
    }

    void checkMembersSelected(String... args) throws Exception {
        for (String arg : args) {
            checkDocletOutputPresent("Selected", ElementKind.METHOD, arg);
        }
    }

    void checkModuleMode(String mode) throws Exception {
        assertPresent("^ModuleMode" + FS + mode);
    }

    void checkStringPresent(String regex) throws Exception {
        assertPresent(regex);
    }

    void checkDocletOutputPresent(String category, ElementKind kind, String regex) throws Exception {
        assertPresent("^" + category + " " + kind.toString() + " " + regex);
    }

    void assertPresent(String regex) throws Exception {
        assertPresent(regex, STDOUT);
    }

    void assertMessagePresent(String regex) throws Exception {
        assertPresent(regex, Task.OutputKind.DIRECT);
    }

    void assertMessageNotPresent(String regex) throws Exception {
        assertNotPresent(regex, Task.OutputKind.DIRECT);
    }

    void assertPresent(String regex, Task.OutputKind kind) throws Exception {
        List<String> foundList = tb.grep(regex, currentTask.getOutputLines(kind));
        if (foundList.isEmpty()) {
            dumpDocletDiagnostics();
            throw new Exception(regex + " not found in: " + kind);
        }
    }

    void assertNotPresent(String regex, Task.OutputKind kind) throws Exception {
        List<String> foundList = tb.grep(regex, currentTask.getOutputLines(kind));
        if (!foundList.isEmpty()) {
            dumpDocletDiagnostics();
            throw new Exception(regex + " found in: " + kind);
        }
    }

    void dumpDocletDiagnostics() {
        for (Task.OutputKind kind : Task.OutputKind.values()) {
            String output = currentTask.getOutput(kind);
            if (output != null && !output.isEmpty()) {
                System.err.println("<" + kind + ">");
                System.err.println(output);
            }
        }
    }

    void checkModulesNotSpecified(String... args) throws Exception {
        for (String arg : args) {
            checkDocletOutputAbsent("Specified", ElementKind.MODULE, arg);
        }
    }

    void checkPackagesNotSpecified(String... args) throws Exception {
        for (String arg : args) {
            checkDocletOutputAbsent("Specified", ElementKind.PACKAGE, arg);
        }
    }

    void checkTypesNotSpecified(String... args) throws Exception {
        for (String arg : args) {
            checkDocletOutputAbsent("Specified", ElementKind.CLASS, arg);
        }
    }

    void checkModulesNotIncluded(String... args) throws Exception {
        for (String arg : args) {
            checkDocletOutputAbsent("Included", ElementKind.MODULE, arg);
        }
    }

    void checkPackagesNotIncluded(String... args) throws Exception {
        for (String arg : args) {
            checkDocletOutputAbsent("Included", ElementKind.PACKAGE, arg);
        }
    }

    void checkTypesNotIncluded(String... args) throws Exception {
        for (String arg : args) {
            checkDocletOutputAbsent("Included", ElementKind.CLASS, arg);
        }
    }

    void checkMembersNotSelected(String... args) throws Exception {
        for (String arg : args) {
            checkDocletOutputAbsent("Selected", ElementKind.METHOD, arg);
        }
    }

    void checkStringAbsent(String regex) throws Exception {
        assertAbsent(regex);
    }

    void checkDocletOutputAbsent(String category, ElementKind kind, String regex) throws Exception {
        assertAbsent("^" + category + FS + kind.toString() + FS + regex);
    }

    void assertAbsent(String regex) throws Exception {
        assertAbsent(regex, STDOUT);
    }

    void assertAbsent(String regex, Task.OutputKind kind) throws Exception {
        List<String> foundList = tb.grep(regex, currentTask.getOutputLines(kind));
        if (!foundList.isEmpty()) {
            dumpDocletDiagnostics();
            throw new Exception(regex + " found in: " + kind);
        }
    }

    public static class ModulesTesterDoclet implements Doclet {
        StringWriter sw = new StringWriter();
        PrintWriter ps = new PrintWriter(sw);

        DocletEnvironment docEnv = null;

        boolean hasDocComments = false;

        String hasDocComments(Element e) {
            String comment = docEnv.getElementUtils().getDocComment(e);
            return comment != null && !comment.isEmpty()
                    ? "hasDocComments"
                    : "noDocComments";
        }

        // csv style output, for simple regex verification
        void printDataSet(String header, Set<? extends Element> set) {
            for (Element e : set) {
                ps.print(header);
                new SimpleElementVisitor14<Void, Void>() {
                    @Override
                    public Void visitModule(ModuleElement e, Void p) {
                        ps.print(FS);
                        ps.print(e.getKind());
                        ps.print(FS);
                        ps.print(e.getQualifiedName());
                        if (hasDocComments) {
                            ps.print(FS);
                            ps.print(hasDocComments(e));
                        }
                        ps.println();
                        return null;
                    }

                    @Override
                    public Void visitPackage(PackageElement e, Void p) {
                        ps.print(FS);
                        ps.print(e.getKind());
                        ps.print(FS);
                        ps.print(e.getQualifiedName());
                        if (hasDocComments) {
                            ps.print(FS);
                            ps.print(hasDocComments(e));
                        }
                        ps.println();
                        return null;
                    }

                    @Override
                    public Void visitType(TypeElement e, Void p) {
                        ps.print(FS);
                        ps.print(ElementKind.CLASS);
                        ps.print(FS);
                        ps.print(e.getQualifiedName());
                        if (hasDocComments) {
                            ps.print(FS);
                            ps.print(hasDocComments(e));
                        }
                        ps.println();
                        return null;
                    }

                    @Override
                    protected Void defaultAction(Element e, Void p) {
                        Element encl = e.getEnclosingElement();
                        CharSequence fqn = new SimpleElementVisitor14<CharSequence, Void>() {
                            @Override
                            public CharSequence visitModule(ModuleElement e, Void p) {
                                return e.getQualifiedName();
                            }

                            @Override
                            public CharSequence visitType(TypeElement e, Void p) {
                                return e.getQualifiedName();
                            }

                            @Override
                            public CharSequence visitPackage(PackageElement e, Void p) {
                                return e.getQualifiedName();
                            }

                        }.visit(encl);

                        ps.print(FS);
                        ps.print(ElementKind.METHOD); // always METHOD
                        ps.print(FS);
                        ps.print(fqn);
                        ps.print(".");
                        ps.print(e.getSimpleName());
                        if (hasDocComments) {
                            ps.print(FS);
                            ps.print(hasDocComments(e));
                        }
                        ps.println();
                        return null;
                    }
                }.visit(e);
            }
        }

        @Override
        public boolean run(DocletEnvironment docenv) {
            this.docEnv = docenv;
            ps.println("ModuleMode" + FS + docenv.getModuleMode());
            printDataSet("Specified", docenv.getSpecifiedElements());
            printDataSet("Included", docenv.getIncludedElements());
            printDataSet("Selected", getAllSelectedElements(docenv));
            System.out.println(sw);
            return true;
        }

        Set<Element> getAllSelectedElements(DocletEnvironment docenv) {
            Set<Element> result = new TreeSet<Element>((Element e1, Element e2) -> {
                // some grouping by kind preferred
                int rc = e1.getKind().compareTo(e2.getKind());
                if (rc != 0) return rc;
                rc = e1.toString().compareTo(e2.toString());
                if (rc != 0) return rc;
                return Integer.compare(e1.hashCode(), e2.hashCode());
            });
            Set<? extends Element> elements = docenv.getIncludedElements();
            for (ModuleElement me : ElementFilter.modulesIn(elements)) {
                addEnclosedElements(docenv, result, me);
            }
            for (PackageElement pe : ElementFilter.packagesIn(elements)) {
                ModuleElement mdle = docenv.getElementUtils().getModuleOf(pe);
                if (mdle != null)
                    addEnclosedElements(docenv, result, mdle);
                addEnclosedElements(docenv, result, pe);
            }
            for (TypeElement te : ElementFilter.typesIn(elements)) {
                addEnclosedElements(docenv, result, te);
            }
            return result;
        }

        void addEnclosedElements(DocletEnvironment docenv, Set<Element> result, Element e) {
            List<Element> elems = e.getEnclosedElements().stream()
                    .filter(el -> docenv.isIncluded(el))
                    .collect(Collectors.toList());
            result.addAll(elems);
            for (TypeElement t : ElementFilter.typesIn(elems)) {
                addEnclosedElements(docenv, result, t);
            }
        }

        @Override
        public Set<Doclet.Option> getSupportedOptions() {
            Option[] options = {
                new Option() {
                    private final List<String> someOption = Arrays.asList(
                            "-hasDocComments"
                    );

                    @Override
                    public int getArgumentCount() {
                        return 0;
                    }

                    @Override
                    public String getDescription() {
                        return "print disposition of doc comments on an element";
                    }

                    @Override
                    public Option.Kind getKind() {
                        return Option.Kind.STANDARD;
                    }

                    @Override
                    public List<String> getNames() {
                        return someOption;
                    }

                    @Override
                    public String getParameters() {
                        return "flag";
                    }

                    @Override
                    public boolean process(String opt, List<String> arguments) {
                        hasDocComments = true;
                        return true;
                    }
                }
            };
            return new HashSet<>(Arrays.asList(options));
        }

        @Override
        public void init(Locale locale, Reporter reporter) {}

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

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