KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > tasks > junit > TestResult


1 package hudson.tasks.junit;
2
3 import hudson.model.AbstractBuild;
4 import hudson.util.IOException2;
5 import org.apache.tools.ant.DirectoryScanner;
6 import org.dom4j.DocumentException;
7 import org.kohsuke.stapler.StaplerRequest;
8 import org.kohsuke.stapler.StaplerResponse;
9
10 import java.io.File JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Collection JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.TreeMap JavaDoc;
19
20 /**
21  * Root of all the test results for one build.
22  *
23  * @author Kohsuke Kawaguchi
24  */

25 public final class TestResult extends MetaTabulatedResult {
26     /**
27      * List of all {@link SuiteResult}s in this test.
28      * This is the core data structure to be persisted in the disk.
29      */

30     private final List JavaDoc<SuiteResult> suites = new ArrayList JavaDoc<SuiteResult>();
31
32     /**
33      * {@link #suites} keyed by their names for faster lookup.
34      */

35     private transient Map JavaDoc<String JavaDoc,SuiteResult> suitesByName;
36
37     /**
38      * Results tabulated by package.
39      */

40     private transient Map JavaDoc<String JavaDoc,PackageResult> byPackages;
41
42     // set during the freeze phase
43
private transient TestResultAction parent;
44
45     /**
46      * Number of all tests.
47      */

48     private transient int totalTests;
49     /**
50      * Number of failed/error tests.
51      */

52     private transient List JavaDoc<CaseResult> failedTests;
53
54     /**
55      * Creates an empty result.
56      */

57     TestResult() {
58     }
59
60     /**
61      * Collect reports from the given {@link DirectoryScanner}, while
62      * filtering out all files that were created before the given tiem.
63      */

64     public TestResult(long buildTime, DirectoryScanner results) throws IOException JavaDoc {
65         String JavaDoc[] includedFiles = results.getIncludedFiles();
66         File JavaDoc baseDir = results.getBasedir();
67
68         for (String JavaDoc value : includedFiles) {
69             File JavaDoc reportFile = new File JavaDoc(baseDir, value);
70             // only count files that were actually updated during this build
71
if(buildTime <= reportFile.lastModified())
72                 parse(reportFile);
73         }
74     }
75
76     /**
77      * Parses an additional report file.
78      */

79     public void parse(File JavaDoc reportFile) throws IOException JavaDoc {
80         try {
81             suites.add(new SuiteResult(reportFile));
82         } catch (DocumentException e) {
83             throw new IOException2("Failed to read "+reportFile,e);
84         }
85     }
86
87     public String JavaDoc getDisplayName() {
88         return "Test Result";
89     }
90
91     public AbstractBuild<?,?> getOwner() {
92         return parent.owner;
93     }
94
95     @Override JavaDoc
96     public TestResult getPreviousResult() {
97         TestResultAction p = parent.getPreviousResult();
98         if(p!=null)
99             return p.getResult();
100         else
101             return null;
102     }
103
104     public String JavaDoc getTitle() {
105         return "Test Result";
106     }
107
108     public String JavaDoc getChildTitle() {
109         return "Package";
110     }
111
112     @Override JavaDoc
113     public int getPassCount() {
114         return totalTests-getFailCount();
115     }
116
117     @Override JavaDoc
118     public int getFailCount() {
119         return failedTests.size();
120     }
121
122     @Override JavaDoc
123     public List JavaDoc<CaseResult> getFailedTests() {
124         return failedTests;
125     }
126
127     @Override JavaDoc
128     public Collection JavaDoc<PackageResult> getChildren() {
129         return byPackages.values();
130     }
131
132     public PackageResult getDynamic(String JavaDoc packageName, StaplerRequest req, StaplerResponse rsp) {
133         return byPackage(packageName);
134     }
135
136     public PackageResult byPackage(String JavaDoc packageName) {
137         return byPackages.get(packageName);
138     }
139
140     public SuiteResult getSuite(String JavaDoc name) {
141         return suitesByName.get(name);
142     }
143
144     /**
145      * Builds up the transient part of the data structure
146      * from results {@link #parse(File) parsed} so far.
147      *
148      * <p>
149      * After the data is frozen, more files can be parsed
150      * and then freeze can be called again.
151      */

152     public void freeze(TestResultAction parent) {
153         this.parent = parent;
154         if(suitesByName==null) {
155             // freeze for the first time
156
suitesByName = new HashMap JavaDoc<String JavaDoc,SuiteResult>();
157             totalTests = 0;
158             failedTests = new ArrayList JavaDoc<CaseResult>();
159             byPackages = new TreeMap JavaDoc<String JavaDoc,PackageResult>();
160         }
161
162         for (SuiteResult s : suites) {
163             if(!s.freeze(this))
164                 continue;
165
166             suitesByName.put(s.getName(),s);
167
168             totalTests += s.getCases().size();
169             for(CaseResult cr : s.getCases()) {
170                 if(!cr.isPassed())
171                     failedTests.add(cr);
172
173                 String JavaDoc pkg = cr.getPackageName();
174                 PackageResult pr = byPackage(pkg);
175                 if(pr==null)
176                     byPackages.put(pkg,pr=new PackageResult(this,pkg));
177                 pr.add(cr);
178             }
179         }
180
181         Collections.sort(failedTests,CaseResult.BY_AGE);
182
183         for (PackageResult pr : byPackages.values())
184             pr.freeze();
185     }
186 }
187
Popular Tags