ClassWithJavascript stjsClass = new Generator().getExistingStjsClass(getConfig().getClassLoader(), testClass);
List<FrameworkMethod> beforeMethods = meth.getTestClass().getAnnotatedMethods(Before.class);
List<FrameworkMethod> afterMethods = meth.getTestClass().getAnnotatedMethods(After.class);
final HTMLFixture htmlFixture = testClass.getAnnotation(HTMLFixture.class);
final Scripts addedScripts = testClass.getAnnotation(Scripts.class);
final ScriptsBefore addedScriptsBefore = testClass.getAnnotation(ScriptsBefore.class);
final ScriptsAfter addedScriptsAfter = testClass.getAnnotation(ScriptsAfter.class);
final Test test = meth.getMethod().getAnnotation(Test.class);
StringBuilder resp = new StringBuilder(8192);
resp.append("<html>\n");
resp.append("<head>\n");
appendScriptTag(resp, "/stjs.js");
appendScriptTag(resp, "/junit.js");
resp.append("<script language='javascript'>stjs.mainCallDisabled=true;</script>\n");
// scripts added explicitly
if (addedScripts != null) {
for (String script : addedScripts.value()) {
appendScriptTag(resp, script);
}
}
// scripts before - new style
if (addedScriptsBefore != null) {
for (String script : addedScriptsBefore.value()) {
appendScriptTag(resp, script);
}
}
Set<URI> jsFiles = new LinkedHashSet<URI>();
for (ClassWithJavascript dep : new DependencyCollection(stjsClass).orderAllDependencies(getConfig().getClassLoader())) {
if (addedScripts != null && dep instanceof BridgeClass) {
// bridge dependencies are not added when using @Scripts
System.out
.println("WARNING: You're using @Scripts deprecated annotation that disables the automatic inclusion of the Javascript files of the bridges you're using! "
+ "Please consider using @ScriptsBefore and/or @ScriptsAfter instead.");
continue;
}
for (URI file : dep.getJavascriptFiles()) {
jsFiles.add(file);
}
}
for (URI file : jsFiles) {
appendScriptTag(resp, file.toString());
}
// scripts after - new style
if (addedScriptsAfter != null) {
for (String script : addedScriptsAfter.value()) {
appendScriptTag(resp, script);
}
}
resp.append("<script language='javascript'>\n");
resp.append(" window.onload=function(){\n");
// resp.append(" console.error(document.getElementsByTagName('html')[0].innerHTML);\n");
// Adapter between generated assert (not global) and JS-test-driver assert (which is a
// set of global methods)
resp.append(" Assert=window;\n");
String testedClassName = testClass.getSimpleName();
resp.append(" parent.startingTest('" + testedClassName + "', '" + method.getName() + "');");
resp.append(" var stjsTest = new " + testedClassName + "();\n");
resp.append(" var stjsResult = 'OK';\n");
resp.append(" var expectedException = " + (test.expected() != Test.None.class ? getTypeName(test.expected()) : null) + ";\n");
resp.append(" try{\n");
// call before methods
for (FrameworkMethod beforeMethod : beforeMethods) {
resp.append(" stjsTest." + beforeMethod.getName() + "();\n");
}
// call the test's method
resp.append(" stjsTest." + method.getName() + "();\n");
resp.append(" if(expectedException){\n");
resp.append(" stjsResult = 'Expected an exception, but none was thrown';\n");
resp.append(" }\n");
resp.append(" }catch(ex){\n");
// an exception was caught while executing the test method
resp.append(" if(!expectedException){\n");
resp.append(" stjsResult = ex;\n");
resp.append(" } else if (!stjs.isInstanceOf(ex.constructor,expectedException)){\n");
resp.append(" stjsResult = ex;\n");
resp.append(" }\n");
resp.append(" }finally{\n");
// call after methods
for (FrameworkMethod afterMethod : afterMethods) {
resp.append(" stjsTest." + afterMethod.getName() + "();\n");
}
resp.append(" parent.reportResultAndRunNextTest(stjsResult, stjsResult.location);\n");
resp.append(" }\n");
resp.append(" }\n");
resp.append("</script>\n");
resp.append("</head>\n");
resp.append("<body>\n");
if (htmlFixture != null) {
if (!Strings.isNullOrEmpty(htmlFixture.value())) {
resp.append(htmlFixture.value());
} else if (!Strings.isNullOrEmpty(htmlFixture.url())) {
StringWriter writer = new StringWriter();
StreamUtils.copy(getConfig().getClassLoader(), htmlFixture.url(), writer);
resp.append(writer.toString());
}
}
resp.append("</body>\n");
resp.append("</html>\n");