Path: blob/master/test/langtools/jdk/javadoc/tool/api/basic/APITest.java
40983 views
/*1* Copyright (c) 2012, 2020, 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;38import java.util.stream.*;3940import javax.tools.JavaFileObject;41import javax.tools.SimpleJavaFileObject;424344/*45* Superclass with utility methods for API tests.46*/47class APITest {48protected APITest() { }4950/** Marker annotation for test cases. */51@Retention(RetentionPolicy.RUNTIME)52@interface Test { }5354/** Invoke all methods annotated with @Test. */55protected void run() throws Exception {56for (Method m: getClass().getDeclaredMethods()) {57Annotation a = m.getAnnotation(Test.class);58if (a != null) {59testCount++;60testName = m.getName();61System.err.println("test: " + testName);62try {63m.invoke(this, new Object[] { });64} catch (InvocationTargetException e) {65Throwable cause = e.getCause();66throw (cause instanceof Exception) ? ((Exception) cause) : e;67}68System.err.println();69}70}7172if (testCount == 0)73error("no tests found");7475StringBuilder summary = new StringBuilder();76if (testCount != 1)77summary.append(testCount).append(" tests");78if (errorCount > 0) {79if (summary.length() > 0) summary.append(", ");80summary.append(errorCount).append(" errors");81}82System.err.println(summary);83if (errorCount > 0)84throw new Exception(errorCount + " errors found");85}8687/**88* Create a directory in which to store generated doc files.89* Avoid using the default (current) directory, so that we can90* be sure that javadoc is writing in the intended location,91* not a default location.92*/93protected File getOutDir() {94File dir = new File(testName);95dir.mkdirs();96return dir;97}9899/**100* Create a directory in which to store generated doc files.101* Avoid using the default (current) directory, so that we can102* be sure that javadoc is writing in the intended location,103* not a default location.104*/105protected File getOutDir(String path) {106File dir = new File(testName, path);107dir.mkdirs();108return dir;109}110111protected JavaFileObject createSimpleJavaFileObject() {112return createSimpleJavaFileObject("pkg/C",113"package pkg; public class C { @Deprecated public static final int ZERO = 0; }");114}115116protected JavaFileObject createSimpleJavaFileObject(final String binaryName, final String content) {117return new SimpleJavaFileObject(118URI.create("myfo:///" + binaryName + ".java"), JavaFileObject.Kind.SOURCE) {119@Override120public CharSequence getCharContent(boolean ignoreEncoding) {121return content;122}123};124}125126protected void checkFiles(File dir, Set<String> expectFiles) {127Set<File> files = new HashSet<File>();128listFiles(dir, files);129Set<String> foundFiles = new HashSet<String>();130URI dirURI = dir.toURI();131for (File f: files)132foundFiles.add(dirURI.relativize(f.toURI()).getPath());133checkFiles(foundFiles, expectFiles, dir);134}135136protected void checkFiles(Path dir, Set<String> expectFiles) throws IOException {137Set<Path> files = new HashSet<Path>();138listFiles(dir, files);139Set<String> foundFiles = new HashSet<String>();140for (Path f: files) {141foundFiles.add(dir.relativize(f).toString().replace(f.getFileSystem().getSeparator(), "/"));142}143checkFiles(foundFiles, expectFiles, dir);144}145146private void checkFiles(Set<String> foundFiles, Set<String> expectFiles, Object where) {147if (!foundFiles.equals(expectFiles)) {148Set<String> missing = new TreeSet<String>(expectFiles);149missing.removeAll(foundFiles);150if (!missing.isEmpty())151error("the following files were not found in " + where + ": " + missing);152Set<String> unexpected = foundFiles.stream()153.filter(p -> !p.startsWith("legal"))154.collect(Collectors.toCollection(TreeSet::new));155unexpected.removeAll(expectFiles);156if (!unexpected.isEmpty())157error("the following unexpected files were found in " + where + ": " + unexpected);158}159}160161protected void listFiles(File dir, Set<File> files) {162for (File f: dir.listFiles()) {163if (f.isDirectory())164listFiles(f, files);165else if (f.isFile())166files.add(f);167}168}169170private void listFiles(Path dir, Set<Path> files) throws IOException {171try (DirectoryStream<Path> ds = Files.newDirectoryStream(dir)) {172for (Path f: ds) {173if (Files.isDirectory(f))174listFiles(f, files);175else if (Files.isRegularFile(f))176files.add(f);177}178}179}180181protected void error(String msg) {182System.err.println("Error: " + msg);183errorCount++;184}185186protected int testCount;187protected int errorCount;188189protected String testName;190191/**192* Standard files generated by processing a documented class pkg.C.193*/194protected static Set<String> standardExpectFiles = new HashSet<>(Arrays.asList(195"allclasses-index.html",196"allpackages-index.html",197"constant-values.html",198"deprecated-list.html",199"help-doc.html",200"index-all.html",201"index.html",202"script-dir/jquery-3.5.1.min.js",203"script-dir/jquery-ui.min.js",204"script-dir/jquery-ui.min.css",205"script-dir/jquery-ui.structure.min.css",206"script-dir/images/ui-bg_glass_65_dadada_1x400.png",207"script-dir/images/ui-icons_454545_256x240.png",208"script-dir/images/ui-bg_glass_95_fef1ec_1x400.png",209"script-dir/images/ui-bg_glass_75_dadada_1x400.png",210"script-dir/images/ui-bg_highlight-soft_75_cccccc_1x100.png",211"script-dir/images/ui-icons_888888_256x240.png",212"script-dir/images/ui-icons_2e83ff_256x240.png",213"script-dir/images/ui-icons_cd0a0a_256x240.png",214"script-dir/images/ui-bg_glass_55_fbf9ee_1x400.png",215"script-dir/images/ui-icons_222222_256x240.png",216"script-dir/images/ui-bg_glass_75_e6e6e6_1x400.png",217"member-search-index.js",218"module-search-index.js",219"overview-tree.html",220"element-list",221"package-search-index.js",222"pkg/C.html",223"pkg/package-summary.html",224"pkg/package-tree.html",225"resources/glass.png",226"resources/x.png",227"script.js",228"search.js",229"jquery-ui.overrides.css",230"stylesheet.css",231"tag-search-index.js",232"type-search-index.js"233));234235protected static Set<String> noIndexFiles = standardExpectFiles.stream()236.filter(s -> !s.startsWith("script-dir")237&& !s.startsWith("resources")238&& !s.endsWith("-search-index.js")239&& !s.equals("index-all.html")240&& !s.equals("search.js")241&& !s.equals("jquery-ui.overrides.css")242&& !s.equals("allclasses-index.html")243&& !s.equals("allpackages-index.html")244&& !s.equals("system-properties.html"))245.collect(Collectors.toSet());246}247248249250