File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\tasks\junit\PackageResult.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Daniel Dyer, id:cactusman, Tom Huybrechts, Yahoo!, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.tasks.junit;
25
26 import hudson.model.AbstractBuild;
27 import hudson.tasks.test.MetaTabulatedResult;
28 import hudson.tasks.test.TestResult;
29 import org.kohsuke.stapler.StaplerRequest;
30 import org.kohsuke.stapler.StaplerResponse;
31 import org.kohsuke.stapler.export.Exported;
32
33 import java.util.*;
34
35 /**
36 * Cumulative test result for a package.
37 *
38 * @author Kohsuke Kawaguchi
39 */
40 public final class PackageResult extends MetaTabulatedResult implements Comparable<PackageResult> {
41
42 private final String packageName;
43 private transient String safeName;
44 /**
45 * All {@link ClassResult}s keyed by their short name.
46 */
47 private final Map<String,ClassResult> classes = new TreeMap<String,ClassResult>();
48 private int passCount,failCount,skipCount;
49 private final hudson.tasks.junit.TestResult parent;
50 private float duration;
51
52 PackageResult(hudson.tasks.junit.TestResult parent, String packageName) {
53 this.packageName = packageName;
54 this.parent = parent;
55 }
56
57 @Override
58 public AbstractBuild<?, ?> getOwner() {
59 return (parent == null ? null : parent.getOwner());
60 }
61
62 public hudson.tasks.junit.TestResult getParent() {
63 return parent;
64 }
65
66 @Exported(visibility=999)
67 public String getName() {
68 return packageName;
69 }
70
71 @Override
72 public synchronized String getSafeName() {
73 if (safeName != null) {
74 return safeName;
75 }
76 Collection<PackageResult> siblings = (parent == null ? Collections.EMPTY_LIST : parent.getChildren());
77 return safeName = uniquifyName(
78 siblings,
79 safe(getName()));
80 }
81
82 @Override
83 public TestResult findCorrespondingResult(String id) {
84 String myID = safe(getName());
85 int base = id.indexOf(myID);
86 String className;
87 String subId = null;
88 if (base > 0) {
89 int classNameStart = base + myID.length() + 1;
90 className = id.substring(classNameStart);
91 } else {
92 className = id;
93 }
94 int classNameEnd = className.indexOf('/');
95 if (classNameEnd > 0) {
96 subId = className.substring(classNameEnd + 1);
97 if (subId.length() == 0) {
98 subId = null;
99 }
100 className = className.substring(0, classNameEnd);
101 }
102
103 ClassResult child = getClassResult(className);
104 if (child != null) {
105 if (subId != null) {
106 return child.findCorrespondingResult(subId);
107 } else {
108 return child;
109 }
110 }
111
112 return null;
113 }
114
115 @Override
116 public String getTitle() {
117 return Messages.PackageResult_getTitle(getName());
118 }
119
120 @Override
121 public String getChildTitle() {
122 return Messages.PackageResult_getChildTitle();
123 }
124
125 // TODO: wait until stapler 1.60 to do this @Exported
126 @Override
127 public float getDuration() {
128 return duration;
129 }
130
131 @Exported
132 @Override
133 public int getPassCount() {
134 return passCount;
135 }
136
137 @Exported
138 @Override
139 public int getFailCount() {
140 return failCount;
141 }
142
143 @Exported
144 @Override
145 public int getSkipCount() {
146 return skipCount;
147 }
148
149 @Override
150 public Object getDynamic(String name, StaplerRequest req, StaplerResponse rsp) {
151 ClassResult result = getClassResult(name);
152 if (result != null) {
153 return result;
154 } else {
155 return super.getDynamic(name, req, rsp);
156 }
157 }
158
159 public ClassResult getClassResult(String name) {
160 return classes.get(name);
161 }
162
163 @Exported(name="child")
164 public Collection<ClassResult> getChildren() {
165 return classes.values();
166 }
167
168 /**
169 * Whether this test result has children.
170 */
171 @Override
172 public boolean hasChildren() {
173 int totalTests = passCount + failCount + skipCount;
174 return (totalTests != 0);
175 }
176
177 /**
178 * Returns a list of the failed cases, in no particular
179 * sort order
180 * @return
181 */
182 public List<CaseResult> getFailedTests() {
183 List<CaseResult> r = new ArrayList<CaseResult>();
184 for (ClassResult clr : classes.values()) {
185 for (CaseResult cr : clr.getChildren()) {
186 if (!cr.isPassed() && !cr.isSkipped()) {
187 r.add(cr);
188 }
189 }
190 }
191 return r;
192 }
193
194 /**
195 * Returns a list of the failed cases, sorted by age.
196 * @return
197 */
198 public List<CaseResult> getFailedTestsSortedByAge() {
199 List<CaseResult> failedTests = getFailedTests();
200 Collections.sort(failedTests, CaseResult.BY_AGE);
201 return failedTests;
202 }
203
204 /**
205 * Gets the "children" of this test result that passed
206 *
207 * @return the children of this test result, if any, or an empty collection
208 */
209 @Override
210 public Collection<? extends hudson.tasks.test.TestResult> getPassedTests() {
211 List<CaseResult> r = new ArrayList<CaseResult>();
212 for (ClassResult clr : classes.values()) {
213 for (CaseResult cr : clr.getChildren()) {
214 if (cr.isPassed()) {
215 r.add(cr);
216 }
217 }
218 }
219 Collections.sort(r,CaseResult.BY_AGE);
220 return r;
221 }
222
223 /**
224 * Gets the "children" of this test result that were skipped
225 *
226 * @return the children of this test result, if any, or an empty list
227 */
228 @Override
229 public Collection<? extends TestResult> getSkippedTests() {
230 List<CaseResult> r = new ArrayList<CaseResult>();
231 for (ClassResult clr : classes.values()) {
232 for (CaseResult cr : clr.getChildren()) {
233 if (cr.isSkipped()) {
234 r.add(cr);
235 }
236 }
237 }
238 Collections.sort(r, CaseResult.BY_AGE);
239 return r;
240 }
241
242 // /**
243 // * If this test failed, then return the build number
244 // * when this test started failing.
245 // */
246 // @Override
247 // TODO: implement! public int getFailedSince() {
248 // return 0; // (FIXME: generated)
249 // }
250 // /**
251 // * If this test failed, then return the run
252 // * when this test started failing.
253 // */
254 // TODO: implement! @Override
255 // public Run<?, ?> getFailedSinceRun() {
256 // return null; // (FIXME: generated)
257 // }
258 /**
259 * @return true if every test was not skipped and every test did not fail, false otherwise.
260 */
261 @Override
262 public boolean isPassed() {
263 return (failCount == 0 && skipCount == 0);
264 }
265
266 void add(CaseResult r) {
267 String n = r.getSimpleName(), sn = safe(n);
268 ClassResult c = getClassResult(sn);
269 if (c == null) {
270 classes.put(sn,c=new ClassResult(this,n));
271 }
272 c.add(r);
273 duration += r.getDuration();
274 }
275
276 /**
277 * Recount my children
278 */
279 @Override
280 public void tally() {
281 passCount = 0;
282 failCount = 0;
283 skipCount = 0;
284 duration = 0;
285
286 for (ClassResult cr : classes.values()) {
287 cr.tally();
288 passCount += cr.getPassCount();
289 failCount += cr.getFailCount();
290 skipCount += cr.getSkipCount();
291 duration += cr.getDuration();
292 }
293 }
294
295 void freeze() {
296 passCount = failCount = skipCount = 0;
297 for (ClassResult cr : classes.values()) {
298 cr.freeze();
299 passCount += cr.getPassCount();
300 failCount += cr.getFailCount();
301 skipCount += cr.getSkipCount();
302 }
303 }
304
305 public int compareTo(PackageResult that) {
306 return this.packageName.compareTo(that.packageName);
307 }
308
309 public String getDisplayName() {
310 return packageName;
311 }
312 }
313