DefaultSpecRunnerHtmlGenerator.java

  1. package com.github.searls.jasmine.runner;

  2. import com.github.searls.jasmine.io.scripts.ScriptResolver;
  3. import com.github.searls.jasmine.io.scripts.ScriptResolverException;
  4. import org.apache.commons.lang3.StringUtils;
  5. import org.stringtemplate.v4.ST;

  6. import java.io.IOException;
  7. import java.util.Arrays;
  8. import java.util.Set;

  9. public class DefaultSpecRunnerHtmlGenerator extends AbstractSpecRunnerHtmlGenerator implements SpecRunnerHtmlGenerator {

  10.   protected DefaultSpecRunnerHtmlGenerator(HtmlGeneratorConfiguration configuration) {
  11.     super(configuration);
  12.   }

  13.   @Override
  14.   public String generate() {
  15.     try {
  16.       ScriptResolver resolver = this.getConfiguration().getScriptResolver();
  17.       return this.generateHtml(
  18.         resolver.getAllScripts(),
  19.         resolver.getPreloads(),
  20.         resolver.getSources(),
  21.         resolver.getSpecs(),
  22.         resolver.getSourceDirectory(),
  23.         resolver.getSpecDirectory()
  24.       );
  25.     } catch (ScriptResolverException e) {
  26.       throw new RuntimeException("Failed to load files for dependencies, sources, or a custom runner", e);
  27.     } catch (IOException e) {
  28.       throw new RuntimeException("Failed to load files for dependencies, sources, or a custom runner", e);
  29.     }
  30.   }

  31.   private String generateHtml(Set<String> allScripts,
  32.                               Set<String> preloads,
  33.                               Set<String> sources,
  34.                               Set<String> specs,
  35.                               String sourceDirectory,
  36.                               String specDirectory) throws IOException {
  37.     ST template = this.resolveHtmlTemplate();
  38.     this.applyScriptTagsToTemplate(
  39.       JAVASCRIPT_DEPENDENCIES_TEMPLATE_ATTR_NAME,
  40.       Arrays.asList(JASMINE_JS, JASMINE_HTML_JS, JASMINE_HTMLSPECFILTER_PATCH_JS, JASMINE_BOOT_JS),
  41.       template);
  42.     this.applyCssToTemplate(Arrays.asList(JASMINE_CSS), template);
  43.     this.applyScriptTagsToTemplate("allScriptTags", allScripts, template);
  44.     this.applyScriptTagsToTemplate("preloadScriptTags", preloads, template);
  45.     this.applyScriptTagsToTemplate("sourceScriptTags", sources, template);
  46.     this.applyScriptTagsToTemplate("specScriptTags", specs, template);
  47.     template.add("allScriptsList", this.createJsonArray(allScripts));
  48.     template.add("preloadsList", this.createJsonArray(preloads));
  49.     template.add("sourcesList", this.createJsonArray(sources));
  50.     template.add("specsList", this.createJsonArray(specs));
  51.     template.add("sourceDir", sourceDirectory);
  52.     template.add("specDir", specDirectory);

  53.     template.add("autoRefresh", this.getConfiguration().getAutoRefresh());
  54.     template.add("autoRefreshInterval", this.getConfiguration().getAutoRefreshInterval());

  55.     this.setCustomRunnerConfig(template);
  56.     template.add(REPORTER_ATTR_NAME, this.getConfiguration().getReporterType().name());
  57.     this.setEncoding(this.getConfiguration(), template);

  58.     // these fields are being preserved for backwards compatibility
  59.     this.applyScriptTagsToTemplate("sources", allScripts, template);
  60.     template.add("specs", this.createJsonArray(specs));
  61.     template.add("priority", this.createJsonArray(preloads));

  62.     return template.render();
  63.   }

  64.   private String createJsonArray(Set<String> scripts) {
  65.     if (null == scripts || scripts.isEmpty()) {
  66.       return "[]";
  67.     }
  68.     StringBuilder builder = new StringBuilder("['");
  69.     builder.append(StringUtils.join(scripts, "', '"));
  70.     builder.append("']");
  71.     return builder.toString();
  72.   }

  73.   private void setCustomRunnerConfig(ST template) throws IOException {
  74.     String customRunnerConfiguration = this.getConfiguration().getCustomRunnerConfiguration();
  75.     template.add("customRunnerConfiguration", customRunnerConfiguration);
  76.   }
  77. }