carbidecpp22devenv/plugins/org.eclipse.test.source_3.5.0.r20080925/src/org.junit4_4.5.0.v20090423/junitsrc/org/junit/runners/Suite.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.runners;
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.lang.annotation.ElementType;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     4
import java.lang.annotation.Retention;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     5
import java.lang.annotation.RetentionPolicy;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     6
import java.lang.annotation.Target;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     7
import java.util.List;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     8
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     9
import org.junit.internal.builders.AllDefaultPossibilitiesBuilder;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    10
import org.junit.runner.Description;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    11
import org.junit.runner.Runner;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    12
import org.junit.runner.notification.RunNotifier;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    13
import org.junit.runners.model.InitializationError;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    14
import org.junit.runners.model.RunnerBuilder;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    15
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    16
/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    17
 * Using <code>Suite</code> as a runner allows you to manually
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    18
 * build a suite containing tests from many classes. It is the JUnit 4 equivalent of the JUnit 3.8.x
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    19
 * static {@link junit.framework.Test} <code>suite()</code> method. To use it, annotate a class
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    20
 * with <code>@RunWith(Suite.class)</code> and <code>@SuiteClasses(TestClass1.class, ...)</code>.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    21
 * When you run this class, it will run all the tests in all the suite classes.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    22
 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    23
public class Suite extends ParentRunner<Runner> {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    24
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    25
	 * The <code>SuiteClasses</code> annotation specifies the classes to be run when a class
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    26
	 * annotated with <code>@RunWith(Suite.class)</code> is run.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    27
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    28
	@Retention(RetentionPolicy.RUNTIME)
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    29
	@Target(ElementType.TYPE)
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    30
	public @interface SuiteClasses {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    31
		/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    32
		 * @return the classes to be run
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 Class<?>[] value();
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    35
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    36
	
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    37
	private static Class<?>[] getAnnotatedClasses(Class<?> klass) throws InitializationError {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    38
		SuiteClasses annotation= klass.getAnnotation(SuiteClasses.class);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    39
		if (annotation == null)
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    40
			throw new InitializationError(String.format("class '%s' must have a SuiteClasses annotation", klass.getName()));
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    41
		return annotation.value();
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    42
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    43
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    44
	private final List<Runner> fRunners;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    45
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    46
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    47
	 * Called reflectively on classes annotated with <code>@RunWith(Suite.class)</code>
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    48
	 * 
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    49
	 * @param klass the root class
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    50
	 * @param builder builds runners for classes in the suite
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    51
	 * @throws InitializationError
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 Suite(Class<?> klass, RunnerBuilder builder) throws InitializationError {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    54
		this(builder, klass, getAnnotatedClasses(klass));
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    55
	}
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
	 * Call this when there is no single root class (for example, multiple class names
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    59
	 * passed on the command line to {@link org.junit.runner.JUnitCore}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    60
	 * 
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    61
	 * @param builder builds runners for classes in the suite
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    62
	 * @param classes the classes in the suite
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    63
	 * @throws InitializationError 
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 Suite(RunnerBuilder builder, Class<?>[] classes) throws InitializationError {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    66
		this(null, builder.runners(null, 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
	 * Call this when the default builder is good enough. Left in for compatibility with JUnit 4.4.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    71
	 * @param klass the root of the suite
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    72
	 * @param suiteClasses the classes in the suite
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    73
	 * @throws InitializationError
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    74
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    75
	protected Suite(Class<?> klass, Class<?>[] suiteClasses) throws InitializationError {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    76
		this(new AllDefaultPossibilitiesBuilder(true), klass, suiteClasses);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    77
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    78
	
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    79
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    80
	 * Called by this class and subclasses once the classes making up the suite have been determined
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    81
	 * 
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    82
	 * @param builder builds runners for classes in the suite
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    83
	 * @param klass the root of the suite
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    84
	 * @param suiteClasses the classes in the suite
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    85
	 * @throws InitializationError
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    86
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    87
	protected Suite(RunnerBuilder builder, Class<?> klass, Class<?>[] suiteClasses) throws InitializationError {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    88
		this(klass, builder.runners(klass, suiteClasses));
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    89
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    90
	
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    91
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    92
	 * Called by this class and subclasses once the runners making up the suite have been determined
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    93
	 * 
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    94
	 * @param klass root of the suite
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    95
	 * @param runners for each class in the suite, a {@link Runner}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    96
	 * @throws InitializationError 
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    97
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    98
	protected Suite(Class<?> klass, List<Runner> runners) throws InitializationError {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    99
		super(klass);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   100
		fRunners = runners;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   101
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   102
	
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   103
	@Override
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   104
	protected List<Runner> getChildren() {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   105
		return fRunners;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   106
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   107
	
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   108
	@Override
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   109
	protected Description describeChild(Runner child) {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   110
		return child.getDescription();
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   111
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   112
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   113
	@Override
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   114
	protected void runChild(Runner runner, final RunNotifier notifier) {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   115
		runner.run(notifier);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   116
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   117
}