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.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;
46 private CompareCase(Method m, Bck2BrwsrCase first, Bck2BrwsrCase second) {
52 /** Inspects <code>clazz</code> and for each {@lik Compare} method creates
53 * instances of tests. Each instance runs the test in different virtual
54 * machine and at the end they compare the results.
56 * @param clazz the class to inspect
57 * @return the set of created tests
59 public static Object[] create(Class<?> clazz) {
60 Method[] arr = clazz.getMethods();
61 List<Object> ret = new ArrayList<>();
63 final LaunchSetup l = LaunchSetup.INSTANCE;
68 String p = System.getProperty("vmtest.brwsrs");
72 brwsr = new String[0];
76 for (Method m : arr) {
77 Compare c = m.getAnnotation(Compare.class);
81 final Bck2BrwsrCase real = new Bck2BrwsrCase(m, "Java", null);
82 final Bck2BrwsrCase js = new Bck2BrwsrCase(m, "JavaScript", l.javaScript());
85 ret.add(new CompareCase(m, real, js));
87 for (String b : brwsr) {
88 final Launcher s = l.brwsr(b);
90 final Bck2BrwsrCase cse = new Bck2BrwsrCase(m, b, s);
92 ret.add(new CompareCase(m, real, cse));
98 /** Test that compares the previous results.
101 @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
102 Object v1 = first.value;
103 Object v2 = second.value;
109 Assert.assertEquals(v2, v1, "Comparing results");
113 * @return name of the tested method followed by a suffix
116 public String getTestName() {
117 return m.getName() + "[Compare " + second.typeName() + "]";
120 static StringBuilder dumpJS(CharSequence sb) throws IOException {
121 File f = File.createTempFile("execution", ".js");
122 try (FileWriter w = new FileWriter(f)) {
125 return new StringBuilder(f.getPath());