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 | 417 | private List<Report> reports = new ArrayList<Report>(); |
67 | 417 | private StoryNameResolver nameResolver = new UnderscoredToCapitalized(); |
68 | |
|
69 | 417 | public FreemarkerViewGenerator() { |
70 | 417 | this.configuration = configure(); |
71 | 417 | } |
72 | |
|
73 | |
public static Properties defaultViewProperties() { |
74 | 241 | Properties properties = new Properties(); |
75 | 241 | properties.setProperty("views", "ftl/jbehave-views.ftl"); |
76 | 241 | properties.setProperty("maps", "ftl/jbehave-maps.ftl"); |
77 | 241 | properties.setProperty("reports", "ftl/jbehave-reports-with-totals.ftl"); |
78 | 241 | properties.setProperty("decorated", "ftl/jbehave-report-decorated.ftl"); |
79 | 241 | properties.setProperty("nonDecorated", "ftl/jbehave-report-non-decorated.ftl"); |
80 | 241 | properties.setProperty("decorateNonHtml", "true"); |
81 | 241 | properties.setProperty("defaultFormats", "stats"); |
82 | 241 | properties.setProperty("viewDirectory", "view"); |
83 | 241 | return properties; |
84 | |
} |
85 | |
|
86 | |
private Properties mergeWithDefault(Properties properties) { |
87 | 17 | Properties merged = defaultViewProperties(); |
88 | 17 | merged.putAll(properties); |
89 | 17 | return merged; |
90 | |
} |
91 | |
|
92 | |
private void generateViewsIndex(File outputDirectory) { |
93 | 16 | String outputName = templateResource("viewDirectory") + "/index.html"; |
94 | 16 | String viewsTemplate = templateResource("views"); |
95 | 16 | Map<String, Object> dataModel = newDataModel(); |
96 | 16 | dataModel.put("date", new Date()); |
97 | 16 | write(outputDirectory, outputName, viewsTemplate, dataModel); |
98 | 16 | } |
99 | |
|
100 | |
public void generateMapsView(File outputDirectory, StoryMaps storyMaps, Properties viewProperties) { |
101 | 1 | this.viewProperties = mergeWithDefault(viewProperties); |
102 | 1 | String outputName = templateResource("viewDirectory") + "/maps.html"; |
103 | 1 | String mapsTemplate = templateResource("maps"); |
104 | 1 | Map<String, Object> dataModel = newDataModel(); |
105 | 1 | dataModel.put("storyLanes", new StoryLanes(storyMaps, nameResolver)); |
106 | 1 | dataModel.put("date", new Date()); |
107 | 1 | write(outputDirectory, outputName, mapsTemplate, dataModel); |
108 | 1 | generateViewsIndex(outputDirectory); |
109 | 1 | } |
110 | |
|
111 | |
public void generateReportsView(File outputDirectory, List<String> formats, Properties viewProperties) { |
112 | 16 | this.viewProperties = mergeWithDefault(viewProperties); |
113 | 16 | String outputName = templateResource("viewDirectory") + "/reports.html"; |
114 | 16 | String reportsTemplate = templateResource("reports"); |
115 | 16 | List<String> mergedFormats = mergeFormatsWithDefaults(formats); |
116 | 16 | reports = createReports(readReportFiles(outputDirectory, outputName, mergedFormats)); |
117 | 16 | Map<String, Object> dataModel = newDataModel(); |
118 | 16 | dataModel.put("reportsTable", new ReportsTable(reports, nameResolver)); |
119 | 16 | dataModel.put("date", new Date()); |
120 | 16 | write(outputDirectory, outputName, reportsTemplate, dataModel); |
121 | 15 | generateViewsIndex(outputDirectory); |
122 | 15 | } |
123 | |
|
124 | |
public ReportsCount getReportsCount() { |
125 | 13 | int stories = reports.size(); |
126 | 13 | int storiesNotAllowed = count("storiesNotAllowed", reports); |
127 | 13 | int scenarios = count("scenarios", reports); |
128 | 13 | int scenariosFailed = count("scenariosFailed", reports); |
129 | 13 | int scenariosNotAllowed = count("scenariosNotAllowed", reports); |
130 | 13 | return new ReportsCount(stories, storiesNotAllowed, scenarios, scenariosFailed, scenariosNotAllowed); |
131 | |
} |
132 | |
|
133 | |
int count(String event, Collection<Report> reports) { |
134 | 54 | int count = 0; |
135 | 54 | for (Report report : reports) { |
136 | 54 | Properties stats = report.asProperties("stats"); |
137 | 54 | if (stats.containsKey(event)) { |
138 | 1 | count = count + Integer.parseInt((String) stats.get(event)); |
139 | |
} |
140 | 54 | } |
141 | 54 | return count; |
142 | |
} |
143 | |
|
144 | |
private List<String> mergeFormatsWithDefaults(List<String> formats) { |
145 | 16 | List<String> merged = new ArrayList<String>(); |
146 | 16 | merged.addAll(asList(templateResource("defaultFormats").split(","))); |
147 | 16 | merged.addAll(formats); |
148 | 16 | return merged; |
149 | |
} |
150 | |
|
151 | |
List<Report> createReports(Map<String, List<File>> reportFiles) { |
152 | |
try { |
153 | 17 | String decoratedTemplate = templateResource("decorated"); |
154 | 16 | String nonDecoratedTemplate = templateResource("nonDecorated"); |
155 | 16 | String viewDirectory = templateResource("viewDirectory"); |
156 | 16 | boolean decorateNonHtml = Boolean.valueOf(templateResource("decorateNonHtml")); |
157 | 16 | List<Report> reports = new ArrayList<Report>(); |
158 | 16 | for (String name : reportFiles.keySet()) { |
159 | 15 | Map<String, File> filesByFormat = new HashMap<String, File>(); |
160 | 15 | for (File file : reportFiles.get(name)) { |
161 | 17 | String fileName = file.getName(); |
162 | 17 | String format = FilenameUtils.getExtension(fileName); |
163 | 17 | Map<String, Object> dataModel = newDataModel(); |
164 | 17 | dataModel.put("name", name); |
165 | 17 | dataModel.put("body", IOUtils.toString(new FileReader(file))); |
166 | 17 | dataModel.put("format", format); |
167 | 17 | File outputDirectory = file.getParentFile(); |
168 | 17 | String outputName = viewDirectory + "/" + fileName; |
169 | 17 | String template = decoratedTemplate; |
170 | 17 | if (!format.equals("html")) { |
171 | 15 | if (decorateNonHtml) { |
172 | 14 | outputName = outputName + ".html"; |
173 | |
} else { |
174 | 1 | template = nonDecoratedTemplate; |
175 | |
} |
176 | |
} |
177 | 17 | File written = write(outputDirectory, outputName, template, dataModel); |
178 | 17 | filesByFormat.put(format, written); |
179 | 17 | } |
180 | 15 | reports.add(new Report(name, filesByFormat)); |
181 | 15 | } |
182 | 16 | 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 | 19 | SortedMap<String, List<File>> reportFiles = new TreeMap<String, List<File>>(); |
191 | 19 | if (outputDirectory == null || !outputDirectory.exists()) { |
192 | 2 | return reportFiles; |
193 | |
} |
194 | 17 | String[] fileNames = outputDirectory.list(new FilenameFilter() { |
195 | |
public boolean accept(File dir, String name) { |
196 | 127 | return !name.equals(outputName) && hasFormats(name, formats); |
197 | |
} |
198 | |
|
199 | |
private boolean hasFormats(String name, List<String> formats) { |
200 | 126 | for (String format : formats) { |
201 | 163 | if (name.endsWith(format)) { |
202 | 21 | return true; |
203 | |
} |
204 | |
} |
205 | 105 | return false; |
206 | |
} |
207 | |
}); |
208 | 38 | for (String fileName : fileNames) { |
209 | 21 | String name = FilenameUtils.getBaseName(fileName); |
210 | 21 | List<File> filesByName = reportFiles.get(name); |
211 | 21 | if (filesByName == null) { |
212 | 17 | filesByName = new ArrayList<File>(); |
213 | 17 | reportFiles.put(name, filesByName); |
214 | |
} |
215 | 21 | filesByName.add(new File(outputDirectory, fileName)); |
216 | |
} |
217 | 17 | 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 | 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 Configuration configure() { |
233 | 417 | Configuration configuration = new Configuration(); |
234 | 417 | configuration.setClassForTemplateLoading(FreemarkerViewGenerator.class, "/"); |
235 | 417 | configuration.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER); |
236 | 417 | return configuration; |
237 | |
} |
238 | |
|
239 | |
private void process(String resource, Map<String, Object> dataModel, Writer writer) throws TemplateException, |
240 | |
IOException { |
241 | 50 | Template template = configuration.getTemplate(resource); |
242 | 49 | template.process(dataModel, writer); |
243 | 49 | } |
244 | |
|
245 | |
private String templateResource(String format) { |
246 | 147 | return viewProperties.getProperty(format); |
247 | |
} |
248 | |
|
249 | |
private Map<String, Object> newDataModel() { |
250 | 50 | 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 | 16 | private final Map<String, Report> reports = new HashMap<String, Report>(); |
273 | |
private final StoryNameResolver nameResolver; |
274 | |
|
275 | 16 | public ReportsTable(List<Report> reports, StoryNameResolver nameResolver) { |
276 | 16 | this.nameResolver = nameResolver; |
277 | 16 | index(reports); |
278 | 16 | addTotalsReport(); |
279 | 16 | } |
280 | |
|
281 | |
private void index(List<Report> reports) { |
282 | 16 | for (Report report : reports) { |
283 | 15 | report.nameAs(nameResolver.resolveName(report.getPath())); |
284 | 15 | this.reports.put(report.getName(), report); |
285 | |
} |
286 | 16 | } |
287 | |
|
288 | |
private void addTotalsReport() { |
289 | 16 | Report report = totals(reports.values()); |
290 | 16 | report.nameAs(nameResolver.resolveName(report.getPath())); |
291 | 16 | reports.put(report.getName(), report); |
292 | 16 | } |
293 | |
|
294 | |
private Report totals(Collection<Report> values) { |
295 | 16 | Map<String, Integer> totals = new HashMap<String, Integer>(); |
296 | 16 | for (Report report : values) { |
297 | 15 | Map<String, Integer> stats = report.getStats(); |
298 | 15 | for (String key : stats.keySet()) { |
299 | 234 | Integer total = totals.get(key); |
300 | 234 | if (total == null) { |
301 | 234 | total = 0; |
302 | |
} |
303 | 234 | total = total + stats.get(key); |
304 | 234 | totals.put(key, total); |
305 | 234 | } |
306 | 15 | } |
307 | 16 | 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 | 15 | List<String> list = new ArrayList<String>(reports.keySet()); |
318 | 15 | Collections.sort(list); |
319 | 15 | return list; |
320 | |
} |
321 | |
|
322 | |
public Report getReport(String name) { |
323 | 45 | 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 | 16 | this(path, filesByFormat, null); |
336 | 16 | } |
337 | |
|
338 | 32 | public Report(String path, Map<String, File> filesByFormat, Map<String, Integer> stats) { |
339 | 32 | this.path = path; |
340 | 32 | this.filesByFormat = filesByFormat; |
341 | 32 | this.stats = stats; |
342 | 32 | } |
343 | |
|
344 | |
public String getPath() { |
345 | 31 | return path; |
346 | |
} |
347 | |
|
348 | |
public String getName() { |
349 | 46 | return name != null ? name : path; |
350 | |
} |
351 | |
|
352 | |
public void nameAs(String name) { |
353 | 31 | this.name = name; |
354 | 31 | } |
355 | |
|
356 | |
public Map<String, File> getFilesByFormat() { |
357 | 15 | return filesByFormat; |
358 | |
} |
359 | |
|
360 | |
public Properties asProperties(String format) { |
361 | 68 | Properties p = new Properties(); |
362 | 68 | File stats = filesByFormat.get(format); |
363 | |
try { |
364 | 68 | p.load(new FileInputStream(stats)); |
365 | 3 | } catch (Exception e) { |
366 | |
|
367 | 65 | } |
368 | 68 | return p; |
369 | |
} |
370 | |
|
371 | |
public Map<String, Integer> getStats() { |
372 | 45 | if (stats == null) { |
373 | 15 | Properties p = asProperties("stats"); |
374 | 15 | stats = new HashMap<String, Integer>(); |
375 | 15 | for (Enumeration<?> e = p.propertyNames(); e.hasMoreElements();) { |
376 | 234 | String key = (String) e.nextElement(); |
377 | 234 | stats.put(key, valueOf(key, p)); |
378 | 234 | } |
379 | |
} |
380 | 45 | return stats; |
381 | |
} |
382 | |
|
383 | |
private Integer valueOf(String key, Properties p) { |
384 | |
try { |
385 | 234 | return Integer.valueOf(p.getProperty(key)); |
386 | 234 | } catch (NumberFormatException e) { |
387 | 234 | return 0; |
388 | |
} |
389 | |
} |
390 | |
|
391 | |
public int compareTo(Report that) { |
392 | 0 | return CompareToBuilder.reflectionCompare(this.getName(), that.getName()); |
393 | |
} |
394 | |
|
395 | |
@Override |
396 | |
public String toString() { |
397 | 0 | return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append(path).toString(); |
398 | |
} |
399 | |
} |
400 | |
|
401 | |
} |