1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javadoc/api/basic/GetTask_DocletClassTest.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,157 @@ 1.4 +/* 1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 6493690 1.30 + * @summary javadoc should have a javax.tools.Tool service provider 1.31 + * @build APITest 1.32 + * @run main GetTask_DocletClassTest 1.33 + */ 1.34 + 1.35 +import com.sun.javadoc.DocErrorReporter; 1.36 +import com.sun.javadoc.LanguageVersion; 1.37 +import com.sun.javadoc.RootDoc; 1.38 +import java.io.File; 1.39 +import java.util.Arrays; 1.40 +import java.util.Collections; 1.41 +import java.util.Random; 1.42 +import javax.tools.DocumentationTool; 1.43 +import javax.tools.DocumentationTool.DocumentationTask; 1.44 +import javax.tools.JavaFileObject; 1.45 +import javax.tools.StandardJavaFileManager; 1.46 +import javax.tools.ToolProvider; 1.47 + 1.48 +/** 1.49 + * Tests for DocumentationTool.getTask docletClass parameter. 1.50 + */ 1.51 +public class GetTask_DocletClassTest extends APITest { 1.52 + public static void main(String... args) throws Exception { 1.53 + new GetTask_DocletClassTest().run(); 1.54 + } 1.55 + 1.56 + /** 1.57 + * Verify that an alternate doclet can be specified. 1.58 + * 1.59 + * There is no standard interface or superclass for a doclet; 1.60 + * the only requirement is that it provides static methods that 1.61 + * can be invoked via reflection. So, for now, the doclet is 1.62 + * specified as a class. 1.63 + * Because we cannot create and use a unique instance of the class, 1.64 + * we verify that the doclet has been called by having it record 1.65 + * (in a static field!) the comment from the last time it was invoked, 1.66 + * which is randomly generated each time the test is run. 1.67 + */ 1.68 + @Test 1.69 + public void testDoclet() throws Exception { 1.70 + Random r = new Random(); 1.71 + int key = r.nextInt(); 1.72 + JavaFileObject srcFile = createSimpleJavaFileObject( 1.73 + "pkg/C", 1.74 + "package pkg; /** " + key + "*/ public class C { }"); 1.75 + DocumentationTool tool = ToolProvider.getSystemDocumentationTool(); 1.76 + StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); 1.77 + File outDir = getOutDir(); 1.78 + fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir)); 1.79 + Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile); 1.80 + DocumentationTask t = tool.getTask(null, fm, null, TestDoclet.class, null, files); 1.81 + if (t.call()) { 1.82 + System.err.println("task succeeded"); 1.83 + if (TestDoclet.lastCaller.equals(String.valueOf(key))) 1.84 + System.err.println("found expected key: " + key); 1.85 + else 1.86 + error("Expected key not found"); 1.87 + checkFiles(outDir, Collections.<String>emptySet()); 1.88 + } else { 1.89 + throw new Exception("task failed"); 1.90 + } 1.91 + } 1.92 + 1.93 + public static class TestDoclet { 1.94 + static String lastCaller; 1.95 + public static boolean start(RootDoc root) { 1.96 + lastCaller = root.classNamed("pkg.C").commentText().trim(); 1.97 + return true; 1.98 + } 1.99 + 1.100 + public static int optionLength(String option) { 1.101 + return 0; // default is option unknown 1.102 + } 1.103 + 1.104 + public static boolean validOptions(String options[][], 1.105 + DocErrorReporter reporter) { 1.106 + return true; // default is options are valid 1.107 + } 1.108 + 1.109 + public static LanguageVersion languageVersion() { 1.110 + return LanguageVersion.JAVA_1_1; 1.111 + } 1.112 + } 1.113 + 1.114 + /** 1.115 + * Verify that exceptions from a doclet are thrown as expected. 1.116 + */ 1.117 + @Test 1.118 + public void testBadDoclet() throws Exception { 1.119 + JavaFileObject srcFile = createSimpleJavaFileObject(); 1.120 + DocumentationTool tool = ToolProvider.getSystemDocumentationTool(); 1.121 + StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); 1.122 + File outDir = getOutDir(); 1.123 + fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir)); 1.124 + Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile); 1.125 + DocumentationTask t = tool.getTask(null, fm, null, BadDoclet.class, null, files); 1.126 + try { 1.127 + t.call(); 1.128 + error("call completed without exception"); 1.129 + } catch (RuntimeException e) { 1.130 + Throwable c = e.getCause(); 1.131 + if (c.getClass() == UnexpectedError.class) 1.132 + System.err.println("exception caught as expected: " + c); 1.133 + else 1.134 + throw e; 1.135 + } 1.136 + } 1.137 + 1.138 + public static class UnexpectedError extends Error { } 1.139 + 1.140 + public static class BadDoclet { 1.141 + public static boolean start(RootDoc root) { 1.142 + throw new UnexpectedError(); 1.143 + } 1.144 + 1.145 + public static int optionLength(String option) { 1.146 + return 0; // default is option unknown 1.147 + } 1.148 + 1.149 + public static boolean validOptions(String options[][], 1.150 + DocErrorReporter reporter) { 1.151 + return true; // default is options are valid 1.152 + } 1.153 + 1.154 + public static LanguageVersion languageVersion() { 1.155 + return LanguageVersion.JAVA_1_1; 1.156 + } 1.157 + } 1.158 + 1.159 +} 1.160 +