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