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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Tom Huybrechts, Yahoo!, Inc., Seiji Sogabe
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 jenkins.model.Jenkins;
28 import hudson.tasks.test.TestObject;
29 import hudson.tasks.test.TestResult;
30 import hudson.util.ChartUtil;
31 import hudson.util.ColorPalette;
32 import hudson.util.DataSetBuilder;
33 import hudson.util.Graph;
34 import hudson.util.ShiftedCategoryAxis;
35 import hudson.util.StackedAreaRenderer2;
36
37 import java.awt.Color;
38 import java.awt.Paint;
39 import java.util.ArrayList;
40 import java.util.List;
41
42 import org.jfree.chart.ChartFactory;
43 import org.jfree.chart.JFreeChart;
44 import org.jfree.chart.axis.CategoryAxis;
45 import org.jfree.chart.axis.CategoryLabelPositions;
46 import org.jfree.chart.axis.NumberAxis;
47 import org.jfree.chart.plot.CategoryPlot;
48 import org.jfree.chart.plot.PlotOrientation;
49 import org.jfree.chart.renderer.category.StackedAreaRenderer;
50 import org.jfree.data.category.CategoryDataset;
51 import org.jfree.ui.RectangleInsets;
52 import org.kohsuke.stapler.Stapler;
53
54 /**
55 * History of {@link hudson.tasks.test.TestObject} over time.
56 *
57 * @since 1.320
58 */
59 public class History {
60 private final TestObject testObject;
61
62 public History(TestObject testObject) {
63 this.testObject = testObject;
64 }
65
66 public TestObject getTestObject() {
67 return testObject;
68 }
69
70 public boolean historyAvailable() {
71 if (testObject.getOwner().getParent().getBuilds().size() > 1)
72 return true;
73 else
74 return false;
75 }
76
77 public List<TestResult> getList(int start, int end) {
78 List<TestResult> list = new ArrayList<TestResult>();
79 end = Math.min(end, testObject.getOwner().getParent().getBuilds().size());
80 for (AbstractBuild<?,?> b: testObject.getOwner().getParent().getBuilds().subList(start, end)) {
81 if (b.isBuilding()) continue;
82 TestResult o = testObject.getResultInBuild(b);
83 if (o != null) {
84 list.add(o);
85 }
86 }
87 return list;
88 }
89
90 public List<TestResult> getList() {
91 return getList(0, testObject.getOwner().getParent().getBuilds().size());
92 }
93
94 /**
95 * Graph of duration of tests over time.
96 */
97 public Graph getDurationGraph() {
98 return new GraphImpl("seconds") {
99
100 protected DataSetBuilder<String, ChartLabel> createDataSet() {
101 DataSetBuilder<String, ChartLabel> data = new DataSetBuilder<String, ChartLabel>();
102
103 List<TestResult> list;
104 try {
105 list = getList(
106 Integer.parseInt(Stapler.getCurrentRequest().getParameter("start")),
107 Integer.parseInt(Stapler.getCurrentRequest().getParameter("end")));
108 } catch (NumberFormatException e) {
109 list = getList();
110 }
111
112 for (hudson.tasks.test.TestResult o: list) {
113 data.add(((double) o.getDuration()) / (1000), "", new ChartLabel(o) {
114 @Override
115 public Color getColor() {
116 if (o.getFailCount() > 0)
117 return ColorPalette.RED;
118 else if (o.getSkipCount() > 0)
119 return ColorPalette.YELLOW;
120 else
121 return ColorPalette.BLUE;
122 }
123 });
124 }
125 return data;
126 }
127
128 };
129 }
130
131 /**
132 * Graph of # of tests over time.
133 */
134 public Graph getCountGraph() {
135 return new GraphImpl("") {
136 protected DataSetBuilder<String, ChartLabel> createDataSet() {
137 DataSetBuilder<String, ChartLabel> data = new DataSetBuilder<String, ChartLabel>();
138
139 List<TestResult> list;
140 try {
141 list = getList(
142 Integer.parseInt(Stapler.getCurrentRequest().getParameter("start")),
143 Integer.parseInt(Stapler.getCurrentRequest().getParameter("end")));
144 } catch (NumberFormatException e) {
145 list = getList();
146 }
147
148 for (TestResult o: list) {
149 data.add(o.getPassCount(), "2Passed", new ChartLabel(o));
150 data.add(o.getFailCount(), "1Failed", new ChartLabel(o));
151 data.add(o.getSkipCount(), "0Skipped", new ChartLabel(o));
152 }
153 return data;
154 }
155 };
156 }
157
158 private abstract class GraphImpl extends Graph {
159 private final String yLabel;
160
161 protected GraphImpl(String yLabel) {
162 super(-1,600,300); // cannot use timestamp, since ranges may change
163 this.yLabel = yLabel;
164 }
165
166 protected abstract DataSetBuilder<String, ChartLabel> createDataSet();
167
168 protected JFreeChart createGraph() {
169 final CategoryDataset dataset = createDataSet().build();
170
171 final JFreeChart chart = ChartFactory.createStackedAreaChart(null, // chart
172 // title
173 null, // unused
174 yLabel, // range axis label
175 dataset, // data
176 PlotOrientation.VERTICAL, // orientation
177 false, // include legend
178 true, // tooltips
179 false // urls
180 );
181
182 chart.setBackgroundPaint(Color.white);
183
184 final CategoryPlot plot = chart.getCategoryPlot();
185
186 // plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
187 plot.setBackgroundPaint(Color.WHITE);
188 plot.setOutlinePaint(null);
189 plot.setForegroundAlpha(0.8f);
190 // plot.setDomainGridlinesVisible(true);
191 // plot.setDomainGridlinePaint(Color.white);
192 plot.setRangeGridlinesVisible(true);
193 plot.setRangeGridlinePaint(Color.black);
194
195 CategoryAxis domainAxis = new ShiftedCategoryAxis(null);
196 plot.setDomainAxis(domainAxis);
197 domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
198 domainAxis.setLowerMargin(0.0);
199 domainAxis.setUpperMargin(0.0);
200 domainAxis.setCategoryMargin(0.0);
201
202 final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
203 ChartUtil.adjustChebyshev(dataset, rangeAxis);
204 rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
205 rangeAxis.setAutoRange(true);
206
207 StackedAreaRenderer ar = new StackedAreaRenderer2() {
208 @Override
209 public Paint getItemPaint(int row, int column) {
210 ChartLabel key = (ChartLabel) dataset.getColumnKey(column);
211 if (key.getColor() != null) return key.getColor();
212 return super.getItemPaint(row, column);
213 }
214
215 @Override
216 public String generateURL(CategoryDataset dataset, int row,
217 int column) {
218 ChartLabel label = (ChartLabel) dataset.getColumnKey(column);
219 return label.getUrl();
220 }
221
222 @Override
223 public String generateToolTip(CategoryDataset dataset, int row,
224 int column) {
225 ChartLabel label = (ChartLabel) dataset.getColumnKey(column);
226 return label.o.getOwner().getDisplayName() + " : "
227 + label.o.getDurationString();
228 }
229 };
230 plot.setRenderer(ar);
231 ar.setSeriesPaint(0,ColorPalette.RED); // Failures.
232 ar.setSeriesPaint(1,ColorPalette.YELLOW); // Skips.
233 ar.setSeriesPaint(2,ColorPalette.BLUE); // Total.
234
235 // crop extra space around the graph
236 plot.setInsets(new RectangleInsets(0, 0, 0, 5.0));
237
238 return chart;
239 }
240 }
241
242 class ChartLabel implements Comparable<ChartLabel> {
243 TestResult o;
244 String url;
245 public ChartLabel(TestResult o) {
246 this.o = o;
247 this.url = null;
248 }
249
250 public String getUrl() {
251 if (this.url == null) generateUrl();
252 return url;
253 }
254
255 private void generateUrl() {
256 AbstractBuild<?,?> build = o.getOwner();
257 String buildLink = build.getUrl();
258 String actionUrl = o.getTestResultAction().getUrlName();
259 this.url = Jenkins.getInstance().getRootUrl() + buildLink + actionUrl + o.getUrl();
260 }
261
262 public int compareTo(ChartLabel that) {
263 return this.o.getOwner().number - that.o.getOwner().number;
264 }
265
266 @Override
267 public boolean equals(Object o) {
268 if (!(o instanceof ChartLabel)) {
269 return false;
270 }
271 ChartLabel that = (ChartLabel) o;
272 return this.o == that.o;
273 }
274
275 public Color getColor() {
276 return null;
277 }
278
279 @Override
280 public int hashCode() {
281 return o.hashCode();
282 }
283
284 @Override
285 public String toString() {
286 String l = o.getOwner().getDisplayName();
287 String s = o.getOwner().getBuiltOnStr();
288 if (s != null)
289 l += ' ' + s;
290 return l;
291 // return o.getDisplayName() + " " + o.getOwner().getDisplayName();
292 }
293
294 }
295
296 public static int asInt(String s, int defalutValue) {
297 if (s==null) return defalutValue;
298 try {
299 return Integer.parseInt(s);
300 } catch (NumberFormatException e) {
301 return defalutValue;
302 }
303 }
304 }
305