Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javadoc/api/basic/APITest.java
38828 views
/*1* Copyright (c) 2012, 2013, 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*/2223import java.io.File;24import java.io.IOException;25import java.lang.annotation.Annotation;26import java.lang.annotation.Retention;27import java.lang.annotation.RetentionPolicy;28import java.lang.reflect.InvocationTargetException;29import java.lang.reflect.Method;30import java.net.URI;31import java.nio.file.DirectoryStream;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;3839import javax.tools.JavaFileObject;40import javax.tools.SimpleJavaFileObject;414243/*44* Superclass with utility methods for API tests.45*/46class APITest {47protected APITest() { }4849/** Marker annotation for test cases. */50@Retention(RetentionPolicy.RUNTIME)51@interface Test { }5253/** Invoke all methods annotated with @Test. */54protected void run() throws Exception {55for (Method m: getClass().getDeclaredMethods()) {56Annotation a = m.getAnnotation(Test.class);57if (a != null) {58testCount++;59testName = m.getName();60System.err.println("test: " + testName);61try {62m.invoke(this, new Object[] { });63} catch (InvocationTargetException e) {64Throwable cause = e.getCause();65throw (cause instanceof Exception) ? ((Exception) cause) : e;66}67System.err.println();68}69}7071if (testCount == 0)72error("no tests found");7374StringBuilder summary = new StringBuilder();75if (testCount != 1)76summary.append(testCount).append(" tests");77if (errorCount > 0) {78if (summary.length() > 0) summary.append(", ");79summary.append(errorCount).append(" errors");80}81System.err.println(summary);82if (errorCount > 0)83throw new Exception(errorCount + " errors found");84}8586/**87* Create a directory in which to store generated doc files.88* Avoid using the default (current) directory, so that we can89* be sure that javadoc is writing in the intended location,90* not a default location.91*/92protected File getOutDir() {93File dir = new File(testName);94dir.mkdirs();95return dir;96}9798/**99* Create a directory in which to store generated doc files.100* Avoid using the default (current) directory, so that we can101* be sure that javadoc is writing in the intended location,102* not a default location.103*/104protected File getOutDir(String path) {105File dir = new File(testName, path);106dir.mkdirs();107return dir;108}109110protected JavaFileObject createSimpleJavaFileObject() {111return createSimpleJavaFileObject("pkg/C", "package pkg; public class C { }");112}113114protected JavaFileObject createSimpleJavaFileObject(final String binaryName, final String content) {115return new SimpleJavaFileObject(116URI.create("myfo:///" + binaryName + ".java"), JavaFileObject.Kind.SOURCE) {117@Override118public CharSequence getCharContent(boolean ignoreEncoding) {119return content;120}121};122}123124protected void checkFiles(File dir, Set<String> expectFiles) {125Set<File> files = new HashSet<File>();126listFiles(dir, files);127Set<String> foundFiles = new HashSet<String>();128URI dirURI = dir.toURI();129for (File f: files)130foundFiles.add(dirURI.relativize(f.toURI()).getPath());131checkFiles(foundFiles, expectFiles, dir);132}133134protected void checkFiles(Path dir, Set<String> expectFiles) throws IOException {135Set<Path> files = new HashSet<Path>();136listFiles(dir, files);137Set<String> foundFiles = new HashSet<String>();138for (Path f: files) {139foundFiles.add(dir.relativize(f).toString().replace(f.getFileSystem().getSeparator(), "/"));140}141checkFiles(foundFiles, expectFiles, dir);142}143144private void checkFiles(Set<String> foundFiles, Set<String> expectFiles, Object where) {145if (!foundFiles.equals(expectFiles)) {146Set<String> missing = new TreeSet<String>(expectFiles);147missing.removeAll(foundFiles);148if (!missing.isEmpty())149error("the following files were not found in " + where + ": " + missing);150Set<String> unexpected = new TreeSet<String>(foundFiles);151unexpected.removeAll(expectFiles);152if (!unexpected.isEmpty())153error("the following unexpected files were found in " + where + ": " + unexpected);154}155}156157protected void listFiles(File dir, Set<File> files) {158for (File f: dir.listFiles()) {159if (f.isDirectory())160listFiles(f, files);161else if (f.isFile())162files.add(f);163}164}165166private void listFiles(Path dir, Set<Path> files) throws IOException {167try (DirectoryStream<Path> ds = Files.newDirectoryStream(dir)) {168for (Path f: ds) {169if (Files.isDirectory(f))170listFiles(f, files);171else if (Files.isRegularFile(f))172files.add(f);173}174}175}176177protected void error(String msg) {178System.err.println("Error: " + msg);179errorCount++;180}181182protected int testCount;183protected int errorCount;184185protected String testName;186187/**188* Standard files generated by processing a documented class pkg.C.189*/190protected static Set<String> standardExpectFiles = new HashSet<String>(Arrays.asList(191"allclasses-frame.html",192"allclasses-noframe.html",193"constant-values.html",194"deprecated-list.html",195"help-doc.html",196"index-all.html",197"index.html",198"overview-tree.html",199"package-list",200"pkg/C.html",201"pkg/package-frame.html",202"pkg/package-summary.html",203"pkg/package-tree.html",204"script.js",205"stylesheet.css"206));207}208209210211