JavadocTest.java revision 3792:d516975e8110
1/*
2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 8131019
27 * @summary Test Javadoc
28 * @library /tools/lib
29 * @modules jdk.compiler/com.sun.tools.javac.api
30 *          jdk.compiler/com.sun.tools.javac.main
31 *          jdk.jshell/jdk.jshell:open
32 * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask
33 * @build KullaTesting TestingInputStream Compiler
34 * @run testng JavadocTest
35 */
36
37import java.io.IOException;
38import java.lang.reflect.Field;
39import java.nio.file.Files;
40import java.nio.file.Path;
41import java.nio.file.Paths;
42import java.util.Arrays;
43import java.util.jar.JarEntry;
44import java.util.jar.JarOutputStream;
45
46import org.testng.annotations.Test;
47
48@Test
49public class JavadocTest extends KullaTesting {
50
51    private final Compiler compiler = new Compiler();
52
53    public void testJavadoc() {
54        prepareZip();
55        assertJavadoc("test.Clazz|", "test.Clazz\n" +
56                                     "Top level. ");
57        assertEval("test.Clazz clz = null;");
58        assertJavadoc("clz.test(|", "String test.Clazz.test(int p) throws IllegalStateException\n" +
59                                    " javadoc1A\n" +
60                                    "\n" +
61                                    " @param p param\n" +
62                                    " @throws IllegalStateException exc\n" +
63                                    " @return value\n");
64        //undefined handling:
65        assertJavadoc("clz.undef|");
66    }
67
68    private void prepareZip() {
69        String clazz =
70                "package test;\n" +
71                "/**Top level." +
72                " */\n" +
73                "public class Clazz {\n" +
74                "    /**\n" +
75                "     * javadoc1A\n" +
76                "     *\n" +
77                "     * @param p param\n" +
78                "     * @throws IllegalStateException exc\n" +
79                "     * @return value\n" +
80                "     */\n" +
81                "    public String test(int p) throws IllegalStateException { return null;}\n" +
82                "}\n";
83
84        Path srcZip = Paths.get("src.zip");
85
86        try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(srcZip))) {
87            out.putNextEntry(new JarEntry("test/Clazz.java"));
88            out.write(clazz.getBytes());
89        } catch (IOException ex) {
90            throw new IllegalStateException(ex);
91        }
92
93        compiler.compile(clazz);
94
95        try {
96            Field availableSources = getAnalysis().getClass().getDeclaredField("availableSources");
97            availableSources.setAccessible(true);
98            availableSources.set(getAnalysis(), Arrays.asList(srcZip));
99        } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException ex) {
100            throw new IllegalStateException(ex);
101        }
102        addToClasspath(compiler.getClassDir());
103    }
104
105}
106