Path: blob/master/test/langtools/jdk/javadoc/tool/api/basic/GetTask_FileManagerTest.java
40983 views
/*1* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6493690 802443426* @summary javadoc should have a javax.tools.Tool service provider27* @modules jdk.compiler/com.sun.tools.javac.file28* jdk.compiler/com.sun.tools.javac.util29* @build APITest30* @run main GetTask_FileManagerTest31*/3233import java.io.File;34import java.io.IOException;35import java.nio.file.Path;36import java.util.Arrays;37import java.util.Set;3839import javax.tools.DocumentationTool;40import javax.tools.DocumentationTool.DocumentationTask;41import javax.tools.FileObject;42import javax.tools.ForwardingJavaFileManager;43import javax.tools.JavaFileObject;44import javax.tools.JavaFileObject.Kind;45import javax.tools.StandardJavaFileManager;46import javax.tools.ToolProvider;4748import com.sun.tools.javac.file.JavacFileManager;49import com.sun.tools.javac.util.Context;5051/**52* Tests for DocumentationTool.getTask fileManager parameter.53*/54public class GetTask_FileManagerTest extends APITest {55public static void main(String... args) throws Exception {56new GetTask_FileManagerTest().run();57}5859/**60* Verify that an alternate file manager can be specified:61* in this case, a TestFileManager.62*/63@Test64public void testFileManager() throws Exception {65JavaFileObject srcFile = createSimpleJavaFileObject();66DocumentationTool tool = ToolProvider.getSystemDocumentationTool();67StandardJavaFileManager fm = new TestFileManager();68File outDir = getOutDir();69fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));70Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);71DocumentationTask t = tool.getTask(null, fm, null, null, Arrays.asList("-verbose"), files);72if (t.call()) {73System.err.println("task succeeded");74checkFiles(outDir, standardExpectFiles);75} else {76throw new Exception("task failed");77}78}7980/**81* Verify that exceptions from a bad file manager are thrown as expected.82*/83@Test84public void testBadFileManager() throws Exception {85JavaFileObject srcFile = createSimpleJavaFileObject();86DocumentationTool tool = ToolProvider.getSystemDocumentationTool();87StandardJavaFileManager fm = new TestFileManager() {88@Override89public Iterable<JavaFileObject> list(Location location,90String packageName,91Set<Kind> kinds,92boolean recurse)93throws IOException {94throw new UnexpectedError();95}96};97fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(getOutDir()));98Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);99DocumentationTask t = tool.getTask(null, fm, null, null, null, files);100try {101t.call();102error("call completed without exception");103} catch (RuntimeException e) {104Throwable c = e.getCause();105if (c.getClass() == UnexpectedError.class)106System.err.println("exception caught as expected: " + c);107else108throw e;109}110}111112public static class UnexpectedError extends Error { }113114/*115* A JavaFileManager which is not a JavacFileManager, even though it uses one internally for116* convenience.117*/118static class TestFileManager extends ForwardingJavaFileManager<StandardJavaFileManager>119implements StandardJavaFileManager {120TestFileManager() {121super(new JavacFileManager(new Context(), false, null));122}123124@Override125public Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles(Iterable<? extends File> files) {126return fileManager.getJavaFileObjectsFromFiles(files);127}128129@Override130public Iterable<? extends JavaFileObject> getJavaFileObjects(File... files) {131return fileManager.getJavaFileObjects(files);132}133134@Override135public Iterable<? extends JavaFileObject> getJavaFileObjectsFromStrings(Iterable<String> names) {136return fileManager.getJavaFileObjectsFromStrings(names);137}138139@Override140public Iterable<? extends JavaFileObject> getJavaFileObjects(String... names) {141return fileManager.getJavaFileObjects(names);142}143144@Override145public void setLocation(Location location, Iterable<? extends File> path) throws IOException {146fileManager.setLocation(location, path);147}148149@Override150public Iterable<? extends File> getLocation(Location location) {151return fileManager.getLocation(location);152}153154}155}156157158