carbidecpp22devenv/plugins/org.eclipse.test.source_3.5.0.r20080925/src/org.junit4_4.5.0.v20090423/junitsrc/org/junit/runner/JUnitCore.java
author cawthron
Fri, 04 Dec 2009 11:49:54 -0600
changeset 636 3ef299ba838f
permissions -rw-r--r--
add files for RCL_2_2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
636
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     1
package org.junit.runner;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     2
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     3
import java.util.ArrayList;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     4
import java.util.List;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     5
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     6
import junit.runner.Version;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     7
import org.junit.internal.JUnitSystem;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     8
import org.junit.internal.RealSystem;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     9
import org.junit.internal.TextListener;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    10
import org.junit.internal.runners.JUnit38ClassRunner;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    11
import org.junit.runner.notification.Failure;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    12
import org.junit.runner.notification.RunListener;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    13
import org.junit.runner.notification.RunNotifier;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    14
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    15
/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    16
 * <code>JUnitCore</code> is a facade for running tests. It supports running JUnit 4 tests, 
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    17
 * JUnit 3.8.x tests, and mixtures. To run tests from the command line, run 
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    18
 * <code>java org.junit.runner.JUnitCore TestClass1 TestClass2 ...</code>.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    19
 * For one-shot test runs, use the static method {@link #runClasses(Class[])}. 
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    20
 * If you want to add special listeners,
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    21
 * create an instance of {@link org.junit.runner.JUnitCore} first and use it to run the tests.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    22
 * 
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    23
 * @see org.junit.runner.Result
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    24
 * @see org.junit.runner.notification.RunListener
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    25
 * @see org.junit.runner.Request
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    26
 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    27
public class JUnitCore {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    28
	
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    29
	private RunNotifier fNotifier;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    30
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    31
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    32
	 * Create a new <code>JUnitCore</code> to run tests.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    33
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    34
	public JUnitCore() {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    35
		fNotifier= new RunNotifier();
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    36
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    37
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    38
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    39
	 * Run the tests contained in the classes named in the <code>args</code>.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    40
	 * If all tests run successfully, exit with a status of 0. Otherwise exit with a status of 1.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    41
	 * Write feedback while tests are running and write
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    42
	 * stack traces for all failed tests after the tests all complete.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    43
	 * @param args names of classes in which to find tests to run
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    44
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    45
	public static void main(String... args) {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    46
		runMainAndExit(new RealSystem(), args);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    47
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    48
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    49
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    50
	 * Do not use. Testing purposes only.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    51
	 * @param system 
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    52
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    53
	public static void runMainAndExit(JUnitSystem system, String... args) {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    54
		Result result= new JUnitCore().runMain(system, args);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    55
		system.exit(result.wasSuccessful() ? 0 : 1);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    56
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    57
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    58
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    59
	 * Run the tests contained in <code>classes</code>. Write feedback while the tests
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    60
	 * are running and write stack traces for all failed tests after all tests complete. This is
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    61
	 * similar to {@link #main(String[])}, but intended to be used programmatically.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    62
	 * @param classes Classes in which to find tests
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    63
	 * @return a {@link Result} describing the details of the test run and the failed tests.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    64
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    65
	public static Result runClasses(Class<?>... classes) {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    66
		return new JUnitCore().run(classes);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    67
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    68
	
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    69
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    70
	 * Do not use. Testing purposes only.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    71
	 * @param system 
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    72
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    73
	public Result runMain(JUnitSystem system, String... args) {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    74
		system.out().println("JUnit version " + Version.id());
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    75
		List<Class<?>> classes= new ArrayList<Class<?>>();
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    76
		List<Failure> missingClasses= new ArrayList<Failure>();
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    77
		for (String each : args)
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    78
			try {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    79
				classes.add(Class.forName(each));
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    80
			} catch (ClassNotFoundException e) {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    81
				system.out().println("Could not find class: " + each);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    82
				Description description= Description.createSuiteDescription(each);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    83
				Failure failure= new Failure(description, e);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    84
				missingClasses.add(failure);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    85
			}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    86
		RunListener listener= new TextListener(system);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    87
		addListener(listener);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    88
		Result result= run(classes.toArray(new Class[0]));
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    89
		for (Failure each : missingClasses)
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    90
			result.getFailures().add(each);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    91
		return result;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    92
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    93
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    94
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    95
	 * @return the version number of this release
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    96
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    97
	public String getVersion() {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    98
		return Version.id();
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    99
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   100
	
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   101
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   102
	 * Run all the tests in <code>classes</code>.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   103
	 * @param classes the classes containing tests
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   104
	 * @return a {@link Result} describing the details of the test run and the failed tests.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   105
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   106
	public Result run(Class<?>... classes) {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   107
		return run(Request.classes(classes));
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   108
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   109
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   110
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   111
	 * Run all the tests contained in <code>request</code>.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   112
	 * @param request the request describing tests
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   113
	 * @return a {@link Result} describing the details of the test run and the failed tests.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   114
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   115
	public Result run(Request request) {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   116
		return run(request.getRunner());
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   117
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   118
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   119
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   120
	 * Run all the tests contained in JUnit 3.8.x <code>test</code>. Here for backward compatibility.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   121
	 * @param test the old-style test
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   122
	 * @return a {@link Result} describing the details of the test run and the failed tests.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   123
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   124
	public Result run(junit.framework.Test test) { 
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   125
		return run(new JUnit38ClassRunner(test));
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   126
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   127
	
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   128
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   129
	 * Do not use. Testing purposes only.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   130
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   131
	public Result run(Runner runner) {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   132
		Result result= new Result();
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   133
		RunListener listener= result.createListener();
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   134
		addFirstListener(listener);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   135
		try {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   136
			fNotifier.fireTestRunStarted(runner.getDescription());
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   137
			runner.run(fNotifier);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   138
			fNotifier.fireTestRunFinished(result);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   139
		} finally {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   140
			removeListener(listener);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   141
		}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   142
		return result;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   143
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   144
	
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   145
	private void addFirstListener(RunListener listener) {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   146
		fNotifier.addFirstListener(listener);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   147
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   148
	
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   149
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   150
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   151
	 * Add a listener to be notified as the tests run.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   152
	 * @param listener the listener to add
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   153
	 * @see org.junit.runner.notification.RunListener
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   154
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   155
	public void addListener(RunListener listener) {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   156
		fNotifier.addListener(listener);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   157
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   158
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   159
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   160
	 * Remove a listener.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   161
	 * @param listener the listener to remove
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   162
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   163
	public void removeListener(RunListener listener) {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   164
		fNotifier.removeListener(listener);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   165
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   166
}