test/tools/javadoc/api/basic/APITest.java

Thu, 15 Nov 2012 23:07:24 -0800

author
jjg
date
Thu, 15 Nov 2012 23:07:24 -0800
changeset 1413
bdcef2ef52d2
child 1417
522a1ee72340
permissions
-rw-r--r--

6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
Reviewed-by: darcy

jjg@1413 1 /*
jjg@1413 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
jjg@1413 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1413 4 *
jjg@1413 5 * This code is free software; you can redistribute it and/or modify it
jjg@1413 6 * under the terms of the GNU General Public License version 2 only, as
jjg@1413 7 * published by the Free Software Foundation.
jjg@1413 8 *
jjg@1413 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1413 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1413 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1413 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@1413 13 * accompanied this code).
jjg@1413 14 *
jjg@1413 15 * You should have received a copy of the GNU General Public License version
jjg@1413 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1413 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1413 18 *
jjg@1413 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1413 20 * or visit www.oracle.com if you need additional information or have any
jjg@1413 21 * questions.
jjg@1413 22 */
jjg@1413 23
jjg@1413 24 import java.io.File;
jjg@1413 25 import java.io.IOException;
jjg@1413 26 import java.lang.annotation.Annotation;
jjg@1413 27 import java.lang.annotation.Retention;
jjg@1413 28 import java.lang.annotation.RetentionPolicy;
jjg@1413 29 import java.lang.reflect.InvocationTargetException;
jjg@1413 30 import java.lang.reflect.Method;
jjg@1413 31 import java.net.URI;
jjg@1413 32 import java.nio.file.Files;
jjg@1413 33 import java.nio.file.Path;
jjg@1413 34 import java.util.Arrays;
jjg@1413 35 import java.util.HashSet;
jjg@1413 36 import java.util.Set;
jjg@1413 37 import java.util.TreeSet;
jjg@1413 38
jjg@1413 39 import javax.tools.JavaFileObject;
jjg@1413 40 import javax.tools.SimpleJavaFileObject;
jjg@1413 41
jjg@1413 42
jjg@1413 43 /*
jjg@1413 44 * Superclass with utility methods for API tests.
jjg@1413 45 */
jjg@1413 46 class APITest {
jjg@1413 47 protected APITest() { }
jjg@1413 48
jjg@1413 49 /** Marker annotation for test cases. */
jjg@1413 50 @Retention(RetentionPolicy.RUNTIME)
jjg@1413 51 @interface Test { }
jjg@1413 52
jjg@1413 53 /** Invoke all methods annotated with @Test. */
jjg@1413 54 protected void run() throws Exception {
jjg@1413 55 for (Method m: getClass().getDeclaredMethods()) {
jjg@1413 56 Annotation a = m.getAnnotation(Test.class);
jjg@1413 57 if (a != null) {
jjg@1413 58 testCount++;
jjg@1413 59 testName = m.getName();
jjg@1413 60 System.err.println("test: " + testName);
jjg@1413 61 try {
jjg@1413 62 m.invoke(this, new Object[] { });
jjg@1413 63 } catch (InvocationTargetException e) {
jjg@1413 64 Throwable cause = e.getCause();
jjg@1413 65 throw (cause instanceof Exception) ? ((Exception) cause) : e;
jjg@1413 66 }
jjg@1413 67 System.err.println();
jjg@1413 68 }
jjg@1413 69 }
jjg@1413 70
jjg@1413 71 if (testCount == 0)
jjg@1413 72 error("no tests found");
jjg@1413 73
jjg@1413 74 StringBuilder summary = new StringBuilder();
jjg@1413 75 if (testCount != 1)
jjg@1413 76 summary.append(testCount).append(" tests");
jjg@1413 77 if (errorCount > 0) {
jjg@1413 78 if (summary.length() > 0) summary.append(", ");
jjg@1413 79 summary.append(errorCount).append(" errors");
jjg@1413 80 }
jjg@1413 81 System.err.println(summary);
jjg@1413 82 if (errorCount > 0)
jjg@1413 83 throw new Exception(errorCount + " errors found");
jjg@1413 84 }
jjg@1413 85
jjg@1413 86 /**
jjg@1413 87 * Create a directory in which to store generated doc files.
jjg@1413 88 * Avoid using the default (current) directory, so that we can
jjg@1413 89 * be sure that javadoc is writing in the intended location,
jjg@1413 90 * not a default location.
jjg@1413 91 */
jjg@1413 92 protected File getOutDir() {
jjg@1413 93 File dir = new File(testName);
jjg@1413 94 dir.mkdirs();
jjg@1413 95 return dir;
jjg@1413 96 }
jjg@1413 97
jjg@1413 98 /**
jjg@1413 99 * Create a directory in which to store generated doc files.
jjg@1413 100 * Avoid using the default (current) directory, so that we can
jjg@1413 101 * be sure that javadoc is writing in the intended location,
jjg@1413 102 * not a default location.
jjg@1413 103 */
jjg@1413 104 protected File getOutDir(String path) {
jjg@1413 105 File dir = new File(testName, path);
jjg@1413 106 dir.mkdirs();
jjg@1413 107 return dir;
jjg@1413 108 }
jjg@1413 109
jjg@1413 110 protected JavaFileObject createSimpleJavaFileObject() {
jjg@1413 111 return createSimpleJavaFileObject("pkg/C", "package pkg; public class C { }");
jjg@1413 112 }
jjg@1413 113
jjg@1413 114 protected JavaFileObject createSimpleJavaFileObject(final String binaryName, final String content) {
jjg@1413 115 return new SimpleJavaFileObject(
jjg@1413 116 URI.create("myfo:///" + binaryName + ".java"), JavaFileObject.Kind.SOURCE) {
jjg@1413 117 @Override
jjg@1413 118 public CharSequence getCharContent(boolean ignoreEncoding) {
jjg@1413 119 return content;
jjg@1413 120 }
jjg@1413 121 };
jjg@1413 122 }
jjg@1413 123
jjg@1413 124 protected void checkFiles(File dir, Set<String> expectFiles) {
jjg@1413 125 Set<File> files = new HashSet<File>();
jjg@1413 126 listFiles(dir, files);
jjg@1413 127 Set<String> foundFiles = new HashSet<String>();
jjg@1413 128 URI dirURI = dir.toURI();
jjg@1413 129 for (File f: files)
jjg@1413 130 foundFiles.add(dirURI.relativize(f.toURI()).getPath());
jjg@1413 131 checkFiles(foundFiles, expectFiles, dir);
jjg@1413 132 }
jjg@1413 133
jjg@1413 134 protected void checkFiles(Path dir, Set<String> expectFiles) throws IOException {
jjg@1413 135 Set<Path> files = new HashSet<Path>();
jjg@1413 136 listFiles(dir, files);
jjg@1413 137 Set<String> foundFiles = new HashSet<String>();
jjg@1413 138 for (Path f: files) {
jjg@1413 139 foundFiles.add(dir.relativize(f).toString().replace(f.getFileSystem().getSeparator(), "/"));
jjg@1413 140 }
jjg@1413 141 checkFiles(foundFiles, expectFiles, dir);
jjg@1413 142 }
jjg@1413 143
jjg@1413 144 private void checkFiles(Set<String> foundFiles, Set<String> expectFiles, Object where) {
jjg@1413 145 if (!foundFiles.equals(expectFiles)) {
jjg@1413 146 Set<String> missing = new TreeSet<String>(expectFiles);
jjg@1413 147 missing.removeAll(foundFiles);
jjg@1413 148 if (!missing.isEmpty())
jjg@1413 149 error("the following files were not found in " + where + ": " + missing);
jjg@1413 150 Set<String> unexpected = new TreeSet<String>(foundFiles);
jjg@1413 151 unexpected.removeAll(expectFiles);
jjg@1413 152 if (!unexpected.isEmpty())
jjg@1413 153 error("the following unexpected files were found in " + where + ": " + unexpected);
jjg@1413 154 }
jjg@1413 155 }
jjg@1413 156
jjg@1413 157 protected void listFiles(File dir, Set<File> files) {
jjg@1413 158 for (File f: dir.listFiles()) {
jjg@1413 159 if (f.isDirectory())
jjg@1413 160 listFiles(f, files);
jjg@1413 161 else if (f.isFile())
jjg@1413 162 files.add(f);
jjg@1413 163 }
jjg@1413 164 }
jjg@1413 165
jjg@1413 166 private void listFiles(Path dir, Set<Path> files) throws IOException {
jjg@1413 167 for (Path f: Files.newDirectoryStream(dir)) {
jjg@1413 168 if (Files.isDirectory(f))
jjg@1413 169 listFiles(f, files);
jjg@1413 170 else if (Files.isRegularFile(f))
jjg@1413 171 files.add(f);
jjg@1413 172 }
jjg@1413 173 }
jjg@1413 174
jjg@1413 175 protected void error(String msg) {
jjg@1413 176 System.err.println("Error: " + msg);
jjg@1413 177 errorCount++;
jjg@1413 178 }
jjg@1413 179
jjg@1413 180 protected int testCount;
jjg@1413 181 protected int errorCount;
jjg@1413 182
jjg@1413 183 protected String testName;
jjg@1413 184
jjg@1413 185 /**
jjg@1413 186 * Standard files generated by processing a documented class pkg.C.
jjg@1413 187 */
jjg@1413 188 protected static Set<String> standardExpectFiles = new HashSet<String>(Arrays.asList(
jjg@1413 189 "allclasses-frame.html",
jjg@1413 190 "allclasses-noframe.html",
jjg@1413 191 "constant-values.html",
jjg@1413 192 "deprecated-list.html",
jjg@1413 193 "help-doc.html",
jjg@1413 194 "index-all.html",
jjg@1413 195 "index.html",
jjg@1413 196 "overview-tree.html",
jjg@1413 197 "package-list",
jjg@1413 198 "pkg/C.html",
jjg@1413 199 "pkg/package-frame.html",
jjg@1413 200 "pkg/package-summary.html",
jjg@1413 201 "pkg/package-tree.html",
jjg@1413 202 "resources/background.gif",
jjg@1413 203 "resources/tab.gif",
jjg@1413 204 "resources/titlebar_end.gif",
jjg@1413 205 "resources/titlebar.gif",
jjg@1413 206 "stylesheet.css"
jjg@1413 207 ));
jjg@1413 208 }
jjg@1413 209

mercurial