1 package com.github.searls.jasmine.runner;
2
3 import com.github.searls.jasmine.io.scripts.ScriptResolver;
4 import com.github.searls.jasmine.io.scripts.ScriptResolverException;
5 import org.apache.commons.lang3.StringUtils;
6 import org.stringtemplate.v4.ST;
7
8 import java.io.IOException;
9 import java.util.Arrays;
10 import java.util.Set;
11
12 public class DefaultSpecRunnerHtmlGenerator extends AbstractSpecRunnerHtmlGenerator implements SpecRunnerHtmlGenerator {
13
14 protected DefaultSpecRunnerHtmlGenerator(HtmlGeneratorConfiguration configuration) {
15 super(configuration);
16 }
17
18 @Override
19 public String generate() {
20 try {
21 ScriptResolver resolver = this.getConfiguration().getScriptResolver();
22 return this.generateHtml(
23 resolver.getAllScripts(),
24 resolver.getPreloads(),
25 resolver.getSources(),
26 resolver.getSpecs(),
27 resolver.getSourceDirectory(),
28 resolver.getSpecDirectory()
29 );
30 } catch (ScriptResolverException e) {
31 throw new RuntimeException("Failed to load files for dependencies, sources, or a custom runner", e);
32 } catch (IOException e) {
33 throw new RuntimeException("Failed to load files for dependencies, sources, or a custom runner", e);
34 }
35 }
36
37 private String generateHtml(Set<String> allScripts,
38 Set<String> preloads,
39 Set<String> sources,
40 Set<String> specs,
41 String sourceDirectory,
42 String specDirectory) throws IOException {
43 ST template = this.resolveHtmlTemplate();
44 this.applyScriptTagsToTemplate(
45 JAVASCRIPT_DEPENDENCIES_TEMPLATE_ATTR_NAME,
46 Arrays.asList(JASMINE_JS, JASMINE_HTML_JS, JASMINE_HTMLSPECFILTER_PATCH_JS, JASMINE_BOOT_JS),
47 template);
48 this.applyCssToTemplate(Arrays.asList(JASMINE_CSS), template);
49 this.applyScriptTagsToTemplate("allScriptTags", allScripts, template);
50 this.applyScriptTagsToTemplate("preloadScriptTags", preloads, template);
51 this.applyScriptTagsToTemplate("sourceScriptTags", sources, template);
52 this.applyScriptTagsToTemplate("specScriptTags", specs, template);
53 template.add("allScriptsList", this.createJsonArray(allScripts));
54 template.add("preloadsList", this.createJsonArray(preloads));
55 template.add("sourcesList", this.createJsonArray(sources));
56 template.add("specsList", this.createJsonArray(specs));
57 template.add("sourceDir", sourceDirectory);
58 template.add("specDir", specDirectory);
59
60 template.add("autoRefresh", this.getConfiguration().getAutoRefresh());
61 template.add("autoRefreshInterval", this.getConfiguration().getAutoRefreshInterval());
62
63 this.setCustomRunnerConfig(template);
64 template.add(REPORTER_ATTR_NAME, this.getConfiguration().getReporterType().name());
65 this.setEncoding(this.getConfiguration(), template);
66
67
68 this.applyScriptTagsToTemplate("sources", allScripts, template);
69 template.add("specs", this.createJsonArray(specs));
70 template.add("priority", this.createJsonArray(preloads));
71
72 return template.render();
73 }
74
75 private String createJsonArray(Set<String> scripts) {
76 if (null == scripts || scripts.isEmpty()) {
77 return "[]";
78 }
79 StringBuilder builder = new StringBuilder("['");
80 builder.append(StringUtils.join(scripts, "', '"));
81 builder.append("']");
82 return builder.toString();
83 }
84
85 private void setCustomRunnerConfig(ST template) throws IOException {
86 String customRunnerConfiguration = this.getConfiguration().getCustomRunnerConfiguration();
87 template.add("customRunnerConfiguration", customRunnerConfiguration);
88 }
89 }