Thu, 31 Aug 2017 15:17:03 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | */ |
aoqi@0 | 23 | |
aoqi@0 | 24 | import java.io.*; |
aoqi@0 | 25 | import java.util.*; |
aoqi@0 | 26 | import java.lang.annotation.*; |
aoqi@0 | 27 | import java.lang.reflect.InvocationTargetException; |
aoqi@0 | 28 | |
aoqi@0 | 29 | /** |
aoqi@0 | 30 | * {@code Tester} is an abstract test-driver that provides the logic |
aoqi@0 | 31 | * to execute test-cases, grouped by test classes. |
aoqi@0 | 32 | * A test class is a main class extending this class, that instantiate |
aoqi@0 | 33 | * itself, and calls the {@link run} method, passing any command line |
aoqi@0 | 34 | * arguments. |
aoqi@0 | 35 | * <p> |
aoqi@0 | 36 | * The {@code run} method, expects arguments to identify test-case classes. |
aoqi@0 | 37 | * A test-case class is a class extending the test class, and annotated |
aoqi@0 | 38 | * with {@code TestCase}. |
aoqi@0 | 39 | * <p> |
aoqi@0 | 40 | * If no test-cases are specified, the test class directory is searched for |
aoqi@0 | 41 | * co-located test-case classes (i.e. any class extending the test class, |
aoqi@0 | 42 | * annotated with {@code TestCase}). |
aoqi@0 | 43 | * <p> |
aoqi@0 | 44 | * Besides serving to group test-cases, extending the driver allow |
aoqi@0 | 45 | * setting up a test-case template, and possibly overwrite default |
aoqi@0 | 46 | * test-driver behaviour. |
aoqi@0 | 47 | */ |
aoqi@0 | 48 | public abstract class Tester { |
aoqi@0 | 49 | |
aoqi@0 | 50 | private static boolean debug = false; |
aoqi@0 | 51 | private static final PrintStream out = System.err; |
aoqi@0 | 52 | private static final PrintStream err = System.err; |
aoqi@0 | 53 | |
aoqi@0 | 54 | |
aoqi@0 | 55 | protected void run(String... args) throws Exception { |
aoqi@0 | 56 | |
aoqi@0 | 57 | final File classesdir = new File(System.getProperty("test.classes", ".")); |
aoqi@0 | 58 | |
aoqi@0 | 59 | String[] classNames = args; |
aoqi@0 | 60 | |
aoqi@0 | 61 | // If no test-cases are specified, we regard all co-located classes |
aoqi@0 | 62 | // as potential test-cases. |
aoqi@0 | 63 | if (args.length == 0) { |
aoqi@0 | 64 | final String pattern = ".*\\.class"; |
aoqi@0 | 65 | final File classFiles[] = classesdir.listFiles(new FileFilter() { |
aoqi@0 | 66 | public boolean accept(File f) { |
aoqi@0 | 67 | return f.getName().matches(pattern); |
aoqi@0 | 68 | } |
aoqi@0 | 69 | }); |
aoqi@0 | 70 | ArrayList<String> names = new ArrayList<String>(classFiles.length); |
aoqi@0 | 71 | for (File f : classFiles) { |
aoqi@0 | 72 | String fname = f.getName(); |
aoqi@0 | 73 | names.add(fname.substring(0, fname.length() -6)); |
aoqi@0 | 74 | } |
aoqi@0 | 75 | classNames = names.toArray(new String[names.size()]); |
aoqi@0 | 76 | } else { |
aoqi@0 | 77 | debug = true; |
aoqi@0 | 78 | } |
aoqi@0 | 79 | // Test-cases must extend the driver type, and be marked |
aoqi@0 | 80 | // @TestCase. Other arguments (classes) are ignored. |
aoqi@0 | 81 | // Test-cases are instantiated, and thereby executed. |
aoqi@0 | 82 | for (String clname : classNames) { |
aoqi@0 | 83 | try { |
aoqi@0 | 84 | final Class tclass = Class.forName(clname); |
aoqi@0 | 85 | if (!getClass().isAssignableFrom(tclass)) continue; |
aoqi@0 | 86 | TestCase anno = (TestCase) tclass.getAnnotation(TestCase.class); |
aoqi@0 | 87 | if (anno == null) continue; |
aoqi@0 | 88 | if (!debug) { |
aoqi@0 | 89 | ignore i = (ignore) tclass.getAnnotation(ignore.class); |
aoqi@0 | 90 | if (i != null) { |
aoqi@0 | 91 | out.println("Ignore: " + clname); |
aoqi@0 | 92 | ignored++; |
aoqi@0 | 93 | continue; |
aoqi@0 | 94 | } |
aoqi@0 | 95 | } |
aoqi@0 | 96 | out.println("TestCase: " + clname); |
aoqi@0 | 97 | cases++; |
aoqi@0 | 98 | Tester tc = (Tester) tclass.getConstructor().newInstance(); |
aoqi@0 | 99 | if (tc.errors > 0) { |
aoqi@0 | 100 | error("" + tc.errors + " test points failed in " + clname); |
aoqi@0 | 101 | errors += tc.errors - 1; |
aoqi@0 | 102 | fcases++; |
aoqi@0 | 103 | } |
aoqi@0 | 104 | } catch(ReflectiveOperationException roe) { |
aoqi@0 | 105 | error("Warning: " + clname + " - ReflectiveOperationException"); |
aoqi@0 | 106 | roe.printStackTrace(err); |
aoqi@0 | 107 | } catch(Exception unknown) { |
aoqi@0 | 108 | error("Warning: " + clname + " - uncaught exception"); |
aoqi@0 | 109 | unknown.printStackTrace(err); |
aoqi@0 | 110 | } |
aoqi@0 | 111 | } |
aoqi@0 | 112 | |
aoqi@0 | 113 | String imsg = ignored > 0 ? " (" + ignored + " ignored)" : ""; |
aoqi@0 | 114 | if (errors > 0) |
aoqi@0 | 115 | throw new Error(errors + " error, in " + fcases + " of " + cases + " test-cases" + imsg); |
aoqi@0 | 116 | else |
aoqi@0 | 117 | err.println("" + cases + " test-cases executed" + imsg + ", no errors"); |
aoqi@0 | 118 | } |
aoqi@0 | 119 | |
aoqi@0 | 120 | |
aoqi@0 | 121 | /** |
aoqi@0 | 122 | * Test-cases must be marked with the {@code TestCase} annotation, |
aoqi@0 | 123 | * as well as extend {@code Tester} (or an driver extension |
aoqi@0 | 124 | * specified as the first argument to the {@code main()} method. |
aoqi@0 | 125 | */ |
aoqi@0 | 126 | @Retention(RetentionPolicy.RUNTIME) |
aoqi@0 | 127 | @interface TestCase { } |
aoqi@0 | 128 | |
aoqi@0 | 129 | /** |
aoqi@0 | 130 | * Individual test-cases failing due to product bugs, may temporarily |
aoqi@0 | 131 | * be excluded by marking them like this, (where "at-" is replaced by "@") |
aoqi@0 | 132 | * at-ignore // 1234567: bug synopsis |
aoqi@0 | 133 | */ |
aoqi@0 | 134 | @Retention(RetentionPolicy.RUNTIME) |
aoqi@0 | 135 | @interface ignore { } |
aoqi@0 | 136 | |
aoqi@0 | 137 | /** |
aoqi@0 | 138 | * Test-cases are classes extending {@code Tester}, and |
aoqi@0 | 139 | * calling {@link setSrc}, followed by one or more invocations |
aoqi@0 | 140 | * of {@link verify} in the body of the constructor. |
aoqi@0 | 141 | * <p> |
aoqi@0 | 142 | * Sets a default test-case template, which is empty except |
aoqi@0 | 143 | * for a key of {@code "TESTCASE"}. |
aoqi@0 | 144 | * Subclasses will typically call {@code setSrc(TestSource)} |
aoqi@0 | 145 | * to setup a useful test-case template. |
aoqi@0 | 146 | */ |
aoqi@0 | 147 | public Tester() { |
aoqi@0 | 148 | this.testCase = this.getClass().getName(); |
aoqi@0 | 149 | src = new TestSource("TESTCASE"); |
aoqi@0 | 150 | } |
aoqi@0 | 151 | |
aoqi@0 | 152 | /** |
aoqi@0 | 153 | * Set the top-level source template. |
aoqi@0 | 154 | */ |
aoqi@0 | 155 | protected Tester setSrc(TestSource src) { |
aoqi@0 | 156 | this.src = src; |
aoqi@0 | 157 | return this; |
aoqi@0 | 158 | } |
aoqi@0 | 159 | |
aoqi@0 | 160 | /** |
aoqi@0 | 161 | * Convenience method for calling {@code innerSrc("TESTCASE", ...)}. |
aoqi@0 | 162 | */ |
aoqi@0 | 163 | protected Tester setSrc(String... lines) { |
aoqi@0 | 164 | return innerSrc("TESTCASE", lines); |
aoqi@0 | 165 | } |
aoqi@0 | 166 | |
aoqi@0 | 167 | /** |
aoqi@0 | 168 | * Convenience method for calling {@code innerSrc(key, new TestSource(...))}. |
aoqi@0 | 169 | */ |
aoqi@0 | 170 | protected Tester innerSrc(String key, String... lines) { |
aoqi@0 | 171 | return innerSrc(key, new TestSource(lines)); |
aoqi@0 | 172 | } |
aoqi@0 | 173 | |
aoqi@0 | 174 | /** |
aoqi@0 | 175 | * Specialize the testcase template, setting replacement content |
aoqi@0 | 176 | * for the specified key. |
aoqi@0 | 177 | */ |
aoqi@0 | 178 | protected Tester innerSrc(String key, TestSource content) { |
aoqi@0 | 179 | if (src == null) { |
aoqi@0 | 180 | src = new TestSource(key); |
aoqi@0 | 181 | } |
aoqi@0 | 182 | src.setInner(key, content); |
aoqi@0 | 183 | return this; |
aoqi@0 | 184 | } |
aoqi@0 | 185 | |
aoqi@0 | 186 | /** |
aoqi@0 | 187 | * On the first invocation, call {@code execute()} to compile |
aoqi@0 | 188 | * the test-case source and process the resulting class(se) |
aoqi@0 | 189 | * into verifiable output. |
aoqi@0 | 190 | * <p> |
aoqi@0 | 191 | * Verify that the output matches each of the regular expressions |
aoqi@0 | 192 | * given as argument. |
aoqi@0 | 193 | * <p> |
aoqi@0 | 194 | * Any failure to match constitutes a test failure, but doesn't |
aoqi@0 | 195 | * abort the test-case. |
aoqi@0 | 196 | * <p> |
aoqi@0 | 197 | * Any exception (e.g. bad regular expression syntax) results in |
aoqi@0 | 198 | * a test failure, and aborts the test-case. |
aoqi@0 | 199 | */ |
aoqi@0 | 200 | protected void verify(String... expect) { |
aoqi@0 | 201 | if (!didExecute) { |
aoqi@0 | 202 | try { |
aoqi@0 | 203 | execute(); |
aoqi@0 | 204 | } catch(Exception ue) { |
aoqi@0 | 205 | throw new Error(ue); |
aoqi@0 | 206 | } finally { |
aoqi@0 | 207 | didExecute = true; |
aoqi@0 | 208 | } |
aoqi@0 | 209 | } |
aoqi@0 | 210 | if (output == null) { |
aoqi@0 | 211 | error("output is null"); |
aoqi@0 | 212 | return; |
aoqi@0 | 213 | } |
aoqi@0 | 214 | for (String e: expect) { |
aoqi@0 | 215 | // Escape regular expressions (to allow input to be literals). |
aoqi@0 | 216 | // Notice, characters to be escaped are themselves identified |
aoqi@0 | 217 | // using regular expressions |
aoqi@0 | 218 | String rc[] = { "(", ")", "[", "]", "{", "}", "$" }; |
aoqi@0 | 219 | for (String c : rc) { |
aoqi@0 | 220 | e = e.replace(c, "\\" + c); |
aoqi@0 | 221 | } |
aoqi@0 | 222 | // DEBUG: Uncomment this to test modulo constant pool index. |
aoqi@0 | 223 | // e = e.replaceAll("#[0-9]{2}", "#[0-9]{2}"); |
aoqi@0 | 224 | if (!output.matches("(?s).*" + e + ".*")) { |
aoqi@0 | 225 | if (!didPrint) { |
aoqi@0 | 226 | out.println(output); |
aoqi@0 | 227 | didPrint = true; |
aoqi@0 | 228 | } |
aoqi@0 | 229 | error("not matched: '" + e + "'"); |
aoqi@0 | 230 | } else if(debug) { |
aoqi@0 | 231 | out.println("matched: '" + e + "'"); |
aoqi@0 | 232 | } |
aoqi@0 | 233 | } |
aoqi@0 | 234 | } |
aoqi@0 | 235 | |
aoqi@0 | 236 | /** |
aoqi@0 | 237 | * Calls {@code writeTestFile()} to write out the test-case source |
aoqi@0 | 238 | * content to a file, then call {@code compileTestFile()} to |
aoqi@0 | 239 | * compile it, and finally run the {@link process} method to produce |
aoqi@0 | 240 | * verifiable output. The default {@code process} method runs javap. |
aoqi@0 | 241 | * <p> |
aoqi@0 | 242 | * If an exception occurs, it results in a test failure, and |
aoqi@0 | 243 | * aborts the test-case. |
aoqi@0 | 244 | */ |
aoqi@0 | 245 | protected void execute() throws IOException { |
aoqi@0 | 246 | err.println("TestCase: " + testCase); |
aoqi@0 | 247 | writeTestFile(); |
aoqi@0 | 248 | compileTestFile(); |
aoqi@0 | 249 | process(); |
aoqi@0 | 250 | } |
aoqi@0 | 251 | |
aoqi@0 | 252 | /** |
aoqi@0 | 253 | * Generate java source from test-case. |
aoqi@0 | 254 | * TBD: change to use javaFileObject, possibly make |
aoqi@0 | 255 | * this class extend JavaFileObject. |
aoqi@0 | 256 | */ |
aoqi@0 | 257 | protected void writeTestFile() throws IOException { |
aoqi@0 | 258 | javaFile = new File("Test.java"); |
aoqi@0 | 259 | FileWriter fw = new FileWriter(javaFile); |
aoqi@0 | 260 | BufferedWriter bw = new BufferedWriter(fw); |
aoqi@0 | 261 | PrintWriter pw = new PrintWriter(bw); |
aoqi@0 | 262 | for (String line : src) { |
aoqi@0 | 263 | pw.println(line); |
aoqi@0 | 264 | if (debug) out.println(line); |
aoqi@0 | 265 | } |
aoqi@0 | 266 | pw.close(); |
aoqi@0 | 267 | } |
aoqi@0 | 268 | |
aoqi@0 | 269 | /** |
aoqi@0 | 270 | * Compile the Java source code. |
aoqi@0 | 271 | */ |
aoqi@0 | 272 | protected void compileTestFile() { |
aoqi@0 | 273 | String path = javaFile.getPath(); |
aoqi@0 | 274 | String params[] = { "-source", "1.8", "-g", path }; |
aoqi@0 | 275 | int rc = com.sun.tools.javac.Main.compile(params); |
aoqi@0 | 276 | if (rc != 0) |
aoqi@0 | 277 | throw new Error("compilation failed. rc=" + rc); |
aoqi@0 | 278 | classFile = new File(path.substring(0, path.length() - 5) + ".class"); |
aoqi@0 | 279 | } |
aoqi@0 | 280 | |
aoqi@0 | 281 | |
aoqi@0 | 282 | /** |
aoqi@0 | 283 | * Process class file to generate output for verification. |
aoqi@0 | 284 | * The default implementation simply runs javap. This might be |
aoqi@0 | 285 | * overwritten to generate output in a different manner. |
aoqi@0 | 286 | */ |
aoqi@0 | 287 | protected void process() { |
aoqi@0 | 288 | String testClasses = "."; //System.getProperty("test.classes", "."); |
aoqi@0 | 289 | StringWriter sw = new StringWriter(); |
aoqi@0 | 290 | PrintWriter pw = new PrintWriter(sw); |
aoqi@0 | 291 | String[] args = { "-v", "-classpath", testClasses, "Test" }; |
aoqi@0 | 292 | int rc = com.sun.tools.javap.Main.run(args, pw); |
aoqi@0 | 293 | if (rc != 0) |
aoqi@0 | 294 | throw new Error("javap failed. rc=" + rc); |
aoqi@0 | 295 | pw.close(); |
aoqi@0 | 296 | output = sw.toString(); |
aoqi@0 | 297 | if (debug) { |
aoqi@0 | 298 | out.println(output); |
aoqi@0 | 299 | didPrint = true; |
aoqi@0 | 300 | } |
aoqi@0 | 301 | |
aoqi@0 | 302 | } |
aoqi@0 | 303 | |
aoqi@0 | 304 | |
aoqi@0 | 305 | private String testCase; |
aoqi@0 | 306 | private TestSource src; |
aoqi@0 | 307 | private File javaFile = null; |
aoqi@0 | 308 | private File classFile = null; |
aoqi@0 | 309 | private String output = null; |
aoqi@0 | 310 | private boolean didExecute = false; |
aoqi@0 | 311 | private boolean didPrint = false; |
aoqi@0 | 312 | |
aoqi@0 | 313 | |
aoqi@0 | 314 | protected void error(String msg) { |
aoqi@0 | 315 | err.println("Error: " + msg); |
aoqi@0 | 316 | errors++; |
aoqi@0 | 317 | } |
aoqi@0 | 318 | |
aoqi@0 | 319 | private int cases; |
aoqi@0 | 320 | private int fcases; |
aoqi@0 | 321 | private int errors; |
aoqi@0 | 322 | private int ignored; |
aoqi@0 | 323 | |
aoqi@0 | 324 | /** |
aoqi@0 | 325 | * The TestSource class provides a simple container for |
aoqi@0 | 326 | * test cases. It contains an array of source code lines, |
aoqi@0 | 327 | * where zero or more lines may be markers for nested lines. |
aoqi@0 | 328 | * This allows representing templates, with specialization. |
aoqi@0 | 329 | * <P> |
aoqi@0 | 330 | * This may be generalized to support more advance combo |
aoqi@0 | 331 | * tests, but presently it's only used with a static template, |
aoqi@0 | 332 | * and one level of specialization. |
aoqi@0 | 333 | */ |
aoqi@0 | 334 | public class TestSource implements Iterable<String> { |
aoqi@0 | 335 | |
aoqi@0 | 336 | private String[] lines; |
aoqi@0 | 337 | private Hashtable<String, TestSource> innerSrc; |
aoqi@0 | 338 | |
aoqi@0 | 339 | public TestSource(String... lines) { |
aoqi@0 | 340 | this.lines = lines; |
aoqi@0 | 341 | innerSrc = new Hashtable<String, TestSource>(); |
aoqi@0 | 342 | } |
aoqi@0 | 343 | |
aoqi@0 | 344 | public void setInner(String key, TestSource inner) { |
aoqi@0 | 345 | innerSrc.put(key, inner); |
aoqi@0 | 346 | } |
aoqi@0 | 347 | |
aoqi@0 | 348 | public void setInner(String key, String... lines) { |
aoqi@0 | 349 | innerSrc.put(key, new TestSource(lines)); |
aoqi@0 | 350 | } |
aoqi@0 | 351 | |
aoqi@0 | 352 | public Iterator<String> iterator() { |
aoqi@0 | 353 | return new LineIterator(); |
aoqi@0 | 354 | } |
aoqi@0 | 355 | |
aoqi@0 | 356 | private class LineIterator implements Iterator<String> { |
aoqi@0 | 357 | |
aoqi@0 | 358 | int nextLine = 0; |
aoqi@0 | 359 | Iterator<String> innerIt = null; |
aoqi@0 | 360 | |
aoqi@0 | 361 | public boolean hasNext() { |
aoqi@0 | 362 | return nextLine < lines.length; |
aoqi@0 | 363 | } |
aoqi@0 | 364 | |
aoqi@0 | 365 | public String next() { |
aoqi@0 | 366 | if (!hasNext()) throw new NoSuchElementException(); |
aoqi@0 | 367 | String str = lines[nextLine]; |
aoqi@0 | 368 | TestSource inner = innerSrc.get(str); |
aoqi@0 | 369 | if (inner == null) { |
aoqi@0 | 370 | nextLine++; |
aoqi@0 | 371 | return str; |
aoqi@0 | 372 | } |
aoqi@0 | 373 | if (innerIt == null) { |
aoqi@0 | 374 | innerIt = inner.iterator(); |
aoqi@0 | 375 | } |
aoqi@0 | 376 | if (innerIt.hasNext()) { |
aoqi@0 | 377 | return innerIt.next(); |
aoqi@0 | 378 | } |
aoqi@0 | 379 | innerIt = null; |
aoqi@0 | 380 | nextLine++; |
aoqi@0 | 381 | return next(); |
aoqi@0 | 382 | } |
aoqi@0 | 383 | |
aoqi@0 | 384 | public void remove() { |
aoqi@0 | 385 | throw new UnsupportedOperationException(); |
aoqi@0 | 386 | } |
aoqi@0 | 387 | } |
aoqi@0 | 388 | } |
aoqi@0 | 389 | } |