001: package org.junit.runner;
002:
003: import java.lang.annotation.Annotation;
004: import java.util.ArrayList;
005: import java.util.Arrays;
006: import java.util.Collection;
007:
008: /**
009: * <p>A <code>Description</code> describes a test which is to be run or has been run. <code>Descriptions</code>
010: * can be atomic (a single test) or compound (containing children tests). <code>Descriptions</code> are used
011: * to provide feedback about the tests that are about to run (for example, the tree view
012: * visible in many IDEs) or tests that have been run (for example, the failures view).</p>
013: *
014: * <p><code>Descriptions</code> are implemented as a single class rather than a Composite because
015: * they are entirely informational. They contain no logic aside from counting their tests.</p>
016: *
017: * <p>In the past, we used the raw {@link junit.framework.TestCase}s and {@link junit.framework.TestSuite}s
018: * to display the tree of tests. This was no longer viable in JUnit 4 because atomic tests no longer have
019: * a superclass below {@link Object}. We needed a way to pass a class and name together. Description
020: * emerged from this.</p>
021: *
022: * @see org.junit.runner.Request
023: * @see org.junit.runner.Runner
024: */
025: public class Description {
026:
027: /**
028: * Create a <code>Description</code> named <code>name</code>.
029: * Generally, you will add children to this <code>Description</code>.
030: * @param name the name of the <code>Description</code>
031: * @param annotations
032: * @return a <code>Description</code> named <code>name</code>
033: */
034: public static Description createSuiteDescription(String name,
035: Annotation... annotations) {
036: return new Description(name, annotations);
037: }
038:
039: /**
040: * Create a <code>Description</code> of a single test named <code>name</code> in the class <code>clazz</code>.
041: * Generally, this will be a leaf <code>Description</code>.
042: * @param clazz the class of the test
043: * @param name the name of the test (a method name for test annotated with {@link org.junit.Test})
044: * @param annotations meta-data about the test, for downstream interpreters
045: * @return a <code>Description</code> named <code>name</code>
046: */
047: public static Description createTestDescription(Class<?> clazz,
048: String name, Annotation... annotations) {
049: return new Description(String.format("%s(%s)", name, clazz
050: .getName()), annotations);
051: }
052:
053: /**
054: * Create a <code>Description</code> of a single test named <code>name</code> in the class <code>clazz</code>.
055: * Generally, this will be a leaf <code>Description</code>.
056: * (This remains for binary compatibility with clients of JUnit 4.3)
057: * @param clazz the class of the test
058: * @param name the name of the test (a method name for test annotated with {@link org.junit.Test})
059: * @return a <code>Description</code> named <code>name</code>
060: */
061: public static Description createTestDescription(Class<?> clazz,
062: String name) {
063: return createTestDescription(clazz, name, new Annotation[0]);
064: }
065:
066: /**
067: * Create a <code>Description</code> named after <code>testClass</code>
068: * @param testClass A {@link Class} containing tests
069: * @return a <code>Description</code> of <code>testClass</code>
070: */
071: public static Description createSuiteDescription(Class<?> testClass) {
072: return new Description(testClass.getName(), testClass
073: .getAnnotations());
074: }
075:
076: public static final Description EMPTY = new Description("No Tests");
077: public static final Description TEST_MECHANISM = new Description(
078: "Test mechanism");
079:
080: private final ArrayList<Description> fChildren = new ArrayList<Description>();
081: private final String fDisplayName;
082:
083: private final Annotation[] fAnnotations;
084:
085: private Description(final String displayName,
086: Annotation... annotations) {
087: fDisplayName = displayName;
088: fAnnotations = annotations;
089: }
090:
091: /**
092: * @return a user-understandable label
093: */
094: public String getDisplayName() {
095: return fDisplayName;
096: }
097:
098: /**
099: * Add <code>Description</code> as a child of the receiver.
100: * @param description the soon-to-be child.
101: */
102: public void addChild(Description description) {
103: getChildren().add(description);
104: }
105:
106: /**
107: * @return the receiver's children, if any
108: */
109: public ArrayList<Description> getChildren() {
110: return fChildren;
111: }
112:
113: /**
114: * @return <code>true</code> if the receiver is a suite
115: */
116: public boolean isSuite() {
117: return !isTest();
118: }
119:
120: /**
121: * @return <code>true</code> if the receiver is an atomic test
122: */
123: public boolean isTest() {
124: return getChildren().isEmpty();
125: }
126:
127: /**
128: * @return the total number of atomic tests in the receiver
129: */
130: public int testCount() {
131: if (isTest())
132: return 1;
133: int result = 0;
134: for (Description child : getChildren())
135: result += child.testCount();
136: return result;
137: }
138:
139: @Override
140: public int hashCode() {
141: return getDisplayName().hashCode();
142: }
143:
144: @Override
145: public boolean equals(Object obj) {
146: if (!(obj instanceof Description))
147: return false;
148: Description d = (Description) obj;
149: return getDisplayName().equals(d.getDisplayName())
150: && getChildren().equals(d.getChildren());
151: }
152:
153: @Override
154: public String toString() {
155: return getDisplayName();
156: }
157:
158: public boolean isEmpty() {
159: return equals(EMPTY);
160: }
161:
162: public Description childlessCopy() {
163: return new Description(fDisplayName, fAnnotations);
164: }
165:
166: public <T extends Annotation> T getAnnotation(
167: Class<T> annotationType) {
168: for (Annotation each : fAnnotations)
169: if (each.annotationType().equals(annotationType))
170: return annotationType.cast(each);
171: return null;
172: }
173:
174: public Collection<Annotation> getAnnotations() {
175: return Arrays.asList(fAnnotations);
176: }
177: }
|