aoqi@0: /* aoqi@0: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 6493690 aoqi@0: * @summary javadoc should have a javax.tools.Tool service provider aoqi@0: * @build APITest aoqi@0: * @run main GetTask_DocletClassTest aoqi@0: */ aoqi@0: aoqi@0: import com.sun.javadoc.DocErrorReporter; aoqi@0: import com.sun.javadoc.LanguageVersion; aoqi@0: import com.sun.javadoc.RootDoc; aoqi@0: import java.io.File; aoqi@0: import java.util.Arrays; aoqi@0: import java.util.Collections; aoqi@0: import java.util.Random; aoqi@0: import javax.tools.DocumentationTool; aoqi@0: import javax.tools.DocumentationTool.DocumentationTask; aoqi@0: import javax.tools.JavaFileObject; aoqi@0: import javax.tools.StandardJavaFileManager; aoqi@0: import javax.tools.ToolProvider; aoqi@0: aoqi@0: /** aoqi@0: * Tests for DocumentationTool.getTask docletClass parameter. aoqi@0: */ aoqi@0: public class GetTask_DocletClassTest extends APITest { aoqi@0: public static void main(String... args) throws Exception { aoqi@0: new GetTask_DocletClassTest().run(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Verify that an alternate doclet can be specified. aoqi@0: * aoqi@0: * There is no standard interface or superclass for a doclet; aoqi@0: * the only requirement is that it provides static methods that aoqi@0: * can be invoked via reflection. So, for now, the doclet is aoqi@0: * specified as a class. aoqi@0: * Because we cannot create and use a unique instance of the class, aoqi@0: * we verify that the doclet has been called by having it record aoqi@0: * (in a static field!) the comment from the last time it was invoked, aoqi@0: * which is randomly generated each time the test is run. aoqi@0: */ aoqi@0: @Test aoqi@0: public void testDoclet() throws Exception { aoqi@0: Random r = new Random(); aoqi@0: int key = r.nextInt(); aoqi@0: JavaFileObject srcFile = createSimpleJavaFileObject( aoqi@0: "pkg/C", aoqi@0: "package pkg; /** " + key + "*/ public class C { }"); aoqi@0: DocumentationTool tool = ToolProvider.getSystemDocumentationTool(); aoqi@0: StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); aoqi@0: File outDir = getOutDir(); aoqi@0: fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir)); aoqi@0: Iterable files = Arrays.asList(srcFile); aoqi@0: DocumentationTask t = tool.getTask(null, fm, null, TestDoclet.class, null, files); aoqi@0: if (t.call()) { aoqi@0: System.err.println("task succeeded"); aoqi@0: if (TestDoclet.lastCaller.equals(String.valueOf(key))) aoqi@0: System.err.println("found expected key: " + key); aoqi@0: else aoqi@0: error("Expected key not found"); aoqi@0: checkFiles(outDir, Collections.emptySet()); aoqi@0: } else { aoqi@0: throw new Exception("task failed"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public static class TestDoclet { aoqi@0: static String lastCaller; aoqi@0: public static boolean start(RootDoc root) { aoqi@0: lastCaller = root.classNamed("pkg.C").commentText().trim(); aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: public static int optionLength(String option) { aoqi@0: return 0; // default is option unknown aoqi@0: } aoqi@0: aoqi@0: public static boolean validOptions(String options[][], aoqi@0: DocErrorReporter reporter) { aoqi@0: return true; // default is options are valid aoqi@0: } aoqi@0: aoqi@0: public static LanguageVersion languageVersion() { aoqi@0: return LanguageVersion.JAVA_1_1; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Verify that exceptions from a doclet are thrown as expected. aoqi@0: */ aoqi@0: @Test aoqi@0: public void testBadDoclet() throws Exception { aoqi@0: JavaFileObject srcFile = createSimpleJavaFileObject(); aoqi@0: DocumentationTool tool = ToolProvider.getSystemDocumentationTool(); aoqi@0: StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); aoqi@0: File outDir = getOutDir(); aoqi@0: fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir)); aoqi@0: Iterable files = Arrays.asList(srcFile); aoqi@0: DocumentationTask t = tool.getTask(null, fm, null, BadDoclet.class, null, files); aoqi@0: try { aoqi@0: t.call(); aoqi@0: error("call completed without exception"); aoqi@0: } catch (RuntimeException e) { aoqi@0: Throwable c = e.getCause(); aoqi@0: if (c.getClass() == UnexpectedError.class) aoqi@0: System.err.println("exception caught as expected: " + c); aoqi@0: else aoqi@0: throw e; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public static class UnexpectedError extends Error { } aoqi@0: aoqi@0: public static class BadDoclet { aoqi@0: public static boolean start(RootDoc root) { aoqi@0: throw new UnexpectedError(); aoqi@0: } aoqi@0: aoqi@0: public static int optionLength(String option) { aoqi@0: return 0; // default is option unknown aoqi@0: } aoqi@0: aoqi@0: public static boolean validOptions(String options[][], aoqi@0: DocErrorReporter reporter) { aoqi@0: return true; // default is options are valid aoqi@0: } aoqi@0: aoqi@0: public static LanguageVersion languageVersion() { aoqi@0: return LanguageVersion.JAVA_1_1; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: