Coverage Report - org.jbehave.core.reporters.TemplateableViewGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
TemplateableViewGenerator
95%
105/110
100%
20/20
1.838
TemplateableViewGenerator$1
100%
6/6
100%
8/8
1.838
TemplateableViewGenerator$Report
91%
31/34
83%
5/6
1.838
TemplateableViewGenerator$ReportCreationFailed
100%
2/2
N/A
1.838
TemplateableViewGenerator$ReportsTable
90%
30/33
87%
7/8
1.838
TemplateableViewGenerator$TimeFormatter
100%
9/9
N/A
1.838
TemplateableViewGenerator$ViewGenerationFailedForTemplate
100%
2/2
N/A
1.838
 
 1  
 package org.jbehave.core.reporters;
 2  
 
 3  
 import java.io.File;
 4  
 import java.io.FileInputStream;
 5  
 import java.io.FileReader;
 6  
 import java.io.FileWriter;
 7  
 import java.io.FilenameFilter;
 8  
 import java.io.InputStream;
 9  
 import java.io.Writer;
 10  
 import java.util.ArrayList;
 11  
 import java.util.Collection;
 12  
 import java.util.Collections;
 13  
 import java.util.Date;
 14  
 import java.util.Enumeration;
 15  
 import java.util.Formatter;
 16  
 import java.util.HashMap;
 17  
 import java.util.List;
 18  
 import java.util.Map;
 19  
 import java.util.Properties;
 20  
 import java.util.SortedMap;
 21  
 import java.util.TreeMap;
 22  
 
 23  
 import org.apache.commons.io.FilenameUtils;
 24  
 import org.apache.commons.io.IOUtils;
 25  
 import org.apache.commons.lang.builder.CompareToBuilder;
 26  
 import org.apache.commons.lang.builder.ToStringBuilder;
 27  
 import org.apache.commons.lang.builder.ToStringStyle;
 28  
 import org.jbehave.core.io.StoryNameResolver;
 29  
 import org.jbehave.core.model.StoryLanes;
 30  
 import org.jbehave.core.model.StoryMaps;
 31  
 
 32  
 import static java.util.Arrays.asList;
 33  
 
 34  
 /**
 35  
  * <p>
 36  
  * {@link ViewGenerator}, which uses the configured {@link TemplateProcessor} to
 37  
  * generate the views from templates. The default view properties are
 38  
  * overridable via the method {@link Properties} parameter. To override, specify
 39  
  * the path to the new template under the appropriate key:
 40  
  * 
 41  
  * <pre>
 42  
  * &quot;views&quot;: the path to global view template, including reports and maps views
 43  
  * &quot;maps&quot;: the path to the maps view template
 44  
  * &quot;reports&quot;: the path to the reports view template
 45  
  * &quot;decorated&quot;: the path to the template to generate a decorated (i.e. styled) single report
 46  
  * &quot;nonDecorated&quot;: the path to the template to generated a non decorated single report
 47  
  * </pre>
 48  
  * <p>
 49  
  * The view generator provides the following resources:
 50  
  * 
 51  
  * <pre>
 52  
  * &quot;decorateNonHtml&quot; = &quot;true&quot;
 53  
  * &quot;defaultFormats&quot; = &quot;stats&quot;
 54  
  * &quot;viewDirectory&quot; = &quot;view&quot;
 55  
  * </pre>
 56  
  * 
 57  
  * </p>
 58  
  * 
 59  
  * @author Mauro Talevi
 60  
  */
 61  
 public class TemplateableViewGenerator implements ViewGenerator {
 62  
 
 63  
     private final StoryNameResolver nameResolver;
 64  
     private final TemplateProcessor processor;
 65  
     private Properties viewProperties;
 66  936
     private List<Report> reports = new ArrayList<Report>();
 67  
 
 68  936
     public TemplateableViewGenerator(StoryNameResolver nameResolver, TemplateProcessor processor) {
 69  936
         this.nameResolver = nameResolver;
 70  936
         this.processor = processor;
 71  936
     }
 72  
 
 73  
     public Properties defaultViewProperties() {
 74  0
         Properties properties = new Properties();
 75  0
         properties.setProperty("decorateNonHtml", "true");
 76  0
         properties.setProperty("defaultFormats", "stats");
 77  0
         properties.setProperty("viewDirectory", "view");
 78  0
         return properties;
 79  
     }
 80  
 
 81  
     private Properties mergeWithDefault(Properties properties) {
 82  14
         Properties merged = defaultViewProperties();
 83  14
         merged.putAll(properties);
 84  14
         return merged;
 85  
     }
 86  
 
 87  
     private void generateViewsIndex(File outputDirectory) {
 88  13
         String outputName = templateResource("viewDirectory") + "/index.html";
 89  13
         String viewsTemplate = templateResource("views");
 90  13
         Map<String, Object> dataModel = newDataModel();
 91  13
         dataModel.put("date", new Date());
 92  13
         write(outputDirectory, outputName, viewsTemplate, dataModel);
 93  13
     }
 94  
 
 95  
     public void generateMapsView(File outputDirectory, StoryMaps storyMaps, Properties viewProperties) {
 96  1
         this.viewProperties = mergeWithDefault(viewProperties);
 97  1
         String outputName = templateResource("viewDirectory") + "/maps.html";
 98  1
         String mapsTemplate = templateResource("maps");
 99  1
         Map<String, Object> dataModel = newDataModel();
 100  1
         dataModel.put("storyLanes", new StoryLanes(storyMaps, nameResolver));
 101  1
         dataModel.put("date", new Date());
 102  1
         write(outputDirectory, outputName, mapsTemplate, dataModel);
 103  1
         generateViewsIndex(outputDirectory);
 104  1
     }
 105  
 
 106  
     public void generateReportsView(File outputDirectory, List<String> formats, Properties viewProperties) {
 107  13
         this.viewProperties = mergeWithDefault(viewProperties);
 108  13
         String outputName = templateResource("viewDirectory") + "/reports.html";
 109  13
         String reportsTemplate = templateResource("reports");
 110  13
         List<String> mergedFormats = mergeFormatsWithDefaults(formats);
 111  13
         reports = createReports(readReportFiles(outputDirectory, outputName, mergedFormats));
 112  13
         Map<String, Object> dataModel = newDataModel();
 113  13
         dataModel.put("reportsTable", new ReportsTable(reports, nameResolver));
 114  13
         dataModel.put("date", new Date());
 115  13
         dataModel.put("timeFormatter", new TimeFormatter());
 116  13
         write(outputDirectory, outputName, reportsTemplate, dataModel);
 117  12
         generateViewsIndex(outputDirectory);
 118  12
     }
 119  
 
 120  
     public ReportsCount getReportsCount() {
 121  10
         int stories = reports.size();
 122  10
         int storiesNotAllowed = count("notAllowed", reports);
 123  10
         int storiesPending = count("pending", reports);
 124  10
         int scenarios = count("scenarios", reports);
 125  10
         int scenariosFailed = count("scenariosFailed", reports);
 126  10
         int scenariosNotAllowed = count("scenariosNotAllowed", reports);
 127  10
         int scenariosPending = count("scenariosPending", reports);
 128  10
         int stepsFailed = count("stepsFailed", reports);
 129  10
         return new ReportsCount(stories, storiesNotAllowed, storiesPending, scenarios, scenariosFailed,
 130  
                 scenariosNotAllowed, scenariosPending, stepsFailed);
 131  
     }
 132  
 
 133  
     int count(String event, Collection<Report> reports) {
 134  72
         int count = 0;
 135  72
         for (Report report : reports) {
 136  93
             Properties stats = report.asProperties("stats");
 137  93
             if (stats.containsKey(event)) {
 138  1
                 count = count + Integer.parseInt((String) stats.get(event));
 139  
             }
 140  93
         }
 141  72
         return count;
 142  
     }
 143  
 
 144  
     private List<String> mergeFormatsWithDefaults(List<String> formats) {
 145  13
         List<String> merged = new ArrayList<String>();
 146  13
         merged.addAll(asList(templateResource("defaultFormats").split(",")));
 147  13
         merged.addAll(formats);
 148  13
         return merged;
 149  
     }
 150  
 
 151  
     List<Report> createReports(Map<String, List<File>> reportFiles) {
 152  
         try {
 153  14
             String decoratedTemplate = templateResource("decorated");
 154  13
             String nonDecoratedTemplate = templateResource("nonDecorated");
 155  13
             String viewDirectory = templateResource("viewDirectory");
 156  13
             boolean decorateNonHtml = Boolean.valueOf(templateResource("decorateNonHtml"));
 157  13
             List<Report> reports = new ArrayList<Report>();
 158  13
             for (String name : reportFiles.keySet()) {
 159  16
                 Map<String, File> filesByFormat = new HashMap<String, File>();
 160  16
                 for (File file : reportFiles.get(name)) {
 161  23
                     String fileName = file.getName();
 162  23
                     String format = FilenameUtils.getExtension(fileName);
 163  23
                     Map<String, Object> dataModel = newDataModel();
 164  23
                     dataModel.put("name", name);
 165  23
                     dataModel.put("body", IOUtils.toString(new FileReader(file)));
 166  23
                     dataModel.put("format", format);
 167  23
                     File outputDirectory = file.getParentFile();
 168  23
                     String outputName = viewDirectory + "/" + fileName;
 169  23
                     String template = decoratedTemplate;
 170  23
                     if (!format.equals("html")) {
 171  16
                         if (decorateNonHtml) {
 172  15
                             outputName = outputName + ".html";
 173  
                         } else {
 174  1
                             template = nonDecoratedTemplate;
 175  
                         }
 176  
                     }
 177  23
                     File written = write(outputDirectory, outputName, template, dataModel);
 178  23
                     filesByFormat.put(format, written);
 179  23
                 }
 180  16
                 reports.add(new Report(name, filesByFormat));
 181  16
             }
 182  13
             return reports;
 183  1
         } catch (Exception e) {
 184  1
             throw new ReportCreationFailed(reportFiles, e);
 185  
         }
 186  
     }
 187  
 
 188  
     SortedMap<String, List<File>> readReportFiles(File outputDirectory, final String outputName,
 189  
             final List<String> formats) {
 190  16
         SortedMap<String, List<File>> reportFiles = new TreeMap<String, List<File>>();
 191  16
         if (outputDirectory == null || !outputDirectory.exists()) {
 192  2
             return reportFiles;
 193  
         }
 194  14
         String[] fileNames = outputDirectory.list(new FilenameFilter() {
 195  
             public boolean accept(File dir, String name) {
 196  169
                 return !name.equals(outputName) && hasFormats(name, formats);
 197  
             }
 198  
 
 199  
             private boolean hasFormats(String name, List<String> formats) {
 200  168
                 for (String format : formats) {
 201  240
                     if (name.endsWith(format)) {
 202  27
                         return true;
 203  
                     }
 204  
                 }
 205  141
                 return false;
 206  
             }
 207  
         });
 208  41
         for (String fileName : fileNames) {
 209  27
             String name = FilenameUtils.getBaseName(fileName);
 210  27
             List<File> filesByName = reportFiles.get(name);
 211  27
             if (filesByName == null) {
 212  18
                 filesByName = new ArrayList<File>();
 213  18
                 reportFiles.put(name, filesByName);
 214  
             }
 215  27
             filesByName.add(new File(outputDirectory, fileName));
 216  
         }
 217  14
         return reportFiles;
 218  
     }
 219  
 
 220  
     private File write(File outputDirectory, String outputName, String resource, Map<String, Object> dataModel) {
 221  
         try {
 222  50
             File file = new File(outputDirectory, outputName);
 223  50
             file.getParentFile().mkdirs();
 224  50
             Writer writer = new FileWriter(file);
 225  50
             processor.process(resource, dataModel, writer);
 226  49
             return file;
 227  1
         } catch (Exception e) {
 228  1
             throw new ViewGenerationFailedForTemplate(resource, e);
 229  
         }
 230  
     }
 231  
 
 232  
     private String templateResource(String format) {
 233  120
         return viewProperties.getProperty(format);
 234  
     }
 235  
 
 236  
     private Map<String, Object> newDataModel() {
 237  50
         return new HashMap<String, Object>();
 238  
     }
 239  
 
 240  
     @SuppressWarnings("serial")
 241  
     public static class ReportCreationFailed extends RuntimeException {
 242  
 
 243  
         public ReportCreationFailed(Map<String, List<File>> reportFiles, Exception cause) {
 244  1
             super("Report creation failed from file " + reportFiles, cause);
 245  1
         }
 246  
     }
 247  
 
 248  
     @SuppressWarnings("serial")
 249  
     public static class ViewGenerationFailedForTemplate extends RuntimeException {
 250  
 
 251  
         public ViewGenerationFailedForTemplate(String resource, Exception cause) {
 252  1
             super(resource, cause);
 253  1
         }
 254  
 
 255  
     }
 256  
 
 257  
     public static class ReportsTable {
 258  
 
 259  13
         private final Map<String, Report> reports = new HashMap<String, Report>();
 260  
         private final StoryNameResolver nameResolver;
 261  
 
 262  13
         public ReportsTable(List<Report> reports, StoryNameResolver nameResolver) {
 263  13
             this.nameResolver = nameResolver;
 264  13
             index(reports);
 265  13
             addTotalsReport();
 266  13
         }
 267  
 
 268  
         private void index(List<Report> reports) {
 269  13
             for (Report report : reports) {
 270  16
                 report.nameAs(nameResolver.resolveName(report.getPath()));
 271  16
                 this.reports.put(report.getName(), report);
 272  
             }
 273  13
         }
 274  
 
 275  
         private void addTotalsReport() {
 276  13
             Report report = totals(reports.values());
 277  13
             report.nameAs(nameResolver.resolveName(report.getPath()));
 278  13
             reports.put(report.getName(), report);
 279  13
         }
 280  
 
 281  
         private Report totals(Collection<Report> values) {
 282  13
             Map<String, Integer> totals = new HashMap<String, Integer>();
 283  13
             for (Report report : values) {
 284  16
                 Map<String, Integer> stats = report.getStats();
 285  16
                 for (String key : stats.keySet()) {
 286  180
                     Integer total = totals.get(key);
 287  180
                     if (total == null) {
 288  180
                         total = 0;
 289  
                     }
 290  180
                     total = total + stats.get(key);
 291  180
                     totals.put(key, total);
 292  180
                 }
 293  16
             }
 294  13
             return new Report("Totals", new HashMap<String, File>(), totals);
 295  
         }
 296  
 
 297  
         public List<Report> getReports() {
 298  0
             List<Report> list = new ArrayList<Report>(reports.values());
 299  0
             Collections.sort(list);
 300  0
             return list;
 301  
         }
 302  
 
 303  
         public List<String> getReportNames() {
 304  12
             List<String> list = new ArrayList<String>(reports.keySet());
 305  12
             Collections.sort(list);
 306  12
             return list;
 307  
         }
 308  
 
 309  
         public Report getReport(String name) {
 310  39
             return reports.get(name);
 311  
         }
 312  
     }
 313  
 
 314  0
     public static class Report implements Comparable<Report> {
 315  
 
 316  
         private final String path;
 317  
         private final Map<String, File> filesByFormat;
 318  
         private Map<String, Integer> stats;
 319  
         private String name;
 320  
 
 321  
         public Report(String path, Map<String, File> filesByFormat) {
 322  17
             this(path, filesByFormat, null);
 323  17
         }
 324  
 
 325  30
         public Report(String path, Map<String, File> filesByFormat, Map<String, Integer> stats) {
 326  30
             this.path = path;
 327  30
             this.filesByFormat = filesByFormat;
 328  30
             this.stats = stats;
 329  30
         }
 330  
 
 331  
         public String getPath() {
 332  29
             return path;
 333  
         }
 334  
 
 335  
         public String getName() {
 336  44
             return name != null ? name : path;
 337  
         }
 338  
 
 339  
         public void nameAs(String name) {
 340  29
             this.name = name;
 341  29
         }
 342  
 
 343  
         public Map<String, File> getFilesByFormat() {
 344  15
             return filesByFormat;
 345  
         }
 346  
 
 347  
         public Properties asProperties(String format) {
 348  108
             Properties p = new Properties();
 349  108
             File stats = filesByFormat.get(format);
 350  
             try {
 351  108
                 InputStream inputStream = new FileInputStream(stats);
 352  80
                 p.load(inputStream);
 353  80
                 inputStream.close();
 354  28
             } catch (Exception e) {
 355  
                 // return empty map
 356  80
             }
 357  108
             return p;
 358  
         }
 359  
 
 360  
         public Map<String, Integer> getStats() {
 361  43
             if (stats == null) {
 362  16
                 Properties p = asProperties("stats");
 363  16
                 stats = new HashMap<String, Integer>();
 364  16
                 for (Enumeration<?> e = p.propertyNames(); e.hasMoreElements();) {
 365  180
                     String key = (String) e.nextElement();
 366  180
                     stats.put(key, valueOf(key, p));
 367  180
                 }
 368  
             }
 369  43
             return stats;
 370  
         }
 371  
 
 372  
         private Integer valueOf(String key, Properties p) {
 373  
             try {
 374  180
                 return Integer.valueOf(p.getProperty(key));
 375  180
             } catch (NumberFormatException e) {
 376  180
                 return 0;
 377  
             }
 378  
         }
 379  
 
 380  
         public int compareTo(Report that) {
 381  0
             return CompareToBuilder.reflectionCompare(this.getName(), that.getName());
 382  
         }
 383  
 
 384  
         @Override
 385  
         public String toString() {
 386  0
             return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append(path).toString();
 387  
         }
 388  
     }
 389  
 
 390  14
     public static class TimeFormatter {
 391  
 
 392  
         public String formatMillis(long millis) {
 393  34
             int second = 1000;
 394  34
             int minute = 60 * second;
 395  34
             int hour = 60 * minute;
 396  34
             long hours = millis / hour;
 397  34
             long minutes = (millis % hour) / minute;
 398  34
             long seconds = ((millis % hour) % minute) / second;
 399  34
             long milliseconds = ((millis % hour) % minute % second);
 400  34
             return new Formatter().format("%02d:%02d:%02d.%03d", hours, minutes, seconds, milliseconds).toString();
 401  
         }
 402  
 
 403  
     }
 404  
 }