APITest.java revision 1412:bdcef2ef52d2
1/* 2 * Copyright (c) 2012, 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 24import java.io.File; 25import java.io.IOException; 26import java.lang.annotation.Annotation; 27import java.lang.annotation.Retention; 28import java.lang.annotation.RetentionPolicy; 29import java.lang.reflect.InvocationTargetException; 30import java.lang.reflect.Method; 31import java.net.URI; 32import java.nio.file.Files; 33import java.nio.file.Path; 34import java.util.Arrays; 35import java.util.HashSet; 36import java.util.Set; 37import java.util.TreeSet; 38 39import javax.tools.JavaFileObject; 40import javax.tools.SimpleJavaFileObject; 41 42 43/* 44 * Superclass with utility methods for API tests. 45 */ 46class APITest { 47 protected APITest() { } 48 49 /** Marker annotation for test cases. */ 50 @Retention(RetentionPolicy.RUNTIME) 51 @interface Test { } 52 53 /** Invoke all methods annotated with @Test. */ 54 protected void run() throws Exception { 55 for (Method m: getClass().getDeclaredMethods()) { 56 Annotation a = m.getAnnotation(Test.class); 57 if (a != null) { 58 testCount++; 59 testName = m.getName(); 60 System.err.println("test: " + testName); 61 try { 62 m.invoke(this, new Object[] { }); 63 } catch (InvocationTargetException e) { 64 Throwable cause = e.getCause(); 65 throw (cause instanceof Exception) ? ((Exception) cause) : e; 66 } 67 System.err.println(); 68 } 69 } 70 71 if (testCount == 0) 72 error("no tests found"); 73 74 StringBuilder summary = new StringBuilder(); 75 if (testCount != 1) 76 summary.append(testCount).append(" tests"); 77 if (errorCount > 0) { 78 if (summary.length() > 0) summary.append(", "); 79 summary.append(errorCount).append(" errors"); 80 } 81 System.err.println(summary); 82 if (errorCount > 0) 83 throw new Exception(errorCount + " errors found"); 84 } 85 86 /** 87 * Create a directory in which to store generated doc files. 88 * Avoid using the default (current) directory, so that we can 89 * be sure that javadoc is writing in the intended location, 90 * not a default location. 91 */ 92 protected File getOutDir() { 93 File dir = new File(testName); 94 dir.mkdirs(); 95 return dir; 96 } 97 98 /** 99 * Create a directory in which to store generated doc files. 100 * Avoid using the default (current) directory, so that we can 101 * be sure that javadoc is writing in the intended location, 102 * not a default location. 103 */ 104 protected File getOutDir(String path) { 105 File dir = new File(testName, path); 106 dir.mkdirs(); 107 return dir; 108 } 109 110 protected JavaFileObject createSimpleJavaFileObject() { 111 return createSimpleJavaFileObject("pkg/C", "package pkg; public class C { }"); 112 } 113 114 protected JavaFileObject createSimpleJavaFileObject(final String binaryName, final String content) { 115 return new SimpleJavaFileObject( 116 URI.create("myfo:///" + binaryName + ".java"), JavaFileObject.Kind.SOURCE) { 117 @Override 118 public CharSequence getCharContent(boolean ignoreEncoding) { 119 return content; 120 } 121 }; 122 } 123 124 protected void checkFiles(File dir, Set<String> expectFiles) { 125 Set<File> files = new HashSet<File>(); 126 listFiles(dir, files); 127 Set<String> foundFiles = new HashSet<String>(); 128 URI dirURI = dir.toURI(); 129 for (File f: files) 130 foundFiles.add(dirURI.relativize(f.toURI()).getPath()); 131 checkFiles(foundFiles, expectFiles, dir); 132 } 133 134 protected void checkFiles(Path dir, Set<String> expectFiles) throws IOException { 135 Set<Path> files = new HashSet<Path>(); 136 listFiles(dir, files); 137 Set<String> foundFiles = new HashSet<String>(); 138 for (Path f: files) { 139 foundFiles.add(dir.relativize(f).toString().replace(f.getFileSystem().getSeparator(), "/")); 140 } 141 checkFiles(foundFiles, expectFiles, dir); 142 } 143 144 private void checkFiles(Set<String> foundFiles, Set<String> expectFiles, Object where) { 145 if (!foundFiles.equals(expectFiles)) { 146 Set<String> missing = new TreeSet<String>(expectFiles); 147 missing.removeAll(foundFiles); 148 if (!missing.isEmpty()) 149 error("the following files were not found in " + where + ": " + missing); 150 Set<String> unexpected = new TreeSet<String>(foundFiles); 151 unexpected.removeAll(expectFiles); 152 if (!unexpected.isEmpty()) 153 error("the following unexpected files were found in " + where + ": " + unexpected); 154 } 155 } 156 157 protected void listFiles(File dir, Set<File> files) { 158 for (File f: dir.listFiles()) { 159 if (f.isDirectory()) 160 listFiles(f, files); 161 else if (f.isFile()) 162 files.add(f); 163 } 164 } 165 166 private void listFiles(Path dir, Set<Path> files) throws IOException { 167 for (Path f: Files.newDirectoryStream(dir)) { 168 if (Files.isDirectory(f)) 169 listFiles(f, files); 170 else if (Files.isRegularFile(f)) 171 files.add(f); 172 } 173 } 174 175 protected void error(String msg) { 176 System.err.println("Error: " + msg); 177 errorCount++; 178 } 179 180 protected int testCount; 181 protected int errorCount; 182 183 protected String testName; 184 185 /** 186 * Standard files generated by processing a documented class pkg.C. 187 */ 188 protected static Set<String> standardExpectFiles = new HashSet<String>(Arrays.asList( 189 "allclasses-frame.html", 190 "allclasses-noframe.html", 191 "constant-values.html", 192 "deprecated-list.html", 193 "help-doc.html", 194 "index-all.html", 195 "index.html", 196 "overview-tree.html", 197 "package-list", 198 "pkg/C.html", 199 "pkg/package-frame.html", 200 "pkg/package-summary.html", 201 "pkg/package-tree.html", 202 "resources/background.gif", 203 "resources/tab.gif", 204 "resources/titlebar_end.gif", 205 "resources/titlebar.gif", 206 "stylesheet.css" 207 )); 208} 209 210