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