Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javadoc/api/basic/DocletPathTest.java
38828 views
/*1* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 649369026* @summary javadoc should have a javax.tools.Tool service provider27* @build APITest28* @run main DocletPathTest29*/3031import java.io.File;32import java.io.PrintWriter;33import java.io.StringWriter;34import java.util.Arrays;35import javax.tools.DocumentationTool;36import javax.tools.DocumentationTool.DocumentationTask;37import javax.tools.JavaCompiler;38import javax.tools.JavaFileObject;39import javax.tools.StandardJavaFileManager;40import javax.tools.StandardLocation;41import javax.tools.ToolProvider;4243/**44* Tests for locating a doclet via the file manager's DOCLET_PATH.45*/46public class DocletPathTest extends APITest {47public static void main(String... args) throws Exception {48new DocletPathTest().run();49}5051/**52* Verify that an alternate doclet can be specified, and located via53* the file manager's DOCLET_PATH.54*/55@Test56public void testDocletPath() throws Exception {57JavaFileObject docletSrc =58createSimpleJavaFileObject("DocletOnDocletPath", docletSrcText);59File docletDir = getOutDir("classes");60JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();61StandardJavaFileManager cfm = compiler.getStandardFileManager(null, null, null);62cfm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(docletDir));63Iterable<? extends JavaFileObject> cfiles = Arrays.asList(docletSrc);64if (!compiler.getTask(null, cfm, null, null, null, cfiles).call())65throw new Exception("cannot compile doclet");6667JavaFileObject srcFile = createSimpleJavaFileObject();68DocumentationTool tool = ToolProvider.getSystemDocumentationTool();69StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);70File outDir = getOutDir("api");71fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));72fm.setLocation(DocumentationTool.Location.DOCLET_PATH, Arrays.asList(docletDir));73Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);74Iterable<String> options = Arrays.asList("-doclet", "DocletOnDocletPath");75StringWriter sw = new StringWriter();76PrintWriter pw = new PrintWriter(sw);77DocumentationTask t = tool.getTask(pw, fm, null, null, options, files);78boolean ok = t.call();79String out = sw.toString();80System.err.println(">>" + out + "<<");81if (ok) {82if (out.contains(TEST_STRING)) {83System.err.println("doclet executed as expected");84} else {85error("test string not found in doclet output");86}87} else {88error("task failed");89}90}9192private static final String TEST_STRING = "DocletOnDocletPath found and running";9394private static final String docletSrcText =95"import com.sun.javadoc.*;\n" +96"public class DocletOnDocletPath {\n" +97" public static boolean start(RootDoc doc) {\n" +98" doc.printNotice(\"" + TEST_STRING + "\");\n" +99" return true;\n" +100" }\n" +101" public static int optionLength(String option) { return 0; }\n" +102" public static boolean validOptions(String options[][],\n" +103" DocErrorReporter reporter) { return true; }\n" +104" public static LanguageVersion languageVersion() {\n" +105" return LanguageVersion.JAVA_1_1;\n" +106" }\n" +107"}\n";108}109110111112