KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > tasks > test > AbstractTestResultAction


1 package hudson.tasks.test;
2
3 import hudson.Functions;
4 import hudson.Util;
5 import hudson.model.Action;
6 import hudson.model.Build;
7 import hudson.model.Project;
8 import hudson.model.Result;
9 import hudson.model.AbstractBuild;
10 import hudson.util.ChartUtil;
11 import hudson.util.ChartUtil.NumberOnlyBuildLabel;
12 import hudson.util.ColorPalette;
13 import hudson.util.DataSetBuilder;
14 import hudson.util.ShiftedCategoryAxis;
15 import hudson.util.StackedAreaRenderer2;
16 import org.jfree.chart.ChartFactory;
17 import org.jfree.chart.JFreeChart;
18 import org.jfree.chart.axis.CategoryAxis;
19 import org.jfree.chart.axis.CategoryLabelPositions;
20 import org.jfree.chart.axis.NumberAxis;
21 import org.jfree.chart.plot.CategoryPlot;
22 import org.jfree.chart.plot.PlotOrientation;
23 import org.jfree.chart.renderer.category.StackedAreaRenderer;
24 import org.jfree.data.category.CategoryDataset;
25 import org.jfree.ui.RectangleInsets;
26 import org.kohsuke.stapler.StaplerRequest;
27 import org.kohsuke.stapler.StaplerResponse;
28
29 import java.awt.Color JavaDoc;
30 import java.io.IOException JavaDoc;
31
32 /**
33  * Common base class for recording test result.
34  *
35  * <p>
36  * {@link Project} and {@link Build} recognizes {@link Action}s that derive from this,
37  * and displays it nicely (regardless of the underlying implementation.)
38  *
39  * @author Kohsuke Kawaguchi
40  */

41 public abstract class AbstractTestResultAction<T extends AbstractTestResultAction> implements Action {
42     public final AbstractBuild<?,?> owner;
43
44     protected AbstractTestResultAction(AbstractBuild owner) {
45         this.owner = owner;
46     }
47
48     /**
49      * Gets the number of failed tests.
50      */

51     public abstract int getFailCount();
52
53     /**
54      * Gets the total number of tests.
55      */

56     public abstract int getTotalCount();
57
58     /**
59      * Gets the diff string of failures.
60      */

61     public final String JavaDoc getFailureDiffString() {
62         T prev = getPreviousResult();
63         if(prev==null) return ""; // no record
64

65         return " / "+Functions.getDiffString(this.getFailCount()-prev.getFailCount());
66     }
67
68     public String JavaDoc getDisplayName() {
69         return "Test Result";
70     }
71
72     public String JavaDoc getUrlName() {
73         return "testReport";
74     }
75
76     public String JavaDoc getIconFileName() {
77         return "clipboard.gif";
78     }
79
80     /**
81      * Gets the test result of the previous build, if it's recorded, or null.
82      */

83     public T getPreviousResult() {
84         return (T)getPreviousResult(getClass());
85     }
86
87     private <U extends AbstractTestResultAction> U getPreviousResult(Class JavaDoc<U> type) {
88         AbstractBuild<?,?> b = owner;
89         while(true) {
90             b = b.getPreviousBuild();
91             if(b==null)
92                 return null;
93             if(b.getResult()== Result.FAILURE)
94                 continue;
95             U r = b.getAction(type);
96             if(r!=null)
97                 return r;
98         }
99     }
100
101     /**
102      * Generates a PNG image for the test result trend.
103      */

104     public void doGraph( StaplerRequest req, StaplerResponse rsp) throws IOException JavaDoc {
105         if(ChartUtil.awtProblem) {
106             // not available. send out error message
107
rsp.sendRedirect2(req.getContextPath()+"/images/headless.png");
108             return;
109         }
110
111         if(req.checkIfModified(owner.getTimestamp(),rsp))
112             return;
113
114         ChartUtil.generateGraph(req,rsp,createChart(req,buildDataSet(req)),500,200);
115     }
116
117     /**
118      * Generates a clickable map HTML for {@link #doGraph(StaplerRequest, StaplerResponse)}.
119      */

120     public void doGraphMap( StaplerRequest req, StaplerResponse rsp) throws IOException JavaDoc {
121         if(req.checkIfModified(owner.getTimestamp(),rsp))
122             return;
123         ChartUtil.generateClickableMap(req,rsp,createChart(req,buildDataSet(req)),500,200);
124     }
125
126     private CategoryDataset buildDataSet(StaplerRequest req) {
127         boolean failureOnly = Boolean.valueOf(req.getParameter("failureOnly"));
128
129         DataSetBuilder<String JavaDoc,NumberOnlyBuildLabel> dsb = new DataSetBuilder<String JavaDoc,NumberOnlyBuildLabel>();
130
131         for( AbstractTestResultAction<?> a=this; a!=null; a=a.getPreviousResult(AbstractTestResultAction.class) ) {
132             dsb.add( a.getFailCount(), "failed", new NumberOnlyBuildLabel(a.owner));
133             if(!failureOnly)
134                 dsb.add( a.getTotalCount()-a.getFailCount(),"total", new NumberOnlyBuildLabel(a.owner));
135         }
136         return dsb.build();
137     }
138
139     private JFreeChart createChart(StaplerRequest req,CategoryDataset dataset) {
140
141         final String JavaDoc relPath = getRelPath(req);
142
143         final JFreeChart chart = ChartFactory.createStackedAreaChart(
144             null, // chart title
145
null, // unused
146
"count", // range axis label
147
dataset, // data
148
PlotOrientation.VERTICAL, // orientation
149
false, // include legend
150
true, // tooltips
151
false // urls
152
);
153
154         // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
155

156         // set the background color for the chart...
157

158 // final StandardLegend legend = (StandardLegend) chart.getLegend();
159
// legend.setAnchor(StandardLegend.SOUTH);
160

161         chart.setBackgroundPaint(Color.white);
162
163         final CategoryPlot plot = chart.getCategoryPlot();
164
165         // plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
166
plot.setBackgroundPaint(Color.WHITE);
167         plot.setOutlinePaint(null);
168         plot.setForegroundAlpha(0.8f);
169 // plot.setDomainGridlinesVisible(true);
170
// plot.setDomainGridlinePaint(Color.white);
171
plot.setRangeGridlinesVisible(true);
172         plot.setRangeGridlinePaint(Color.black);
173
174         CategoryAxis domainAxis = new ShiftedCategoryAxis(null);
175         plot.setDomainAxis(domainAxis);
176         domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
177         domainAxis.setLowerMargin(0.0);
178         domainAxis.setUpperMargin(0.0);
179         domainAxis.setCategoryMargin(0.0);
180
181         final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
182         rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
183
184         StackedAreaRenderer ar = new StackedAreaRenderer2() {
185             @Override JavaDoc
186             public String JavaDoc generateURL(CategoryDataset dataset, int row, int column) {
187                 NumberOnlyBuildLabel label = (NumberOnlyBuildLabel) dataset.getColumnKey(column);
188                 return relPath+label.build.getNumber()+"/testReport/";
189             }
190
191             @Override JavaDoc
192             public String JavaDoc generateToolTip(CategoryDataset dataset, int row, int column) {
193                 NumberOnlyBuildLabel label = (NumberOnlyBuildLabel) dataset.getColumnKey(column);
194                 AbstractTestResultAction a = label.build.getAction(AbstractTestResultAction.class);
195                 if(row==0)
196                     return String.valueOf(Util.combine(a.getFailCount(),"failure"));
197                 else
198                     return String.valueOf(Util.combine(a.getTotalCount(),"test"));
199             }
200         };
201         plot.setRenderer(ar);
202         ar.setSeriesPaint(0,ColorPalette.RED);
203         ar.setSeriesPaint(1,ColorPalette.BLUE);
204
205         // crop extra space around the graph
206
plot.setInsets(new RectangleInsets(0,0,0,5.0));
207
208         return chart;
209     }
210
211     private String JavaDoc getRelPath(StaplerRequest req) {
212         String JavaDoc relPath = req.getParameter("rel");
213         if(relPath==null) return "";
214         return relPath;
215     }
216 }
217
Popular Tags