Adds ability to include additional scripts into tests. Includes

knockout.js into TCK tests
This commit is contained in:
konsoletyper 2014-02-27 17:11:01 +04:00
parent 5c613d2f7f
commit 9e37304fdf
4 changed files with 62 additions and 9 deletions

View File

@ -93,6 +93,9 @@
<transformers>
<param>org.teavm.javascript.NullPointerExceptionTransformer</param>
</transformers>
<additionalScripts>
<param>org/netbeans/html/ko4j/knockout-2.2.1.js</param>
</additionalScripts>
</configuration>
</execution>
</executions>

View File

@ -54,6 +54,7 @@ import net.java.html.BrwsrCtx;
import net.java.html.js.JavaScriptBody;
import org.apidesign.html.boot.spi.Fn;
import org.apidesign.html.context.spi.Contexts;
import org.apidesign.html.json.spi.Technology;
import org.apidesign.html.json.spi.Transfer;
import org.apidesign.html.json.tck.KnockoutTCK;
import org.netbeans.html.ko4j.KO4J;
@ -98,6 +99,7 @@ public final class KnockoutFXTest extends KnockoutTCK {
KO4J ko4j = new KO4J();
return Contexts.newBuilder()
.register(Transfer.class, ko4j.transfer(), 1)
.register(Technology.class, new KO4J().knockout(), 1)
.build();
}

View File

@ -95,6 +95,11 @@ public class BuildJavascriptTestMojo extends AbstractMojo {
private List<ClassHolderTransformer> transformerInstances;
@Parameter
private String[] additionalScripts;
private List<String> additionalScriptLocalPaths = new ArrayList<>();
public void setProject(MavenProject project) {
this.project = project;
}
@ -162,6 +167,7 @@ public class BuildJavascriptTestMojo extends AbstractMojo {
}
transformerInstances = instantiateTransformers(classLoader);
includeAdditionalScripts(classLoader);
File allTestsFile = new File(outputDir, "tests/all.js");
try (Writer allTestsWriter = new OutputStreamWriter(new FileOutputStream(allTestsFile), "UTF-8")) {
allTestsWriter.write("doRunTests = function() {\n");
@ -193,6 +199,13 @@ public class BuildJavascriptTestMojo extends AbstractMojo {
firstException = false;
allTestsWriter.append("\"" + exception + "\"");
}
allTestsWriter.append("], additionalScripts : [");
for (int i = 0; i < additionalScriptLocalPaths.size(); ++i) {
if (i > 0) {
allTestsWriter.append(", ");
}
escapeString(additionalScriptLocalPaths.get(i), allTestsWriter);
}
allTestsWriter.append("] }");
}
allTestsWriter.append("] }");
@ -505,4 +518,28 @@ public class BuildJavascriptTestMojo extends AbstractMojo {
}
return transformerInstances;
}
private void includeAdditionalScripts(ClassLoader classLoader) throws MojoExecutionException {
if (additionalScripts == null) {
return;
}
for (String script : additionalScripts) {
String simpleName = script.substring(script.lastIndexOf('/') + 1);
additionalScriptLocalPaths.add("tests/" + simpleName);
if (classLoader.getResource(script) == null) {
throw new MojoExecutionException("Additional script " + script + " was not found");
}
File file = new File(outputDir, "tests/" + simpleName);
try (InputStream in = classLoader.getResourceAsStream(script)) {
if (!file.exists()) {
file.createNewFile();
}
try(OutputStream out = new FileOutputStream(file)) {
IOUtils.copy(in, out);
}
} catch (IOException e) {
throw new MojoExecutionException("Error copying additional script " + script, e);
}
}
}
}

View File

@ -66,12 +66,12 @@ JUnitServer.prototype.isExpectedException = function(ex) {
}
return false;
}
JUnitServer.prototype.runTestCase = function(methodName, path, expectedExceptions, callback) {
JUnitServer.prototype.runTestCase = function(methodName, path, expectedExceptions, additionalScripts, callback) {
this.createRow(methodName);
this.startTime = new Date().getTime();
this.expectedExceptions = expectedExceptions;
var self = this;
this.loadCode(path, function() {
this.loadCode(path, additionalScripts, function() {
messageHandler = function(event) {
window.removeEventListener("message", messageHandler);
self.handleEvent(JSON.parse(event.data), callback);
@ -93,16 +93,27 @@ JUnitServer.prototype.createRow = function(methodName) {
this.timeCell = document.createElement("td");
row.appendChild(this.timeCell);
}
JUnitServer.prototype.loadCode = function(path, callback) {
JUnitServer.prototype.loadCode = function(path, additionalScripts, callback) {
this.frame = document.createElement("iframe");
document.body.appendChild(this.frame);
var frameDoc = this.frame.contentWindow.document;
var self = this;
this.loadScript("junit-support.js", function() {
self.loadScript("runtime.js", function() {
self.loadScript(path, callback);
});
});
var sequence = ["junit-support.js", "runtime.js"];
if (additionalScripts) {
for (var i = 0; i < additionalScripts.length; ++i) {
sequence.push(additionalScripts[i]);
}
}
sequence.push(path);
self.loadScripts(sequence, 0, callback);
}
JUnitServer.prototype.loadScripts = function(scripts, index, callback) {
var self = this;
if (index == scripts.length) {
callback();
} else {
this.loadScript(scripts[index], function() { self.loadScripts(scripts, index + 1, callback) });
}
}
JUnitServer.prototype.loadScript = function(name, callback) {
var doc = this.frame.contentWindow.document;
@ -141,7 +152,7 @@ JUnitServer.prototype.runMethodFromList = function(methods, index, callback) {
if (index < methods.length) {
var method = methods[index];
var self = this;
this.runTestCase(method.name, method.script, method.expected, function() {
this.runTestCase(method.name, method.script, method.expected, method.additionalScripts, function() {
self.runMethodFromList(methods, index + 1, callback);
});
} else {