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 8024434
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 * @build APITest
31 * @run main GetTask_FileManagerTest
32 */
33
34import java.io.File;
35import java.io.IOException;
36import java.nio.file.Path;
37import java.util.Arrays;
38import java.util.Set;
39
40import javax.tools.DocumentationTool;
41import javax.tools.DocumentationTool.DocumentationTask;
42import javax.tools.FileObject;
43import javax.tools.ForwardingJavaFileManager;
44import javax.tools.JavaFileObject;
45import javax.tools.JavaFileObject.Kind;
46import javax.tools.StandardJavaFileManager;
47import javax.tools.ToolProvider;
48
49import com.sun.tools.javac.file.JavacFileManager;
50import com.sun.tools.javac.util.Context;
51
52/**
53 * Tests for DocumentationTool.getTask  fileManager  parameter.
54 */
55public class GetTask_FileManagerTest extends APITest {
56    public static void main(String... args) throws Exception {
57        new GetTask_FileManagerTest().run();
58    }
59
60    /**
61     * Verify that an alternate file manager can be specified:
62     * in this case, a TestFileManager.
63     */
64    @Test
65    public void testFileManager() throws Exception {
66        JavaFileObject srcFile = createSimpleJavaFileObject();
67        DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
68        StandardJavaFileManager fm = new TestFileManager();
69        File outDir = getOutDir();
70        fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
71        Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);
72        DocumentationTask t = tool.getTask(null, fm, null, null, Arrays.asList("-verbose"), files);
73        if (t.call()) {
74            System.err.println("task succeeded");
75            checkFiles(outDir, standardExpectFiles);
76        } else {
77            throw new Exception("task failed");
78        }
79    }
80
81    /**
82     * Verify that exceptions from a bad file manager are thrown as expected.
83     */
84    @Test
85    public void testBadFileManager() throws Exception {
86        JavaFileObject srcFile = createSimpleJavaFileObject();
87        DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
88        StandardJavaFileManager fm = new TestFileManager() {
89            @Override
90            public Iterable<JavaFileObject> list(Location location,
91                    String packageName,
92                    Set<Kind> kinds,
93                    boolean recurse)
94                    throws IOException {
95                throw new UnexpectedError();
96            }
97        };
98        fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(getOutDir()));
99        Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);
100        DocumentationTask t = tool.getTask(null, fm, null, null, null, files);
101        try {
102            t.call();
103            error("call completed without exception");
104        } catch (RuntimeException e) {
105            Throwable c = e.getCause();
106            if (c.getClass() == UnexpectedError.class)
107                System.err.println("exception caught as expected: " + c);
108            else
109                throw e;
110        }
111    }
112
113    public static class UnexpectedError extends Error { }
114
115    /*
116     * A JavaFileManager which is not a JavacFileManager, even though it uses one internally for
117     * convenience.
118     */
119    static class TestFileManager extends ForwardingJavaFileManager<StandardJavaFileManager>
120            implements StandardJavaFileManager  {
121        TestFileManager() {
122            super(new JavacFileManager(new Context(), false, null));
123        }
124
125        @Override
126        public Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles(Iterable<? extends File> files) {
127            return fileManager.getJavaFileObjectsFromFiles(files);
128        }
129
130        @Override
131        public Iterable<? extends JavaFileObject> getJavaFileObjects(File... files) {
132            return fileManager.getJavaFileObjects(files);
133        }
134
135        @Override
136        public Iterable<? extends JavaFileObject> getJavaFileObjectsFromStrings(Iterable<String> names) {
137            return fileManager.getJavaFileObjectsFromStrings(names);
138        }
139
140        @Override
141        public Iterable<? extends JavaFileObject> getJavaFileObjects(String... names) {
142            return fileManager.getJavaFileObjects(names);
143        }
144
145        @Override
146        public void setLocation(Location location, Iterable<? extends File> path) throws IOException {
147            fileManager.setLocation(location, path);
148        }
149
150        @Override
151        public Iterable<? extends File> getLocation(Location location) {
152            return fileManager.getLocation(location);
153        }
154
155    }
156}
157