jaroslav@273: /** jaroslav@273: * Back 2 Browser Bytecode Translator jaroslav@273: * Copyright (C) 2012 Jaroslav Tulach jaroslav@273: * jaroslav@273: * This program is free software: you can redistribute it and/or modify jaroslav@273: * it under the terms of the GNU General Public License as published by jaroslav@273: * the Free Software Foundation, version 2 of the License. jaroslav@273: * jaroslav@273: * This program is distributed in the hope that it will be useful, jaroslav@273: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@273: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@273: * GNU General Public License for more details. jaroslav@273: * jaroslav@273: * You should have received a copy of the GNU General Public License jaroslav@273: * along with this program. Look for COPYING file in the top folder. jaroslav@273: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@273: */ jaroslav@372: package org.apidesign.bck2brwsr.vmtest.impl; jaroslav@273: jaroslav@372: import org.apidesign.bck2brwsr.vmtest.*; jaroslav@346: import java.io.File; jaroslav@346: import java.io.FileWriter; jaroslav@346: import java.io.IOException; jaroslav@273: import java.lang.reflect.Method; jaroslav@372: import java.util.ArrayList; jaroslav@372: import java.util.List; jaroslav@273: import org.testng.Assert; jaroslav@273: import org.testng.ITest; jaroslav@273: import org.testng.annotations.Factory; jaroslav@273: import org.testng.annotations.Test; jaroslav@273: jaroslav@273: /** A TestNG {@link Factory} that seeks for {@link Compare} annotations jaroslav@273: * in provided class and builds set of tests that compare the computations jaroslav@273: * in real as well as JavaScript virtual machines. Use as:
jaroslav@273:  * {@code @}{@link Factory} public static create() {
jaroslav@346:  *   return @{link VMTest}.{@link #create(YourClass.class);
jaroslav@273:  * }
jaroslav@273: * jaroslav@273: * @author Jaroslav Tulach jaroslav@273: */ jaroslav@372: public final class CompareCase implements ITest { jaroslav@372: private final Bck2BrwsrCase first, second; jaroslav@273: private final Method m; jaroslav@273: jaroslav@372: private CompareCase(Method m, Bck2BrwsrCase first, Bck2BrwsrCase second) { jaroslav@273: this.first = first; jaroslav@273: this.second = second; jaroslav@273: this.m = m; jaroslav@273: } jaroslav@273: jaroslav@346: /** Inspects clazz and for each {@lik Compare} method creates jaroslav@347: * instances of tests. Each instance runs the test in different virtual jaroslav@346: * machine and at the end they compare the results. jaroslav@346: * jaroslav@346: * @param clazz the class to inspect jaroslav@346: * @return the set of created tests jaroslav@346: */ jaroslav@273: public static Object[] create(Class clazz) { jaroslav@273: Method[] arr = clazz.getMethods(); jaroslav@372: List ret = new ArrayList<>(); jaroslav@372: jaroslav@372: final Launchers l = Launchers.INSTANCE; jaroslav@372: jaroslav@372: ret.add(l); jaroslav@372: jaroslav@273: for (Method m : arr) { jaroslav@273: Compare c = m.getAnnotation(Compare.class); jaroslav@273: if (c == null) { jaroslav@273: continue; jaroslav@273: } jaroslav@372: final Bck2BrwsrCase real = new Bck2BrwsrCase(m, 0, null); jaroslav@372: final Bck2BrwsrCase js = new Bck2BrwsrCase(m, 1, l); jaroslav@372: final Bck2BrwsrCase brwsr = new Bck2BrwsrCase(m, 2, l); jaroslav@372: jaroslav@372: ret.add(real); jaroslav@372: ret.add(js); jaroslav@372: ret.add(brwsr); jaroslav@372: jaroslav@372: ret.add(new CompareCase(m, real, js)); jaroslav@372: ret.add(new CompareCase(m, real, brwsr)); jaroslav@273: } jaroslav@372: return ret.toArray(); jaroslav@273: } jaroslav@346: jaroslav@346: /** Test that compares the previous results. jaroslav@346: * @throws Throwable jaroslav@346: */ jaroslav@280: @Test(dependsOnGroups = "run") public void compareResults() throws Throwable { jaroslav@280: Object v1 = first.value; jaroslav@280: Object v2 = second.value; jaroslav@356: if (v1 != null) { jaroslav@356: v1 = v1.toString(); jaroslav@356: } else { jaroslav@356: v1 = "null"; jaroslav@280: } jaroslav@296: Assert.assertEquals(v2, v1, "Comparing results"); jaroslav@280: } jaroslav@280: jaroslav@346: /** Test name. jaroslav@346: * @return name of the tested method followed by a suffix jaroslav@346: */ jaroslav@280: @Override jaroslav@280: public String getTestName() { jaroslav@349: return m.getName() + "[Compare " + second.typeName() + "]"; jaroslav@280: } jaroslav@346: jaroslav@346: static StringBuilder dumpJS(CharSequence sb) throws IOException { jaroslav@346: File f = File.createTempFile("execution", ".js"); jaroslav@347: try (FileWriter w = new FileWriter(f)) { jaroslav@347: w.append(sb); jaroslav@347: } jaroslav@346: return new StringBuilder(f.getPath()); jaroslav@346: } jaroslav@273: }