Refactor to package JS resources in jar

Instead of requiring each project to have their own copy of the
supporting JavaScript and HTML resources (like jasmine.js), package
them into this project's JAR and load the resources from the
classpath.
This commit is contained in:
Brian Lalor 2012-11-21 09:15:47 -05:00
parent e3e6dc55f8
commit 2b639d921d
17 changed files with 17380 additions and 17355 deletions

View File

@ -4,6 +4,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
class JasmineSpecRunnerGenerator { class JasmineSpecRunnerGenerator {
@ -66,12 +67,17 @@ class JasmineSpecRunnerGenerator {
} }
private String loadTemplate() { private String loadTemplate() {
String template = null;
try { try {
template = FileUtils.readFileToString(new File(suite.jsRootDir() + "/lib/specRunner.tpl")); return IOUtils.toString(
Thread
.currentThread()
.getContextClassLoader()
.getResourceAsStream("js/lib/specRunner.tpl")
);
} catch (NullPointerException e) {
throw new IllegalStateException("spec runner template file not found!");
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("spec runner template file not found!", e); throw new IllegalStateException("spec runner template file could not be read!", e);
} }
return template;
} }
} }

View File

@ -4,6 +4,9 @@ import java.lang.annotation.Annotation;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import java.net.URL;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.runner.Description; import org.junit.runner.Description;
@ -18,7 +21,9 @@ import be.klak.rhino.RhinoContext;
public class JasmineTestRunner extends Runner { public class JasmineTestRunner extends Runner {
private static final int SLEEP_TIME_MILISECONDS = 50; private static final int SLEEP_TIME_MILISECONDS = 50;
private static final String JASMINE_LIB_DIR = "/lib/jasmine-1.0.2/";
// relative to classpath
private static final String JASMINE_LIB_DIR = "js/lib/jasmine-1.0.2";
private JasmineDescriptions jasmineSuite; private JasmineDescriptions jasmineSuite;
@ -63,8 +68,8 @@ public class JasmineTestRunner extends Runner {
} }
private void setUpJasmine(RhinoContext context) { private void setUpJasmine(RhinoContext context) {
context.load(getJsLibDir() + "jasmine.js"); context.loadFromClasspath(JASMINE_LIB_DIR + "/jasmine.js");
context.load(getJsLibDir() + "jasmine.delegator_reporter.js"); context.loadFromClasspath(JASMINE_LIB_DIR + "/jasmine.delegator_reporter.js");
context.evalJS("jasmine.getEnv().addReporter(new jasmine.DelegatorJUnitReporter());"); context.evalJS("jasmine.getEnv().addReporter(new jasmine.DelegatorJUnitReporter());");
} }
@ -102,11 +107,26 @@ public class JasmineTestRunner extends Runner {
} }
private void resetEnvjsWindowSpace() { private void resetEnvjsWindowSpace() {
this.rhinoContext.evalJS("window.location = '" + suiteAnnotation.jsRootDir() + "/lib/blank.html';"); URL blankUrl = Thread
.currentThread()
.getContextClassLoader()
.getResource("js/lib/blank.html");
if (blankUrl == null) {
throw new IllegalStateException("Unable to load js/lib/blank.html from classpath");
} }
private String getJsLibDir() { String blankUrlStr = blankUrl.toExternalForm();
return suiteAnnotation.jsRootDir() + JASMINE_LIB_DIR;
// "file:/path/to/file" is not legal, but "file:///path/to/file" is
if (blankUrlStr.startsWith("file:/") && (! blankUrlStr.startsWith("file:///"))) {
blankUrlStr = "file://" + blankUrlStr.substring(5);
}
this.rhinoContext.evalJS(String.format(
"window.location = '%s';",
blankUrlStr
));
} }
private JasmineDescriptions getJasmineDescriptions() { private JasmineDescriptions getJasmineDescriptions() {

View File

@ -128,8 +128,8 @@ public class RhinoContext {
} }
public void loadEnv(String jsDir) { public void loadEnv(String jsDir) {
load(jsDir + "/lib/env.rhino.1.2.js"); loadFromClasspath("js/lib/env.rhino.1.2.js");
load(jsDir + "/lib/env.utils.js"); loadFromClasspath("js/lib/env.utils.js");
load(jsDir + "/envJsOptions.js"); load(jsDir + "/envJsOptions.js");
} }

View File

@ -1093,7 +1093,7 @@ Envjs.uri = function(path, base) {
} }
// if path is absolute, then just normalize and return // if path is absolute, then just normalize and return
if (path.match('^[a-zA-Z]+://')) { if (path.match('^([a-zA-Z]+://|jar:file:/)')) {
return urlparse.urlnormalize(path); return urlparse.urlnormalize(path);
} }
@ -1499,7 +1499,7 @@ Envjs.connection = function(xhr, responseHandler, data){
instream, instream,
responseXML, responseXML,
i; i;
if ( /^file\:/.test(url) ) { if ( /^(jar:)?file\:/.test(url) ) {
try{ try{
if ( "PUT" == xhr.method || "POST" == xhr.method ) { if ( "PUT" == xhr.method || "POST" == xhr.method ) {
data = data || "" ; data = data || "" ;

View File

@ -1,4 +1,3 @@
/** /**
* Rhino and Envjs additions, filling the missing pieces such as a decent stacktrace * Rhino and Envjs additions, filling the missing pieces such as a decent stacktrace
* 1) Firefox knows new Error().stack but Envjs does not. * 1) Firefox knows new Error().stack but Envjs does not.

View File

@ -13,7 +13,7 @@ public class EnvUtilsTest {
@Before @Before
public void loadJasmineJQueryMatchers(RhinoContext context) { public void loadJasmineJQueryMatchers(RhinoContext context) {
context.load("src/test/javascript/lib/jasmine-1.0.2/jasmine-jquery-rhino.js"); context.loadFromClasspath("js/lib/jasmine-1.0.2/jasmine-jquery-rhino.js");
} }
} }