Mon, 19 Sep 2016 09:50:03 -0700
Merge
1 /*
2 * Copyright (c) 2012, 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 */
24 /*
25 * @test
26 * @bug 6493690
27 * @summary javadoc should have a javax.tools.Tool service provider
28 * @build APITest
29 * @run main DocletPathTest
30 */
32 import java.io.File;
33 import java.io.PrintWriter;
34 import java.io.StringWriter;
35 import java.util.Arrays;
36 import javax.tools.DocumentationTool;
37 import javax.tools.DocumentationTool.DocumentationTask;
38 import javax.tools.JavaCompiler;
39 import javax.tools.JavaFileObject;
40 import javax.tools.StandardJavaFileManager;
41 import javax.tools.StandardLocation;
42 import javax.tools.ToolProvider;
44 /**
45 * Tests for locating a doclet via the file manager's DOCLET_PATH.
46 */
47 public class DocletPathTest extends APITest {
48 public static void main(String... args) throws Exception {
49 new DocletPathTest().run();
50 }
52 /**
53 * Verify that an alternate doclet can be specified, and located via
54 * the file manager's DOCLET_PATH.
55 */
56 @Test
57 public void testDocletPath() throws Exception {
58 JavaFileObject docletSrc =
59 createSimpleJavaFileObject("DocletOnDocletPath", docletSrcText);
60 File docletDir = getOutDir("classes");
61 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
62 StandardJavaFileManager cfm = compiler.getStandardFileManager(null, null, null);
63 cfm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(docletDir));
64 Iterable<? extends JavaFileObject> cfiles = Arrays.asList(docletSrc);
65 if (!compiler.getTask(null, cfm, null, null, null, cfiles).call())
66 throw new Exception("cannot compile doclet");
68 JavaFileObject srcFile = createSimpleJavaFileObject();
69 DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
70 StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
71 File outDir = getOutDir("api");
72 fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
73 fm.setLocation(DocumentationTool.Location.DOCLET_PATH, Arrays.asList(docletDir));
74 Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);
75 Iterable<String> options = Arrays.asList("-doclet", "DocletOnDocletPath");
76 StringWriter sw = new StringWriter();
77 PrintWriter pw = new PrintWriter(sw);
78 DocumentationTask t = tool.getTask(pw, fm, null, null, options, files);
79 boolean ok = t.call();
80 String out = sw.toString();
81 System.err.println(">>" + out + "<<");
82 if (ok) {
83 if (out.contains(TEST_STRING)) {
84 System.err.println("doclet executed as expected");
85 } else {
86 error("test string not found in doclet output");
87 }
88 } else {
89 error("task failed");
90 }
91 }
93 private static final String TEST_STRING = "DocletOnDocletPath found and running";
95 private static final String docletSrcText =
96 "import com.sun.javadoc.*;\n" +
97 "public class DocletOnDocletPath {\n" +
98 " public static boolean start(RootDoc doc) {\n" +
99 " doc.printNotice(\"" + TEST_STRING + "\");\n" +
100 " return true;\n" +
101 " }\n" +
102 " public static int optionLength(String option) { return 0; }\n" +
103 " public static boolean validOptions(String options[][],\n" +
104 " DocErrorReporter reporter) { return true; }\n" +
105 " public static LanguageVersion languageVersion() {\n" +
106 " return LanguageVersion.JAVA_1_1;\n" +
107 " }\n" +
108 "}\n";
109 }