File: EnsureNewOldDoclet.java

package info (click to toggle)
libnb-javaparser-java 9%2B2018-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 65,172 kB
  • sloc: java: 440,096; xml: 6,359; sh: 865; makefile: 314
file content (367 lines) | stat: -rw-r--r-- 12,720 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
/*
 * Copyright (c) 2015, 2017, 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 8035473 8154482 8154399 8159096 8176131 8176331
 * @summary make sure the javadoc tool responds correctly to Xold,
 *          old doclets and taglets.
 * @library /tools/lib
 * @build toolbox.ToolBox toolbox.TestRunner
 * @run main EnsureNewOldDoclet
 */

import java.io.*;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.lang.model.element.Element;

import com.sun.javadoc.Tag;
import com.sun.source.doctree.DocTree;

import toolbox.*;


/**
 * This test ensures the doclet responds correctly when given
 * various conditions that force a fall back to the old javadoc
 * tool. The following condition in the order described will
 * force a dispatch to the old tool, -Xold, old doclet and old taglet.
 *
 */
public class EnsureNewOldDoclet extends TestRunner {

    final ToolBox tb;
    final File testSrc;
    final Path javadocPath;
    final ExecTask task;
    final String testClasses;
    final PrintStream ostream;

    final static String CLASS_NAME = "EnsureNewOldDoclet";
    final static String OLD_DOCLET_CLASS_NAME = CLASS_NAME + "$OldDoclet";
    final static String NEW_DOCLET_CLASS_NAME = CLASS_NAME + "$NewDoclet"; //unused
    final static String OLD_TAGLET_CLASS_NAME = CLASS_NAME + "$OldTaglet";
    final static String NEW_TAGLET_CLASS_NAME = CLASS_NAME + "$NewTaglet";

    final static Pattern OLD_HEADER = Pattern.compile("^Standard Doclet \\(Old\\) version.*");
    final static Pattern NEW_HEADER = Pattern.compile("^Standard Doclet version.*");


    final static String OLD_DOCLET_MARKER = "OLD_DOCLET_MARKER";
    final static String OLD_TAGLET_MARKER = "Registered: OldTaglet";

    final static String NEW_DOCLET_MARKER = "NEW_DOCLET_MARKER";
    final static String NEW_TAGLET_MARKER = "Registered Taglet " + CLASS_NAME + "\\$NewTaglet";

    final static Pattern WARN_TEXT = Pattern.compile("Users are strongly recommended to migrate" +
                                                    " to the new APIs.");
    final static String OLD_DOCLET_ERROR = "java.lang.NoSuchMethodException: " +
            CLASS_NAME +"\\$NewTaglet";
    final static Pattern NEW_DOCLET_ERROR = Pattern.compile(".*java.lang.ClassCastException.*Taglet " +
            CLASS_NAME + "\\$OldTaglet.*");

    final static String OLD_STDDOCLET = "com.sun.tools.doclets.standard.Standard";
    final static String NEW_STDDOCLET = "jdk.javadoc.doclet.StandardDoclet";


    public EnsureNewOldDoclet() throws Exception {
        super(System.err);
        ostream = System.err;
        testClasses = System.getProperty("test.classes");
        tb = new ToolBox();
        javadocPath = tb.getJDKTool("javadoc");
        task = new ExecTask(tb, javadocPath);
        testSrc = new File("Foo.java");
        generateSample(testSrc);
    }

    void generateSample(File testSrc) throws Exception {
        String nl = System.getProperty("line.separator");
        String src = Arrays.asList(
            "/**",
            " * A test class to test javadoc. Nothing more nothing less.",
            " */",
            " public class Foo{}").stream().collect(Collectors.joining(nl));
        tb.writeFile(testSrc.getPath(), src);
    }

    public static void main(String... args) throws Exception {
        new EnsureNewOldDoclet().runTests();
    }

    // input: nothing, default mode
    // outcome: new tool and new doclet
    @Test
    public void testDefault() throws Exception {
        setArgs("-classpath", ".", // insulates us from ambient classpath
                  testSrc.toString());
        Task.Result tr = task.run(Task.Expect.SUCCESS);
        List<String> out = tr.getOutputLines(Task.OutputKind.STDOUT);
        checkOutput(testName, out, NEW_HEADER);
    }

    // input: -Xold
    // outcome: old tool
    @Test
    public void testXold() throws Exception {
        setArgs("-Xold",
                "-classpath", ".", // ambient classpath insulation
                testSrc.toString());
        Task.Result tr = task.run(Task.Expect.SUCCESS);
        List<String> out = tr.getOutputLines(Task.OutputKind.STDOUT);
        List<String> err = tr.getOutputLines(Task.OutputKind.STDERR);
        checkOutput(testName, out, OLD_HEADER);
        checkOutput(testName, err, WARN_TEXT);
    }

    // input: old doclet
    // outcome: old tool
    @Test
    public void testOldDoclet() throws Exception {
        setArgs("-classpath", ".", // ambient classpath insulation
                "-doclet",
                OLD_DOCLET_CLASS_NAME,
                "-docletpath",
                testClasses,
                testSrc.toString());
        Task.Result tr = task.run(Task.Expect.SUCCESS);
        List<String> out = tr.getOutputLines(Task.OutputKind.STDOUT);
        List<String> err = tr.getOutputLines(Task.OutputKind.STDERR);
        checkOutput(testName, out, OLD_DOCLET_MARKER);
        checkOutput(testName, err, WARN_TEXT);
    }

    // input: old taglet
    // outcome: old tool
    @Test
    public void testOldTaglet() throws Exception {
        setArgs("-classpath", ".", // ambient classpath insulation
            "-taglet",
            OLD_TAGLET_CLASS_NAME,
            "-tagletpath",
            testClasses,
            testSrc.toString());
        Task.Result tr = task.run(Task.Expect.SUCCESS);
        List<String> out = tr.getOutputLines(Task.OutputKind.STDOUT);
        List<String> err = tr.getOutputLines(Task.OutputKind.STDERR);
        checkOutput(testName, out, OLD_TAGLET_MARKER);
        checkOutput(testName, err, WARN_TEXT);
    }

    // input: new doclet and old taglet
    // outcome: new doclet with failure
    @Test
    public void testNewDocletOldTaglet() throws Exception {
        setArgs("-classpath", ".", // ambient classpath insulation
                "-doclet",
                NEW_STDDOCLET,
                "-taglet",
                OLD_TAGLET_CLASS_NAME,
                "-tagletpath",
                testClasses,
                testSrc.toString());
        Task.Result tr = task.run(Task.Expect.FAIL, 1).writeAll();
        //Task.Result tr = task.run();
        List<String> out = tr.getOutputLines(Task.OutputKind.STDOUT);
        List<String> err = tr.getOutputLines(Task.OutputKind.STDERR);
        checkOutput(testName, out, NEW_HEADER);
        checkOutput(testName, err, NEW_DOCLET_ERROR);
    }

    // input: old doclet and old taglet
    // outcome: old doclet and old taglet should register
    @Test
    public void testOldDocletOldTaglet() throws Exception {
        setArgs("-classpath", ".", // ambient classpath insulation
                "-doclet",
                OLD_STDDOCLET,
                "-taglet",
                OLD_TAGLET_CLASS_NAME,
                "-tagletpath",
                testClasses,
                testSrc.toString());
        Task.Result tr = task.run(Task.Expect.SUCCESS);
        List<String> out = tr.getOutputLines(Task.OutputKind.STDOUT);
        List<String> err = tr.getOutputLines(Task.OutputKind.STDERR);
        checkOutput(testName, out, OLD_HEADER);
        checkOutput(testName, out, OLD_TAGLET_MARKER);
        checkOutput(testName, err, WARN_TEXT);
    }

    // input: new doclet and new taglet
    // outcome: new doclet and new taglet should register
    @Test
    public void testNewDocletNewTaglet() throws Exception {
        setArgs("-classpath", ".", // ambient classpath insulation
                "-doclet",
                NEW_STDDOCLET,
                "-taglet",
                NEW_TAGLET_CLASS_NAME,
                "-tagletpath",
                testClasses,
                testSrc.toString());
        Task.Result tr = task.run(Task.Expect.SUCCESS);
        List<String> out = tr.getOutputLines(Task.OutputKind.STDOUT);
        List<String> err = tr.getOutputLines(Task.OutputKind.STDERR);
        checkOutput(testName, out, NEW_HEADER);
        checkOutput(testName, out, NEW_TAGLET_MARKER);
    }

    // input: old doclet and new taglet
    // outcome: old doclet and error
    @Test
    public void testOldDocletNewTaglet() throws Exception {
        setArgs("-classpath", ".", // ambient classpath insulation
                "-doclet",
                OLD_STDDOCLET,
                "-taglet",
                NEW_TAGLET_CLASS_NAME,
                "-tagletpath",
                testClasses,
                testSrc.toString());
        Task.Result tr = task.run(Task.Expect.FAIL, 1);
        List<String> out = tr.getOutputLines(Task.OutputKind.STDOUT);
        List<String> err = tr.getOutputLines(Task.OutputKind.STDERR);
        checkOutput(testName, out, OLD_HEADER);
        checkOutput(testName, err, WARN_TEXT);
        checkOutput(testName, err, OLD_DOCLET_ERROR);
    }

    void setArgs(String... args) {
        ostream.println("cmds: " + Arrays.asList(args));
        task.args(args);
    }

    void checkOutput(String testCase, List<String> content, String toFind) throws Exception {
        checkOutput(testCase, content, Pattern.compile(".*" + toFind + ".*"));
    }

    void checkOutput(String testCase, List<String> content, Pattern toFind) throws Exception {
        ostream.println("---" + testCase + "---");
        content.stream().forEach(x -> System.out.println(x));
        for (String x : content) {
            ostream.println(x);
            if (toFind.matcher(x).matches()) {
                return;
            }
        }
        throw new Exception(testCase + ": Expected string not found: " +  toFind);
    }

    public static class OldDoclet extends com.sun.javadoc.Doclet {
        public static boolean start(com.sun.javadoc.RootDoc root) {
            System.out.println(OLD_DOCLET_MARKER);
            return true;
        }
    }

    public static class OldTaglet implements com.sun.tools.doclets.Taglet {

        public static void register(Map map) {
            EnsureNewOldDoclet.OldTaglet tag = new OldTaglet();
            com.sun.tools.doclets.Taglet t = (com.sun.tools.doclets.Taglet) map.get(tag.getName());
            System.out.println(OLD_TAGLET_MARKER);
        }

        @Override
        public boolean inField() {
            return true;
        }

        @Override
        public boolean inConstructor() {
            return true;
        }

        @Override
        public boolean inMethod() {
            return true;
        }

        @Override
        public boolean inOverview() {
            return true;
        }

        @Override
        public boolean inPackage() {
            return true;
        }

        @Override
        public boolean inType() {
            return true;
        }

        @Override
        public boolean isInlineTag() {
            return true;
        }

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

        @Override
        public String toString(Tag tag) {
            return getName();
        }

        @Override
        public String toString(Tag[] tags) {
            return getName();
        }
    }

    public static class NewTaglet implements jdk.javadoc.doclet.Taglet {

        @Override
        public Set<Location> getAllowedLocations() {
            return Collections.emptySet();
        }

        @Override
        public boolean isInlineTag() {
            return true;
        }

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

        @Override
        public String toString(List<? extends DocTree> tags, Element element) {
            return tags.toString();
        }

    }
}