JavadocTaskImplTest.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (c) 2012, 2016, 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.compiler/com.sun.tools.javac.file
29 *          jdk.compiler/com.sun.tools.javac.util
30 *          jdk.javadoc/jdk.javadoc.internal.api
31 *          jdk.javadoc/jdk.javadoc.internal.tool
32 * @build APITest
33 * @run main JavadocTaskImplTest
34 */
35
36import java.io.File;
37import java.util.Arrays;
38import java.util.concurrent.Callable;
39
40import javax.tools.DocumentationTool;
41import javax.tools.DocumentationTool.DocumentationTask;
42import javax.tools.JavaFileObject;
43import javax.tools.StandardJavaFileManager;
44import javax.tools.ToolProvider;
45
46import com.sun.tools.javac.file.JavacFileManager;
47import com.sun.tools.javac.util.Context;
48import jdk.javadoc.internal.api.JavadocTaskImpl;
49import jdk.javadoc.internal.tool.Messager;
50
51/**
52 *  Misc tests for JavacTaskImpl.
53 */
54public class JavadocTaskImplTest extends APITest {
55    public static void main(String... args) throws Exception {
56        new JavadocTaskImplTest().run();
57    }
58
59    @Test
60    public void testRawCall() throws Exception {
61        JavaFileObject srcFile = createSimpleJavaFileObject();
62        DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
63        try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
64            File outDir = getOutDir();
65            fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
66            Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);
67
68            @SuppressWarnings("rawtypes")
69            Callable t = tool.getTask(null, fm, null, null, null, files);
70
71            if (t.call() == Boolean.TRUE) {
72                System.err.println("task succeeded");
73            } else {
74                throw new Exception("task failed");
75            }
76        }
77    }
78
79    @Test
80    public void testDirectAccess1() throws Exception {
81        JavaFileObject srcFile = createSimpleJavaFileObject();
82        Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);
83        Context c = new Context();
84        Messager.preRegister(c, "javadoc");
85        try (StandardJavaFileManager fm = new JavacFileManager(c, true, null)) {
86            File outDir = getOutDir();
87            fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
88            DocumentationTask t = new JavadocTaskImpl(c, null, null, files);
89            if (t.call()) {
90                System.err.println("task succeeded");
91            } else {
92                throw new Exception("task failed");
93            }
94        }
95    }
96
97    @Test
98    public void testDirectAccess2() throws Exception {
99        JavaFileObject srcFile = null; // error, provokes NPE
100        Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);
101        Context c = new Context();
102        Messager.preRegister(c, "javadoc");
103        try (StandardJavaFileManager fm = new JavacFileManager(c, true, null)) {
104            File outDir = getOutDir();
105            fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
106            try {
107                DocumentationTask t = new JavadocTaskImpl(c, null, null, files);;
108                error("getTask succeeded, no exception thrown");
109            } catch (NullPointerException e) {
110                System.err.println("exception caught as expected: " + e);
111            }
112        }
113    }
114}
115
116