1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javadoc/api/basic/APITest.java Thu Nov 15 23:07:24 2012 -0800 1.3 @@ -0,0 +1,209 @@ 1.4 +/* 1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +import java.io.File; 1.28 +import java.io.IOException; 1.29 +import java.lang.annotation.Annotation; 1.30 +import java.lang.annotation.Retention; 1.31 +import java.lang.annotation.RetentionPolicy; 1.32 +import java.lang.reflect.InvocationTargetException; 1.33 +import java.lang.reflect.Method; 1.34 +import java.net.URI; 1.35 +import java.nio.file.Files; 1.36 +import java.nio.file.Path; 1.37 +import java.util.Arrays; 1.38 +import java.util.HashSet; 1.39 +import java.util.Set; 1.40 +import java.util.TreeSet; 1.41 + 1.42 +import javax.tools.JavaFileObject; 1.43 +import javax.tools.SimpleJavaFileObject; 1.44 + 1.45 + 1.46 +/* 1.47 + * Superclass with utility methods for API tests. 1.48 + */ 1.49 +class APITest { 1.50 + protected APITest() { } 1.51 + 1.52 + /** Marker annotation for test cases. */ 1.53 + @Retention(RetentionPolicy.RUNTIME) 1.54 + @interface Test { } 1.55 + 1.56 + /** Invoke all methods annotated with @Test. */ 1.57 + protected void run() throws Exception { 1.58 + for (Method m: getClass().getDeclaredMethods()) { 1.59 + Annotation a = m.getAnnotation(Test.class); 1.60 + if (a != null) { 1.61 + testCount++; 1.62 + testName = m.getName(); 1.63 + System.err.println("test: " + testName); 1.64 + try { 1.65 + m.invoke(this, new Object[] { }); 1.66 + } catch (InvocationTargetException e) { 1.67 + Throwable cause = e.getCause(); 1.68 + throw (cause instanceof Exception) ? ((Exception) cause) : e; 1.69 + } 1.70 + System.err.println(); 1.71 + } 1.72 + } 1.73 + 1.74 + if (testCount == 0) 1.75 + error("no tests found"); 1.76 + 1.77 + StringBuilder summary = new StringBuilder(); 1.78 + if (testCount != 1) 1.79 + summary.append(testCount).append(" tests"); 1.80 + if (errorCount > 0) { 1.81 + if (summary.length() > 0) summary.append(", "); 1.82 + summary.append(errorCount).append(" errors"); 1.83 + } 1.84 + System.err.println(summary); 1.85 + if (errorCount > 0) 1.86 + throw new Exception(errorCount + " errors found"); 1.87 + } 1.88 + 1.89 + /** 1.90 + * Create a directory in which to store generated doc files. 1.91 + * Avoid using the default (current) directory, so that we can 1.92 + * be sure that javadoc is writing in the intended location, 1.93 + * not a default location. 1.94 + */ 1.95 + protected File getOutDir() { 1.96 + File dir = new File(testName); 1.97 + dir.mkdirs(); 1.98 + return dir; 1.99 + } 1.100 + 1.101 + /** 1.102 + * Create a directory in which to store generated doc files. 1.103 + * Avoid using the default (current) directory, so that we can 1.104 + * be sure that javadoc is writing in the intended location, 1.105 + * not a default location. 1.106 + */ 1.107 + protected File getOutDir(String path) { 1.108 + File dir = new File(testName, path); 1.109 + dir.mkdirs(); 1.110 + return dir; 1.111 + } 1.112 + 1.113 + protected JavaFileObject createSimpleJavaFileObject() { 1.114 + return createSimpleJavaFileObject("pkg/C", "package pkg; public class C { }"); 1.115 + } 1.116 + 1.117 + protected JavaFileObject createSimpleJavaFileObject(final String binaryName, final String content) { 1.118 + return new SimpleJavaFileObject( 1.119 + URI.create("myfo:///" + binaryName + ".java"), JavaFileObject.Kind.SOURCE) { 1.120 + @Override 1.121 + public CharSequence getCharContent(boolean ignoreEncoding) { 1.122 + return content; 1.123 + } 1.124 + }; 1.125 + } 1.126 + 1.127 + protected void checkFiles(File dir, Set<String> expectFiles) { 1.128 + Set<File> files = new HashSet<File>(); 1.129 + listFiles(dir, files); 1.130 + Set<String> foundFiles = new HashSet<String>(); 1.131 + URI dirURI = dir.toURI(); 1.132 + for (File f: files) 1.133 + foundFiles.add(dirURI.relativize(f.toURI()).getPath()); 1.134 + checkFiles(foundFiles, expectFiles, dir); 1.135 + } 1.136 + 1.137 + protected void checkFiles(Path dir, Set<String> expectFiles) throws IOException { 1.138 + Set<Path> files = new HashSet<Path>(); 1.139 + listFiles(dir, files); 1.140 + Set<String> foundFiles = new HashSet<String>(); 1.141 + for (Path f: files) { 1.142 + foundFiles.add(dir.relativize(f).toString().replace(f.getFileSystem().getSeparator(), "/")); 1.143 + } 1.144 + checkFiles(foundFiles, expectFiles, dir); 1.145 + } 1.146 + 1.147 + private void checkFiles(Set<String> foundFiles, Set<String> expectFiles, Object where) { 1.148 + if (!foundFiles.equals(expectFiles)) { 1.149 + Set<String> missing = new TreeSet<String>(expectFiles); 1.150 + missing.removeAll(foundFiles); 1.151 + if (!missing.isEmpty()) 1.152 + error("the following files were not found in " + where + ": " + missing); 1.153 + Set<String> unexpected = new TreeSet<String>(foundFiles); 1.154 + unexpected.removeAll(expectFiles); 1.155 + if (!unexpected.isEmpty()) 1.156 + error("the following unexpected files were found in " + where + ": " + unexpected); 1.157 + } 1.158 + } 1.159 + 1.160 + protected void listFiles(File dir, Set<File> files) { 1.161 + for (File f: dir.listFiles()) { 1.162 + if (f.isDirectory()) 1.163 + listFiles(f, files); 1.164 + else if (f.isFile()) 1.165 + files.add(f); 1.166 + } 1.167 + } 1.168 + 1.169 + private void listFiles(Path dir, Set<Path> files) throws IOException { 1.170 + for (Path f: Files.newDirectoryStream(dir)) { 1.171 + if (Files.isDirectory(f)) 1.172 + listFiles(f, files); 1.173 + else if (Files.isRegularFile(f)) 1.174 + files.add(f); 1.175 + } 1.176 + } 1.177 + 1.178 + protected void error(String msg) { 1.179 + System.err.println("Error: " + msg); 1.180 + errorCount++; 1.181 + } 1.182 + 1.183 + protected int testCount; 1.184 + protected int errorCount; 1.185 + 1.186 + protected String testName; 1.187 + 1.188 + /** 1.189 + * Standard files generated by processing a documented class pkg.C. 1.190 + */ 1.191 + protected static Set<String> standardExpectFiles = new HashSet<String>(Arrays.asList( 1.192 + "allclasses-frame.html", 1.193 + "allclasses-noframe.html", 1.194 + "constant-values.html", 1.195 + "deprecated-list.html", 1.196 + "help-doc.html", 1.197 + "index-all.html", 1.198 + "index.html", 1.199 + "overview-tree.html", 1.200 + "package-list", 1.201 + "pkg/C.html", 1.202 + "pkg/package-frame.html", 1.203 + "pkg/package-summary.html", 1.204 + "pkg/package-tree.html", 1.205 + "resources/background.gif", 1.206 + "resources/tab.gif", 1.207 + "resources/titlebar_end.gif", 1.208 + "resources/titlebar.gif", 1.209 + "stylesheet.css" 1.210 + )); 1.211 +} 1.212 +