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