From c6e7b30bedb40dff369ebfeba44d4cc234aacf18 Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Fri, 6 Jun 2014 18:18:52 +0400 Subject: [PATCH] Adds tests for metadata generators --- pom.xml | 5 + teavm-classlib/pom.xml | 22 +++++ .../metadata/DependentTestResource.java | 5 +- .../metadata/MetadataGeneratorTest.java | 59 +++++++++++ .../teavm/platform/metadata/TestResource.java | 99 +++++++++++++++++++ .../metadata/TestResourceGenerator.java | 65 ++++++++++++ .../java/org/teavm/javascript/Decompiler.java | 1 + .../MetadataProviderNativeGenerator.java | 2 +- .../plugin/MetadataProviderTransformer.java | 5 +- .../plugin/ResourceAccessorTransformer.java | 4 +- .../platform/plugin/ResourceTransformer.java | 2 +- .../teavm/platform/metadata/TestResource.java | 31 ------ 12 files changed, 262 insertions(+), 38 deletions(-) rename teavm-platform/src/test/java/org/teavm/platform/metadata/MetadataGeneratorTest.java => teavm-classlib/src/test/java/org/teavm/platform/metadata/DependentTestResource.java (87%) create mode 100644 teavm-classlib/src/test/java/org/teavm/platform/metadata/MetadataGeneratorTest.java create mode 100644 teavm-classlib/src/test/java/org/teavm/platform/metadata/TestResource.java create mode 100644 teavm-classlib/src/test/java/org/teavm/platform/metadata/TestResourceGenerator.java delete mode 100644 teavm-platform/src/test/java/org/teavm/platform/metadata/TestResource.java diff --git a/pom.xml b/pom.xml index c144930dc..6e0e1f8f5 100644 --- a/pom.xml +++ b/pom.xml @@ -178,6 +178,11 @@ + + org.apache.maven.plugins + maven-surefire-plugin + 2.17 + diff --git a/teavm-classlib/pom.xml b/teavm-classlib/pom.xml index 199365cb0..57d85caa8 100644 --- a/teavm-classlib/pom.xml +++ b/teavm-classlib/pom.xml @@ -30,6 +30,12 @@ junit test + + org.teavm + teavm-platform + ${project.version} + test + org.teavm teavm-core @@ -51,6 +57,13 @@ org.teavm teavm-maven-plugin ${project.version} + + + org.teavm + teavm-platform + ${project.version} + + generate-javascript-tests @@ -96,6 +109,15 @@ + + org.apache.maven.plugins + maven-surefire-plugin + + + org/teavm/platform/metadata/*.java + + + org.apache.maven.plugins maven-source-plugin diff --git a/teavm-platform/src/test/java/org/teavm/platform/metadata/MetadataGeneratorTest.java b/teavm-classlib/src/test/java/org/teavm/platform/metadata/DependentTestResource.java similarity index 87% rename from teavm-platform/src/test/java/org/teavm/platform/metadata/MetadataGeneratorTest.java rename to teavm-classlib/src/test/java/org/teavm/platform/metadata/DependentTestResource.java index 4f240dd36..3415d46a1 100644 --- a/teavm-platform/src/test/java/org/teavm/platform/metadata/MetadataGeneratorTest.java +++ b/teavm-classlib/src/test/java/org/teavm/platform/metadata/DependentTestResource.java @@ -19,6 +19,9 @@ package org.teavm.platform.metadata; * * @author Alexey Andreev */ -public class MetadataGeneratorTest { +@Resource +public interface DependentTestResource { + String getBar(); + void setBar(String bar); } diff --git a/teavm-classlib/src/test/java/org/teavm/platform/metadata/MetadataGeneratorTest.java b/teavm-classlib/src/test/java/org/teavm/platform/metadata/MetadataGeneratorTest.java new file mode 100644 index 000000000..280ee32f5 --- /dev/null +++ b/teavm-classlib/src/test/java/org/teavm/platform/metadata/MetadataGeneratorTest.java @@ -0,0 +1,59 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.platform.metadata; + +import static org.junit.Assert.*; +import org.junit.Test; + +/** + * + * @author Alexey Andreev + */ +public class MetadataGeneratorTest { + @MetadataProvider(TestResourceGenerator.class) + private native TestResource getNull(); + + @Test + public void nullExposed() { + assertNull(getNull()); + } + + @MetadataProvider(TestResourceGenerator.class) + private native TestResource getInt(); + + @Test + public void intExposed() { + assertEquals(23, getInt()); + } + + @MetadataProvider(TestResourceGenerator.class) + private native TestResource getResource(); + + @Test + public void resourceObjectExposed() { + TestResource res = getResource(); + assertEquals(23, res.getA()); + assertFalse(res.getB()); + assertEquals(24, res.getD()); + assertEquals(25, res.getE()); + assertEquals(3.14, res.getF(), 0.001); + assertEquals(2.72, res.getG(), 0.001); + assertEquals(Integer.valueOf(26), res.getH()); + assertNull(res.getI()); + assertEquals(Byte.valueOf((byte)27), res.getJ()); + assertEquals(Short.valueOf((short)28), res.getK()); + } +} diff --git a/teavm-classlib/src/test/java/org/teavm/platform/metadata/TestResource.java b/teavm-classlib/src/test/java/org/teavm/platform/metadata/TestResource.java new file mode 100644 index 000000000..d64800bbd --- /dev/null +++ b/teavm-classlib/src/test/java/org/teavm/platform/metadata/TestResource.java @@ -0,0 +1,99 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.platform.metadata; + +/** + * + * @author Alexey Andreev + */ +@Resource +public interface TestResource { + int getA(); + + void setA(int a); + + boolean getB(); + + void setB(boolean b); + + byte getD(); + + void setD(byte d); + + short getE(); + + void setE(short e); + + float getF(); + + void setF(float f); + + double getG(); + + void setG(double g); + + Integer getH(); + + void setH(Integer a); + + Boolean getI(); + + void setI(Boolean i); + + Byte getJ(); + + void setJ(Byte d); + + Short getK(); + + void setK(Short k); + + Float getL(); + + void setL(Float l); + + Double getM(); + + void setM(Double g); + + String getFoo(); + + void setFoo(String foo); + + ResourceArray getArrayA(); + + void setArrayA(ResourceArray arrayA); + + ResourceArray getArrayB(); + + void setArrayB(ResourceArray arrayB); + + ResourceArray> getArrayC(); + + void setArrayC(ResourceArray> arrayC); + + ResourceMap getMapA(); + + void setMapA(ResourceMap mapA); + + ResourceMap getMapB(); + + void setMapB(ResourceMap mapB); + + ResourceMap> getMapC(); + + void setMapC(ResourceMap> mapC); +} diff --git a/teavm-classlib/src/test/java/org/teavm/platform/metadata/TestResourceGenerator.java b/teavm-classlib/src/test/java/org/teavm/platform/metadata/TestResourceGenerator.java new file mode 100644 index 000000000..6b787b37c --- /dev/null +++ b/teavm-classlib/src/test/java/org/teavm/platform/metadata/TestResourceGenerator.java @@ -0,0 +1,65 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.platform.metadata; + +import org.teavm.model.MethodReference; + +/** + * + * @author Alexey Andreev + */ +public class TestResourceGenerator implements MetadataGenerator { + @Override + public Object generateMetadata(MetadataGeneratorContext context, MethodReference method) { + switch (method.getName()) { + case "getNull": + return null; + case "getInt": + return 23; + case "getResource": + return getResource(context); + default: + throw new RuntimeException("Unsupported method: " + method); + } + } + + private Object getResource(MetadataGeneratorContext context) { + TestResource resource = context.createResource(TestResource.class); + resource.setA(23); + resource.setB(false); + resource.setD((byte)24); + resource.setE((short)25); + resource.setF(3.14f); + resource.setG(2.72); + resource.setH(26); + resource.setI(null); + resource.setJ((byte)27); + resource.setK((short)28); + resource.setL(100f); + resource.setM(200.0); + + ResourceArray array = context.createResourceArray(); + array.add(2); + array.add(3); + resource.setArrayA(array); + DependentTestResource dep = context.createResource(DependentTestResource.class); + dep.setBar("baz"); + ResourceArray resArray = context.createResourceArray(); + resArray.add(dep); + resource.setArrayB(resArray); + return resource; + } +} diff --git a/teavm-core/src/main/java/org/teavm/javascript/Decompiler.java b/teavm-core/src/main/java/org/teavm/javascript/Decompiler.java index 72f45cefc..35d7f5fdd 100644 --- a/teavm-core/src/main/java/org/teavm/javascript/Decompiler.java +++ b/teavm-core/src/main/java/org/teavm/javascript/Decompiler.java @@ -85,6 +85,7 @@ public class Decompiler { @Override public void run() { Decompiler copy = new Decompiler(classSource, classLoader, executor); copy.generators = generators; + copy.methodsToPass = methodsToPass; result.set(index, copy.decompile(classSource.get(className))); } }); diff --git a/teavm-platform/src/main/java/org/teavm/platform/plugin/MetadataProviderNativeGenerator.java b/teavm-platform/src/main/java/org/teavm/platform/plugin/MetadataProviderNativeGenerator.java index 2d43695e4..62497207d 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/plugin/MetadataProviderNativeGenerator.java +++ b/teavm-platform/src/main/java/org/teavm/platform/plugin/MetadataProviderNativeGenerator.java @@ -74,7 +74,7 @@ public class MetadataProviderNativeGenerator implements Generator { // Generate resource loader Object resource = generator.generateMetadata(metadataContext, methodRef); - writer.append("if (!").appendMethodBody(methodRef).append("$$resource === undefined").append(") {") + writer.append("if (!window.hasOwnProperty(\"").appendMethodBody(methodRef).append("$$resource\")) {") .indent().softNewLine(); writer.appendMethodBody(methodRef).append("$$resource = "); ResourceWriterHelper.write(writer, resource); diff --git a/teavm-platform/src/main/java/org/teavm/platform/plugin/MetadataProviderTransformer.java b/teavm-platform/src/main/java/org/teavm/platform/plugin/MetadataProviderTransformer.java index 703052af3..f8d25aadf 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/plugin/MetadataProviderTransformer.java +++ b/teavm-platform/src/main/java/org/teavm/platform/plugin/MetadataProviderTransformer.java @@ -29,10 +29,11 @@ class MetadataProviderTransformer implements ClassHolderTransformer { for (MethodHolder method : cls.getMethods()) { AnnotationReader providerAnnot = method.getAnnotations().get(MetadataProvider.class.getName()); if (providerAnnot == null) { - return; + continue; } AnnotationHolder genAnnot = new AnnotationHolder(GeneratedBy.class.getName()); - genAnnot.getValues().put("value", new AnnotationValue(ValueType.object(null))); + genAnnot.getValues().put("value", new AnnotationValue(ValueType.object( + MetadataProviderNativeGenerator.class.getName()))); method.getAnnotations().add(genAnnot); } } diff --git a/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceAccessorTransformer.java b/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceAccessorTransformer.java index bb0ae0c26..6a3819013 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceAccessorTransformer.java +++ b/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceAccessorTransformer.java @@ -34,8 +34,8 @@ class ResourceAccessorTransformer implements ClassHolderTransformer { @Override public void transformClass(ClassHolder cls, ClassReaderSource innerSource) { - ResourceAccessorGenerator generator = new ResourceAccessorGenerator(); - if (cls.getName().equals(ResourceAccessor.class)) { + if (cls.getName().equals(ResourceAccessor.class.getName())) { + ResourceAccessorGenerator generator = new ResourceAccessorGenerator(); for (MethodHolder method : cls.getMethods()) { vm.add(method.getReference(), generator); } diff --git a/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceTransformer.java b/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceTransformer.java index ee4d82b4d..cf7f0b4af 100644 --- a/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceTransformer.java +++ b/teavm-platform/src/main/java/org/teavm/platform/plugin/ResourceTransformer.java @@ -211,7 +211,7 @@ class ResourceTransformer implements ClassHolderTransformer { String primitiveCapitalized = primitive.getName(); primitiveCapitalized = Character.toUpperCase(primitiveCapitalized.charAt(0)) + primitiveCapitalized.substring(1); - castInvoke.setMethod(new MethodReference(ResourceAccessor.class, "castTo" + primitiveCapitalized + "Primitive", + castInvoke.setMethod(new MethodReference(ResourceAccessor.class, "castTo" + primitiveCapitalized + "Wrapper", Object.class, wrapper)); castInvoke.getArguments().add(resultVar); castInvoke.setReceiver(insn.getReceiver()); diff --git a/teavm-platform/src/test/java/org/teavm/platform/metadata/TestResource.java b/teavm-platform/src/test/java/org/teavm/platform/metadata/TestResource.java deleted file mode 100644 index d75474deb..000000000 --- a/teavm-platform/src/test/java/org/teavm/platform/metadata/TestResource.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2014 Alexey Andreev. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.teavm.platform.metadata; - -/** - * - * @author Alexey Andreev - */ -@Resource -public interface TestResource { - int getInt(); - - void setInt(int value); - - String getString(); - - void setString(String string); -}