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