Path: blob/master/test/langtools/tools/lib/toolbox/TestRunner.java
41149 views
/*1* Copyright (c) 2016, 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*/2223package toolbox;2425import java.io.PrintStream;26import java.lang.annotation.Annotation;27import java.lang.annotation.Retention;28import java.lang.annotation.RetentionPolicy;29import java.lang.reflect.InvocationTargetException;30import java.lang.reflect.Method;31import java.util.function.Function;3233/**34* Utility class to manage and execute sub-tests within a test.35*36* This class does the following:37* <ul>38* <li> invokes those test methods annotated with @Test39* <li> keeps track of successful and failed tests40* <li> throws an Exception if any test fails.41* <li> provides a test summary at the end of the run.42* </ul>4344* Tests must extend this class, annotate the test methods45* with @Test and call one of the runTests method.46*/47public abstract class TestRunner {48/** Marker annotation for test cases. */49@Retention(RetentionPolicy.RUNTIME)50public @interface Test { }5152int testCount = 0;53int errorCount = 0;5455public String testName = null;5657protected PrintStream out;5859/**60* Constructs the Object.61* @param out the PrintStream to print output to.62*/63public TestRunner(PrintStream out) {64this.out = out;65}6667/**68* Invoke all methods annotated with @Test.69* @throws java.lang.Exception if any errors occur70*/71protected void runTests() throws Exception {72runTests(f -> new Object[0]);73}7475/**76* Invoke all methods annotated with @Test.77* @param f a lambda expression to specify arguments for the test method78* @throws java.lang.Exception if any errors occur79*/80protected void runTests(Function<Method, Object[]> f) throws Exception {81for (Method m : getClass().getDeclaredMethods()) {82Annotation a = m.getAnnotation(Test.class);83if (a != null) {84testName = m.getName();85try {86testCount++;87out.println("test: " + testName);88m.invoke(this, f.apply(m));89} catch (InvocationTargetException e) {90errorCount++;91Throwable cause = e.getCause();92out.println("Exception running test " + testName + ": " + e.getCause());93cause.printStackTrace(out);94}95out.println();96}97}9899if (testCount == 0) {100throw new Error("no tests found");101}102103StringBuilder summary = new StringBuilder();104if (testCount != 1) {105summary.append(testCount).append(" tests");106}107if (errorCount > 0) {108if (summary.length() > 0) {109summary.append(", ");110}111summary.append(errorCount).append(" errors");112}113out.println(summary);114if (errorCount > 0) {115throw new Exception(errorCount + " errors found");116}117}118119protected void error(String message) {120out.println("Error: " + message);121errorCount++;122}123}124125126