1 /* 2 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 6493690 27 * @summary javadoc should have a javax.tools.Tool service provider 28 * @modules jdk.javadoc 29 * @build APITest 30 * @run main GetTask_DocletClassTest 31 * @key randomness 32 */ 33 34 import com.sun.javadoc.DocErrorReporter; 35 import com.sun.javadoc.LanguageVersion; 36 import com.sun.javadoc.RootDoc; 37 import java.io.File; 38 import java.util.Arrays; 39 import java.util.Collections; 40 import java.util.Random; 41 import javax.tools.DocumentationTool; 42 import javax.tools.DocumentationTool.DocumentationTask; 43 import javax.tools.JavaFileObject; 44 import javax.tools.StandardJavaFileManager; 45 import javax.tools.ToolProvider; 46 47 /** 48 * Tests for DocumentationTool.getTask docletClass parameter. 49 */ 50 public class GetTask_DocletClassTest extends APITest { 51 public static void main(String... args) throws Exception { 52 new GetTask_DocletClassTest().run(); 53 } 54 55 /** 56 * Verify that an alternate doclet can be specified. 57 * 58 * There is no standard interface or superclass for a doclet; 59 * the only requirement is that it provides static methods that 60 * can be invoked via reflection. So, for now, the doclet is 61 * specified as a class. 62 * Because we cannot create and use a unique instance of the class, 63 * we verify that the doclet has been called by having it record 64 * (in a static field!) the comment from the last time it was invoked, 65 * which is randomly generated each time the test is run. 66 */ 67 @Test 68 public void testDoclet() throws Exception { 69 Random r = new Random(); 70 int key = r.nextInt(); 71 JavaFileObject srcFile = createSimpleJavaFileObject( 72 "pkg/C", 73 "package pkg; /** " + key + "*/ public class C { }"); 74 DocumentationTool tool = ToolProvider.getSystemDocumentationTool(); 75 try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) { 76 File outDir = getOutDir(); 77 fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir)); 78 Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile); 79 DocumentationTask t = tool.getTask(null, fm, null, TestDoclet.class, null, files); 80 if (t.call()) { 81 System.err.println("task succeeded"); 82 if (TestDoclet.lastCaller.equals(String.valueOf(key))) 83 System.err.println("found expected key: " + key); 84 else 85 error("Expected key not found"); 86 checkFiles(outDir, Collections.<String>emptySet()); 87 } else { 88 throw new Exception("task failed"); 89 } 90 } 91 } 92 93 public static class TestDoclet { 94 static String lastCaller; 95 public static boolean start(RootDoc root) { 96 lastCaller = root.classNamed("pkg.C").commentText().trim(); 97 return true; 98 } 99 100 public static int optionLength(String option) { 101 return 0; // default is option unknown 102 } 103 104 public static boolean validOptions(String options[][], 105 DocErrorReporter reporter) { 106 return true; // default is options are valid 107 } 108 109 public static LanguageVersion languageVersion() { 110 return LanguageVersion.JAVA_1_1; 111 } 112 } 113 114 /** 115 * Verify that exceptions from a doclet are thrown as expected. 116 */ 117 @Test 118 public void testBadDoclet() throws Exception { 119 JavaFileObject srcFile = createSimpleJavaFileObject(); 120 DocumentationTool tool = ToolProvider.getSystemDocumentationTool(); 121 try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) { 122 File outDir = getOutDir(); 123 fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir)); 124 Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile); 125 DocumentationTask t = tool.getTask(null, fm, null, BadDoclet.class, null, files); 126 try { 127 t.call(); 128 error("call completed without exception"); 129 } catch (RuntimeException e) { 130 Throwable c = e.getCause(); 131 if (c.getClass() == UnexpectedError.class) 132 System.err.println("exception caught as expected: " + c); 133 else 134 throw e; 135 } 136 } 137 } 138 139 public static class UnexpectedError extends Error { } 140 141 public static class BadDoclet { 142 public static boolean start(RootDoc root) { 143 throw new UnexpectedError(); 144 } 145 146 public static int optionLength(String option) { 147 return 0; // default is option unknown 148 } 149 150 public static boolean validOptions(String options[][], 151 DocErrorReporter reporter) { 152 return true; // default is options are valid 153 } 154 155 public static LanguageVersion languageVersion() { 156 return LanguageVersion.JAVA_1_1; 157 } 158 } 159 160 } 161