Path: blob/master/test/langtools/jdk/internal/shellsupport/doc/JavadocHelperTest.java
41153 views
/*1* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8131019 8189778 819055226* @summary Test JavadocHelper27* @library /tools/lib28* @modules jdk.compiler/com.sun.tools.javac.api29* jdk.compiler/com.sun.tools.javac.main30* jdk.compiler/jdk.internal.shellsupport.doc31* @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask32* @run testng/timeout=900/othervm JavadocHelperTest33*/3435import java.io.IOException;36import java.net.URI;37import java.net.URISyntaxException;38import java.nio.file.DirectoryStream;39import java.nio.file.FileSystem;40import java.nio.file.FileSystems;41import java.nio.file.Files;42import java.nio.file.Path;43import java.nio.file.Paths;44import java.util.ArrayList;45import java.util.Arrays;46import java.util.Collections;47import java.util.List;48import java.util.function.Function;49import java.util.jar.JarEntry;50import java.util.jar.JarOutputStream;5152import javax.lang.model.element.Element;53import javax.lang.model.element.ModuleElement;54import javax.lang.model.element.ModuleElement.ExportsDirective;55import javax.lang.model.element.TypeElement;56import javax.lang.model.util.ElementFilter;57import javax.tools.Diagnostic.Kind;58import javax.tools.DiagnosticListener;59import javax.tools.JavaCompiler;60import javax.tools.JavaFileObject;61import javax.tools.SimpleJavaFileObject;62import javax.tools.StandardJavaFileManager;63import javax.tools.StandardLocation;64import javax.tools.ToolProvider;6566import com.sun.source.util.JavacTask;67import jdk.internal.shellsupport.doc.JavadocHelper;68import org.testng.annotations.Test;69import static org.testng.Assert.assertEquals;70import static org.testng.Assert.assertTrue;7172@Test73public class JavadocHelperTest {7475public void testJavadoc() throws Exception {76doTestJavadoc("",77t -> t.getElements().getTypeElement("test.Super"),78"Top level. ");79doTestJavadoc("",80t -> getFirstMethod(t, "test.Super"),81" javadoc1\n" +82"\n" +83" @param p1 param1\n" +84" @param p2 param2\n" +85" @param p3 param3\n" +86" @throws IllegalStateException exc1\n" +87" @throws IllegalArgumentException exc2\n" +88" @throws IllegalAccessException exc3\n" +89" @return value\n");90}9192private Element getFirstMethod(JavacTask task, String typeName) {93return ElementFilter.methodsIn(task.getElements().getTypeElement(typeName).getEnclosedElements()).get(0);94}9596private Function<JavacTask, Element> getSubTest = t -> getFirstMethod(t, "test.Sub");9798public void testInheritNoJavadoc() throws Exception {99doTestJavadoc("",100getSubTest,101" javadoc1\n" +102"\n" +103" @param p1 param1\n" +104" @param p2 param2\n" +105" @param p3 param3\n" +106" @throws IllegalStateException exc1\n" +107" @throws IllegalArgumentException exc2\n" +108" @throws IllegalAccessException exc3\n" +109" @return value\n");110}111112public void testInheritFull() throws Exception {113doTestJavadoc(" /**\n" +114" * Prefix {@inheritDoc} suffix.\n" +115" *\n" +116" * @param p1 prefix {@inheritDoc} suffix\n" +117" * @param p2 prefix {@inheritDoc} suffix\n" +118" * @param p3 prefix {@inheritDoc} suffix\n" +119" * @throws IllegalStateException prefix {@inheritDoc} suffix\n" +120" * @throws IllegalArgumentException prefix {@inheritDoc} suffix\n" +121" * @throws IllegalAccessException prefix {@inheritDoc} suffix\n" +122" * @return prefix {@inheritDoc} suffix\n" +123" */\n",124getSubTest,125" Prefix javadoc1 suffix.\n" +126"\n" +127" @param p1 prefix param1 suffix\n" +128" @param p2 prefix param2 suffix\n" +129" @param p3 prefix param3 suffix\n" +130" @throws IllegalStateException prefix exc1 suffix\n" +131" @throws IllegalArgumentException prefix exc2 suffix\n" +132" @throws IllegalAccessException prefix exc3 suffix\n" +133" @return prefix value suffix\n");134}135136public void testInheritMissingParam() throws Exception {137doTestJavadoc(" /**\n" +138" * Prefix {@inheritDoc} suffix.\n" +139" *\n" +140" * @param p1 prefix {@inheritDoc} suffix\n" +141" * @param p3 prefix {@inheritDoc} suffix\n" +142" * @throws IllegalStateException prefix {@inheritDoc} suffix\n" +143" * @throws IllegalArgumentException prefix {@inheritDoc} suffix\n" +144" * @throws IllegalAccessException prefix {@inheritDoc} suffix\n" +145" * @return prefix {@inheritDoc} suffix\n" +146" */\n",147getSubTest,148" Prefix javadoc1 suffix.\n" +149"\n" +150" @param p1 prefix param1 suffix\n" +151"@param p2 param2\n" +152" @param p3 prefix param3 suffix\n" +153" @throws IllegalStateException prefix exc1 suffix\n" +154" @throws IllegalArgumentException prefix exc2 suffix\n" +155" @throws IllegalAccessException prefix exc3 suffix\n" +156" @return prefix value suffix\n");157}158159public void testInheritMissingFirstParam() throws Exception {160doTestJavadoc(" /**\n" +161" * Prefix {@inheritDoc} suffix.\n" +162" *\n" +163" * @param p2 prefix {@inheritDoc} suffix\n" +164" * @param p3 prefix {@inheritDoc} suffix\n" +165" * @throws IllegalStateException prefix {@inheritDoc} suffix\n" +166" * @throws IllegalArgumentException prefix {@inheritDoc} suffix\n" +167" * @throws IllegalAccessException prefix {@inheritDoc} suffix\n" +168" * @return prefix {@inheritDoc} suffix\n" +169" */\n",170getSubTest,171" Prefix javadoc1 suffix.\n" +172"\n" +173"@param p1 param1\n" +174" @param p2 prefix param2 suffix\n" +175" @param p3 prefix param3 suffix\n" +176" @throws IllegalStateException prefix exc1 suffix\n" +177" @throws IllegalArgumentException prefix exc2 suffix\n" +178" @throws IllegalAccessException prefix exc3 suffix\n" +179" @return prefix value suffix\n");180}181182public void testInheritMissingThrows() throws Exception {183doTestJavadoc(" /**\n" +184" * Prefix {@inheritDoc} suffix.\n" +185" *\n" +186" * @param p1 prefix {@inheritDoc} suffix\n" +187" * @param p2 prefix {@inheritDoc} suffix\n" +188" * @param p3 prefix {@inheritDoc} suffix\n" +189" * @throws IllegalStateException prefix {@inheritDoc} suffix\n" +190" * @throws IllegalAccessException prefix {@inheritDoc} suffix\n" +191" * @return prefix {@inheritDoc} suffix\n" +192" */\n",193getSubTest,194" Prefix javadoc1 suffix.\n" +195"\n" +196" @param p1 prefix param1 suffix\n" +197" @param p2 prefix param2 suffix\n" +198" @param p3 prefix param3 suffix\n" +199" @throws IllegalStateException prefix exc1 suffix\n" +200"@throws java.lang.IllegalArgumentException exc2\n" +201" @throws IllegalAccessException prefix exc3 suffix\n" +202" @return prefix value suffix\n");203}204205public void testInheritMissingReturn() throws Exception {206doTestJavadoc(" /**\n" +207" * Prefix {@inheritDoc} suffix.\n" +208" *\n" +209" * @param p1 prefix {@inheritDoc} suffix\n" +210" * @param p2 prefix {@inheritDoc} suffix\n" +211" * @param p3 prefix {@inheritDoc} suffix\n" +212" * @throws IllegalStateException prefix {@inheritDoc} suffix\n" +213" * @throws IllegalArgumentException prefix {@inheritDoc} suffix\n" +214" * @throws IllegalAccessException prefix {@inheritDoc} suffix\n" +215" */\n",216getSubTest,217" Prefix javadoc1 suffix.\n" +218"\n" +219" @param p1 prefix param1 suffix\n" +220" @param p2 prefix param2 suffix\n" +221" @param p3 prefix param3 suffix\n" +222" @throws IllegalStateException prefix exc1 suffix\n" +223" @throws IllegalArgumentException prefix exc2 suffix\n" +224" @throws IllegalAccessException prefix exc3 suffix\n" +225"@return value\n");226}227228public void testInheritAllButOne() throws Exception {229doTestJavadoc(" /**\n" +230" * @throws IllegalArgumentException {@inheritDoc}\n" +231" */\n",232getSubTest,233"javadoc1\n" +234"@param p1 param1\n" +235"@param p2 param2\n" +236"@param p3 param3\n" +237"@throws java.lang.IllegalStateException exc1\n" +238" @throws IllegalArgumentException exc2\n" +239"@throws java.lang.IllegalAccessException exc3\n" +240"@return value\n");241}242243public void testInheritEmpty() throws Exception {244doTestJavadoc(" /**\n" +245" */\n",246" /**@param p1\n" +247" * @param p2\n" +248" * @param p3\n" +249" * @throws IllegalStateException\n" +250" * @throws IllegalArgumentException\n" +251" * @throws IllegalAccessException\n" +252" * @return\n" +253" */\n",254getSubTest,255"\n" +256"@param p1 \n" +257"@param p2 \n" +258"@param p3 \n" +259"@throws java.lang.IllegalStateException \n" +260"@throws java.lang.IllegalArgumentException \n" +261"@throws java.lang.IllegalAccessException \n" +262"@return \n");263}264265public void testEmptyValue() throws Exception {266doTestJavadoc(" /**\n" +267" */\n",268" /**@param p1 {@value}\n" +269" * @param p2\n" +270" * @param p3\n" +271" * @throws IllegalStateException\n" +272" * @throws IllegalArgumentException\n" +273" * @throws IllegalAccessException\n" +274" * @return\n" +275" */\n",276getSubTest,277"\n" +278"@param p1 {@value}\n" +279"@param p2 \n" +280"@param p3 \n" +281"@throws java.lang.IllegalStateException \n" +282"@throws java.lang.IllegalArgumentException \n" +283"@throws java.lang.IllegalAccessException \n" +284"@return \n");285}286287public void testShortComment() throws Exception {288doTestJavadoc(" /**Test.*/\n",289getSubTest,290"Test." +291"@param p1 param1\n" +292"@param p2 param2\n" +293"@param p3 param3\n" +294"@throws java.lang.IllegalStateException exc1\n" +295"@throws java.lang.IllegalArgumentException exc2\n" +296"@throws java.lang.IllegalAccessException exc3\n" +297"@return value\n");298}299300private void doTestJavadoc(String origJavadoc, Function<JavacTask, Element> getElement, String expectedJavadoc) throws Exception {301doTestJavadoc(origJavadoc,302" /**\n" +303" * javadoc1\n" +304" *\n" +305" * @param p1 param1\n" +306" * @param p2 param2\n" +307" * @param p3 param3\n" +308" * @throws IllegalStateException exc1\n" +309" * @throws IllegalArgumentException exc2\n" +310" * @throws IllegalAccessException exc3\n" +311" * @return value\n" +312" */\n",313getElement, expectedJavadoc);314}315316private void doTestJavadoc(String origJavadoc,317String superJavadoc,318Function<JavacTask, Element> getElement,319String expectedJavadoc) throws Exception {320JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();321String subClass =322"package test;\n" +323"public class Sub extends Super {\n" +324origJavadoc +325" public String test(int p1, int p2, int p3) throws IllegalStateException, IllegalArgumentException, IllegalAccessException { return null;} \n" +326"}\n";327String superClass =328"package test;\n" +329"/**Top level." +330" */\n" +331"public class Super {\n" +332superJavadoc +333" public String test(int p1, int p2, int p3) throws IllegalStateException, IllegalArgumentException, IllegalAccessException { return null;} \n" +334"}\n";335336Path srcZip = Paths.get("src.zip");337338try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(srcZip))) {339out.putNextEntry(new JarEntry("test/Sub.java"));340out.write(subClass.getBytes());341out.putNextEntry(new JarEntry("test/Super.java"));342out.write(superClass.getBytes());343} catch (IOException ex) {344throw new IllegalStateException(ex);345}346347DiagnosticListener<? super JavaFileObject> noErrors = d -> {348if (d.getKind() == Kind.ERROR) {349throw new AssertionError(d.getMessage(null));350}351};352353assertTrue(compiler.getTask(null, null, noErrors, Arrays.asList("-d", "."), null, Arrays.asList(new JFOImpl("Super", superClass), new JFOImpl("Sub", subClass))).call());354355try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {356fm.setLocationFromPaths(StandardLocation.CLASS_PATH, Arrays.asList(Paths.get(".").toAbsolutePath()));357JavacTask task = (JavacTask) compiler.getTask(null, fm, noErrors, null, null, null);358359Element el = getElement.apply(task);360361try (JavadocHelper helper = JavadocHelper.create(task, Arrays.asList(srcZip))) {362String javadoc = helper.getResolvedDocComment(el);363364assertEquals(javadoc, expectedJavadoc);365}366}367}368369private static final class JFOImpl extends SimpleJavaFileObject {370371private final String code;372373public JFOImpl(String name, String code) throws URISyntaxException {374super(new URI("mem:///" + name + ".java"), Kind.SOURCE);375this.code = code;376}377378@Override379public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {380return code;381}382383}384385public void testAllDocs() throws IOException {386JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();387DiagnosticListener<? super JavaFileObject> noErrors = d -> {388if (d.getKind() == Kind.ERROR) {389throw new AssertionError(d.getMessage(null));390}391};392393List<Path> sources = new ArrayList<>();394Path home = Paths.get(System.getProperty("java.home"));395Path srcZip = home.resolve("lib").resolve("src.zip");396if (Files.isReadable(srcZip)) {397URI uri = URI.create("jar:" + srcZip.toUri());398try (FileSystem zipFO = FileSystems.newFileSystem(uri, Collections.emptyMap())) {399Path root = zipFO.getRootDirectories().iterator().next();400401//modular format:402try (DirectoryStream<Path> ds = Files.newDirectoryStream(root)) {403for (Path p : ds) {404if (Files.isDirectory(p)) {405sources.add(p);406}407}408}409try (StandardJavaFileManager fm =410compiler.getStandardFileManager(null, null, null)) {411JavacTask task =412(JavacTask) compiler.getTask(null, fm, noErrors, null, null, null);413task.getElements().getTypeElement("java.lang.Object");414for (ModuleElement me : task.getElements().getAllModuleElements()) {415List<ExportsDirective> exports =416ElementFilter.exportsIn(me.getDirectives());417for (ExportsDirective ed : exports) {418try (JavadocHelper helper = JavadocHelper.create(task, sources)) {419List<? extends Element> content =420ed.getPackage().getEnclosedElements();421for (TypeElement clazz : ElementFilter.typesIn(content)) {422for (Element el : clazz.getEnclosedElements()) {423helper.getResolvedDocComment(el);424}425}426}427}428}429}430}431}432}433}434435436