All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.searls.jasmine.io.scripts.ContextPathScriptResolver Maven / Gradle / Ivy

Go to download

A JavaScript unit test plugin that processes JavaScript sources and Jasmine specs, prepares test runner HTML files, executes Jasmine specs headlessly with HtmlUnit, and produces JUnit XML reports

There is a newer version: 3.0-beta-02
Show newest version
package com.github.searls.jasmine.io.scripts;

import java.util.LinkedHashSet;
import java.util.Set;

public class ContextPathScriptResolver implements ScriptResolver {

  private final ScriptResolver scriptResolver;
  private final String baseContextPath;
  private final String sourceContextPath;
  private final String specContextPath;

  public ContextPathScriptResolver(ScriptResolver scriptResolver,
                                   String sourceContextPath,
                                   String specContextPath) {
    this(scriptResolver, "", sourceContextPath, specContextPath);
  }

  public ContextPathScriptResolver(ScriptResolver scriptResolver,
                                   String baseContextPath,
                                   String sourceContextPath,
                                   String specContextPath) {
    this.scriptResolver = scriptResolver;
    this.baseContextPath = baseContextPath;
    this.sourceContextPath = sourceContextPath;
    this.specContextPath = specContextPath;
  }

  @Override
  public String getSourceDirectory() {
    return this.sourceContextPath;
  }

  @Override
  public String getSpecDirectory() {
    return this.specContextPath;
  }

  @Override
  public String getBaseDirectory() {
    return this.baseContextPath;
  }

  @Override
  public Set getSources() throws ScriptResolverException {
    return relativeToContextPath(
      this.scriptResolver.getSourceDirectory(),
      this.sourceContextPath,
      this.scriptResolver.getSources());
  }

  @Override
  public Set getSpecs() throws ScriptResolverException {
    return relativeToContextPath(
      this.scriptResolver.getSpecDirectory(),
      this.specContextPath,
      this.scriptResolver.getSpecs());
  }

  @Override
  public Set getPreloads() throws ScriptResolverException {
    Set scripts = this.scriptResolver.getPreloads();
    scripts = relativeToContextPath(
      this.scriptResolver.getSourceDirectory(),
      this.sourceContextPath,
      scripts);
    scripts = relativeToContextPath(
      this.scriptResolver.getSpecDirectory(),
      this.specContextPath,
      scripts);
    scripts = relativeToContextPath(
      this.scriptResolver.getBaseDirectory(),
      this.baseContextPath,
      scripts);
    return scripts;
  }

  @Override
  public Set getAllScripts() throws ScriptResolverException {
    LinkedHashSet allScripts = new LinkedHashSet();
    allScripts.addAll(this.getPreloads());
    allScripts.addAll(this.getSources());
    allScripts.addAll(this.getSpecs());
    return allScripts;
  }

  private Set relativeToContextPath(String realPath, String contextPath, Set absoluteScripts) {
    Set relativeScripts = new LinkedHashSet();
    for (String absoluteScript : absoluteScripts) {
      relativeScripts.add(absoluteScript.replace(realPath, contextPath));
    }
    return relativeScripts;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy