com.cj.qunit.mojo.http.AutoGeneratedQunitHtmlWrappers Maven / Gradle / Ivy
package com.cj.qunit.mojo.http;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import org.apache.commons.io.IOUtils;
import org.eclipse.jetty.util.log.Log;
import org.httpobjects.HttpObject;
import org.httpobjects.Response;
import com.cj.qunit.mojo.LocatedTest;
import com.cj.qunit.mojo.QunitMavenRunner;
import com.cj.qunit.mojo.QunitTestLocator;
import com.cj.qunit.mojo.QunitTestLocator.TestType;
import com.cj.qunit.mojo.Scanner;
class AutoGeneratedQunitHtmlWrappers extends HttpObject {
private static final String TEMPLATE = readClasspathResource("/qunit.template.html");
private final String webPathToRequireDotJsConfig;
private final String webRoot;
private final List codePaths;
private final Scanner scanner;
public AutoGeneratedQunitHtmlWrappers(String webRoot, String webPathToRequireDotJsConfig, List codePaths, Scanner scanner) {
super("/{resource*}");
this.webPathToRequireDotJsConfig = webPathToRequireDotJsConfig==null?"/qunit-mojo/default-require-config.js":webPathToRequireDotJsConfig;
this.webRoot = webRoot;
this.codePaths = codePaths;
this.scanner = scanner;
}
private String minusPrefix(final String prefix, final String str) {
if(!str.startsWith(prefix)) throw new RuntimeException(str + " doesn't start with " + prefix);
return str.substring(prefix.length());
}
private String createConfigTag(String configPath) {
if (!configPath.trim().equals("")) {
return "";
} else {
return "";
}
}
private LocatedTest locateTest(String path){
for(LocatedTest test: scanner.findTests()){
// System.out.println("Considering '" + test.relativePathToDetectedFile + "'");
if(test.relativePathToDetectedFile.trim().equals(path.trim())) {
// System.out.println("Found it at " + test.relativePathToDetectedFile);
return test;
}else{
// System.out.println("nope");
}
}
return null;
}
public Response get(org.httpobjects.Request req) {
final String path = req.path().valueFor("resource");
if(!path.startsWith("qunit-mojo/")) return null;
// System.out.println("Handling request for " + path);
String impliedCodeFile = impliedCodeFile(path);
if(impliedCodeFile==null)
return null;
// System.out.println("You're telling me there is a '" + impliedCodeFile + "'");
final LocatedTest test = locateTest(impliedCodeFile);
if(test==null){
return NOT_FOUND();
}else{
// System.out.println("web file name is " + test.requireJsModuleName);
final String generatedHtmlFileContent = TEMPLATE.
replaceAll("YOUR_JAVASCRIPT_TEST_FILE_GOES_HERE", Matcher.quoteReplacement(test.requireJsModuleName)).
replaceAll("YOUR_REQUIRE_DOT_JS_CONFIG_GOES_HERE", createConfigTag(Matcher.quoteReplacement(webPathToRequireDotJsConfig))).
replaceAll("YOUR_JAVASCRIPT_TEST_FILE_NAME_GOES_HERE", test.requireJsModuleName);
return OK(Html(generatedHtmlFileContent));
}
}
private String impliedCodeFile(final String path) {
final String p = minusPrefix("qunit-mojo/" , path);
final String impliedJavascriptFile;
if(p.endsWith(".js"))
impliedJavascriptFile = p;
else if(p.endsWith(".coffee"))
impliedJavascriptFile = p;
else if(p.endsWith(".jsx"))
impliedJavascriptFile = p;
else
impliedJavascriptFile = null;
return impliedJavascriptFile;
}
private String lastPathSegment(final String impliedJavascriptFile) {
final String[] parts = impliedJavascriptFile.split("/");
final String fileName = parts.length == 0 ? impliedJavascriptFile : parts[parts.length-1];
return fileName;
}
private static String readClasspathResource(String name){
try {
return IOUtils.toString(AutoGeneratedQunitHtmlWrappers.class.getResourceAsStream(name));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy