Coverage Report - org.jbehave.core.reporters.TemplateableViewGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
TemplateableViewGenerator
94%
111/118
88%
23/26
1.895
TemplateableViewGenerator$1
100%
6/6
100%
8/8
1.895
TemplateableViewGenerator$Report
91%
31/34
83%
5/6
1.895
TemplateableViewGenerator$ReportCreationFailed
100%
2/2
N/A
1.895
TemplateableViewGenerator$ReportsTable
90%
30/33
87%
7/8
1.895
TemplateableViewGenerator$TimeFormatter
100%
9/9
N/A
1.895
TemplateableViewGenerator$ViewGenerationFailedForTemplate
100%
2/2
N/A
1.895
 
 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  972
     private List<Report> reports = new ArrayList<Report>();
 67  
 
 68  972
     public TemplateableViewGenerator(StoryNameResolver nameResolver, TemplateProcessor processor) {
 69  972
         this.nameResolver = nameResolver;
 70  972
         this.processor = processor;
 71  972
     }
 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 = countStoriesWithScenarios();
 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  
     private int countStoriesWithScenarios(){
 134  10
         int storyCount = 0;
 135  10
         for (Report report : reports){
 136  14
             Map<String, Integer> stats = report.getStats();
 137  14
             if (stats.containsKey("scenarios")){
 138  0
                 if (stats.get("scenarios") > 0)
 139  0
                 storyCount++;
 140  
             }
 141  14
         }
 142  10
         return storyCount;
 143  
     }
 144  
     
 145  
     int count(String event, Collection<Report> reports) {
 146  72
         int count = 0;
 147  72
         for (Report report : reports) {
 148  100
             Properties stats = report.asProperties("stats");
 149  100
             if (stats.containsKey(event)) {
 150  1
                 count = count + Integer.parseInt((String) stats.get(event));
 151  
             }
 152  100
         }
 153  72
         return count;
 154  
     }
 155  
 
 156  
     private List<String> mergeFormatsWithDefaults(List<String> formats) {
 157  13
         List<String> merged = new ArrayList<String>();
 158  13
         merged.addAll(asList(templateResource("defaultFormats").split(",")));
 159  13
         merged.addAll(formats);
 160  13
         return merged;
 161  
     }
 162  
 
 163  
     List<Report> createReports(Map<String, List<File>> reportFiles) {
 164  
         try {
 165  14
             String decoratedTemplate = templateResource("decorated");
 166  13
             String nonDecoratedTemplate = templateResource("nonDecorated");
 167  13
             String viewDirectory = templateResource("viewDirectory");
 168  13
             boolean decorateNonHtml = Boolean.valueOf(templateResource("decorateNonHtml"));
 169  13
             List<Report> reports = new ArrayList<Report>();
 170  13
             for (String name : reportFiles.keySet()) {
 171  17
                 Map<String, File> filesByFormat = new HashMap<String, File>();
 172  17
                 for (File file : reportFiles.get(name)) {
 173  25
                     String fileName = file.getName();
 174  25
                     String format = FilenameUtils.getExtension(fileName);
 175  25
                     Map<String, Object> dataModel = newDataModel();
 176  25
                     dataModel.put("name", name);
 177  25
                     dataModel.put("body", IOUtils.toString(new FileReader(file)));
 178  25
                     dataModel.put("format", format);
 179  25
                     File outputDirectory = file.getParentFile();
 180  25
                     String outputName = viewDirectory + "/" + fileName;
 181  25
                     String template = decoratedTemplate;
 182  25
                     if (!format.equals("html")) {
 183  17
                         if (decorateNonHtml) {
 184  16
                             outputName = outputName + ".html";
 185  
                         } else {
 186  1
                             template = nonDecoratedTemplate;
 187  
                         }
 188  
                     }
 189  25
                     File written = write(outputDirectory, outputName, template, dataModel);
 190  25
                     filesByFormat.put(format, written);
 191  25
                 }
 192  17
                 reports.add(new Report(name, filesByFormat));
 193  17
             }
 194  13
             return reports;
 195  1
         } catch (Exception e) {
 196  1
             throw new ReportCreationFailed(reportFiles, e);
 197  
         }
 198  
     }
 199  
 
 200  
     SortedMap<String, List<File>> readReportFiles(File outputDirectory, final String outputName,
 201  
             final List<String> formats) {
 202  16
         SortedMap<String, List<File>> reportFiles = new TreeMap<String, List<File>>();
 203  16
         if (outputDirectory == null || !outputDirectory.exists()) {
 204  2
             return reportFiles;
 205  
         }
 206  14
         String[] fileNames = outputDirectory.list(new FilenameFilter() {
 207  
             public boolean accept(File dir, String name) {
 208  189
                 return !name.equals(outputName) && hasFormats(name, formats);
 209  
             }
 210  
 
 211  
             private boolean hasFormats(String name, List<String> formats) {
 212  188
                 for (String format : formats) {
 213  265
                     if (name.endsWith(format)) {
 214  29
                         return true;
 215  
                     }
 216  
                 }
 217  159
                 return false;
 218  
             }
 219  
         });
 220  43
         for (String fileName : fileNames) {
 221  29
             String name = FilenameUtils.getBaseName(fileName);
 222  29
             List<File> filesByName = reportFiles.get(name);
 223  29
             if (filesByName == null) {
 224  19
                 filesByName = new ArrayList<File>();
 225  19
                 reportFiles.put(name, filesByName);
 226  
             }
 227  29
             filesByName.add(new File(outputDirectory, fileName));
 228  
         }
 229  14
         return reportFiles;
 230  
     }
 231  
 
 232  
     private File write(File outputDirectory, String outputName, String resource, Map<String, Object> dataModel) {
 233  
         try {
 234  52
             File file = new File(outputDirectory, outputName);
 235  52
             file.getParentFile().mkdirs();
 236  52
             Writer writer = new FileWriter(file);
 237  52
             processor.process(resource, dataModel, writer);
 238  51
             return file;
 239  1
         } catch (Exception e) {
 240  1
             throw new ViewGenerationFailedForTemplate(resource, e);
 241  
         }
 242  
     }
 243  
 
 244  
     private String templateResource(String format) {
 245  120
         return viewProperties.getProperty(format);
 246  
     }
 247  
 
 248  
     private Map<String, Object> newDataModel() {
 249  52
         return new HashMap<String, Object>();
 250  
     }
 251  
 
 252  
     @SuppressWarnings("serial")
 253  
     public static class ReportCreationFailed extends RuntimeException {
 254  
 
 255  
         public ReportCreationFailed(Map<String, List<File>> reportFiles, Exception cause) {
 256  1
             super("Report creation failed from file " + reportFiles, cause);
 257  1
         }
 258  
     }
 259  
 
 260  
     @SuppressWarnings("serial")
 261  
     public static class ViewGenerationFailedForTemplate extends RuntimeException {
 262  
 
 263  
         public ViewGenerationFailedForTemplate(String resource, Exception cause) {
 264  1
             super(resource, cause);
 265  1
         }
 266  
 
 267  
     }
 268  
 
 269  
     public static class ReportsTable {
 270  
 
 271  13
         private final Map<String, Report> reports = new HashMap<String, Report>();
 272  
         private final StoryNameResolver nameResolver;
 273  
 
 274  13
         public ReportsTable(List<Report> reports, StoryNameResolver nameResolver) {
 275  13
             this.nameResolver = nameResolver;
 276  13
             index(reports);
 277  13
             addTotalsReport();
 278  13
         }
 279  
 
 280  
         private void index(List<Report> reports) {
 281  13
             for (Report report : reports) {
 282  17
                 report.nameAs(nameResolver.resolveName(report.getPath()));
 283  17
                 this.reports.put(report.getName(), report);
 284  
             }
 285  13
         }
 286  
 
 287  
         private void addTotalsReport() {
 288  13
             Report report = totals(reports.values());
 289  13
             report.nameAs(nameResolver.resolveName(report.getPath()));
 290  13
             reports.put(report.getName(), report);
 291  13
         }
 292  
 
 293  
         private Report totals(Collection<Report> values) {
 294  13
             Map<String, Integer> totals = new HashMap<String, Integer>();
 295  13
             for (Report report : values) {
 296  17
                 Map<String, Integer> stats = report.getStats();
 297  17
                 for (String key : stats.keySet()) {
 298  180
                     Integer total = totals.get(key);
 299  180
                     if (total == null) {
 300  180
                         total = 0;
 301  
                     }
 302  180
                     total = total + stats.get(key);
 303  180
                     totals.put(key, total);
 304  180
                 }
 305  17
             }
 306  13
             return new Report("Totals", new HashMap<String, File>(), totals);
 307  
         }
 308  
 
 309  
         public List<Report> getReports() {
 310  0
             List<Report> list = new ArrayList<Report>(reports.values());
 311  0
             Collections.sort(list);
 312  0
             return list;
 313  
         }
 314  
 
 315  
         public List<String> getReportNames() {
 316  12
             List<String> list = new ArrayList<String>(reports.keySet());
 317  12
             Collections.sort(list);
 318  12
             return list;
 319  
         }
 320  
 
 321  
         public Report getReport(String name) {
 322  40
             return reports.get(name);
 323  
         }
 324  
     }
 325  
 
 326  0
     public static class Report implements Comparable<Report> {
 327  
 
 328  
         private final String path;
 329  
         private final Map<String, File> filesByFormat;
 330  
         private Map<String, Integer> stats;
 331  
         private String name;
 332  
 
 333  
         public Report(String path, Map<String, File> filesByFormat) {
 334  18
             this(path, filesByFormat, null);
 335  18
         }
 336  
 
 337  31
         public Report(String path, Map<String, File> filesByFormat, Map<String, Integer> stats) {
 338  31
             this.path = path;
 339  31
             this.filesByFormat = filesByFormat;
 340  31
             this.stats = stats;
 341  31
         }
 342  
 
 343  
         public String getPath() {
 344  30
             return path;
 345  
         }
 346  
 
 347  
         public String getName() {
 348  46
             return name != null ? name : path;
 349  
         }
 350  
 
 351  
         public void nameAs(String name) {
 352  30
             this.name = name;
 353  30
         }
 354  
 
 355  
         public Map<String, File> getFilesByFormat() {
 356  16
             return filesByFormat;
 357  
         }
 358  
 
 359  
         public Properties asProperties(String format) {
 360  116
             Properties p = new Properties();
 361  116
             File stats = filesByFormat.get(format);
 362  
             try {
 363  116
                 InputStream inputStream = new FileInputStream(stats);
 364  80
                 p.load(inputStream);
 365  80
                 inputStream.close();
 366  36
             } catch (Exception e) {
 367  
                 // return empty map
 368  80
             }
 369  116
             return p;
 370  
         }
 371  
 
 372  
         public Map<String, Integer> getStats() {
 373  59
             if (stats == null) {
 374  17
                 Properties p = asProperties("stats");
 375  17
                 stats = new HashMap<String, Integer>();
 376  17
                 for (Enumeration<?> e = p.propertyNames(); e.hasMoreElements();) {
 377  180
                     String key = (String) e.nextElement();
 378  180
                     stats.put(key, valueOf(key, p));
 379  180
                 }
 380  
             }
 381  59
             return stats;
 382  
         }
 383  
 
 384  
         private Integer valueOf(String key, Properties p) {
 385  
             try {
 386  180
                 return Integer.valueOf(p.getProperty(key));
 387  180
             } catch (NumberFormatException e) {
 388  180
                 return 0;
 389  
             }
 390  
         }
 391  
 
 392  
         public int compareTo(Report that) {
 393  0
             return CompareToBuilder.reflectionCompare(this.getName(), that.getName());
 394  
         }
 395  
 
 396  
         @Override
 397  
         public String toString() {
 398  0
             return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append(path).toString();
 399  
         }
 400  
     }
 401  
 
 402  14
     public static class TimeFormatter {
 403  
 
 404  
         public String formatMillis(long millis) {
 405  35
             int second = 1000;
 406  35
             int minute = 60 * second;
 407  35
             int hour = 60 * minute;
 408  35
             long hours = millis / hour;
 409  35
             long minutes = (millis % hour) / minute;
 410  35
             long seconds = ((millis % hour) % minute) / second;
 411  35
             long milliseconds = ((millis % hour) % minute % second);
 412  35
             return new Formatter().format("%02d:%02d:%02d.%03d", hours, minutes, seconds, milliseconds).toString();
 413  
         }
 414  
 
 415  
     }
 416  
 }