# HG changeset patch # User Jaroslav Tulach # Date 1358852365 -3600 # Node ID 5df0a239ebebbfcd895eb9e5502562be8201cd7a # Parent 70c062dbd783320b4ec297ebf859adc8b08ca8de @BrwsrTest to execute tests directly in browser diff -r 70c062dbd783 -r 5df0a239ebeb launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Tue Jan 22 11:14:00 2013 +0100 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Tue Jan 22 11:59:25 2013 +0100 @@ -243,7 +243,8 @@ LOG.log(Level.INFO, "Desktop.browse successfully finished"); return null; } catch (UnsupportedOperationException ex) { - LOG.log(Level.INFO, "Desktop.browse not supported", ex); + LOG.log(Level.INFO, "Desktop.browse not supported: {0}", ex.getMessage()); + LOG.log(Level.FINE, null, ex); } } { diff -r 70c062dbd783 -r 5df0a239ebeb vmtest/pom.xml --- a/vmtest/pom.xml Tue Jan 22 11:14:00 2013 +0100 +++ b/vmtest/pom.xml Tue Jan 22 11:59:25 2013 +0100 @@ -44,13 +44,14 @@ ${project.groupId} vm4brwsr - 0.3-SNAPSHOT + ${project.version} jar ${project.groupId} emul - 0.3-SNAPSHOT + ${project.version} + test ${project.groupId} diff -r 70c062dbd783 -r 5df0a239ebeb vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/BrwsrTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/BrwsrTest.java Tue Jan 22 11:59:25 2013 +0100 @@ -0,0 +1,38 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.vmtest; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** Annotation to indicate that given method should be executed + * in a browser environment. Has to be used in conjunction with {@link VMTest#create(java.lang.Class)} + * factory method. + *

+ * The browser to is by default executed via {@link java.awt.Desktop#browse(java.net.URI)}, + * but one can change that by specifying -Dvmtest.brwsrs=firefox,google-chrome + * property. + * + * @author Jaroslav Tulach + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface BrwsrTest { +} diff -r 70c062dbd783 -r 5df0a239ebeb vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/Compare.java --- a/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/Compare.java Tue Jan 22 11:14:00 2013 +0100 +++ b/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/Compare.java Tue Jan 22 11:59:25 2013 +0100 @@ -32,5 +32,4 @@ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Compare { - } diff -r 70c062dbd783 -r 5df0a239ebeb vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java --- a/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java Tue Jan 22 11:14:00 2013 +0100 +++ b/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java Tue Jan 22 11:59:25 2013 +0100 @@ -20,6 +20,7 @@ import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Map; @@ -37,21 +38,56 @@ private final Method m; private final Launcher l; private final String type; + private final boolean fail; Object value; private static final Map compiled = new WeakHashMap<>(); private Object inst; - Bck2BrwsrCase(Method m, String type, Launcher l) { + Bck2BrwsrCase(Method m, String type, Launcher l, boolean fail) { this.l = l; this.m = m; this.type = type; + this.fail = fail; } @Test(groups = "run") public void executeCode() throws Throwable { if (l != null) { MethodInvocation c = l.invokeMethod(m.getDeclaringClass(), m.getName()); - value = c.toString(); + String res = c.toString(); + value = res; + if (fail) { + int idx = res.indexOf(':'); + if (idx >= 0) { + Class thrwbl = null; + try { + Class exCls = Class.forName(res.substring(0, idx)); + if (Throwable.class.isAssignableFrom(exCls)) { + thrwbl = exCls.asSubclass(Throwable.class); + } + } catch (Exception ex) { + // ignore + } + if (thrwbl != null) { + Throwable t = null; + try { + for (Constructor cnstr : thrwbl.getConstructors()) { + if (cnstr.getParameterTypes().length == 1 && cnstr.getParameterTypes()[0].isAssignableFrom(String.class)) { + t = (Throwable) cnstr.newInstance(res.substring(idx + 1)); + break; + } + } + } catch (Throwable ex) { + t = thrwbl.newInstance().initCause(ex); + } + if (t == null) { + t = thrwbl.newInstance().initCause(new Exception(res.substring(idx))); + } + throw t; + } + throw new AssertionError(res); + } + } } else { try { value = m.invoke(m.getDeclaringClass().newInstance()); diff -r 70c062dbd783 -r 5df0a239ebeb vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java --- a/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java Tue Jan 22 11:14:00 2013 +0100 +++ b/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java Tue Jan 22 11:59:25 2013 +0100 @@ -74,23 +74,8 @@ } for (Method m : arr) { - Compare c = m.getAnnotation(Compare.class); - if (c == null) { - continue; - } - final Bck2BrwsrCase real = new Bck2BrwsrCase(m, "Java", null); - final Bck2BrwsrCase js = new Bck2BrwsrCase(m, "JavaScript", l.javaScript()); - ret.add(real); - ret.add(js); - ret.add(new CompareCase(m, real, js)); - - for (String b : brwsr) { - final Launcher s = l.brwsr(b); - ret.add(s); - final Bck2BrwsrCase cse = new Bck2BrwsrCase(m, b, s); - ret.add(cse); - ret.add(new CompareCase(m, real, cse)); - } + registerCompareCases(m, l, ret, brwsr); + registerBrwsrCases(m, l, ret, brwsr); } return ret.toArray(); } @@ -123,4 +108,40 @@ public String getTestName() { return m.getName() + "[Compare " + second.typeName() + "]"; } + + private static void registerCompareCases(Method m, final LaunchSetup l, List ret, String[] brwsr) { + Compare c = m.getAnnotation(Compare.class); + if (c == null) { + return; + } + final Bck2BrwsrCase real = new Bck2BrwsrCase(m, "Java", null, false); + final Bck2BrwsrCase js = new Bck2BrwsrCase(m, "JavaScript", l.javaScript(), false); + ret.add(real); + ret.add(js); + ret.add(new CompareCase(m, real, js)); + for (String b : brwsr) { + final Launcher s = l.brwsr(b); + ret.add(s); + final Bck2BrwsrCase cse = new Bck2BrwsrCase(m, b, s, false); + ret.add(cse); + ret.add(new CompareCase(m, real, cse)); + } + } + private static void registerBrwsrCases(Method m, final LaunchSetup l, List ret, String[] brwsr) { + BrwsrTest c = m.getAnnotation(BrwsrTest.class); + if (c == null) { + return; + } + if (brwsr.length == 0) { + final Launcher s = l.brwsr(null); + ret.add(s); + ret.add(new Bck2BrwsrCase(m, "Brwsr", s, true)); + } else { + for (String b : brwsr) { + final Launcher s = l.brwsr(b); + ret.add(s); + ret.add(new Bck2BrwsrCase(m, b, s, true)); + } + } + } } diff -r 70c062dbd783 -r 5df0a239ebeb vmtest/src/test/java/org/apidesign/bck2brwsr/tck/BrwsrCheckTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/BrwsrCheckTest.java Tue Jan 22 11:59:25 2013 +0100 @@ -0,0 +1,43 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.tck; + +import org.apidesign.bck2brwsr.core.JavaScriptBody; +import org.apidesign.bck2brwsr.vmtest.BrwsrTest; +import org.apidesign.bck2brwsr.vmtest.VMTest; +import org.testng.annotations.Factory; + +/** + * + * @author Jaroslav Tulach + */ +public class BrwsrCheckTest { + + @BrwsrTest public void assertWindowObjectIsDefined() { + assert window() != null : "No window object found!"; + } + + @Factory + public static Object[] create() { + return VMTest.create(BrwsrCheckTest.class); + } + + @JavaScriptBody(args = {}, body = "return window;") + private static native Object window(); + +}