Execution time reported by TestNG reflect the time it took to run the query inside of a browser
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.*;
22 import java.io.FileWriter;
23 import java.io.IOException;
24 import java.lang.reflect.Method;
25 import java.util.ArrayList;
26 import java.util.List;
27 import org.testng.Assert;
28 import org.testng.ITest;
29 import org.testng.annotations.Factory;
30 import org.testng.annotations.Test;
32 /** A TestNG {@link Factory} that seeks for {@link Compare} annotations
33 * in provided class and builds set of tests that compare the computations
34 * in real as well as JavaScript virtual machines. Use as:<pre>
35 * {@code @}{@link Factory} public static create() {
36 * return @{link VMTest}.{@link #create(YourClass.class);
39 * @author Jaroslav Tulach <jtulach@netbeans.org>
41 public final class CompareCase implements ITest {
42 private final Bck2BrwsrCase first, second;
43 private final Method m;
45 private CompareCase(Method m, Bck2BrwsrCase first, Bck2BrwsrCase second) {
51 /** Inspects <code>clazz</code> and for each {@lik Compare} method creates
52 * instances of tests. Each instance runs the test in different virtual
53 * machine and at the end they compare the results.
55 * @param clazz the class to inspect
56 * @return the set of created tests
58 public static Object[] create(Class<?> clazz) {
59 Method[] arr = clazz.getMethods();
60 List<Object> ret = new ArrayList<>();
62 final Launchers l = Launchers.INSTANCE;
66 for (Method m : arr) {
67 Compare c = m.getAnnotation(Compare.class);
71 final Bck2BrwsrCase real = new Bck2BrwsrCase(m, 0, null);
72 final Bck2BrwsrCase js = new Bck2BrwsrCase(m, 1, l);
73 final Bck2BrwsrCase brwsr = new Bck2BrwsrCase(m, 2, l);
79 ret.add(new CompareCase(m, real, js));
80 ret.add(new CompareCase(m, real, brwsr));
85 /** Test that compares the previous results.
88 @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
89 Object v1 = first.value;
90 Object v2 = second.value;
96 Assert.assertEquals(v2, v1, "Comparing results");
100 * @return name of the tested method followed by a suffix
103 public String getTestName() {
104 return m.getName() + "[Compare " + second.typeName() + "]";
107 static StringBuilder dumpJS(CharSequence sb) throws IOException {
108 File f = File.createTempFile("execution", ".js");
109 try (FileWriter w = new FileWriter(f)) {
112 return new StringBuilder(f.getPath());