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 TagletPathTest
  32  */
  33 
  34 import java.io.File;
  35 import java.io.PrintWriter;
  36 import java.io.StringWriter;
  37 import java.nio.charset.Charset;
  38 import java.nio.file.Files;
  39 import java.util.Arrays;
  40 import java.util.List;
  41 import javax.tools.DocumentationTool;
  42 import javax.tools.DocumentationTool.DocumentationTask;
  43 import javax.tools.JavaCompiler;
  44 import javax.tools.JavaFileObject;
  45 import javax.tools.StandardJavaFileManager;
  46 import javax.tools.StandardLocation;
  47 import javax.tools.ToolProvider;
  48 
  49 /**
  50  * Tests for locating a doclet via the file manager's DOCLET_PATH.
  51  */
  52 public class TagletPathTest extends APITest {
  53     public static void main(String... args) throws Exception {
  54         new TagletPathTest().run();
  55     }
  56 
  57     /**
  58      * Verify that a taglet can be specified, and located via
  59      * the file manager's TAGLET_PATH.
  60      */
  61     @Test
  62     public void testTagletPath() throws Exception {
  63         File testSrc = new File(System.getProperty("test.src"));
  64         File tagletSrcFile = new File(testSrc, "taglets/UnderlineTaglet.java");
  65         File tagletDir = getOutDir("classes");
  66         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  67         try (StandardJavaFileManager cfm = compiler.getStandardFileManager(null, null, null)) {
  68             cfm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(tagletDir));
  69             Iterable<? extends JavaFileObject> cfiles = cfm.getJavaFileObjects(tagletSrcFile);
  70             if (!compiler.getTask(null, cfm, null, null, null, cfiles).call())
  71                 throw new Exception("cannot compile taglet");
  72         }
  73 
  74         JavaFileObject srcFile = createSimpleJavaFileObject("pkg/C", testSrcText);
  75         DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
  76         try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
  77             File outDir = getOutDir("api");
  78             fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
  79             fm.setLocation(DocumentationTool.Location.TAGLET_PATH, Arrays.asList(tagletDir));
  80             Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);
  81             Iterable<String> options = Arrays.asList("-taglet", "UnderlineTaglet");
  82             StringWriter sw = new StringWriter();
  83             PrintWriter pw = new PrintWriter(sw);
  84             DocumentationTask t = tool.getTask(pw, fm, null, com.sun.tools.doclets.standard.Standard.class, options, files);
  85             boolean ok = t.call();
  86             String out = sw.toString();
  87             System.err.println(">>" + out + "<<");
  88             if (ok) {
  89                 File f = new File(outDir, "pkg/C.html");
  90                 List<String> doc = Files.readAllLines(f.toPath(), Charset.defaultCharset());
  91                 for (String line: doc) {
  92                     if (line.contains("<u>" + TEST_STRING + "</u>")) {
  93                         System.err.println("taglet executed as expected");
  94                         return;
  95                     }
  96                 }
  97                 error("expected text not found in output " + f);
  98             } else {
  99                 error("task failed");
 100             }
 101         }
 102     }
 103 
 104     static final String TEST_STRING = "xyzzy";
 105     static final String testSrcText =
 106             "package pkg;\n" +
 107             "/** {@underline " + TEST_STRING + "} */\n" +
 108             "public class C { }";
 109 }
 110