2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.vmtest.impl;
20 import java.lang.annotation.Annotation;
21 import org.apidesign.bck2brwsr.vmtest.*;
22 import java.lang.reflect.Method;
23 import java.util.ArrayList;
24 import java.util.HashSet;
25 import java.util.List;
27 import org.apidesign.bck2brwsr.launcher.Launcher;
28 import org.testng.Assert;
29 import org.testng.ITest;
30 import org.testng.annotations.Factory;
31 import org.testng.annotations.Test;
33 /** A TestNG {@link Factory} that seeks for {@link Compare} annotations
34 * in provided class and builds set of tests that compare the computations
35 * in real as well as JavaScript virtual machines. Use as:<pre>
36 * {@code @}{@link Factory} public static create() {
37 * return @{link VMTest}.{@link #create(YourClass.class);
40 * @author Jaroslav Tulach <jtulach@netbeans.org>
42 public final class CompareCase implements ITest {
43 private final Bck2BrwsrCase first, second;
44 private final Method m;
45 private final double slowdown;
47 private CompareCase(Method m, Bck2BrwsrCase first, Bck2BrwsrCase second, double slowdown) {
51 this.slowdown = slowdown;
54 /** Inspects <code>clazz</code> and for each {@lik Compare} method creates
55 * instances of tests. Each instance runs the test in different virtual
56 * machine and at the end they compare the results.
58 * @param clazz the class to inspect
59 * @return the set of created tests
61 public static Object[] create(String[] brwsr, Class[] classes, Class<? extends Annotation> brwsrTest) {
62 List<Object> ret = new ArrayList<>();
64 final LaunchSetup l = LaunchSetup.INSTANCE;
68 String p = System.getProperty("vmtest.brwsrs");
74 for (Class clazz : classes) {
75 Method[] arr = clazz.getMethods();
76 for (Method m : arr) {
77 registerCompareCases(m, l, ret, brwsr);
78 registerBrwsrCases(brwsrTest, m, l, ret, brwsr);
84 /** Test that compares the previous results.
87 @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
88 Object v1 = first.value;
89 Object v2 = second.value;
90 if (v1 instanceof Integer || v1 instanceof Long || v1 instanceof Byte || v1 instanceof Short) {
92 v1 = Long.parseLong(v1.toString());
93 } catch (NumberFormatException nfe) {
94 v1 = "Can't parse " + v1.toString();
97 v2 = Long.parseLong(v2.toString());
98 } catch (NullPointerException | NumberFormatException nfe) {
99 v2 = "Can't parse " + v2.toString();
101 } else if (v1 instanceof Number) {
103 v1 = Double.parseDouble(v1.toString());
104 } catch (NullPointerException | NumberFormatException nfe) {
105 v1 = "Can't parse " + v1.toString();
108 v2 = Double.parseDouble(v2.toString());
109 } catch (NullPointerException | NumberFormatException nfe) {
110 v2 = "Can't parse " + v2.toString();
120 Assert.assertEquals(v2, v1, "Comparing results");
121 } catch (AssertionError e) {
122 StringBuilder sb = new StringBuilder();
123 sb.append(e.getMessage());
124 Bck2BrwsrCase.dumpJS(sb, second);
125 throw new AssertionError(sb.toString());
127 if (slowdown > 0.0) {
130 if (first.time >= second.time) {
137 if (slow.time * slowdown < fast.time) {
138 Assert.fail("Too slow " + slow.getTestName() + " took " + slow.time + " ms vs. " + fast.time + " ms of " + fast.getTestName());
144 * @return name of the tested method followed by a suffix
147 public String getTestName() {
148 return m.getName() + "[Compare " + second.typeName() + "]";
151 private static void registerCompareCases(Method m, final LaunchSetup l, List<Object> ret, String[] brwsr) {
152 Compare c = m.getAnnotation(Compare.class);
156 String slowdownOverride = System.getProperty("vmtest.slowdown");
157 final Bck2BrwsrCase real = new Bck2BrwsrCase(m, "Java", null, false, null, null);
159 double slowdown = c.slowdown();
160 if (slowdown > 0.0 && slowdownOverride != null) {
161 slowdown = Double.parseDouble(slowdownOverride);
163 Set<Launcher> unique = new HashSet<>();
164 for (String b : brwsr) {
165 final Launcher s = l.brwsr(b);
168 final Bck2BrwsrCase cse = new Bck2BrwsrCase(m, b, s, false, null, null);
170 ret.add(new CompareCase(m, real, cse, slowdown));
173 final Launcher jsLauncher = l.javaScript();
174 if (unique.add(jsLauncher)) {
175 final Bck2BrwsrCase js = new Bck2BrwsrCase(m, "JavaScript", jsLauncher, false, null, null);
177 ret.add(new CompareCase(m, real, js, slowdown));
181 private static void registerBrwsrCases(Class<? extends Annotation> brwsrTest, Method m, final LaunchSetup l, List<Object> ret, String[] brwsr) {
182 Object c = m.getAnnotation(brwsrTest);
186 HtmlFragment f = m.getAnnotation(HtmlFragment.class);
188 f = m.getDeclaringClass().getAnnotation(HtmlFragment.class);
190 Http.Resource[] r = {};
191 Http h = m.getAnnotation(Http.class);
193 h = m.getDeclaringClass().getAnnotation(Http.class);
200 if (brwsr.length == 0) {
201 final Launcher s = l.brwsr(null);
203 ret.add(new Bck2BrwsrCase(m, "Brwsr", s, true, f, r));
205 for (String b : brwsr) {
206 final Launcher s = l.brwsr(b);
208 ret.add(new Bck2BrwsrCase(m, b, s, true, f, r));