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 ; 30 import java.io.IOException ; 31 32 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 51 public abstract int getFailCount(); 52 53 56 public abstract int getTotalCount(); 57 58 61 public final String getFailureDiffString() { 62 T prev = getPreviousResult(); 63 if(prev==null) return ""; 65 return " / "+Functions.getDiffString(this.getFailCount()-prev.getFailCount()); 66 } 67 68 public String getDisplayName() { 69 return "Test Result"; 70 } 71 72 public String getUrlName() { 73 return "testReport"; 74 } 75 76 public String getIconFileName() { 77 return "clipboard.gif"; 78 } 79 80 83 public T getPreviousResult() { 84 return (T)getPreviousResult(getClass()); 85 } 86 87 private <U extends AbstractTestResultAction> U getPreviousResult(Class <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 104 public void doGraph( StaplerRequest req, StaplerResponse rsp) throws IOException { 105 if(ChartUtil.awtProblem) { 106 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 120 public void doGraphMap( StaplerRequest req, StaplerResponse rsp) throws IOException { 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 ,NumberOnlyBuildLabel> dsb = new DataSetBuilder<String ,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 relPath = getRelPath(req); 142 143 final JFreeChart chart = ChartFactory.createStackedAreaChart( 144 null, null, "count", dataset, PlotOrientation.VERTICAL, false, true, false ); 153 154 156 158 161 chart.setBackgroundPaint(Color.white); 162 163 final CategoryPlot plot = chart.getCategoryPlot(); 164 165 plot.setBackgroundPaint(Color.WHITE); 167 plot.setOutlinePaint(null); 168 plot.setForegroundAlpha(0.8f); 169 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 186 public String 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 192 public String 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 plot.setInsets(new RectangleInsets(0,0,0,5.0)); 207 208 return chart; 209 } 210 211 private String getRelPath(StaplerRequest req) { 212 String relPath = req.getParameter("rel"); 213 if(relPath==null) return ""; 214 return relPath; 215 } 216 } 217 | Popular Tags |