1 /*
   2  * Copyright (c) 2012, 2015, 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 6493690
  27  * @summary javadoc should have a javax.tools.Tool service provider
  28  * @modules jdk.javadoc
  29  * @build APITest
  30  * @run main GetTask_DocletClassTest
  31  */
  32 
  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;
  45 
  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     }
  53 
  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         try (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         }
  90     }
  91 
  92     public static class TestDoclet {
  93         static String lastCaller;
  94         public static boolean start(RootDoc root) {
  95             lastCaller = root.classNamed("pkg.C").commentText().trim();
  96             return true;
  97         }
  98 
  99         public static int optionLength(String option) {
 100             return 0;  // default is option unknown
 101         }
 102 
 103         public static boolean validOptions(String options[][],
 104                 DocErrorReporter reporter) {
 105             return true;  // default is options are valid
 106         }
 107 
 108         public static LanguageVersion languageVersion() {
 109             return LanguageVersion.JAVA_1_1;
 110         }
 111     }
 112 
 113     /**
 114      * Verify that exceptions from a doclet are thrown as expected.
 115      */
 116     @Test
 117     public void testBadDoclet() throws Exception {
 118         JavaFileObject srcFile = createSimpleJavaFileObject();
 119         DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
 120         try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
 121             File outDir = getOutDir();
 122             fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
 123             Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);
 124             DocumentationTask t = tool.getTask(null, fm, null, BadDoclet.class, null, files);
 125             try {
 126                 t.call();
 127                 error("call completed without exception");
 128             } catch (RuntimeException e) {
 129                 Throwable c = e.getCause();
 130                 if (c.getClass() == UnexpectedError.class)
 131                     System.err.println("exception caught as expected: " + c);
 132                 else
 133                     throw e;
 134             }
 135         }
 136     }
 137 
 138     public static class UnexpectedError extends Error { }
 139 
 140     public static class BadDoclet {
 141         public static boolean start(RootDoc root) {
 142             throw new UnexpectedError();
 143         }
 144 
 145         public static int optionLength(String option) {
 146             return 0;  // default is option unknown
 147         }
 148 
 149         public static boolean validOptions(String options[][],
 150                 DocErrorReporter reporter) {
 151             return true;  // default is options are valid
 152         }
 153 
 154         public static LanguageVersion languageVersion() {
 155             return LanguageVersion.JAVA_1_1;
 156         }
 157     }
 158 
 159 }
 160