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