Clover coverage report -
Coverage timestamp: Sat Jul 7 2007 16:41:13 CEST
file stats: LOC: 263   Methods: 8
NCLOC: 210   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ExcelExporter.java 90% 97.8% 100% 97.4%
coverage coverage
 1    /**
 2    * JTRunner is free software; you can redistribute it and/or modify it under the
 3    * terms of the GNU General Public License as published by the Free Software
 4    * Foundation; either version 2, or (at your option) any later version.
 5    */
 6   
 7    package jtr.test.results.exporters.impl;
 8   
 9    import java.io.File;
 10    import java.io.FileNotFoundException;
 11    import java.io.FileOutputStream;
 12    import java.io.IOException;
 13    import java.util.List;
 14    import java.util.Set;
 15    import java.util.SortedSet;
 16   
 17    import jtr.remote.test.NodeInfo;
 18    import jtr.test.IOutcome;
 19    import jtr.test.TestOutcomeTable;
 20    import jtr.test.results.exporters.ITestResultsExporter;
 21   
 22    import org.apache.poi.hssf.usermodel.HSSFCell;
 23    import org.apache.poi.hssf.usermodel.HSSFCellStyle;
 24    import org.apache.poi.hssf.usermodel.HSSFDataFormat;
 25    import org.apache.poi.hssf.usermodel.HSSFFont;
 26    import org.apache.poi.hssf.usermodel.HSSFRow;
 27    import org.apache.poi.hssf.usermodel.HSSFSheet;
 28    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 29    import org.apache.poi.hssf.util.HSSFColor;
 30   
 31    /**
 32    * Implementation of the <code>ITestResultExporter</code> interface able to export
 33    * JTR test results in Excel format.
 34    *
 35    * @author frusso
 36    * @version 4.0
 37    * @since 4.0
 38    */
 39    public class ExcelExporter implements ITestResultsExporter {
 40   
 41    /** Creates a new instance of ExcelExporter */
 42  2 public ExcelExporter() {
 43  2 workbook = new HSSFWorkbook();
 44   
 45  2 boldStyle = workbook.createCellStyle();
 46  2 HSSFFont font = workbook.createFont();
 47  2 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
 48  2 boldStyle.setFont(font);
 49  2 boldStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
 50   
 51  2 errorStyle = workbook.createCellStyle();
 52  2 HSSFFont errFont = workbook.createFont();
 53  2 errFont.setColor(HSSFColor.RED.index);
 54  2 errFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
 55  2 errorStyle.setFont(errFont);
 56   
 57  2 normalStyle = workbook.createCellStyle();
 58  2 HSSFFont normalFont = workbook.createFont();
 59  2 normalFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
 60  2 normalStyle.setFont(normalFont);
 61   
 62  2 dateStyle = workbook.createCellStyle();
 63  2 dateStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("h:mm:ss"));
 64   
 65  2 dateErrorStyle = workbook.createCellStyle();
 66  2 dateErrorStyle.setFont(errFont);
 67  2 dateErrorStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("h:mm:ss"));
 68    }
 69   
 70  2 public void export(NodeInfo n, TestOutcomeTable results) {
 71  2 prepareHeader(n);
 72  2 rowNum = 3;
 73   
 74  2 Set<NodeInfo> nodes = results.getNodes();
 75  2 for(NodeInfo node : nodes) {
 76  2 Set<Class> categories = results.getCategories(node);
 77  2 for(Class category : categories) {
 78    // given a category we obtain all of its instances
 79  12 outputCategory(category);
 80  12 Set<String> instances = results.getInstances(category);
 81  12 outputInstances(instances);
 82  12 short tmp = rowNum;
 83  12 short colNum = (new Integer(STATUS_COL+1)).shortValue();
 84  12 for(String instance : instances) {
 85  98 rowNum = tmp;
 86  98 outputDurations(results.getOrderedOutcomes(instance),colNum);
 87  98 colNum = (new Integer(colNum+3)).shortValue();
 88    }
 89  12 rowNum++;
 90  12 rowNum++;
 91    }
 92    }
 93    }
 94   
 95  2 private void prepareHeader(NodeInfo node) {
 96  2 sheet = workbook.createSheet(node.getHost()+"_"+node.getPort());
 97   
 98  2 HSSFRow row = sheet.createRow(HEADER_ROW);
 99  2 HSSFCell cell = row.createCell(HEADER_COL);
 100   
 101  2 cell = row.createCell(HEADER_COL);
 102  2 cell.setCellStyle(boldStyle);
 103  2 cell.setCellType(HSSFCell.CELL_TYPE_STRING);
 104  2 cell.setCellValue(node.toString());
 105  2 short width = sheet.getColumnWidth(HEADER_COL);
 106  2 short newWidth = new Integer((node.toString().length()+5)*256).shortValue();
 107  2 if(width<newWidth)
 108  2 sheet.setColumnWidth(HEADER_COL,new Integer((node.toString().length()+5)*256).shortValue());
 109   
 110  2 outputEpochsRunHeader(HEADER_ROW);
 111    }
 112   
 113  14 private void outputEpochsRunHeader(int row) {
 114  14 HSSFRow r = sheet.createRow(row);
 115   
 116  14 HSSFCell cell = r.createCell(EPOCHS_COL);
 117  14 cell.setCellStyle(boldStyle);
 118  14 cell.setCellType(HSSFCell.CELL_TYPE_STRING);
 119  14 cell.setCellValue(EPOCHS_LABEL);
 120   
 121  14 cell = r.createCell(RUNS_COL);
 122  14 cell.setCellStyle(boldStyle);
 123  14 cell.setCellType(HSSFCell.CELL_TYPE_STRING);
 124  14 cell.setCellValue(RUNS_LABEL);
 125    }
 126   
 127  12 private void outputCategory(Class category) {
 128  12 HSSFRow row = sheet.createRow(rowNum-1);
 129  12 short colNum = STATUS_COL+1;
 130  12 HSSFCell cell = row.createCell(colNum);
 131  12 cell.setCellStyle(boldStyle);
 132  12 cell.setCellType(cell.CELL_TYPE_STRING);
 133  12 cell.setCellValue(category.getSimpleName());
 134   
 135  12 outputEpochsRunHeader(rowNum);
 136    }
 137   
 138  12 private void outputInstances(Set<String> instances) {
 139  12 HSSFRow row = sheet.createRow(rowNum);
 140  12 short colNum = PROG_COL;
 141  12 for(String instance : instances) {
 142  98 HSSFCell cell = row.createCell(colNum);
 143  98 cell.setCellStyle(boldStyle);
 144  98 cell.setCellType(HSSFCell.CELL_TYPE_STRING);
 145  98 cell.setCellValue(PROG_LABEL);
 146  98 sheet.setColumnWidth(colNum,new Integer((PROG_LABEL.length()+5)*256).shortValue());
 147   
 148  98 colNum++;
 149  98 cell = row.createCell(colNum);
 150  98 cell.setCellStyle(boldStyle);
 151  98 cell.setCellType(cell.CELL_TYPE_STRING);
 152  98 cell.setCellValue(STATUS_LABEL);
 153   
 154  98 colNum++;
 155  98 cell = row.createCell(colNum);
 156  98 cell.setCellStyle(boldStyle);
 157  98 cell.setCellType(cell.CELL_TYPE_STRING);
 158  98 cell.setCellValue(instance);
 159  98 short width = sheet.getColumnWidth(colNum);
 160  98 short newWidth = new Integer((instance.length()+10)*256).shortValue();
 161  98 if(width<newWidth)
 162  34 sheet.setColumnWidth(colNum,newWidth);
 163  98 colNum++;
 164    }
 165  12 rowNum++;
 166    }
 167   
 168  98 private void outputDurations(SortedSet<IOutcome> outcomes, short colNum) {
 169  98 for(IOutcome outcome : outcomes) {
 170  44400 HSSFRow row = sheet.createRow(rowNum);
 171   
 172  44400 HSSFCell cell = row.createCell(EPOCHS_COL);
 173  44400 cell.setCellStyle(normalStyle);
 174  44400 cell.getCellStyle().setAlignment(HSSFCellStyle.ALIGN_RIGHT);
 175  44400 cell.setCellType(cell.CELL_TYPE_NUMERIC);
 176  44400 cell.setCellValue(outcome.getEpoch().doubleValue());
 177   
 178  44400 cell = row.createCell(RUNS_COL);
 179  44400 cell.setCellStyle(normalStyle);
 180  44400 cell.getCellStyle().setAlignment(HSSFCellStyle.ALIGN_RIGHT);
 181  44400 cell.setCellType(cell.CELL_TYPE_NUMERIC);
 182  44400 cell.setCellValue(outcome.getRun().doubleValue());
 183   
 184  44400 short pos = (new Integer(colNum-2)).shortValue();
 185  44400 cell = row.createCell(pos);
 186  44400 if(outcome.isFailure()) {
 187  12004 cell.setCellStyle(dateErrorStyle);
 188    } else {
 189  32396 cell.setCellStyle(dateStyle);
 190    }
 191  44400 cell.getCellStyle().setAlignment(HSSFCellStyle.ALIGN_RIGHT);
 192  44400 cell.setCellValue(outcome.getTimeStamp());
 193   
 194    //
 195  44400 pos = (new Integer(colNum-1)).shortValue();
 196  44400 cell = row.createCell(pos);
 197  44400 cell.setCellStyle(normalStyle);
 198  44400 cell.getCellStyle().setAlignment(HSSFCellStyle.ALIGN_RIGHT);
 199  44400 cell.setCellType(cell.CELL_TYPE_BOOLEAN);
 200  44400 if(outcome.isFailure()) {
 201  12004 cell.setCellStyle(dateErrorStyle);
 202  12004 cell.setCellValue(FAILURE_CONSTANT);
 203    } else {
 204  32396 cell.setCellStyle(dateStyle);
 205  32396 cell.setCellValue(SUCCESS_CONSTANT);
 206    }
 207   
 208  44400 cell = row.createCell(colNum);
 209  44400 if(outcome.isFailure()) {
 210  12004 cell.setCellStyle(errorStyle);
 211    } else {
 212  32396 cell.setCellStyle(normalStyle);
 213    }
 214  44400 cell.setCellType(cell.CELL_TYPE_NUMERIC);
 215  44400 cell.setCellValue(outcome.getRunDuration().doubleValue());
 216   
 217  44400 rowNum++;
 218    }
 219    }
 220   
 221  2 public void flush(File outputFile) {
 222  2 try {
 223  2 FileOutputStream fos = new FileOutputStream(outputFile);
 224  2 try {
 225  2 sheet.setZoom(3,4);
 226  2 workbook.write(fos);
 227    } catch (IOException ex) {
 228  0 ex.printStackTrace();
 229    }
 230  2 try {
 231  2 fos.close();
 232    } catch (IOException ex) {
 233  0 ex.printStackTrace();
 234    }
 235    } catch (FileNotFoundException ex) {
 236  0 ex.printStackTrace();
 237    }
 238    }
 239   
 240    private static short HEADER_ROW = 3;
 241    private static short HEADER_COL = 0;
 242    private static String EPOCHS_LABEL = "Epochs";
 243    private static final short EPOCHS_COL = 1;
 244    private static String RUNS_LABEL = "Runs";
 245    private static final short RUNS_COL = 2;
 246    private static String PROG_LABEL = "Start Time";
 247    private static final short PROG_COL = 3;
 248    private static String STATUS_LABEL = "Status";
 249    private static final short STATUS_COL = 4;
 250   
 251    private HSSFWorkbook workbook;
 252    private HSSFSheet sheet;
 253    private HSSFCellStyle boldStyle;
 254    private HSSFCellStyle errorStyle;
 255    private HSSFCellStyle normalStyle;
 256    private HSSFCellStyle dateStyle;
 257    private HSSFCellStyle dateErrorStyle;
 258   
 259    private short rowNum = 3;
 260   
 261    private boolean SUCCESS_CONSTANT = true;
 262    private boolean FAILURE_CONSTANT = false;
 263    }