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