Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javadoc/api/basic/GetTask_DocletClassTest.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 GetTask_DocletClassTest29* @key randomness30*/3132import com.sun.javadoc.DocErrorReporter;33import com.sun.javadoc.LanguageVersion;34import com.sun.javadoc.RootDoc;35import java.io.File;36import java.util.Arrays;37import java.util.Collections;38import java.util.Random;39import javax.tools.DocumentationTool;40import javax.tools.DocumentationTool.DocumentationTask;41import javax.tools.JavaFileObject;42import javax.tools.StandardJavaFileManager;43import javax.tools.ToolProvider;4445/**46* Tests for DocumentationTool.getTask docletClass parameter.47*/48public class GetTask_DocletClassTest extends APITest {49public static void main(String... args) throws Exception {50new GetTask_DocletClassTest().run();51}5253/**54* Verify that an alternate doclet can be specified.55*56* There is no standard interface or superclass for a doclet;57* the only requirement is that it provides static methods that58* can be invoked via reflection. So, for now, the doclet is59* specified as a class.60* Because we cannot create and use a unique instance of the class,61* we verify that the doclet has been called by having it record62* (in a static field!) the comment from the last time it was invoked,63* which is randomly generated each time the test is run.64*/65@Test66public void testDoclet() throws Exception {67Random r = new Random();68int key = r.nextInt();69JavaFileObject srcFile = createSimpleJavaFileObject(70"pkg/C",71"package pkg; /** " + key + "*/ public class C { }");72DocumentationTool tool = ToolProvider.getSystemDocumentationTool();73StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);74File outDir = getOutDir();75fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));76Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);77DocumentationTask t = tool.getTask(null, fm, null, TestDoclet.class, null, files);78if (t.call()) {79System.err.println("task succeeded");80if (TestDoclet.lastCaller.equals(String.valueOf(key)))81System.err.println("found expected key: " + key);82else83error("Expected key not found");84checkFiles(outDir, Collections.<String>emptySet());85} else {86throw new Exception("task failed");87}88}8990public static class TestDoclet {91static String lastCaller;92public static boolean start(RootDoc root) {93lastCaller = root.classNamed("pkg.C").commentText().trim();94return true;95}9697public static int optionLength(String option) {98return 0; // default is option unknown99}100101public static boolean validOptions(String options[][],102DocErrorReporter reporter) {103return true; // default is options are valid104}105106public static LanguageVersion languageVersion() {107return LanguageVersion.JAVA_1_1;108}109}110111/**112* Verify that exceptions from a doclet are thrown as expected.113*/114@Test115public void testBadDoclet() throws Exception {116JavaFileObject srcFile = createSimpleJavaFileObject();117DocumentationTool tool = ToolProvider.getSystemDocumentationTool();118StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);119File outDir = getOutDir();120fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));121Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);122DocumentationTask t = tool.getTask(null, fm, null, BadDoclet.class, null, files);123try {124t.call();125error("call completed without exception");126} catch (RuntimeException e) {127Throwable c = e.getCause();128if (c.getClass() == UnexpectedError.class)129System.err.println("exception caught as expected: " + c);130else131throw e;132}133}134135public static class UnexpectedError extends Error { }136137public static class BadDoclet {138public static boolean start(RootDoc root) {139throw new UnexpectedError();140}141142public static int optionLength(String option) {143return 0; // default is option unknown144}145146public static boolean validOptions(String options[][],147DocErrorReporter reporter) {148return true; // default is options are valid149}150151public static LanguageVersion languageVersion() {152return LanguageVersion.JAVA_1_1;153}154}155156}157158159160