langtools/test/jdk/internal/shellsupport/doc/JavadocHelperTest.java
changeset 41865 3ef02797070d
equal deleted inserted replaced
41864:f7dbab23003a 41865:3ef02797070d
       
     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 JavadocHelper
       
    28  * @library /tools/lib
       
    29  * @modules jdk.compiler/com.sun.tools.javac.api
       
    30  *          jdk.compiler/com.sun.tools.javac.main
       
    31  *          jdk.compiler/jdk.internal.shellsupport.doc
       
    32  * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask
       
    33  * @run testng JavadocHelperTest
       
    34  */
       
    35 
       
    36 import java.io.IOException;
       
    37 import java.net.URI;
       
    38 import java.net.URISyntaxException;
       
    39 import java.nio.file.Files;
       
    40 import java.nio.file.Path;
       
    41 import java.nio.file.Paths;
       
    42 import java.util.Arrays;
       
    43 import java.util.function.Function;
       
    44 import java.util.jar.JarEntry;
       
    45 import java.util.jar.JarOutputStream;
       
    46 
       
    47 import javax.lang.model.element.Element;
       
    48 import javax.lang.model.util.ElementFilter;
       
    49 import javax.tools.Diagnostic.Kind;
       
    50 import javax.tools.DiagnosticListener;
       
    51 import javax.tools.JavaCompiler;
       
    52 import javax.tools.JavaFileObject;
       
    53 import javax.tools.SimpleJavaFileObject;
       
    54 import javax.tools.StandardJavaFileManager;
       
    55 import javax.tools.StandardLocation;
       
    56 import javax.tools.ToolProvider;
       
    57 
       
    58 import com.sun.source.util.JavacTask;
       
    59 import jdk.internal.shellsupport.doc.JavadocHelper;
       
    60 import org.testng.annotations.Test;
       
    61 import static org.testng.Assert.assertEquals;
       
    62 import static org.testng.Assert.assertTrue;
       
    63 
       
    64 @Test
       
    65 public class JavadocHelperTest {
       
    66 
       
    67     public void testJavadoc() throws Exception {
       
    68         doTestJavadoc("",
       
    69                       t -> t.getElements().getTypeElement("test.Super"),
       
    70                       "Top level. ");
       
    71         doTestJavadoc("",
       
    72                       t -> getFirstMethod(t, "test.Super"),
       
    73                       " javadoc1A\n" +
       
    74                       "\n" +
       
    75                       " @param p1 param1A\n" +
       
    76                       " @param p2 param2A\n" +
       
    77                       " @param p3 param3A\n" +
       
    78                       " @throws IllegalStateException exc1A\n" +
       
    79                       " @throws IllegalArgumentException exc2A\n" +
       
    80                       " @throws IllegalAccessException exc3A\n" +
       
    81                       " @return valueA\n");
       
    82     }
       
    83 
       
    84     private Element getFirstMethod(JavacTask task, String typeName) {
       
    85         return ElementFilter.methodsIn(task.getElements().getTypeElement(typeName).getEnclosedElements()).get(0);
       
    86     }
       
    87 
       
    88     private Function<JavacTask, Element> getSubTest = t -> getFirstMethod(t, "test.Sub");
       
    89 
       
    90     public void testInheritNoJavadoc() throws Exception {
       
    91         doTestJavadoc("",
       
    92                       getSubTest,
       
    93                       " javadoc1A\n" +
       
    94                       "\n" +
       
    95                       " @param p1 param1A\n" +
       
    96                       " @param p2 param2A\n" +
       
    97                       " @param p3 param3A\n" +
       
    98                       " @throws IllegalStateException exc1A\n" +
       
    99                       " @throws IllegalArgumentException exc2A\n" +
       
   100                       " @throws IllegalAccessException exc3A\n" +
       
   101                       " @return valueA\n");
       
   102     }
       
   103 
       
   104     public void testInheritFull() throws Exception {
       
   105         doTestJavadoc("    /**\n" +
       
   106                       "     * Prefix {@inheritDoc} suffix.\n" +
       
   107                       "     *\n" +
       
   108                       "     * @param p1 prefix {@inheritDoc} suffix\n" +
       
   109                       "     * @param p2 prefix {@inheritDoc} suffix\n" +
       
   110                       "     * @param p3 prefix {@inheritDoc} suffix\n" +
       
   111                       "     * @throws IllegalStateException prefix {@inheritDoc} suffix\n" +
       
   112                       "     * @throws IllegalArgumentException prefix {@inheritDoc} suffix\n" +
       
   113                       "     * @throws IllegalAccessException prefix {@inheritDoc} suffix\n" +
       
   114                       "     * @return prefix {@inheritDoc} suffix\n" +
       
   115                       "     */\n",
       
   116                       getSubTest,
       
   117                       " Prefix javadoc1 suffix.\n" +
       
   118                       "\n" +
       
   119                       " @param p1 prefix param1 suffix\n" +
       
   120                       " @param p2 prefix param2 suffix\n" +
       
   121                       " @param p3 prefix param3 suffix\n" +
       
   122                       " @throws IllegalStateException prefix exc1 suffix\n" +
       
   123                       " @throws IllegalArgumentException prefix exc2 suffix\n" +
       
   124                       " @throws IllegalAccessException prefix exc3 suffix\n" +
       
   125                       " @return prefix value suffix\n");
       
   126     }
       
   127 
       
   128     public void testInheritMissingParam() throws Exception {
       
   129         doTestJavadoc("    /**\n" +
       
   130                       "     * Prefix {@inheritDoc} suffix.\n" +
       
   131                       "     *\n" +
       
   132                       "     * @param p1 prefix {@inheritDoc} suffix\n" +
       
   133                       "     * @param p3 prefix {@inheritDoc} suffix\n" +
       
   134                       "     * @throws IllegalStateException prefix {@inheritDoc} suffix\n" +
       
   135                       "     * @throws IllegalArgumentException prefix {@inheritDoc} suffix\n" +
       
   136                       "     * @throws IllegalAccessException prefix {@inheritDoc} suffix\n" +
       
   137                       "     * @return prefix {@inheritDoc} suffix\n" +
       
   138                       "     */\n",
       
   139                       getSubTest,
       
   140                       " Prefix javadoc1 suffix.\n" +
       
   141                       "\n" +
       
   142                       " @param p1 prefix param1 suffix\n" +
       
   143                       "@param p2  param2\n" +
       
   144                       " @param p3 prefix param3 suffix\n" +
       
   145                       " @throws IllegalStateException prefix exc1 suffix\n" +
       
   146                       " @throws IllegalArgumentException prefix exc2 suffix\n" +
       
   147                       " @throws IllegalAccessException prefix exc3 suffix\n" +
       
   148                       " @return prefix value suffix\n");
       
   149     }
       
   150 
       
   151     public void testInheritMissingFirstParam() throws Exception {
       
   152         doTestJavadoc("    /**\n" +
       
   153                       "     * Prefix {@inheritDoc} suffix.\n" +
       
   154                       "     *\n" +
       
   155                       "     * @param p2 prefix {@inheritDoc} suffix\n" +
       
   156                       "     * @param p3 prefix {@inheritDoc} suffix\n" +
       
   157                       "     * @throws IllegalStateException prefix {@inheritDoc} suffix\n" +
       
   158                       "     * @throws IllegalArgumentException prefix {@inheritDoc} suffix\n" +
       
   159                       "     * @throws IllegalAccessException prefix {@inheritDoc} suffix\n" +
       
   160                       "     * @return prefix {@inheritDoc} suffix\n" +
       
   161                       "     */\n",
       
   162                       getSubTest,
       
   163                       " Prefix javadoc1 suffix.\n" +
       
   164                       "@param p1  param1\n" +
       
   165                       "\n" +
       
   166                       " @param p2 prefix param2 suffix\n" +
       
   167                       " @param p3 prefix param3 suffix\n" +
       
   168                       " @throws IllegalStateException prefix exc1 suffix\n" +
       
   169                       " @throws IllegalArgumentException prefix exc2 suffix\n" +
       
   170                       " @throws IllegalAccessException prefix exc3 suffix\n" +
       
   171                       " @return prefix value suffix\n");
       
   172     }
       
   173 
       
   174     public void testInheritMissingThrows() throws Exception {
       
   175         doTestJavadoc("    /**\n" +
       
   176                       "     * Prefix {@inheritDoc} suffix.\n" +
       
   177                       "     *\n" +
       
   178                       "     * @param p1 prefix {@inheritDoc} suffix\n" +
       
   179                       "     * @param p2 prefix {@inheritDoc} suffix\n" +
       
   180                       "     * @param p3 prefix {@inheritDoc} suffix\n" +
       
   181                       "     * @throws IllegalStateException prefix {@inheritDoc} suffix\n" +
       
   182                       "     * @throws IllegalAccessException prefix {@inheritDoc} suffix\n" +
       
   183                       "     * @return prefix {@inheritDoc} suffix\n" +
       
   184                       "     */\n",
       
   185                       getSubTest,
       
   186                       " Prefix javadoc1 suffix.\n" +
       
   187                       "\n" +
       
   188                       " @param p1 prefix param1 suffix\n" +
       
   189                       " @param p2 prefix param2 suffix\n" +
       
   190                       " @param p3 prefix param3 suffix\n" +
       
   191                       " @throws IllegalStateException prefix exc1 suffix\n" +
       
   192                       "@throws java.lang.IllegalArgumentException  exc2\n" +
       
   193                       " @throws IllegalAccessException prefix exc3 suffix\n" +
       
   194                       " @return prefix value suffix\n");
       
   195     }
       
   196 
       
   197     public void testInheritMissingReturn() throws Exception {
       
   198         doTestJavadoc("    /**\n" +
       
   199                       "     * Prefix {@inheritDoc} suffix.\n" +
       
   200                       "     *\n" +
       
   201                       "     * @param p1 prefix {@inheritDoc} suffix\n" +
       
   202                       "     * @param p2 prefix {@inheritDoc} suffix\n" +
       
   203                       "     * @param p3 prefix {@inheritDoc} suffix\n" +
       
   204                       "     * @throws IllegalStateException prefix {@inheritDoc} suffix\n" +
       
   205                       "     * @throws IllegalArgumentException prefix {@inheritDoc} suffix\n" +
       
   206                       "     * @throws IllegalAccessException prefix {@inheritDoc} suffix\n" +
       
   207                       "     */\n",
       
   208                       getSubTest,
       
   209                       " Prefix javadoc1 suffix.\n" +
       
   210                       "\n" +
       
   211                       " @param p1 prefix param1 suffix\n" +
       
   212                       " @param p2 prefix param2 suffix\n" +
       
   213                       " @param p3 prefix param3 suffix\n" +
       
   214                       " @throws IllegalStateException prefix exc1 suffix\n" +
       
   215                       " @throws IllegalArgumentException prefix exc2 suffix\n" +
       
   216                       " @throws IllegalAccessException prefix exc3 suffix\n" +
       
   217                       "@return  value\n");
       
   218     }
       
   219 
       
   220 
       
   221     private void doTestJavadoc(String origJavadoc, Function<JavacTask, Element> getElement, String expectedJavadoc) throws Exception {
       
   222         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
       
   223         String subClass =
       
   224                 "package test;\n" +
       
   225                 "public class Sub extends Super {\n" +
       
   226                 origJavadoc +
       
   227                 "    public String test(int p1, int p2, int p3) throws IllegalStateException, IllegalArgumentException, IllegalAccessException { return null;} \n" +
       
   228                 "}\n";
       
   229         String superClass =
       
   230                 "package test;\n" +
       
   231                 "/**Top level." +
       
   232                 " */\n" +
       
   233                 "public class Super {\n" +
       
   234                 "    /**\n" +
       
   235                 "     * javadoc1A\n" +
       
   236                 "     *\n" +
       
   237                 "     * @param p1 param1A\n" +
       
   238                 "     * @param p2 param2A\n" +
       
   239                 "     * @param p3 param3A\n" +
       
   240                 "     * @throws IllegalStateException exc1A\n" +
       
   241                 "     * @throws IllegalArgumentException exc2A\n" +
       
   242                 "     * @throws IllegalAccessException exc3A\n" +
       
   243                 "     * @return valueA\n" +
       
   244                 "     */\n" +
       
   245                 "    public String test(int p1, int p2, int p3) throws IllegalStateException, IllegalArgumentException, IllegalAccessException { return null;} \n" +
       
   246                 "}\n";
       
   247 
       
   248         Path srcZip = Paths.get("src.zip");
       
   249 
       
   250         try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(srcZip))) {
       
   251             out.putNextEntry(new JarEntry("test/Sub.java"));
       
   252             out.write(subClass.getBytes());
       
   253             out.putNextEntry(new JarEntry("test/Super.java"));
       
   254             out.write(superClass.getBytes());
       
   255         } catch (IOException ex) {
       
   256             throw new IllegalStateException(ex);
       
   257         }
       
   258 
       
   259         DiagnosticListener<? super JavaFileObject> noErrors = d -> {
       
   260             if (d.getKind() == Kind.ERROR) {
       
   261                 throw new AssertionError(d.getMessage(null));
       
   262             }
       
   263         };
       
   264 
       
   265         assertTrue(compiler.getTask(null, null, noErrors, Arrays.asList("-d", "."), null, Arrays.asList(new JFOImpl("Super", superClass), new JFOImpl("Sub", subClass))).call());
       
   266 
       
   267         try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {
       
   268             fm.setLocationFromPaths(StandardLocation.CLASS_PATH, Arrays.asList(Paths.get(".").toAbsolutePath()));
       
   269             JavacTask task = (JavacTask) compiler.getTask(null, fm, noErrors, null, null, null);
       
   270 
       
   271             Element el = getElement.apply(task);
       
   272 
       
   273             try (JavadocHelper helper = JavadocHelper.create(task, Arrays.asList(srcZip))) {
       
   274                 String javadoc = helper.getResolvedDocComment(el);
       
   275 
       
   276                 assertEquals(javadoc, expectedJavadoc);
       
   277             }
       
   278         }
       
   279     }
       
   280 
       
   281     private static final class JFOImpl extends SimpleJavaFileObject {
       
   282 
       
   283         private final String code;
       
   284 
       
   285         public JFOImpl(String name, String code) throws URISyntaxException {
       
   286             super(new URI("mem:///" + name + ".java"), Kind.SOURCE);
       
   287             this.code = code;
       
   288         }
       
   289 
       
   290         @Override
       
   291         public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
       
   292             return code;
       
   293         }
       
   294 
       
   295     }
       
   296 }