carbidecpp22devenv/plugins/org.eclipse.test.source_3.5.0.r20080925/src/org.junit4_4.5.0.v20090423/junitsrc/org/junit/runner/Description.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.lang.annotation.Annotation;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     4
import java.util.ArrayList;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     5
import java.util.Arrays;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     6
import java.util.Collection;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     7
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     8
/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     9
 * <p>A <code>Description</code> describes a test which is to be run or has been run. <code>Descriptions</code> 
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    10
 * can be atomic (a single test) or compound (containing children tests). <code>Descriptions</code> are used
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    11
 * to provide feedback about the tests that are about to run (for example, the tree view
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    12
 * visible in many IDEs) or tests that have been run (for example, the failures view).</p>
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    13
 * 
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    14
 * <p><code>Descriptions</code> are implemented as a single class rather than a Composite because
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    15
 * they are entirely informational. They contain no logic aside from counting their tests.</p>
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    16
 * 
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    17
 * <p>In the past, we used the raw {@link junit.framework.TestCase}s and {@link junit.framework.TestSuite}s
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    18
 * to display the tree of tests. This was no longer viable in JUnit 4 because atomic tests no longer have 
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    19
 * a superclass below {@link Object}. We needed a way to pass a class and name together. Description 
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    20
 * emerged from this.</p>
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    21
 * 
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    22
 * @see org.junit.runner.Request
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    23
 * @see org.junit.runner.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
public class Description {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    26
	
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    27
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    28
	 * Create a <code>Description</code> named <code>name</code>.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    29
	 * Generally, you will add children to this <code>Description</code>.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    30
	 * @param name the name of the <code>Description</code> 
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    31
	 * @param annotations 
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    32
	 * @return a <code>Description</code> named <code>name</code>
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 static Description createSuiteDescription(String name, Annotation... annotations) {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    35
		return new Description(name, annotations);
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
	 * Create a <code>Description</code> of a single test named <code>name</code> in the class <code>clazz</code>.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    40
	 * Generally, this will be a leaf <code>Description</code>.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    41
	 * @param clazz the class of the test
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    42
	 * @param name the name of the test (a method name for test annotated with {@link org.junit.Test})
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    43
	 * @param annotations meta-data about the test, for downstream interpreters
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    44
	 * @return a <code>Description</code> named <code>name</code>
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    45
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    46
	public static Description createTestDescription(Class<?> clazz, String name, Annotation... annotations) {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    47
		return new Description(String.format("%s(%s)", name, clazz.getName()), annotations);
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
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    51
	 * Create a <code>Description</code> of a single test named <code>name</code> in the class <code>clazz</code>.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    52
	 * Generally, this will be a leaf <code>Description</code>.  
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    53
	 * (This remains for binary compatibility with clients of JUnit 4.3)
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    54
	 * @param clazz the class of the test
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    55
	 * @param name the name of the test (a method name for test annotated with {@link org.junit.Test})
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    56
	 * @return a <code>Description</code> named <code>name</code>
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    57
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    58
	public static Description createTestDescription(Class<?> clazz, String name) {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    59
		return createTestDescription(clazz, name, new Annotation[0]);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    60
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    61
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    62
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    63
	 * Create a <code>Description</code> named after <code>testClass</code>
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    64
	 * @param testClass A {@link Class} containing tests 
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    65
	 * @return a <code>Description</code> of <code>testClass</code>
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    66
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    67
	public static Description createSuiteDescription(Class<?> testClass) {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    68
		return new Description(testClass.getName(), testClass.getAnnotations());
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    69
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    70
	
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    71
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    72
	 * Describes a Runner which runs no tests
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    73
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    74
	public static final Description EMPTY= new Description("No Tests");
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    75
	
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    76
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    77
	 * Describes a step in the test-running mechanism that goes so wrong no
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    78
	 * other description can be used (for example, an exception thrown from a Runner's
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    79
	 * constructor
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    80
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    81
	public static final Description TEST_MECHANISM= new Description("Test mechanism");
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    82
	
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    83
	private final ArrayList<Description> fChildren= new ArrayList<Description>();
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    84
	private final String fDisplayName;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    85
	
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    86
	private final Annotation[] fAnnotations;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    87
	
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    88
	private Description(final String displayName, Annotation... annotations) {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    89
		fDisplayName= displayName;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    90
		fAnnotations= annotations;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    91
	}
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
	 * @return a user-understandable label
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    95
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    96
	public String getDisplayName() {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    97
		return fDisplayName;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    98
	}
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
	 * Add <code>Description</code> as a child of the receiver.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   102
	 * @param description the soon-to-be child.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   103
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   104
	public void addChild(Description description) {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   105
		getChildren().add(description);
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
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   109
	 * @return the receiver's children, if any
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   110
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   111
	public ArrayList<Description> getChildren() {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   112
		return fChildren;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   113
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   114
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   115
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   116
	 * @return <code>true</code> if the receiver is a suite
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   117
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   118
	public boolean isSuite() {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   119
		return !isTest();
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   120
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   121
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   122
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   123
	 * @return <code>true</code> if the receiver is an atomic test
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   124
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   125
	public boolean isTest() {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   126
		return getChildren().isEmpty();
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
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   130
	 * @return the total number of atomic tests in the receiver
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   131
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   132
	public int testCount() {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   133
		if (isTest())
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   134
			return 1;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   135
		int result= 0;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   136
		for (Description child : getChildren())
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   137
			result+= child.testCount();
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   138
		return result;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   139
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   140
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   141
	@Override
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   142
	public int hashCode() {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   143
		return getDisplayName().hashCode();
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   144
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   145
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   146
	@Override
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   147
	public boolean equals(Object obj) {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   148
		if (!(obj instanceof Description))
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   149
			return false;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   150
		Description d = (Description) obj;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   151
		return getDisplayName().equals(d.getDisplayName())
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   152
				&& getChildren().equals(d.getChildren());
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   153
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   154
	
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   155
	@Override
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   156
	public String toString() {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   157
		return getDisplayName();
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
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   161
	 * @return true if this is a description of a Runner that runs no tests
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 boolean isEmpty() {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   164
		return equals(EMPTY);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   165
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   166
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   167
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   168
	 * @return a copy of this description, with no children (on the assumption that some of the
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   169
	 * children will be added back)
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   170
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   171
	public Description childlessCopy() {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   172
		return new Description(fDisplayName, fAnnotations);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   173
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   174
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   175
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   176
	 * @return the annotation of type annotationType that is attached to this description node, 
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   177
	 * or null if none exists
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   178
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   179
	public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   180
		for (Annotation each : fAnnotations)
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   181
			if (each.annotationType().equals(annotationType))
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   182
				return annotationType.cast(each);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   183
		return null;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   184
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   185
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   186
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   187
	 * @return all of the annotations attached to this description node
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   188
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   189
	public Collection<Annotation> getAnnotations() {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   190
		return Arrays.asList(fAnnotations);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   191
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
   192
}