Merge branch 'master' of https://github.com/jefklak/jasmine-junit-runner into repackage_for_jar

This commit is contained in:
Brian Lalor 2012-11-23 21:44:05 -05:00
commit c284748800
9 changed files with 104 additions and 10 deletions

View File

@ -103,6 +103,7 @@ You can still use Firebug to debug when generating a specRunner HTML file (see b
* specs: one or more spec file to run. Default behavior: use java Class name (replaces Test with Spec, see example)
* sources: one or more JS production file which your spec needs (included before specs, d'uh)
* generateSpecRunner: (the HTML output, useful for firefox/firebug debugging etc)
* envJs: load EnvJS support (defaults to true)
## Requirements

View File

@ -17,4 +17,9 @@ public @interface JasmineSuite {
boolean generateSpecRunner() default false;
boolean debug() default false;
/**
* If set to false, does not load EnvJS support.
*/
boolean envJs() default true;
}

View File

@ -32,8 +32,7 @@ public class JasmineTestRunner extends Runner {
private final Class<?> testClass;
@JasmineSuite
private class DefaultSuite {
}
private class DefaultSuite { }
public JasmineTestRunner(Class<?> testClass) {
this.testClass = testClass;
@ -56,16 +55,21 @@ public class JasmineTestRunner extends Runner {
pre(context);
context.loadEnv(suiteAnnotation.jsRootDir());
if (suiteAnnotation.envJs()) {
context.loadEnv(suiteAnnotation.jsRootDir());
} else {
context.load(suiteAnnotation.jsRootDir(), "/lib/no-env.js");
}
setUpJasmine(context);
context.load(suiteAnnotation.sourcesRootDir() + "/", suiteAnnotation.sources());
context.load(suiteAnnotation.jsRootDir() + "/specs/", getJasmineSpecs(suiteAnnotation));
return context;
}
protected void pre(RhinoContext context) {
}
protected void pre(RhinoContext context) { }
private void setUpJasmine(RhinoContext context) {
context.loadFromClasspath(JASMINE_LIB_DIR + "/jasmine.js");
@ -158,7 +162,10 @@ public class JasmineTestRunner extends Runner {
}
reportSpecResultToNotifier(notifier, spec);
resetEnvjsWindowSpace();
if (suiteAnnotation.envJs()) {
resetEnvjsWindowSpace();
}
} finally {
fireMethodsWithSpecifiedAnnotationIfAny(testClassInstance, After.class);
}
@ -171,7 +178,6 @@ public class JasmineTestRunner extends Runner {
this.rhinoContext.exit();
}
private Object createTestClassInstance() {
try {
return testClass.newInstance();

View File

@ -14,7 +14,6 @@ import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mozilla.javascript.EvaluatorException;
import be.klak.junit.jasmine.JasmineTestRunner;
import be.klak.junit.jasmine.classes.JasmineTestRunnerExceptionInJSCode;
import be.klak.junit.jasmine.classes.JasmineTestRunnerExceptionInSpec;
import be.klak.junit.jasmine.classes.JasmineTestRunnerFailingSpec;
@ -58,7 +57,7 @@ public class JasmineFailingSpecsTest {
assertThat(failure.getDescription()).isEqualTo(startedDescription);
assertThat(failure.getDescription().getDisplayName()).isEqualTo("will always crash");
assertThat(failure.getMessage()).isEqualTo("ReferenceError: \"OEIWANU\" is not defined. in src/test/javascript/specs/crashingSpec.js (line 3)");
assertThat(failure.getMessage()).contains("ReferenceError: \"OEIWANU\"");
}
@Test(expected = EvaluatorException.class)

View File

@ -14,6 +14,7 @@ import org.mockito.runners.MockitoJUnitRunner;
import be.klak.junit.jasmine.JasmineTestRunner;
import be.klak.junit.jasmine.classes.JasmineTestRunnerSuccessSpec;
import be.klak.junit.jasmine.classes.JasmineTestRunnerDoesNotLoadEnvJS;
@RunWith(MockitoJUnitRunner.class)
public class JasmineFinishedSpecsTest {
@ -38,4 +39,21 @@ public class JasmineFinishedSpecsTest {
assertThat(startedDescription.getDisplayName()).isEqualTo("will always run");
}
@Test
public void doesNotLoadEnvJsWhenSoConfigured() {
new JasmineTestRunner(JasmineTestRunnerDoesNotLoadEnvJS.class).run(notifierMock);
ArgumentCaptor<Description> descriptionStartedCaptor = ArgumentCaptor.forClass(Description.class);
ArgumentCaptor<Description> descriptionFinishedCaptor = ArgumentCaptor.forClass(Description.class);
verify(notifierMock).fireTestStarted(descriptionStartedCaptor.capture());
verify(notifierMock).fireTestFinished(descriptionFinishedCaptor.capture());
verifyNoMoreInteractions(notifierMock);
Description startedDescription = descriptionStartedCaptor.getValue();
Description finishedDescription = descriptionFinishedCaptor.getValue();
assertThat(startedDescription).isSameAs(finishedDescription);
assertThat(startedDescription.getDisplayName()).isEqualTo("is not loaded");
}
}

View File

@ -0,0 +1,6 @@
package be.klak.junit.jasmine.classes;
import be.klak.junit.jasmine.JasmineSuite;
@JasmineSuite(specs = { "doesNotLoadEnvJSSpec.js" }, envJs = false)
public class JasmineTestRunnerDoesNotLoadEnvJS { }

View File

@ -12,7 +12,7 @@ import be.klak.rhino.RhinoContext;
public class RhinoContextEnvjsLoadingTest {
@Test
public void loadEnvShouldSetWindowSpaceAndBeES5Complaint() {
public void loadEnvJSShouldSetWindowSpaceAndBeES5Complaint() {
RhinoContext context = new RhinoContext();
context.loadEnv("src/test/javascript");

View File

@ -0,0 +1,54 @@
// shim to allow jasmine to run in Rhino without EnvJS loaded
// inspired by
// https://groups.google.com/d/msg/jasmine-js/waor9RXUsDw/LY6gNLcMxkcJ
// implementation stolen from env.utils.js
(function(global) {
var threadTimeoutPool = new java.util.HashMap();
global.setTimeout = function(closure, timeout) {
var thread = spawn(function() {
try {
java.lang.Thread.sleep(timeout);
closure();
} catch(e) {
// ignore InterruptedExceptions, is probably due to clearTimeout
if (!(e.javaException instanceof java.lang.InterruptedException)) {
throw(e);
}
}
});
threadTimeoutPool.put(thread.getId(), thread);
return thread.getId();
};
global.setInterval = function(closure, timeout) {
var thread = spawn(function() {
try {
while(true) {
java.lang.Thread.sleep(timeout);
closure();
}
} catch(e) {
// ignore InterruptedExceptions, is probably due to clearTimeout
if (!(e.javaException instanceof java.lang.InterruptedException)) {
throw(e);
}
}
});
threadTimeoutPool.put(thread.getId(), thread);
return thread.getId();
};
global.clearTimeout = function(threadId) {
if (threadId) {
if(threadTimeoutPool.containsKey(threadId)) {
threadTimeoutPool.remove(threadId).interrupt();
}
}
};
global.clearInterval = global.clearTimeout;
})(this);

View File

@ -0,0 +1,5 @@
describe("EnvJS", function() {
it("is not loaded", function() {
expect(typeof(window)).toBe('undefined');
});
});