From 958592a4b0a19a23f3b6b754ae9571f487328270 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Sun, 10 Nov 2013 22:49:56 +0400 Subject: [PATCH] Adds generator of HTML page, running junit test cases --- teavm-classlib/pom.xml | 5 ++ .../classlib/java/lang/TObjectTests.java | 30 +++++++ .../java/lang/annotation/TAnnotation.java | 8 ++ .../classlibgen/ClasslibTestGenerator.java | 86 ++++++++++++++++--- .../org/teavm/classlib/junit-support.js | 41 +++++++++ .../java/org/teavm/javascript/Renderer.java | 4 +- .../teavm/javascript/StatementGenerator.java | 4 +- .../resource/ClasspathResourceMapper.java | 5 +- 8 files changed, 167 insertions(+), 16 deletions(-) create mode 100644 teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObjectTests.java create mode 100644 teavm-classlib/src/main/java/org/teavm/classlib/java/lang/annotation/TAnnotation.java create mode 100644 teavm-classlib/src/main/resources/org/teavm/classlib/junit-support.js diff --git a/teavm-classlib/pom.xml b/teavm-classlib/pom.xml index 3d9a13a75..711706dfe 100644 --- a/teavm-classlib/pom.xml +++ b/teavm-classlib/pom.xml @@ -16,5 +16,10 @@ 0.0.1-SNAPSHOT true + + junit + junit + 4.11 + \ No newline at end of file diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObjectTests.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObjectTests.java new file mode 100644 index 000000000..08baad0be --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/TObjectTests.java @@ -0,0 +1,30 @@ +package org.teavm.classlib.java.lang; + +import static org.junit.Assert.*; +import org.junit.Test; + +/** + * + * @author Alexey Andreev + */ +class TObjectTests { + @Test + public void objectCreated() { + Object a = new Object(); + assertNotNull(a); + } + + @Test + public void differentInstancesNotEqual() { + Object a = new Object(); + Object b = new Object(); + assertNotEquals(a, b); + } + + @Test + public void sameInstancesAreEqual() { + Object a = new Object(); + Object b = a; + assertEquals(a, b); + } +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/annotation/TAnnotation.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/annotation/TAnnotation.java new file mode 100644 index 000000000..863f86765 --- /dev/null +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/annotation/TAnnotation.java @@ -0,0 +1,8 @@ +package org.teavm.classlib.java.lang.annotation; + +/** + * + * @author Alexey Andreev + */ +public @interface TAnnotation { +} diff --git a/teavm-classlib/src/main/java/org/teavm/classlibgen/ClasslibTestGenerator.java b/teavm-classlib/src/main/java/org/teavm/classlibgen/ClasslibTestGenerator.java index 455fa93fc..cfa5a23eb 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlibgen/ClasslibTestGenerator.java +++ b/teavm-classlib/src/main/java/org/teavm/classlibgen/ClasslibTestGenerator.java @@ -1,12 +1,15 @@ package org.teavm.classlibgen; +import java.io.IOException; +import java.io.InputStream; +import org.apache.commons.io.IOUtils; import org.teavm.codegen.DefaultAliasProvider; import org.teavm.codegen.DefaultNamingStrategy; import org.teavm.codegen.SourceWriter; import org.teavm.javascript.Decompiler; import org.teavm.javascript.Renderer; import org.teavm.javascript.ast.ClassNode; -import org.teavm.model.ClassHolder; +import org.teavm.model.*; import org.teavm.model.resource.ClasspathClassHolderSource; /** @@ -14,16 +17,77 @@ import org.teavm.model.resource.ClasspathClassHolderSource; * @author Alexey Andreev */ public class ClasslibTestGenerator { - public static void main(String[] args) { - ClasspathClassHolderSource source = new ClasspathClassHolderSource(); - Decompiler decompiler = new Decompiler(source); - ClassHolder cls = source.getClassHolder("java.lang.Object"); - ClassNode clsNode = decompiler.decompile(cls); - DefaultAliasProvider aliasProvider = new DefaultAliasProvider(); - DefaultNamingStrategy naming = new DefaultNamingStrategy(aliasProvider, source); - SourceWriter writer = new SourceWriter(naming); - Renderer renderer = new Renderer(writer, source); - renderer.render(clsNode); + private static ClasspathClassHolderSource classSource; + private static Decompiler decompiler; + private static DefaultAliasProvider aliasProvider; + private static DefaultNamingStrategy naming; + private static SourceWriter writer; + private static Renderer renderer; + + public static void main(String[] args) throws IOException { + classSource = new ClasspathClassHolderSource(); + decompiler = new Decompiler(classSource); + aliasProvider = new DefaultAliasProvider(); + naming = new DefaultNamingStrategy(aliasProvider, classSource); + writer = new SourceWriter(naming); + renderer = new Renderer(writer, classSource); + decompileClass("java.lang.Object"); + decompileClass("java.lang.ObjectTests"); + decompileClass("java.lang.Class"); + decompileClass("java.lang.annotation.Annotation"); + decompileClass("org.junit.Assert"); + decompileClass("org.junit.Test"); + renderHead(); + ClassLoader classLoader = ClasslibTestGenerator.class.getClassLoader(); + try (InputStream input = classLoader.getResourceAsStream( + "org/teavm/classlib/junit-support.js")) { + System.out.println(IOUtils.toString(input)); + } + try (InputStream input = classLoader.getResourceAsStream( + "org/teavm/javascript/runtime.js")) { + System.out.println(IOUtils.toString(input)); + } + renderClassTest(classSource.getClassHolder("java.lang.ObjectTests")); System.out.println(writer); + renderFoot(); + } + + private static void decompileClass(String className) { + ClassHolder cls = classSource.getClassHolder(className); + ClassNode clsNode = decompiler.decompile(cls); + renderer.render(clsNode); + } + + private static void renderHead() { + System.out.println(""); + System.out.println(""); + System.out.println(" "); + System.out.println(" TeaVM JUnit tests"); + System.out.println(" "); + System.out.println(" TeaVM JUnit tests"); + System.out.println(" "); + System.out.println(" "); + System.out.println(" "); + System.out.println(" "); + System.out.println(""); + } + + private static void renderClassTest(ClassHolder cls) { + writer.append("testClass(\"" + cls.getName() + "\", function() {").newLine().indent(); + MethodReference cons = new MethodReference(cls.getName(), + new MethodDescriptor("", ValueType.VOID)); + for (MethodHolder method : cls.getMethods()) { + if (method.getAnnotations().get("org.junit.Test") != null) { + MethodReference ref = new MethodReference(cls.getName(), method.getDescriptor()); + writer.append("runTestCase(").appendClass(cls.getName()).append(".").appendMethod(cons) + .append("(), \"" + method.getName() + "\", \"").appendMethod(ref).append("\");").newLine(); + } + } + writer.outdent().append("})").newLine(); } } 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 new file mode 100644 index 000000000..64245d64f --- /dev/null +++ b/teavm-classlib/src/main/resources/org/teavm/classlib/junit-support.js @@ -0,0 +1,41 @@ +JUnitAssertionFailure = function() {} +JUnitAssertionFailure.prototype = new Error(); + +currentTestReportBody = null; + +runTestCase = function(instance, methodName, realMethodName) { + 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); + try { + instance[realMethodName](); + statusCell.appendChild(document.createTextNode("ok")); + } catch (e) { + if (e instanceof JUnitAssertionFailure) { + statusCell.appendChild(document.createTextNode("assertion failed")); + exceptionCell.appendChild(document.createTextNode(e.stack)); + } else { + statusCell.appendChild(document.createTextNode("unexpected exception")); + exceptionCell.appendChild(document.createTextNode(e.stack)); + } + } +} + +testClass = function(className, classTests) { + var table = document.createElement("table"); + document.body.appendChild(table); + var caption = document.createElement("caption"); + table.appendChild(caption); + caption.appendChild(document.createTextNode(className)); + var tbody = document.createElement("tbody"); + table.appendChild(tbody); + currentTestReportBody = tbody; + classTests(); + currentTestReportBody = null; +} \ No newline at end of file diff --git a/teavm-core/src/main/java/org/teavm/javascript/Renderer.java b/teavm-core/src/main/java/org/teavm/javascript/Renderer.java index 0c45b38d4..848429315 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/Renderer.java +++ b/teavm-core/src/main/java/org/teavm/javascript/Renderer.java @@ -153,7 +153,7 @@ public class Renderer implements ExprVisitor, StatementVisitor { } renderWorkingMethod(method); int startParam = 0; - if (method.getModifiers().contains(ElementModifier.STATIC)) { + if (method.getModifiers().contains(NodeModifier.STATIC)) { startParam = 1; } writer.appendClass(ref.getClassName()).append('.'); @@ -186,7 +186,7 @@ public class Renderer implements ExprVisitor, StatementVisitor { MethodReference ref = method.getReference(); writer.append("function ").appendClass(ref.getClassName()).append('_').appendMethod(ref).append('('); int startParam = 0; - if (method.getModifiers().contains(ElementModifier.STATIC)) { + if (method.getModifiers().contains(NodeModifier.STATIC)) { startParam = 1; } for (int i = startParam; i <= ref.parameterCount(); ++i) { diff --git a/teavm-core/src/main/java/org/teavm/javascript/StatementGenerator.java b/teavm-core/src/main/java/org/teavm/javascript/StatementGenerator.java index f59d23998..171aacfb3 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/StatementGenerator.java +++ b/teavm-core/src/main/java/org/teavm/javascript/StatementGenerator.java @@ -302,11 +302,11 @@ public class StatementGenerator implements InstructionVisitor { insn.getAlternative()); break; case NOT_NULL: - branch(Expr.binary(BinaryOperation.STRICT_EQUALS, Expr.var(insn.getOperand().getIndex()), + branch(Expr.binary(BinaryOperation.STRICT_NOT_EQUALS, Expr.var(insn.getOperand().getIndex()), Expr.constant(null)), insn.getConsequent(), insn.getAlternative()); break; case NULL: - branch(Expr.binary(BinaryOperation.STRICT_NOT_EQUALS, Expr.var(insn.getOperand().getIndex()), + branch(Expr.binary(BinaryOperation.STRICT_EQUALS, Expr.var(insn.getOperand().getIndex()), Expr.constant(null)), insn.getConsequent(), insn.getAlternative()); break; } diff --git a/teavm-core/src/main/java/org/teavm/model/resource/ClasspathResourceMapper.java b/teavm-core/src/main/java/org/teavm/model/resource/ClasspathResourceMapper.java index 29f3bd4f3..5cb6d636e 100644 --- a/teavm-core/src/main/java/org/teavm/model/resource/ClasspathResourceMapper.java +++ b/teavm-core/src/main/java/org/teavm/model/resource/ClasspathResourceMapper.java @@ -81,7 +81,10 @@ public class ClasspathResourceMapper implements Mapper { String packageName = name.substring(0, index); ClassHolder classHolder = innerMapper.map(transformation.packagePrefix + "." + packageName + "." + transformation.classPrefix + className); - return renamer.rename(classHolder); + if (classHolder != null) { + classHolder = renamer.rename(classHolder); + } + return classHolder; } } return innerMapper.map(name);