Add a few hooks to make it possible to extend functionality

This commit is contained in:
Ola Bini 2011-11-08 13:35:22 -06:00
parent 280122cee3
commit c20ee414f6
1 changed files with 214 additions and 203 deletions

View File

@ -1,203 +1,214 @@
package be.klak.junit.jasmine; package be.klak.junit.jasmine;
import java.lang.annotation.Annotation; 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 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;
import org.junit.runner.Runner; import org.junit.runner.Runner;
import org.junit.runner.notification.RunNotifier; import org.junit.runner.notification.RunNotifier;
import org.mozilla.javascript.ContextFactory; import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.NativeArray; import org.mozilla.javascript.NativeArray;
import org.mozilla.javascript.tools.debugger.Main; import org.mozilla.javascript.tools.debugger.Main;
import be.klak.rhino.RhinoContext; 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/"; private static final String JASMINE_LIB_DIR = "/lib/jasmine-1.0.2/";
private JasmineDescriptions jasmineSuite; private JasmineDescriptions jasmineSuite;
private final RhinoContext rhinoContext; protected final RhinoContext rhinoContext;
private final JasmineSuite suiteAnnotation; protected final JasmineSuite suiteAnnotation;
private final Class<?> testClass; private final Class<?> testClass;
@JasmineSuite @JasmineSuite
private class DefaultSuite { private class DefaultSuite {
} }
public JasmineTestRunner(Class<?> testClass) { public JasmineTestRunner(Class<?> testClass) {
this.testClass = testClass; this.testClass = testClass;
this.suiteAnnotation = getJasmineSuiteAnnotationFromTestClass(); this.suiteAnnotation = getJasmineSuiteAnnotationFromTestClass();
Main debugger = null; Main debugger = null;
if (this.suiteAnnotation.debug()) { if (this.suiteAnnotation.debug()) {
debugger = createDebugger(); debugger = createDebugger();
} }
this.rhinoContext = setUpRhinoScope(); this.rhinoContext = setUpRhinoScope();
if (this.suiteAnnotation.debug()) { if (this.suiteAnnotation.debug()) {
debugger.doBreak(); debugger.doBreak();
} }
} }
private RhinoContext setUpRhinoScope() { private RhinoContext setUpRhinoScope() {
RhinoContext context = new RhinoContext(); RhinoContext context = new RhinoContext();
context.loadEnv(suiteAnnotation.jsRootDir());
setUpJasmine(context); pre(context);
context.load(suiteAnnotation.sourcesRootDir() + "/", suiteAnnotation.sources()); context.loadEnv(suiteAnnotation.jsRootDir());
context.load(suiteAnnotation.jsRootDir() + "/specs/", getJasmineSpecs(suiteAnnotation)); setUpJasmine(context);
return context;
} context.load(suiteAnnotation.sourcesRootDir() + "/", suiteAnnotation.sources());
context.load(suiteAnnotation.jsRootDir() + "/specs/", getJasmineSpecs(suiteAnnotation));
private void setUpJasmine(RhinoContext context) { return context;
context.load(getJsLibDir() + "jasmine.js"); }
context.load(getJsLibDir() + "jasmine.delegator_reporter.js");
protected void pre(RhinoContext context) {
context.evalJS("jasmine.getEnv().addReporter(new jasmine.DelegatorJUnitReporter());"); }
}
private void setUpJasmine(RhinoContext context) {
private Main createDebugger() { context.load(getJsLibDir() + "jasmine.js");
Main debugger = new Main("JS Debugger"); context.load(getJsLibDir() + "jasmine.delegator_reporter.js");
debugger.setExitAction(new Runnable() { context.evalJS("jasmine.getEnv().addReporter(new jasmine.DelegatorJUnitReporter());");
public void run() { }
System.exit(0);
} private Main createDebugger() {
}); Main debugger = new Main("JS Debugger");
debugger.attachTo(ContextFactory.getGlobal()); debugger.setExitAction(new Runnable() {
debugger.pack(); public void run() {
debugger.setSize(600, 460); System.exit(0);
debugger.setVisible(true); }
});
return debugger;
} debugger.attachTo(ContextFactory.getGlobal());
debugger.pack();
private JasmineSuite getJasmineSuiteAnnotationFromTestClass() { debugger.setSize(600, 460);
JasmineSuite suiteAnnotation = testClass.getAnnotation(JasmineSuite.class); debugger.setVisible(true);
if (suiteAnnotation == null) {
suiteAnnotation = DefaultSuite.class.getAnnotation(JasmineSuite.class); return debugger;
} }
return suiteAnnotation;
} private JasmineSuite getJasmineSuiteAnnotationFromTestClass() {
JasmineSuite suiteAnnotation = testClass.getAnnotation(JasmineSuite.class);
private String[] getJasmineSpecs(JasmineSuite suiteAnnotation) { if (suiteAnnotation == null) {
if (suiteAnnotation.specs().length == 0) { suiteAnnotation = DefaultSuite.class.getAnnotation(JasmineSuite.class);
return new String[] { StringUtils.uncapitalize(testClass.getSimpleName()).replace("Test", "Spec") + ".js" }; }
} return suiteAnnotation;
return suiteAnnotation.specs(); }
}
private String[] getJasmineSpecs(JasmineSuite suiteAnnotation) {
private void resetEnvjsWindowSpace() { if (suiteAnnotation.specs().length == 0) {
this.rhinoContext.evalJS("window.location = '" + suiteAnnotation.jsRootDir() + "/lib/blank.html';"); return new String[] { StringUtils.uncapitalize(testClass.getSimpleName()).replace("Test", "Spec") + ".js" };
} }
return suiteAnnotation.specs();
private String getJsLibDir() { }
return suiteAnnotation.jsRootDir() + JASMINE_LIB_DIR;
} private void resetEnvjsWindowSpace() {
this.rhinoContext.evalJS("window.location = '" + suiteAnnotation.jsRootDir() + "/lib/blank.html';");
private JasmineDescriptions getJasmineDescriptions() { }
if (this.jasmineSuite == null) {
NativeArray baseSuites = (NativeArray) rhinoContext.evalJS("jasmine.getEnv().currentRunner().suites()"); private String getJsLibDir() {
this.jasmineSuite = new JasmineJSSuiteConverter(rhinoContext).convertToJunitDescriptions(testClass, baseSuites); return suiteAnnotation.jsRootDir() + JASMINE_LIB_DIR;
} }
return this.jasmineSuite;
} private JasmineDescriptions getJasmineDescriptions() {
if (this.jasmineSuite == null) {
@Override NativeArray baseSuites = (NativeArray) rhinoContext.evalJS("jasmine.getEnv().currentRunner().suites()");
public Description getDescription() { this.jasmineSuite = new JasmineJSSuiteConverter(rhinoContext).convertToJunitDescriptions(testClass, baseSuites);
return getJasmineDescriptions().getRootDescription(); }
} return this.jasmineSuite;
}
@Override
public void run(RunNotifier notifier) { @Override
generateSpecRunnerIfNeeded(); public Description getDescription() {
return getJasmineDescriptions().getRootDescription();
for (JasmineSpec spec : getJasmineDescriptions().getSpecs()) { }
Object testClassInstance = createTestClassInstance();
fireMethodsWithSpecifiedAnnotationIfAny(testClassInstance, Before.class); @Override
public void run(RunNotifier notifier) {
try { generateSpecRunnerIfNeeded();
notifier.fireTestStarted(spec.getDescription());
spec.execute(rhinoContext); for (JasmineSpec spec : getJasmineDescriptions().getSpecs()) {
while (!spec.isDone()) { Object testClassInstance = createTestClassInstance();
waitALittle(); fireMethodsWithSpecifiedAnnotationIfAny(testClassInstance, Before.class);
}
try {
reportSpecResultToNotifier(notifier, spec); notifier.fireTestStarted(spec.getDescription());
resetEnvjsWindowSpace(); spec.execute(rhinoContext);
} finally { while (!spec.isDone()) {
fireMethodsWithSpecifiedAnnotationIfAny(testClassInstance, After.class); waitALittle();
} }
}
reportSpecResultToNotifier(notifier, spec);
this.rhinoContext.exit(); resetEnvjsWindowSpace();
} } finally {
fireMethodsWithSpecifiedAnnotationIfAny(testClassInstance, After.class);
private Object createTestClassInstance() { }
try { }
return testClass.newInstance();
} catch (Exception ex) { after();
throw new RuntimeException("Unable to create a new instance of testClass " + testClass.getSimpleName() }
+ " using a no-arg constructor", ex);
} protected void after() {
} this.rhinoContext.exit();
}
private void fireMethodsWithSpecifiedAnnotationIfAny(Object testClassInstance, Class<? extends Annotation> annotation) {
for (Method method : testClass.getMethods()) {
private Object createTestClassInstance() {
try { try {
if (method.getAnnotation(annotation) != null) { return testClass.newInstance();
method.setAccessible(true); } catch (Exception ex) {
Class<?>[] parameterTypes = method.getParameterTypes(); throw new RuntimeException("Unable to create a new instance of testClass " + testClass.getSimpleName()
if (parameterTypes.length == 0) { + " using a no-arg constructor", ex);
method.invoke(testClassInstance, (Object[]) null); }
} else if (parameterTypes.length == 1 && RhinoContext.class.isAssignableFrom(parameterTypes[0])) { }
method.invoke(testClassInstance, new Object[] { this.rhinoContext });
} else { private void fireMethodsWithSpecifiedAnnotationIfAny(Object testClassInstance, Class<? extends Annotation> annotation) {
throw new IllegalStateException("Annotated method does not have zero or rhinoContext as parameterTypes"); for (Method method : testClass.getMethods()) {
}
} try {
} catch (Exception ex) { if (method.getAnnotation(annotation) != null) {
throw new RuntimeException( method.setAccessible(true);
"Exception while firing " + annotation.getSimpleName() + " method: " + method.getName(), ex); Class<?>[] parameterTypes = method.getParameterTypes();
} if (parameterTypes.length == 0) {
} method.invoke(testClassInstance, (Object[]) null);
} } else if (parameterTypes.length == 1 && RhinoContext.class.isAssignableFrom(parameterTypes[0])) {
method.invoke(testClassInstance, new Object[] { this.rhinoContext });
private void generateSpecRunnerIfNeeded() { } else {
if (suiteAnnotation.generateSpecRunner()) { throw new IllegalStateException("Annotated method does not have zero or rhinoContext as parameterTypes");
String[] jasmineSpecs = getJasmineSpecs(suiteAnnotation); }
new JasmineSpecRunnerGenerator(jasmineSpecs, suiteAnnotation, suiteAnnotation.jsRootDir() + "/runners", }
testClass.getSimpleName() } catch (Exception ex) {
+ "Runner.html") throw new RuntimeException(
.generate(); "Exception while firing " + annotation.getSimpleName() + " method: " + method.getName(), ex);
} }
} }
}
private void reportSpecResultToNotifier(RunNotifier notifier, JasmineSpec spec) {
if (spec.isPassed(rhinoContext)) { private void generateSpecRunnerIfNeeded() {
notifier.fireTestFinished(spec.getDescription()); if (suiteAnnotation.generateSpecRunner()) {
} else if (spec.isFailed(rhinoContext)) { String[] jasmineSpecs = getJasmineSpecs(suiteAnnotation);
notifier.fireTestFailure(spec.getJunitFailure(rhinoContext)); new JasmineSpecRunnerGenerator(jasmineSpecs, suiteAnnotation, suiteAnnotation.jsRootDir() + "/runners",
} else { testClass.getSimpleName()
throw new IllegalStateException("Unexpected spec status received: " + spec); + "Runner.html")
} .generate();
} }
}
private void waitALittle() {
try { private void reportSpecResultToNotifier(RunNotifier notifier, JasmineSpec spec) {
Thread.sleep(SLEEP_TIME_MILISECONDS); if (spec.isPassed(rhinoContext)) {
} catch (Exception e) { notifier.fireTestFinished(spec.getDescription());
throw new RuntimeException(e); } else if (spec.isFailed(rhinoContext)) {
} notifier.fireTestFailure(spec.getJunitFailure(rhinoContext));
} } else {
throw new IllegalStateException("Unexpected spec status received: " + spec);
} }
}
private void waitALittle() {
try {
Thread.sleep(SLEEP_TIME_MILISECONDS);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}