test/tools/javadoc/api/basic/GetTask_DocletClassTest.java

Mon, 01 Jun 2015 15:19:54 -0700

author
darcy
date
Mon, 01 Jun 2015 15:19:54 -0700
changeset 3834
45746e46893b
parent 0
959103a6100f
permissions
-rw-r--r--

8075546: Add tiered testing definitions to the langtools repo
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 2012, 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  */
    24 /*
    25  * @test
    26  * @bug 6493690
    27  * @summary javadoc should have a javax.tools.Tool service provider
    28  * @build APITest
    29  * @run main GetTask_DocletClassTest
    30  * @key randomness
    31  */
    33 import com.sun.javadoc.DocErrorReporter;
    34 import com.sun.javadoc.LanguageVersion;
    35 import com.sun.javadoc.RootDoc;
    36 import java.io.File;
    37 import java.util.Arrays;
    38 import java.util.Collections;
    39 import java.util.Random;
    40 import javax.tools.DocumentationTool;
    41 import javax.tools.DocumentationTool.DocumentationTask;
    42 import javax.tools.JavaFileObject;
    43 import javax.tools.StandardJavaFileManager;
    44 import javax.tools.ToolProvider;
    46 /**
    47  * Tests for DocumentationTool.getTask  docletClass  parameter.
    48  */
    49 public class GetTask_DocletClassTest extends APITest {
    50     public static void main(String... args) throws Exception {
    51         new GetTask_DocletClassTest().run();
    52     }
    54     /**
    55      * Verify that an alternate doclet can be specified.
    56      *
    57      * There is no standard interface or superclass for a doclet;
    58      * the only requirement is that it provides static methods that
    59      * can be invoked via reflection. So, for now, the doclet is
    60      * specified as a class.
    61      * Because we cannot create and use a unique instance of the class,
    62      * we verify that the doclet has been called by having it record
    63      * (in a static field!) the comment from the last time it was invoked,
    64      * which is randomly generated each time the test is run.
    65      */
    66     @Test
    67     public void testDoclet() throws Exception {
    68         Random r = new Random();
    69         int key = r.nextInt();
    70         JavaFileObject srcFile = createSimpleJavaFileObject(
    71                 "pkg/C",
    72                 "package pkg; /** " + key + "*/ public class C { }");
    73         DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
    74         StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    75         File outDir = getOutDir();
    76         fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
    77         Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);
    78         DocumentationTask t = tool.getTask(null, fm, null, TestDoclet.class, null, files);
    79         if (t.call()) {
    80             System.err.println("task succeeded");
    81             if (TestDoclet.lastCaller.equals(String.valueOf(key)))
    82                 System.err.println("found expected key: " + key);
    83             else
    84                 error("Expected key not found");
    85             checkFiles(outDir, Collections.<String>emptySet());
    86         } else {
    87             throw new Exception("task failed");
    88         }
    89     }
    91     public static class TestDoclet {
    92         static String lastCaller;
    93         public static boolean start(RootDoc root) {
    94             lastCaller = root.classNamed("pkg.C").commentText().trim();
    95             return true;
    96         }
    98         public static int optionLength(String option) {
    99             return 0;  // default is option unknown
   100         }
   102         public static boolean validOptions(String options[][],
   103                 DocErrorReporter reporter) {
   104             return true;  // default is options are valid
   105         }
   107         public static LanguageVersion languageVersion() {
   108             return LanguageVersion.JAVA_1_1;
   109         }
   110     }
   112     /**
   113      * Verify that exceptions from a doclet are thrown as expected.
   114      */
   115     @Test
   116     public void testBadDoclet() throws Exception {
   117         JavaFileObject srcFile = createSimpleJavaFileObject();
   118         DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
   119         StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
   120         File outDir = getOutDir();
   121         fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
   122         Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);
   123         DocumentationTask t = tool.getTask(null, fm, null, BadDoclet.class, null, files);
   124         try {
   125             t.call();
   126             error("call completed without exception");
   127         } catch (RuntimeException e) {
   128             Throwable c = e.getCause();
   129             if (c.getClass() == UnexpectedError.class)
   130                 System.err.println("exception caught as expected: " + c);
   131             else
   132                 throw e;
   133         }
   134     }
   136     public static class UnexpectedError extends Error { }
   138     public static class BadDoclet {
   139         public static boolean start(RootDoc root) {
   140             throw new UnexpectedError();
   141         }
   143         public static int optionLength(String option) {
   144             return 0;  // default is option unknown
   145         }
   147         public static boolean validOptions(String options[][],
   148                 DocErrorReporter reporter) {
   149             return true;  // default is options are valid
   150         }
   152         public static LanguageVersion languageVersion() {
   153             return LanguageVersion.JAVA_1_1;
   154         }
   155     }
   157 }

mercurial