KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > junit > runner > Description


1 package org.junit.runner;
2
3 import java.util.ArrayList JavaDoc;
4
5 /**
6  * <p>A <code>Description</code> describes a test which is to be run or has been run. <code>Descriptions</code>
7  * can be atomic (a single test) or compound (containing children tests). <code>Descriptions</code> are used
8  * to provide feedback about the tests that are about to run (for example, the tree view
9  * visible in many IDEs) or tests that have been run (for example, the failures view).</p>
10  *
11  * <p><code>Descriptions</code> are implemented as a single class rather than a Composite because
12  * they are entirely informational. They contain no logic aside from counting their tests.</p>
13  *
14  * <p>In the past, we used the raw {@link junit.framework.TestCase}s and {@link junit.framework.TestSuite}s
15  * to display the tree of tests. This was no longer viable in JUnit 4 because atomic tests no longer have
16  * a superclass below {@link Object}. We needed a way to pass a class and name together. Description
17  * emerged from this.</p>
18  *
19  * @see org.junit.runner.Request
20  * @see org.junit.runner.Runner
21  */

22 public class Description {
23     
24     /**
25      * Create a <code>Description</code> named <code>name</code>.
26      * Generally, you will add children to this <code>Description</code>.
27      * @param name the name of the <code>Description</code>
28      * @return a <code>Description</code> named <code>name</code>
29      */

30     public static Description createSuiteDescription(String JavaDoc name) {
31         return new Description(name);
32     }
33
34     /**
35      * Create a <code>Description</code> of a single test named <code>name</code> in the class <code>clazz</code>.
36      * Generally, this will be a leaf <code>Description</code>.
37      * @param clazz the class of the test
38      * @param name the name of the test (a method name for test annotated with {@link org.junit.Test})
39      * @return a <code>Description</code> named <code>name</code>
40      */

41     public static Description createTestDescription(Class JavaDoc<?> clazz, String JavaDoc name) {
42         return new Description(String.format("%s(%s)", name, clazz.getName()));
43     }
44
45     /**
46      * Create a <code>Description</code> named after <code>testClass</code>
47      * @param testClass A {@link Class} containing tests
48      * @return a <code>Description</code> of <code>testClass</code>
49      */

50     public static Description createSuiteDescription(Class JavaDoc<?> testClass) {
51         return new Description(testClass.getName());
52     }
53     
54     public static final Description EMPTY= new Description("No Tests");
55     public static final Description TEST_MECHANISM= new Description("Test mechanism");
56     
57     private final ArrayList JavaDoc<Description> fChildren= new ArrayList JavaDoc<Description>();
58     private final String JavaDoc fDisplayName;
59     
60     /**
61      * @deprecated (since 4.3) use the static factories (createTestDescription, createSuiteDescription) instead
62      */

63     @Deprecated JavaDoc
64     protected Description(final String JavaDoc displayName) {
65         fDisplayName= displayName;
66     }
67
68     /**
69      * @return a user-understandable label
70      */

71     public String JavaDoc getDisplayName() {
72         return fDisplayName;
73     }
74
75     /**
76      * Add <code>Description</code> as a child of the receiver.
77      * @param description the soon-to-be child.
78      */

79     public void addChild(Description description) {
80         getChildren().add(description);
81     }
82
83     /**
84      * @return the receiver's children, if any
85      */

86     public ArrayList JavaDoc<Description> getChildren() {
87         return fChildren;
88     }
89
90     /**
91      * @return <code>true</code> if the receiver is a suite
92      */

93     public boolean isSuite() {
94         return !isTest();
95     }
96
97     /**
98      * @return <code>true</code> if the receiver is an atomic test
99      */

100     public boolean isTest() {
101         return getChildren().isEmpty();
102     }
103
104     /**
105      * @return the total number of atomic tests in the receiver
106      */

107     public int testCount() {
108         if (isTest())
109             return 1;
110         int result= 0;
111         for (Description child : getChildren())
112             result+= child.testCount();
113         return result;
114     }
115
116     @Override JavaDoc
117     public int hashCode() {
118         return getDisplayName().hashCode();
119     }
120
121     @Override JavaDoc
122     public boolean equals(Object JavaDoc obj) {
123         if (!(obj instanceof Description))
124             return false;
125         Description d = (Description) obj;
126         return getDisplayName().equals(d.getDisplayName())
127                 && getChildren().equals(d.getChildren());
128     }
129     
130     @Override JavaDoc
131     public String JavaDoc toString() {
132         return getDisplayName();
133     }
134 }
Popular Tags