From 231c5a43ee11eacc96ae402c905a23445e8c51a7 Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Tue, 10 Dec 2013 22:52:59 +0400 Subject: [PATCH] Refactoring JUnit emulation --- .../org/teavm/classlib/junit-support.js | 71 +++++++++++++++---- .../resources/org/teavm/classlib/junit.css | 19 +++++ .../resources/org/teavm/classlib/junit.html | 18 +++++ 3 files changed, 93 insertions(+), 15 deletions(-) create mode 100644 teavm-classlib/src/main/resources/org/teavm/classlib/junit.css create mode 100644 teavm-classlib/src/main/resources/org/teavm/classlib/junit.html diff --git a/teavm-classlib/src/main/resources/org/teavm/classlib/junit-support.js b/teavm-classlib/src/main/resources/org/teavm/classlib/junit-support.js index 5e16bc9a1..a85f34919 100644 --- a/teavm-classlib/src/main/resources/org/teavm/classlib/junit-support.js +++ b/teavm-classlib/src/main/resources/org/teavm/classlib/junit-support.js @@ -2,23 +2,64 @@ currentTestReportBody = null; currentTimeSpent = 0; totalTimeSpent = 0; currentMethodCount = 0; +currentStatusCell = null; +currentExceptionCell = null; +currentTimeCell = null; +currentStartTime = 0; +currentExpectedExceptions = []; +currentFrame = null; -runTestCase = function(instance, methodName, realMethodName, expectedExceptions) { +window.addEventListener("message", function(event) { + endTime = new Date().getTime(); + var message = JSON.parse(event.data); + if (message.status == "ok") { + if (currentExpectedExceptions.length > 0) { + currentStatusCell.appendChild(document.createTextNode("expected exception not thrown")); + currentStatusCell.style.color = 'yellow'; + } else { + currentStatusCell.appendChild(document.createTextNode("ok")); + currentStatusCell.style.color = 'green'; + } + } else if (message.status == "exception") { + if (isExpectedException(e)) { + currentStatusCell.appendChild(document.createTextNode("ok")); + currentStatusCell.style.color = 'green'; + } else { + currentStatusCell.appendChild(document.createTextNode("unexpected exception")); + var exceptionText = document.createElement("pre"); + exceptionText.appendChild(document.createTextNode(e.stack)); + currentExceptionCell.appendChild(exceptionText); + currentStatusCell.style.color = 'red'; + } + } + ++currentMethodCount; + var timeSpent = (endTime - currentStartTime) / 1000; + currentTimeSpent += timeSpent; + currentTimeCell.appendChild(document.createTextNode(timeSpent.toFixed(3))); + document.body.removeChild(currentFrame); +}, false); + +runTestCase = function(methodName, path, expectedExceptions) { var row = document.createElement("tr"); currentTestReportBody.appendChild(row); var nameCell = document.createElement("td"); row.appendChild(nameCell); nameCell.appendChild(document.createTextNode(methodName)); - var statusCell = document.createElement("td"); - row.appendChild(statusCell); - var exceptionCell = document.createElement("td"); - row.appendChild(exceptionCell); - var timeCell = document.createElement("td"); - row.appendChild(timeCell); - var startTime = new Date().getTime(); - var endTime; - try { - instance[realMethodName](); + currentStatusCell = document.createElement("td"); + row.appendChild(currentStatusCell); + currentExceptionCell = document.createElement("td"); + row.appendChild(currentExceptionCell); + currentTimeCell = document.createElement("td"); + row.appendChild(currentTimeCell); + currentStartTime = new Date().getTime(); + currentExpectedExceptions = expectedExceptions; + var frame = document.createElement("iframe"); + cirremtFrame = frame; + document.body.appendChild(frame); + var frameDoc = frame.contentWindow.document; + var frameScript = frameDoc.createElement("script"); + frameScript.src = path; + frameDoc.body.appendChild(frameScript); endTime = new Date().getTime(); if (expectedExceptions.length > 0) { statusCell.appendChild(document.createTextNode("expected exception not thrown")); @@ -46,10 +87,10 @@ runTestCase = function(instance, methodName, realMethodName, expectedExceptions) timeCell.appendChild(document.createTextNode(timeSpent.toFixed(3))); } -isExpectedException = function(e, expectedExceptions) { - if (e.$javaException !== undefined) { - for (var i = 0; i < expectedExceptions.length; ++i) { - if (expectedExceptions[i] === e.$javaException.$class) { +isExpectedException = function(e) { + if (e.javaException !== undefined) { + for (var i = 0; i < currentExpectedExceptions.length; ++i) { + if (currentExpectedExceptions[i] === e.javaException) { return true; } } diff --git a/teavm-classlib/src/main/resources/org/teavm/classlib/junit.css b/teavm-classlib/src/main/resources/org/teavm/classlib/junit.css new file mode 100644 index 000000000..dc81db782 --- /dev/null +++ b/teavm-classlib/src/main/resources/org/teavm/classlib/junit.css @@ -0,0 +1,19 @@ +table { + border-collapse: collapse; + border: 2px solid black; + margin: 2em 1em 2em 1em; +} +table td, table th { + border: 1px solid gray; + padding: 0.1em 0.5em 0.2em 0.5em; +} +table thead, table tfoot { + border: 2px solid black; +} +iframe { + with: 1px; + height: 1px; + padding: 0px; + margin: 0px; + border-style: none; +} \ No newline at end of file diff --git a/teavm-classlib/src/main/resources/org/teavm/classlib/junit.html b/teavm-classlib/src/main/resources/org/teavm/classlib/junit.html new file mode 100644 index 000000000..dbd335300 --- /dev/null +++ b/teavm-classlib/src/main/resources/org/teavm/classlib/junit.html @@ -0,0 +1,18 @@ + + + + TeaVM JUnit tests + + TeaVM JUnit tests + + + + + + + + \ No newline at end of file