GetTask_DocletClassTest.java revision 3233:b5d08bc0d224
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
34import java.io.File;
35import java.util.Arrays;
36import java.util.Collections;
37import java.util.Random;
38import javax.tools.DocumentationTool;
39import javax.tools.DocumentationTool.DocumentationTask;
40import javax.tools.JavaFileObject;
41import javax.tools.StandardJavaFileManager;
42import javax.tools.ToolProvider;
43
44import com.sun.javadoc.DocErrorReporter;
45import com.sun.javadoc.LanguageVersion;
46import com.sun.javadoc.RootDoc;
47
48/**
49 * Tests for DocumentationTool.getTask  docletClass  parameter.
50 */
51public class GetTask_DocletClassTest extends APITest {
52    public static void main(String... args) throws Exception {
53        new GetTask_DocletClassTest().run();
54    }
55
56    /**
57     * Verify that an alternate doclet can be specified.
58     *
59     * There is no standard interface or superclass for a doclet;
60     * the only requirement is that it provides static methods that
61     * can be invoked via reflection. So, for now, the doclet is
62     * specified as a class.
63     * Because we cannot create and use a unique instance of the class,
64     * we verify that the doclet has been called by having it record
65     * (in a static field!) the comment from the last time it was invoked,
66     * which is randomly generated each time the test is run.
67     */
68    @Test
69    public void testDoclet() throws Exception {
70        Random r = new Random();
71        int key = r.nextInt();
72        JavaFileObject srcFile = createSimpleJavaFileObject(
73                "pkg/C",
74                "package pkg; /** " + key + "*/ public class C { }");
75        DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
76        try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
77            File outDir = getOutDir();
78            fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
79            Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);
80            DocumentationTask t = tool.getTask(null, fm, null, TestDoclet.class, null, files);
81            if (t.call()) {
82                System.err.println("task succeeded");
83                if (TestDoclet.lastCaller.equals(String.valueOf(key)))
84                    System.err.println("found expected key: " + key);
85                else
86                    error("Expected key not found");
87                checkFiles(outDir, Collections.<String>emptySet());
88            } else {
89                throw new Exception("task failed");
90            }
91        }
92    }
93
94    public static class TestDoclet {
95        static String lastCaller;
96        public static boolean start(RootDoc root) {
97            lastCaller = root.classNamed("pkg.C").commentText().trim();
98            return true;
99        }
100
101        public static int optionLength(String option) {
102            return 0;  // default is option unknown
103        }
104
105        public static boolean validOptions(String options[][],
106                DocErrorReporter reporter) {
107            return true;  // default is options are valid
108        }
109
110        public static LanguageVersion languageVersion() {
111            return LanguageVersion.JAVA_1_1;
112        }
113    }
114
115    /**
116     * Verify that exceptions from a doclet are thrown as expected.
117     */
118    @Test
119    public void testBadDoclet() throws Exception {
120        JavaFileObject srcFile = createSimpleJavaFileObject();
121        DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
122        try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
123            File outDir = getOutDir();
124            fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
125            Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);
126            DocumentationTask t = tool.getTask(null, fm, null, BadDoclet.class, null, files);
127            try {
128                t.call();
129                error("call completed without exception");
130            } catch (RuntimeException e) {
131                e.printStackTrace();
132                Throwable c = e.getCause();
133                if (c.getClass() == UnexpectedError.class)
134                    System.err.println("exception caught as expected: " + c);
135                else
136                    throw e;
137            }
138        }
139    }
140
141    public static class UnexpectedError extends Error { }
142
143    public static class BadDoclet {
144        public static boolean start(RootDoc root) {
145            throw new UnexpectedError();
146        }
147
148        public static int optionLength(String option) {
149            return 0;  // default is option unknown
150        }
151
152        public static boolean validOptions(String options[][],
153                DocErrorReporter reporter) {
154            return true;  // default is options are valid
155        }
156
157        public static LanguageVersion languageVersion() {
158            return LanguageVersion.JAVA_1_1;
159        }
160    }
161
162}
163
164