2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 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 org.apidesign.bck2brwsr.vmtest.*;
21 import java.lang.reflect.Method;
22 import java.util.ArrayList;
23 import java.util.List;
24 import org.apidesign.bck2brwsr.launcher.Launcher;
25 import org.testng.Assert;
26 import org.testng.ITest;
27 import org.testng.annotations.Factory;
28 import org.testng.annotations.Test;
30 /** A TestNG {@link Factory} that seeks for {@link Compare} annotations
31 * in provided class and builds set of tests that compare the computations
32 * in real as well as JavaScript virtual machines. Use as:<pre>
33 * {@code @}{@link Factory} public static create() {
34 * return @{link VMTest}.{@link #create(YourClass.class);
37 * @author Jaroslav Tulach <jtulach@netbeans.org>
39 public final class CompareCase implements ITest {
40 private final Bck2BrwsrCase first, second;
41 private final Method m;
43 private CompareCase(Method m, Bck2BrwsrCase first, Bck2BrwsrCase second) {
49 /** Inspects <code>clazz</code> and for each {@lik Compare} method creates
50 * instances of tests. Each instance runs the test in different virtual
51 * machine and at the end they compare the results.
53 * @param clazz the class to inspect
54 * @return the set of created tests
56 public static Object[] create(Class... classes) {
57 List<Object> ret = new ArrayList<>();
59 final LaunchSetup l = LaunchSetup.INSTANCE;
64 String p = System.getProperty("vmtest.brwsrs");
68 brwsr = new String[0];
72 for (Class clazz : classes) {
73 Method[] arr = clazz.getMethods();
74 for (Method m : arr) {
75 registerCompareCases(m, l, ret, brwsr);
76 registerBrwsrCases(m, l, ret, brwsr);
82 /** Test that compares the previous results.
85 @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
86 Object v1 = first.value;
87 Object v2 = second.value;
88 if (v1 instanceof Integer || v1 instanceof Long || v1 instanceof Byte || v1 instanceof Short) {
90 v1 = Long.parseLong(v1.toString());
91 } catch (NumberFormatException nfe) {
92 v1 = "Can't parse " + v1.toString();
95 v2 = Long.parseLong(v2.toString());
96 } catch (NumberFormatException nfe) {
97 v2 = "Can't parse " + v2.toString();
99 } else if (v1 instanceof Number) {
101 v1 = Double.parseDouble(v1.toString());
102 } catch (NumberFormatException nfe) {
103 v1 = "Can't parse " + v1.toString();
106 v2 = Double.parseDouble(v2.toString());
107 } catch (NumberFormatException nfe) {
108 v2 = "Can't parse " + v2.toString();
118 Assert.assertEquals(v2, v1, "Comparing results");
119 } catch (AssertionError e) {
120 StringBuilder sb = new StringBuilder();
121 sb.append(e.getMessage());
122 Bck2BrwsrCase.dumpJS(sb, second);
123 throw new AssertionError(sb.toString());
128 * @return name of the tested method followed by a suffix
131 public String getTestName() {
132 return m.getName() + "[Compare " + second.typeName() + "]";
135 private static void registerCompareCases(Method m, final LaunchSetup l, List<Object> ret, String[] brwsr) {
136 Compare c = m.getAnnotation(Compare.class);
140 final Bck2BrwsrCase real = new Bck2BrwsrCase(m, "Java", null, false, null, null);
143 final Bck2BrwsrCase js = new Bck2BrwsrCase(m, "JavaScript", l.javaScript(), false, null, null);
145 ret.add(new CompareCase(m, real, js));
147 for (String b : brwsr) {
148 final Launcher s = l.brwsr(b);
150 final Bck2BrwsrCase cse = new Bck2BrwsrCase(m, b, s, false, null, null);
152 ret.add(new CompareCase(m, real, cse));
155 private static void registerBrwsrCases(Method m, final LaunchSetup l, List<Object> ret, String[] brwsr) {
156 BrwsrTest c = m.getAnnotation(BrwsrTest.class);
160 HtmlFragment f = m.getAnnotation(HtmlFragment.class);
162 f = m.getDeclaringClass().getAnnotation(HtmlFragment.class);
164 Http.Resource[] r = {};
165 Http h = m.getAnnotation(Http.class);
167 h = m.getDeclaringClass().getAnnotation(Http.class);
174 if (brwsr.length == 0) {
175 final Launcher s = l.brwsr(null);
177 ret.add(new Bck2BrwsrCase(m, "Brwsr", s, true, f, r));
179 for (String b : brwsr) {
180 final Launcher s = l.brwsr(b);
182 ret.add(new Bck2BrwsrCase(m, b, s, true, f, r));