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 java.compiler
  29  *          jdk.compiler
  30  * @build APITest
  31  * @run main DocletPathTest
  32  */
  33 
  34 import java.io.File;
  35 import java.io.PrintWriter;
  36 import java.io.StringWriter;
  37 import java.util.Arrays;
  38 import javax.tools.DocumentationTool;
  39 import javax.tools.DocumentationTool.DocumentationTask;
  40 import javax.tools.JavaCompiler;
  41 import javax.tools.JavaFileObject;
  42 import javax.tools.StandardJavaFileManager;
  43 import javax.tools.StandardLocation;
  44 import javax.tools.ToolProvider;
  45 
  46 /**
  47  * Tests for locating a doclet via the file manager's DOCLET_PATH.
  48  */
  49 public class DocletPathTest extends APITest {
  50     public static void main(String... args) throws Exception {
  51         new DocletPathTest().run();
  52     }
  53 
  54     /**
  55      * Verify that an alternate doclet can be specified, and located via
  56      * the file manager's DOCLET_PATH.
  57      */
  58     @Test
  59     public void testDocletPath() throws Exception {
  60         JavaFileObject docletSrc =
  61                 createSimpleJavaFileObject("DocletOnDocletPath", docletSrcText);
  62         File docletDir = getOutDir("classes");
  63         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  64         try (StandardJavaFileManager cfm = compiler.getStandardFileManager(null, null, null)) {
  65             cfm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(docletDir));
  66             Iterable<? extends JavaFileObject> cfiles = Arrays.asList(docletSrc);
  67             if (!compiler.getTask(null, cfm, null, null, null, cfiles).call())
  68                 throw new Exception("cannot compile doclet");
  69         }
  70 
  71         JavaFileObject srcFile = createSimpleJavaFileObject();
  72         DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
  73         try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
  74             File outDir = getOutDir("api");
  75             fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
  76             fm.setLocation(DocumentationTool.Location.DOCLET_PATH, Arrays.asList(docletDir));
  77             Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);
  78             Iterable<String> options = Arrays.asList("-doclet", "DocletOnDocletPath");
  79             StringWriter sw = new StringWriter();
  80             PrintWriter pw = new PrintWriter(sw);
  81             DocumentationTask t = tool.getTask(pw, fm, null, null, options, files);
  82             boolean ok = t.call();
  83             String out = sw.toString();
  84             System.err.println(">>" + out + "<<");
  85             if (ok) {
  86                 if (out.contains(TEST_STRING)) {
  87                     System.err.println("doclet executed as expected");
  88                 } else {
  89                     error("test string not found in doclet output");
  90                 }
  91             } else {
  92                 error("task failed");
  93             }
  94         }
  95     }
  96 
  97     private static final String TEST_STRING = "DocletOnDocletPath found and running";
  98 
  99     private static final String docletSrcText =
 100         "import com.sun.javadoc.*;\n" +
 101         "public class DocletOnDocletPath {\n" +
 102         "    public static boolean start(RootDoc doc) {\n" +
 103         "        doc.printNotice(\"" + TEST_STRING + "\");\n" +
 104         "        return true;\n" +
 105         "    }\n" +
 106         "    public static int optionLength(String option) { return 0; }\n" +
 107         "    public static boolean validOptions(String options[][],\n" +
 108         "            DocErrorReporter reporter) { return true; }\n" +
 109         "    public static LanguageVersion languageVersion() {\n" +
 110         "        return LanguageVersion.JAVA_1_1;\n" +
 111         "    }\n" +
 112         "}\n";
 113 }
 114