langtools/test/tools/lib/toolbox/TestRunner.java
author vromero
Sat, 17 Dec 2016 16:57:14 -0500
changeset 42837 03ee805ba348
parent 40308 274367a99f98
permissions -rw-r--r--
8169005: tools/javac/T8132562/ClassPathWithDoubleQuotesTest.java fails after fix to JDK-8132562 Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37750
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
     1
/*
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
     4
 *
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
     8
 *
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    13
 * accompanied this code).
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    14
 *
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    18
 *
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    21
 * questions.
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    22
 */
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    23
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    24
package toolbox;
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    25
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    26
import java.io.PrintStream;
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    27
import java.lang.annotation.Annotation;
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    28
import java.lang.annotation.Retention;
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    29
import java.lang.annotation.RetentionPolicy;
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    30
import java.lang.reflect.InvocationTargetException;
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    31
import java.lang.reflect.Method;
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    32
import java.util.function.Function;
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    33
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    34
/**
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    35
 * Utility class to manage and execute sub-tests within a test.
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    36
 *
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    37
 * This class does the following:
37758
3ecf9b414e05 8155774: move code from ModuleTestBase to toolbox
jjg
parents: 37750
diff changeset
    38
 * <ul>
3ecf9b414e05 8155774: move code from ModuleTestBase to toolbox
jjg
parents: 37750
diff changeset
    39
 * <li>  invokes those test methods annotated with @Test
3ecf9b414e05 8155774: move code from ModuleTestBase to toolbox
jjg
parents: 37750
diff changeset
    40
 * <li>  keeps track of successful and failed tests
3ecf9b414e05 8155774: move code from ModuleTestBase to toolbox
jjg
parents: 37750
diff changeset
    41
 * <li>  throws an Exception if any test fails.
3ecf9b414e05 8155774: move code from ModuleTestBase to toolbox
jjg
parents: 37750
diff changeset
    42
 * <li>  provides a test summary at the end of the run.
3ecf9b414e05 8155774: move code from ModuleTestBase to toolbox
jjg
parents: 37750
diff changeset
    43
 * </ul>
3ecf9b414e05 8155774: move code from ModuleTestBase to toolbox
jjg
parents: 37750
diff changeset
    44
37750
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    45
 * Tests must extend this class, annotate the test methods
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    46
 * with @Test and call one of the runTests method.
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    47
 */
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    48
public abstract class TestRunner {
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    49
   /** Marker annotation for test cases. */
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    50
    @Retention(RetentionPolicy.RUNTIME)
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    51
    public @interface Test { }
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    52
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    53
    int testCount = 0;
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    54
    int errorCount = 0;
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    55
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    56
    public String testName = null;
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    57
37758
3ecf9b414e05 8155774: move code from ModuleTestBase to toolbox
jjg
parents: 37750
diff changeset
    58
    protected PrintStream out;
37750
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    59
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    60
    /**
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    61
     * Constructs the Object.
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    62
     * @param out the PrintStream to print output to.
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    63
     */
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    64
    public TestRunner(PrintStream out) {
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    65
        this.out = out;
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    66
    }
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    67
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    68
    /**
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    69
     * Invoke all methods annotated with @Test.
37758
3ecf9b414e05 8155774: move code from ModuleTestBase to toolbox
jjg
parents: 37750
diff changeset
    70
     * @throws java.lang.Exception if any errors occur
37750
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    71
     */
37758
3ecf9b414e05 8155774: move code from ModuleTestBase to toolbox
jjg
parents: 37750
diff changeset
    72
    protected void runTests() throws Exception {
37750
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    73
        runTests(f -> new Object[0]);
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    74
    }
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    75
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    76
    /**
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    77
     * Invoke all methods annotated with @Test.
37758
3ecf9b414e05 8155774: move code from ModuleTestBase to toolbox
jjg
parents: 37750
diff changeset
    78
     * @param f a lambda expression to specify arguments for the test method
3ecf9b414e05 8155774: move code from ModuleTestBase to toolbox
jjg
parents: 37750
diff changeset
    79
     * @throws java.lang.Exception if any errors occur
37750
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    80
     */
37758
3ecf9b414e05 8155774: move code from ModuleTestBase to toolbox
jjg
parents: 37750
diff changeset
    81
    protected void runTests(Function<Method, Object[]> f) throws Exception {
37750
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    82
        for (Method m : getClass().getDeclaredMethods()) {
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    83
            Annotation a = m.getAnnotation(Test.class);
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    84
            if (a != null) {
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    85
                testName = m.getName();
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    86
                try {
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    87
                    testCount++;
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    88
                    out.println("test: " + testName);
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    89
                    m.invoke(this, f.apply(m));
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    90
                } catch (InvocationTargetException e) {
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    91
                    errorCount++;
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    92
                    Throwable cause = e.getCause();
42837
03ee805ba348 8169005: tools/javac/T8132562/ClassPathWithDoubleQuotesTest.java fails after fix to JDK-8132562
vromero
parents: 40308
diff changeset
    93
                    out.println("Exception running test " + testName + ": " + e.getCause());
37750
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    94
                    cause.printStackTrace(out);
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    95
                }
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    96
                out.println();
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    97
            }
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    98
        }
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
    99
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
   100
        if (testCount == 0) {
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
   101
            throw new Error("no tests found");
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
   102
        }
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
   103
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
   104
        StringBuilder summary = new StringBuilder();
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
   105
        if (testCount != 1) {
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
   106
            summary.append(testCount).append(" tests");
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
   107
        }
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
   108
        if (errorCount > 0) {
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
   109
            if (summary.length() > 0) {
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
   110
                summary.append(", ");
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
   111
            }
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
   112
            summary.append(errorCount).append(" errors");
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
   113
        }
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
   114
        out.println(summary);
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
   115
        if (errorCount > 0) {
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
   116
            throw new Exception(errorCount + " errors found");
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
   117
        }
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
   118
    }
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 37758
diff changeset
   119
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 40232
diff changeset
   120
    protected void error(String message) {
40232
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 37758
diff changeset
   121
        out.println("Error: " + message);
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 37758
diff changeset
   122
        errorCount++;
4995ab1a4558 8162359: javac should use stdout for --help and --version
jjg
parents: 37758
diff changeset
   123
    }
37750
d213951c811b 8154482: javadoc tool must support legacy doclet and taglet
ksrini
parents:
diff changeset
   124
}