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 LaunchSetup l = LaunchSetup.javaScript();
67 String p = System.getProperty("vmtest.brwsrs");
71 brwsr = new String[0];
75 for (Method m : arr) {
76 Compare c = m.getAnnotation(Compare.class);
80 final Bck2BrwsrCase real = new Bck2BrwsrCase(m, "Java", null);
81 final Bck2BrwsrCase js = new Bck2BrwsrCase(m, "JavaScript", l);
84 ret.add(new CompareCase(m, real, js));
86 for (String b : brwsr) {
87 final LaunchSetup s = LaunchSetup.brwsr(b);
89 final Bck2BrwsrCase cse = new Bck2BrwsrCase(m, b, s);
91 ret.add(new CompareCase(m, real, cse));
97 /** Test that compares the previous results.
100 @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
101 Object v1 = first.value;
102 Object v2 = second.value;
108 Assert.assertEquals(v2, v1, "Comparing results");
112 * @return name of the tested method followed by a suffix
115 public String getTestName() {
116 return m.getName() + "[Compare " + second.typeName() + "]";
119 static StringBuilder dumpJS(CharSequence sb) throws IOException {
120 File f = File.createTempFile("execution", ".js");
121 try (FileWriter w = new FileWriter(f)) {
124 return new StringBuilder(f.getPath());