JavadocTest.java revision 3982:162b521af7bb
1254721Semaste/*
2254721Semaste * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3254721Semaste * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4254721Semaste *
5254721Semaste * This code is free software; you can redistribute it and/or modify it
6254721Semaste * under the terms of the GNU General Public License version 2 only, as
7254721Semaste * published by the Free Software Foundation.
8254721Semaste *
9254721Semaste * This code is distributed in the hope that it will be useful, but WITHOUT
10254721Semaste * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11254721Semaste * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12254721Semaste * version 2 for more details (a copy is included in the LICENSE file that
13254721Semaste * accompanied this code).
14254721Semaste *
15254721Semaste * You should have received a copy of the GNU General Public License version
16254721Semaste * 2 along with this work; if not, write to the Free Software Foundation,
17288943Sdim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18254721Semaste *
19254721Semaste * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20296417Sdim * or visit www.oracle.com if you need additional information or have any
21254721Semaste * questions.
22254721Semaste */
23254721Semaste
24254721Semaste/*
25314564Sdim * @test
26314564Sdim * @bug 8131019 8169561 8174245
27314564Sdim * @summary Test Javadoc
28314564Sdim * @library /tools/lib
29314564Sdim * @modules jdk.compiler/com.sun.tools.javac.api
30314564Sdim *          jdk.compiler/com.sun.tools.javac.main
31314564Sdim *          jdk.jshell/jdk.jshell:open
32314564Sdim * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask
33314564Sdim * @build KullaTesting TestingInputStream Compiler
34314564Sdim * @run testng JavadocTest
35314564Sdim */
36314564Sdim
37314564Sdimimport java.io.IOException;
38280031Sdimimport java.lang.reflect.Field;
39314564Sdimimport java.nio.file.Files;
40314564Sdimimport java.nio.file.Path;
41314564Sdimimport java.nio.file.Paths;
42314564Sdimimport java.util.Arrays;
43280031Sdimimport java.util.jar.JarEntry;
44314564Sdimimport java.util.jar.JarOutputStream;
45314564Sdim
46314564Sdimimport org.testng.annotations.Test;
47314564Sdim
48314564Sdim@Test
49314564Sdimpublic class JavadocTest extends KullaTesting {
50314564Sdim
51314564Sdim    private final Compiler compiler = new Compiler();
52314564Sdim
53314564Sdim    public void testJavadoc() {
54314564Sdim        prepareZip();
55314564Sdim        assertJavadoc("test.Clazz|", "test.Clazz\n" +
56314564Sdim                                     "Top level. ");
57314564Sdim        assertEval("test.Clazz clz = null;");
58314564Sdim        assertJavadoc("clz.test(|", "String test.Clazz.test(int p) throws IllegalStateException\n" +
59314564Sdim                                    " javadoc1A\n" +
60314564Sdim                                    "\n" +
61314564Sdim                                    " @param p param\n" +
62314564Sdim                                    " @throws IllegalStateException exc\n" +
63314564Sdim                                    " @return value\n");
64314564Sdim        //undefined handling:
65314564Sdim        assertJavadoc("clz.undef|");
66314564Sdim    }
67314564Sdim
68314564Sdim    public void testVariableInRepl() {
69314564Sdim        assertEval("Object o;");
70314564Sdim        assertSignature("o|", "o:java.lang.Object");
71314564Sdim    }
72314564Sdim
73314564Sdim    private void prepareZip() {
74314564Sdim        String clazz =
75314564Sdim                "package test;\n" +
76314564Sdim                "/**Top level." +
77314564Sdim                " */\n" +
78314564Sdim                "public class Clazz {\n" +
79314564Sdim                "    /**\n" +
80314564Sdim                "     * javadoc1A\n" +
81314564Sdim                "     *\n" +
82314564Sdim                "     * @param p param\n" +
83314564Sdim                "     * @throws IllegalStateException exc\n" +
84314564Sdim                "     * @return value\n" +
85314564Sdim                "     */\n" +
86314564Sdim                "    public String test(int p) throws IllegalStateException { return null;}\n" +
87314564Sdim                "}\n";
88314564Sdim
89314564Sdim        Path srcZip = Paths.get("src.zip");
90314564Sdim
91314564Sdim        try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(srcZip))) {
92314564Sdim            out.putNextEntry(new JarEntry("test/Clazz.java"));
93314564Sdim            out.write(clazz.getBytes());
94314564Sdim        } catch (IOException ex) {
95254721Semaste            throw new IllegalStateException(ex);
96314564Sdim        }
97254721Semaste
98314564Sdim        compiler.compile(clazz);
99254721Semaste
100314564Sdim        try {
101314564Sdim            Field availableSources = getAnalysis().getClass().getDeclaredField("availableSources");
102314564Sdim            availableSources.setAccessible(true);
103314564Sdim            availableSources.set(getAnalysis(), Arrays.asList(srcZip));
104314564Sdim        } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException ex) {
105314564Sdim            throw new IllegalStateException(ex);
106314564Sdim        }
107314564Sdim        addToClasspath(compiler.getClassDir());
108314564Sdim    }
109254721Semaste
110314564Sdim    public void testCollectionsMin() {
111314564Sdim        prepareJavaUtilZip();
112314564Sdim        assertJavadoc("java.util.Collections.min(|",
113314564Sdim                      "T java.util.Collections.<T>min(java.util.Collection<? extends T> coll, java.util.Comparator<? super T> comp)\n" +
114314564Sdim                       " min comparator\n",
115314564Sdim                       "T java.util.Collections.<T extends Object & Comparable<? super T>>min(java.util.Collection<? extends T> coll)\n" +
116314564Sdim                       " min comparable\n");
117314564Sdim    }
118314564Sdim
119314564Sdim    private void prepareJavaUtilZip() {
120314564Sdim        String clazz =
121314564Sdim                "package java.util;\n" +
122314564Sdim                "/**Top level." +
123254721Semaste                " */\n" +
124314564Sdim                "public class Collections {\n" +
125314564Sdim                "    /**\n" +
126314564Sdim                "     * min comparable\n" +
127314564Sdim                "     */\n" +
128314564Sdim                "    public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll) {" +
129314564Sdim                "        return null;\n" +
130314564Sdim                "    }\n" +
131254721Semaste                "    /**\n" +
132314564Sdim                "     * min comparator\n" +
133254721Semaste                "     */\n" +
134314564Sdim                "        public static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp) {\n" +
135314564Sdim                "        return null;\n" +
136314564Sdim                "    }\n" +
137314564Sdim                "}\n";
138314564Sdim
139314564Sdim        Path srcZip = Paths.get("src.zip");
140314564Sdim
141314564Sdim        try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(srcZip))) {
142314564Sdim            out.putNextEntry(new JarEntry("java/util/Collections.java"));
143314564Sdim            out.write(clazz.getBytes());
144314564Sdim        } catch (IOException ex) {
145314564Sdim            throw new IllegalStateException(ex);
146314564Sdim        }
147314564Sdim
148314564Sdim        try {
149314564Sdim            Field availableSources = getAnalysis().getClass().getDeclaredField("availableSources");
150314564Sdim            availableSources.setAccessible(true);
151254721Semaste            availableSources.set(getAnalysis(), Arrays.asList(srcZip));
152314564Sdim        } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException ex) {
153314564Sdim            throw new IllegalStateException(ex);
154314564Sdim        }
155314564Sdim    }
156314564Sdim
157314564Sdim}
158314564Sdim