DocletPathTest.java revision 3233:b5d08bc0d224
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 34import java.io.File; 35import java.io.PrintWriter; 36import java.io.StringWriter; 37import java.util.Arrays; 38 39import javax.tools.DocumentationTool; 40import javax.tools.DocumentationTool.DocumentationTask; 41import javax.tools.JavaCompiler; 42import javax.tools.JavaFileObject; 43import javax.tools.StandardJavaFileManager; 44import javax.tools.StandardLocation; 45import javax.tools.ToolProvider; 46 47/** 48 * Tests for locating a doclet via the file manager's DOCLET_PATH. 49 */ 50public class DocletPathTest extends APITest { 51 public static void main(String... args) throws Exception { 52 new DocletPathTest().run(); 53 } 54 55 /** 56 * Verify that an alternate doclet can be specified, and located via 57 * the file manager's DOCLET_PATH. 58 */ 59 @Test 60 public void testDocletPath() throws Exception { 61 JavaFileObject docletSrc = 62 createSimpleJavaFileObject("DocletOnDocletPath", docletSrcText); 63 File docletDir = getOutDir("classes"); 64 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 65 try (StandardJavaFileManager cfm = compiler.getStandardFileManager(null, null, null)) { 66 cfm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(docletDir)); 67 Iterable<? extends JavaFileObject> cfiles = Arrays.asList(docletSrc); 68 if (!compiler.getTask(null, cfm, null, null, null, cfiles).call()) 69 throw new Exception("cannot compile doclet"); 70 } 71 72 JavaFileObject srcFile = createSimpleJavaFileObject(); 73 DocumentationTool tool = ToolProvider.getSystemDocumentationTool(); 74 try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) { 75 File outDir = getOutDir("api"); 76 fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir)); 77 fm.setLocation(DocumentationTool.Location.DOCLET_PATH, Arrays.asList(docletDir)); 78 Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile); 79 Iterable<String> options = Arrays.asList("-doclet", "DocletOnDocletPath"); 80 StringWriter sw = new StringWriter(); 81 PrintWriter pw = new PrintWriter(sw); 82 DocumentationTask t = tool.getTask(pw, fm, null, null, options, files); 83 boolean ok = t.call(); 84 String out = sw.toString(); 85 System.err.println(">>" + out + "<<"); 86 if (ok) { 87 if (out.contains(TEST_STRING)) { 88 System.err.println("doclet executed as expected"); 89 } else { 90 error("test string not found in doclet output"); 91 } 92 } else { 93 error("task failed"); 94 } 95 } 96 } 97 98 private static final String TEST_STRING = "DocletOnDocletPath found and running"; 99 100 private static final String docletSrcText = 101 "import jdk.javadoc.doclet.*;\n" + 102 "import javax.lang.model.SourceVersion;\n" + 103 "import java.util.List;\n" + 104 "import java.util.Collections;\n" + 105 "import java.util.Set;\n" + 106 "import jdk.javadoc.doclet.Doclet;\n" + 107 "import java.util.Locale;\n" + 108 "import jdk.javadoc.doclet.Reporter;\n" + 109 "public class DocletOnDocletPath implements Doclet {\n" + 110 " public boolean run(DocletEnvironment doc) {\n" + 111 " reporter.print(javax.tools.Diagnostic.Kind.NOTE, " + 112 " \"" + TEST_STRING + "\");\n" + 113 " return true;\n" + 114 " }\n" + 115 " public Set<Doclet.Option> getSupportedOptions() {return Collections.emptySet();}\n" + 116 " public SourceVersion getSupportedSourceVersion() {\n" + 117 " return SourceVersion.latestSupported();\n" + 118 " }\n" + 119 " Reporter reporter;\n" + 120 " public void init(Locale locale, Reporter reporter) {\n" + 121 " this.reporter = reporter;\n" + 122 " return;\n" + 123 " }" + 124 " public String getName() { return \"DocletOnPath\"; }\n" + 125 "}\n"; 126} 127 128