com.github.searls.jasmine.TestMojo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jasmine-maven-plugin Show documentation
Show all versions of jasmine-maven-plugin Show documentation
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
package com.github.searls.jasmine;
import com.github.searls.jasmine.format.JasmineResultLogger;
import com.github.searls.jasmine.io.scripts.FindsScriptLocationsInDirectory;
import com.github.searls.jasmine.io.scripts.ResolvesCompleteListOfScriptLocations;
import com.github.searls.jasmine.model.JasmineResult;
import com.github.searls.jasmine.model.ScriptSearch;
import com.github.searls.jasmine.runner.ReporterType;
import com.github.searls.jasmine.runner.SpecRunnerExecutor;
import com.github.searls.jasmine.runner.SpecRunnerHtmlGenerator;
import net.dynamic_tools.exception.CircularDependencyException;
import net.dynamic_tools.model.JSResource;
import net.dynamic_tools.service.*;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.plugin.MojoFailureException;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
import static org.apache.commons.lang.StringUtils.countMatches;
/**
* @component
* @goal test
* @phase test
* @execute lifecycle="jasmine-lifecycle" phase="process-test-resources"
*/
public class TestMojo extends AbstractJasmineMojo {
private ResolvesCompleteListOfScriptLocations resolvesCompleteListOfScriptLocations = new ResolvesCompleteListOfScriptLocations();
public void run() throws Exception {
if (!skipTests) {
getLog().info("Executing Jasmine Specs");
List runnerFiles = writeSpecRunnerToOutputDirectory();
SpecRunnerExecutor specRunnerExecutor = new SpecRunnerExecutor();
for (File runnerFile : runnerFiles) {
JasmineResult result = executeSpecs(runnerFile, specRunnerExecutor);
logResults(result);
throwAnySpecFailures(result);
}
} else {
getLog().info("Skipping Jasmine Specs");
}
}
private List writeSpecRunnerToOutputDirectory() throws IOException, CircularDependencyException {
List runnerFiles = new ArrayList();
File specDir = new File(jasmineTargetDir, specDirectoryName);
File srcDir = new File(jasmineTargetDir, srcDirectoryName);
ScriptSearch specsSearch = searchForDir(specDir, specs);
ScriptSearch sourceSearch = searchForDir(srcDir, sources);
FindsScriptLocationsInDirectory findsScriptLocationsInDirectory = new FindsScriptLocationsInDirectory();
List specs = findsScriptLocationsInDirectory.find(specsSearch);
List sources = findsScriptLocationsInDirectory.find(sourceSearch);
JSResourceDependencyManagerImpl resourceDependencyManager = null;
JSDependencyReader jsDependencyReader = new JSDependencyReader();
jsDependencyReader.setPattern(jsDependencyRegex, jsDependencyRegexGroupNumber);
if (jsDependencyRegex != null) {
resourceDependencyManager = new JSResourceDependencyManagerImpl();
FileFinder fileFinder = new FileFinder();
resourceDependencyManager.setJsDependencyReader(jsDependencyReader);
resourceDependencyManager.setFileFinder(fileFinder);
File javasScriptTestDependenciesDirectory = new File(mavenProject.getBasedir(), javaScriptTestDependenciesDirectoryName);
resourceDependencyManager.addPaths(srcDir, specDir);
if (javasScriptTestDependenciesDirectory.exists()) {
resourceDependencyManager.addPaths(javasScriptTestDependenciesDirectory);
}
File javasScriptMocksDirectory = new File(mavenProject.getBasedir(), javaScriptMocksDirectoryName);
if (javasScriptMocksDirectory.exists()) {
resourceDependencyManager.addMockPaths(javasScriptMocksDirectory);
}
}
String specDirString = specDir.getAbsolutePath();
for (String specURIString : specs) {
File specFile;
try {
specFile = new File(new URI(specURIString));
} catch (URISyntaxException e) {
throw new IOException(e.getMessage(), e);
}
String testPath = specFile.getAbsolutePath().replace(specDirString, "").replace(specFile.getName(), "");
// Set specDependencies = jsDependencyReader.readDependencies(specFile);
File runnerFile = new File(specDir, testPath + specFile.getName().substring(0, specFile.getName().lastIndexOf(".")) + specRunnerHtmlFileName);
// File sourceFile = new File(srcDir, testPath + specFile.getName().replace("Test.js", ".js"));
Set scriptURIStrings = new LinkedHashSet();
if (preloadSources != null) {
for (String source : preloadSources) {
scriptURIStrings.add(new File(source).toURI().getPath());
}
}
if (specFile.exists()) {
if (resourceDependencyManager != null) {
Set realDependencies = jsDependencyReader.readDependencies(specFile);
String[] startingPoints = new String[realDependencies.size()];
realDependencies.toArray(startingPoints);
List jsResources = resourceDependencyManager.getTestResourcesFor(startingPoints);
for (JSResource jsResource : jsResources) {
scriptURIStrings.add(jsResource.getJsResourceURI().getPath());
}
}
}
Set relativeURIs = getRelativeURIs(scriptURIStrings, specFile);
if (getLog().isDebugEnabled()) {
getLog().debug("Files included for " + specFile.getName());
for (String str : scriptURIStrings) {
getLog().debug(str);
}
}
relativeURIs.add(specFile.getName());
SpecRunnerHtmlGenerator htmlGenerator = new SpecRunnerHtmlGenerator(relativeURIs, sourceEncoding);
String html = htmlGenerator.generate(ReporterType.JsApiReporter, customRunnerTemplate);
FileUtils.writeStringToFile(runnerFile, html);
runnerFiles.add(runnerFile);
}
return runnerFiles;
}
private Set getRelativeURIs(Collection uris, File specFile) {
String specFileUri = specFile.toURI().getPath();
Set relativeURIs = new LinkedHashSet();
for (String uri : uris) {
String commonPrefix = getCommonPrefix(specFileUri, uri);
int commonPrefixLength = commonPrefix.length();
String relativeURI = getPathToCommonAncestor(specFileUri.substring(commonPrefixLength)) + uri.substring(commonPrefixLength);
relativeURIs.add(relativeURI);
}
return relativeURIs;
}
private String getCommonPrefix(String specFileUri, String uri) {
String commonPrefix = StringUtils.getCommonPrefix(new String[]{uri, specFileUri});
return commonPrefix.substring(0, commonPrefix.lastIndexOf('/') + 1);
}
private String getPathToCommonAncestor(String path) {
StringBuilder stringBuilder = new StringBuilder();
int numParentDirectories = countMatches(path, "/");
for (int i = 0; i < numParentDirectories; i++) {
stringBuilder.append("../");
}
return stringBuilder.toString();
}
private JasmineResult executeSpecs(File runnerFile, SpecRunnerExecutor specRunnerExecutor) throws MalformedURLException {
JasmineResult result = specRunnerExecutor.execute(
runnerFile.toURI().toURL(),
new File(jasmineTargetDir, junitXmlReportFileName),
browserVersion,
timeout, debug, getLog(), format);
return result;
}
private void logResults(JasmineResult result) {
JasmineResultLogger resultLogger = new JasmineResultLogger();
resultLogger.setLog(getLog());
resultLogger.log(result);
}
private void throwAnySpecFailures(JasmineResult result) throws MojoFailureException {
if (haltOnFailure && !result.didPass()) {
throw new MojoFailureException("There were Jasmine spec failures.");
}
}
private ScriptSearch searchForDir(File dir, ScriptSearch search) {
return new ScriptSearch(dir, search.getIncludes(), search.getExcludes());
}
}