/*
* Copyright (c) 2016, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package toolbox;
import java.io.PrintStream;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Utility class to manage and execute sub-tests within a test.
*
* This class does the following:
*
*
invokes those test methods annotated with @Test
*
keeps track of successful and failed tests
*
throws an Exception if any test fails.
*
provides a test summary at the end of the run.
*
* Tests must extend this class, annotate the test methods
* with @Test and call one of the runTests method.
*/
public abstract class TestRunner {
/** Marker annotation for test cases. */
@Retention(RetentionPolicy.RUNTIME)
public @interface Test { }
int testCount = 0;
int errorCount = 0;
public String testName = null;
protected PrintStream out;
/**
* Constructs the Object.
* @param out the PrintStream to print output to.
*/
public TestRunner(PrintStream out) {
this.out = out;
}
/**
* Invoke all methods annotated with @Test.
* @throws java.lang.Exception if any errors occur
*/
protected void runTests() throws Exception {
runTests(f -> new Object[0]);
}
/**
* Invoke each @Test method once using the parameters returned by the function.
*
*
* If the function returns null for some method, that method is skipped.
*
*
* If system property {@code test.query} is set, only that method is included.
*
* @param f function mapping method name to an array of method parameters
* @throws java.lang.Exception if any errors occur
*/
protected void runTests(Function f) throws Exception {
runTestsMulti(f.andThen(Stream::