📄 testsuite.java
字号:
package momeunit.framework;import java.util.Enumeration;import java.util.Vector;/** * A collection of tests that can be processed together. TestSuite has a name * property that identifies it. It can be returned or modified using property * methods It can be also preset during instantiation using * {@link #TestSuite(String)},{@link #TestSuite(String, Class[])} or * {@link #TestSuite(String, String[])} constructors. A test can be added to * TestSuite by calling {@link #addTest(Class)} or {@link #addTest(String)} * method with class or name of class implementing {@link Test} interface. A * test can also be added to TestSuite by calling {@link #addTest(Test)} method * with test instance. The collection of tests can be preset during * instantiation by using a constructors {@link #TestSuite(Class[])}, * {@link #TestSuite(String[])}, {@link #TestSuite(String, Class[])} or * {@link #TestSuite(String, String[])}. The arguments to these constructors * are optional name of test suite and array of classes or names of classes * implementing {@link Test} interface. To run tests that constitute this * TestSuite call {@link #run(TestResult)}. To run some test call * {@link #runTest(Test, TestResult)} method. There are also getter methods that * return number of tests in test suite, number of TestCases in test suite (test * suite can also contain other test suites or any other {@link Test} * implementations), test at specified position. * * @see Test * @see TestCase * * @version 1.1.2 * @author Sergio Morozov */public class TestSuite implements Test{ /** * Default name of test suite */ private static final String DEFAULT_NAME = "NONAME"; /** * Name of test suite */ private String name = null; /** * Vector of tests of this test suite. */ private Vector tests = new Vector(10); /** * Number of testcases that constitute test suite. */ private int testCasesNumber = 0; /** * Constructs an empty TestSuite. * * @since 1.0 */ public TestSuite() {} /** * Constructs an empty TestSuite with specified name. * * @param name * the name of test suite. * @since 1.0 */ public TestSuite(String name) { this(); this.setName(name); } /** * Constructs a TestSuite with default name consisting of tests specified as * classes. * * @param classes * array of classes implementing Test interface. * @since 1.0 */ public TestSuite(Class[] classes) { this(); if (classes != null) for (int i = classes.length - 1; i >= 0; i--) this.addTest(classes[i]); } /** * Constructs a TestSuite with default name consisting of tests specified as * classnames. * * @param classes * array of names of classes implementing Test interface. * @since 1.0 */ public TestSuite(String[] classes) { this(); if (classes != null) for (int i = classes.length - 1; i >= 0; i--) this.addTest(classes[i]); } /** * Constructs a TestSuite with the given name consisting of tests specified as * classes. * * @param name * the name of test suite. * @param classes * array of classes implementing Test interface. * @since 1.0 */ public TestSuite(String name, Class[] classes) { this(classes); this.setName(name); } /** * Constructs a TestSuite with the given name consisting of tests specified as * classnames. * * @param name * the name of test suite. * @param classes * array of names of classes implementing Test interface. * @since 1.0 */ public TestSuite(String name, String[] classes) { this(classes); this.setName(name); } /** * Returns the name of the test suite. * * @return name of test suite. * @since 1.0 */ public String getName() { return this.name; } /** * Sets the name of test suite to the specified or default, if * <code>name</code> is <code>null</code> . * * @param name * name of test suite or null. * @since 1.0 */ public void setName(String name) { this.name = name; } /** * Adds specified test to the test suite. * * @param test * test to be added to the test suite. * @since 1.0 */ public void addTest(Test test) { if (test != null) { this.testCasesNumber += test.countTestCases(); this.tests.addElement(test); } } /** * Adds test specified by class implementing Test to the test suite. * * @param testClass * class implementing Test to be added to the test suite. * @since 1.0 */ public void addTest(Class testClass) { if (testClass != null) { Test test; try { test = (Test) testClass.newInstance(); } catch (Throwable e) { test = new ErrorTestCase(testClass.getName(), e); } this.addTest(test); } } /** * Adds test specified by name of class implementing Test to the test suite. * * @param className * name of class implementing Test to be added to the test suite. * @since 1.0 */ public void addTest(String className) { if (className != null) { Test test; try { test = (Test) Class.forName(className).newInstance(); } catch (Throwable e) { test = new ErrorTestCase(className, e); } this.addTest(test); } } public int countTestCases() { return this.testCasesNumber; } /** * Runs tests of this test suite and collects results in a TestResult * instance. * * @see momeunit.framework.Test#run(momeunit.framework.TestResult) * @since 1.0 */ public void run(TestResult result) { for (Enumeration e = this.tests(); e.hasMoreElements() && !result.shouldStop();) this.runTest((Test) e.nextElement(), result); } /** * Runs the specified test and populates the specified TestResult with results * of test. * * @param test * test to be run. * @param result * TestResult instance to be populated with results of the test. * @since 1.0 */ public void runTest(Test test, TestResult result) { test.run(result); } /** * Returns the test at the given index. * * @param index * index of the test to be returned. * @return test at the specified index. * @since 1.0 */ public Test testAt(int index) { return (Test) tests.elementAt(index); } /** * Returns the number of tests that constitute test suite. * * @return number of tests that constitute test suite. * @since 1.0 */ public int testCount() { return tests.size(); } /** * Returns enumeration of tests that constitute test suite. * * @return enumeration of tests. * @since 1.0 */ public Enumeration tests() { return tests.elements(); } /** * @since 1.0 */ public String toString() { return ((this.name != null) ? this.name : DEFAULT_NAME) + this.tests; } /** * Returns the name of the suite. Not all test suites have a name and this * method can return null. * * @since 1.0 */ class ErrorTestCase extends TestCase { private Throwable t; /** * @param t * @since 1.0 */ public ErrorTestCase(String name, Throwable t) { super(name); if (t == null) throw new NullPointerException("throwable"); this.t = t; } /* * (non-Javadoc) * * @see momeunit.framework.TestCase#test() * */ public void test() throws Throwable { throw this.t; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -