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

Tue, 06 Jun 2017 10:03:06 -0700

author
asaha
date
Tue, 06 Jun 2017 10:03:06 -0700
changeset 3395
ec280466843d
parent 2225
b8ebde062692
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8u141-b12 for changeset b5259d2465fa

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

mercurial