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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 3834
45746e46893b
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial