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