From 9e37304fdf3946060840829960d6cf450bf62838 Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Thu, 27 Feb 2014 17:11:01 +0400 Subject: [PATCH] Adds ability to include additional scripts into tests. Includes knockout.js into TCK tests --- teavm-html4j/pom.xml | 3 ++ .../org/teavm/html4j/test/KnockoutFXTest.java | 2 + .../teavm/maven/BuildJavascriptTestMojo.java | 37 +++++++++++++++++++ .../org/teavm/maven/junit-support.js | 29 ++++++++++----- 4 files changed, 62 insertions(+), 9 deletions(-) diff --git a/teavm-html4j/pom.xml b/teavm-html4j/pom.xml index 8e98e9fc2..3990ca7bc 100644 --- a/teavm-html4j/pom.xml +++ b/teavm-html4j/pom.xml @@ -93,6 +93,9 @@ org.teavm.javascript.NullPointerExceptionTransformer + + org/netbeans/html/ko4j/knockout-2.2.1.js + diff --git a/teavm-html4j/src/test/java/org/teavm/html4j/test/KnockoutFXTest.java b/teavm-html4j/src/test/java/org/teavm/html4j/test/KnockoutFXTest.java index da4212977..8d4a36c20 100644 --- a/teavm-html4j/src/test/java/org/teavm/html4j/test/KnockoutFXTest.java +++ b/teavm-html4j/src/test/java/org/teavm/html4j/test/KnockoutFXTest.java @@ -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(); } diff --git a/teavm-maven-plugin/src/main/java/org/teavm/maven/BuildJavascriptTestMojo.java b/teavm-maven-plugin/src/main/java/org/teavm/maven/BuildJavascriptTestMojo.java index 08a92dee4..04849cdb1 100644 --- a/teavm-maven-plugin/src/main/java/org/teavm/maven/BuildJavascriptTestMojo.java +++ b/teavm-maven-plugin/src/main/java/org/teavm/maven/BuildJavascriptTestMojo.java @@ -95,6 +95,11 @@ public class BuildJavascriptTestMojo extends AbstractMojo { private List transformerInstances; + @Parameter + private String[] additionalScripts; + + private List 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); + } + } + } } diff --git a/teavm-maven-plugin/src/main/resources/org/teavm/maven/junit-support.js b/teavm-maven-plugin/src/main/resources/org/teavm/maven/junit-support.js index d825dd71d..7baf47358 100644 --- a/teavm-maven-plugin/src/main/resources/org/teavm/maven/junit-support.js +++ b/teavm-maven-plugin/src/main/resources/org/teavm/maven/junit-support.js @@ -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 {