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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Daniel Dyer, Red Hat, Inc., 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 com.thoughtworks.xstream.XStream;
27 import hudson.XmlFile;
28 import hudson.model.AbstractBuild;
29 import hudson.model.Action;
30 import hudson.model.BuildListener;
31 import hudson.tasks.test.AbstractTestResultAction;
32 import hudson.tasks.test.TestObject;
33 import hudson.util.HeapSpaceStringConverter;
34 import hudson.util.XStream2;
35 import org.kohsuke.stapler.StaplerProxy;
36
37 import java.io.File;
38 import java.io.IOException;
39 import java.lang.ref.WeakReference;
40 import java.util.ArrayList;
41 import java.util.Collections;
42 import java.util.List;
43 import java.util.logging.Level;
44 import java.util.logging.Logger;
45
46 /**
47 * {@link Action} that displays the JUnit test result.
48 *
49 * <p>
50 * The actual test reports are isolated by {@link WeakReference}
51 * so that it doesn't eat up too much memory.
52 *
53 * @author Kohsuke Kawaguchi
54 */
55 public class TestResultAction extends AbstractTestResultAction<TestResultAction> implements StaplerProxy {
56 private transient WeakReference<TestResult> result;
57
58 // Hudson < 1.25 didn't set these fields, so use Integer
59 // so that we can distinguish between 0 tests vs not-computed-yet.
60 private int failCount;
61 private int skipCount;
62 private Integer totalCount;
63 private List<Data> testData = new ArrayList<Data>();
64
65 public TestResultAction(AbstractBuild owner, TestResult result, BuildListener listener) {
66 super(owner);
67 setResult(result, listener);
68 }
69
70 /**
71 * Overwrites the {@link TestResult} by a new data set.
72 */
73 public synchronized void setResult(TestResult result, BuildListener listener) {
74 result.freeze(this);
75
76 totalCount = result.getTotalCount();
77 failCount = result.getFailCount();
78 skipCount = result.getSkipCount();
79
80 // persist the data
81 try {
82 getDataFile().write(result);
83 } catch (IOException e) {
84 e.printStackTrace(listener.fatalError("Failed to save the JUnit test result"));
85 }
86
87 this.result = new WeakReference<TestResult>(result);
88 }
89
90 private XmlFile getDataFile() {
91 return new XmlFile(XSTREAM,new File(owner.getRootDir(), "junitResult.xml"));
92 }
93
94 public synchronized TestResult getResult() {
95 TestResult r;
96 if(result==null) {
97 r = load();
98 result = new WeakReference<TestResult>(r);
99 } else {
100 r = result.get();
101 }
102
103 if(r==null) {
104 r = load();
105 result = new WeakReference<TestResult>(r);
106 }
107 if(totalCount==null) {
108 totalCount = r.getTotalCount();
109 failCount = r.getFailCount();
110 skipCount = r.getSkipCount();
111 }
112 return r;
113 }
114
115 @Override
116 public int getFailCount() {
117 if(totalCount==null)
118 getResult(); // this will compute the result
119 return failCount;
120 }
121
122 @Override
123 public int getSkipCount() {
124 if(totalCount==null)
125 getResult(); // this will compute the result
126 return skipCount;
127 }
128
129 @Override
130 public int getTotalCount() {
131 if(totalCount==null)
132 getResult(); // this will compute the result
133 return totalCount;
134 }
135
136 @Override
137 public List<CaseResult> getFailedTests() {
138 return getResult().getFailedTests();
139 }
140
141 /**
142 * Loads a {@link TestResult} from disk.
143 */
144 private TestResult load() {
145 TestResult r;
146 try {
147 r = (TestResult)getDataFile().read();
148 } catch (IOException e) {
149 logger.log(Level.WARNING, "Failed to load "+getDataFile(),e);
150 r = new TestResult(); // return a dummy
151 }
152 r.freeze(this);
153 return r;
154 }
155
156 public Object getTarget() {
157 return getResult();
158 }
159
160 public List<TestAction> getActions(TestObject object) {
161 List<TestAction> result = new ArrayList<TestAction>();
162 // Added check for null testData to avoid NPE from issue 4257.
163 if (testData!=null) {
164 for (Data data : testData) {
165 result.addAll(data.getTestAction(object));
166 }
167 }
168 return Collections.unmodifiableList(result);
169
170 }
171 public void setData(List<Data> testData) {
172 this.testData = testData;
173 }
174
175 /**
176 * Resolves {@link TestAction}s for the given {@link TestObject}.
177 *
178 * <p>
179 * This object itself is persisted as a part of {@link AbstractBuild}, so it needs to be XStream-serializable.
180 *
181 * @see TestDataPublisher
182 */
183 public static abstract class Data {
184 /**
185 * Returns all TestActions for the testObject.
186 *
187 * @return
188 * Can be empty but never null. The caller must assume that the returned list is read-only.
189 */
190 public abstract List<? extends TestAction> getTestAction(hudson.tasks.junit.TestObject testObject);
191 }
192
193 public Object readResolve() {
194 super.readResolve(); // let it do the post-deserialization work
195 if (testData == null) {
196 testData = new ArrayList<Data>(0);
197 }
198
199 return this;
200 }
201
202 private static final Logger logger = Logger.getLogger(TestResultAction.class.getName());
203
204 private static final XStream XSTREAM = new XStream2();
205
206 static {
207 XSTREAM.alias("result",TestResult.class);
208 XSTREAM.alias("suite",SuiteResult.class);
209 XSTREAM.alias("case",CaseResult.class);
210 XSTREAM.registerConverter(new HeapSpaceStringConverter(),100);
211 }
212
213 }
214