vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java
branchlauncher
changeset 372 3485327d3080
parent 370 ed48023d1d85
child 383 88ed1f51eb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java	Sun Dec 23 23:30:06 2012 +0100
     1.3 @@ -0,0 +1,114 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.bck2brwsr.vmtest.impl;
    1.22 +
    1.23 +import org.apidesign.bck2brwsr.vmtest.*;
    1.24 +import java.io.File;
    1.25 +import java.io.FileWriter;
    1.26 +import java.io.IOException;
    1.27 +import java.lang.reflect.Method;
    1.28 +import java.util.ArrayList;
    1.29 +import java.util.List;
    1.30 +import org.testng.Assert;
    1.31 +import org.testng.ITest;
    1.32 +import org.testng.annotations.Factory;
    1.33 +import org.testng.annotations.Test;
    1.34 +
    1.35 +/** A TestNG {@link Factory} that seeks for {@link Compare} annotations
    1.36 + * in provided class and builds set of tests that compare the computations
    1.37 + * in real as well as JavaScript virtual machines. Use as:<pre>
    1.38 + * {@code @}{@link Factory} public static create() {
    1.39 + *   return @{link VMTest}.{@link #create(YourClass.class);
    1.40 + * }</pre>
    1.41 + *
    1.42 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.43 + */
    1.44 +public final class CompareCase implements ITest {
    1.45 +    private final Bck2BrwsrCase first, second;
    1.46 +    private final Method m;
    1.47 +    
    1.48 +    private CompareCase(Method m, Bck2BrwsrCase first, Bck2BrwsrCase second) {
    1.49 +        this.first = first;
    1.50 +        this.second = second;
    1.51 +        this.m = m;
    1.52 +    }
    1.53 +
    1.54 +    /** Inspects <code>clazz</code> and for each {@lik Compare} method creates
    1.55 +     * instances of tests. Each instance runs the test in different virtual
    1.56 +     * machine and at the end they compare the results.
    1.57 +     * 
    1.58 +     * @param clazz the class to inspect
    1.59 +     * @return the set of created tests
    1.60 +     */
    1.61 +    public static Object[] create(Class<?> clazz) {
    1.62 +        Method[] arr = clazz.getMethods();
    1.63 +        List<Object> ret = new ArrayList<>();
    1.64 +        
    1.65 +        final Launchers l = Launchers.INSTANCE;
    1.66 +    
    1.67 +        ret.add(l);
    1.68 +        
    1.69 +        for (Method m : arr) {
    1.70 +            Compare c = m.getAnnotation(Compare.class);
    1.71 +            if (c == null) {
    1.72 +                continue;
    1.73 +            }
    1.74 +            final Bck2BrwsrCase real = new Bck2BrwsrCase(m, 0, null);
    1.75 +            final Bck2BrwsrCase js = new Bck2BrwsrCase(m, 1, l);
    1.76 +            final Bck2BrwsrCase brwsr = new Bck2BrwsrCase(m, 2, l);
    1.77 +            
    1.78 +            ret.add(real);
    1.79 +            ret.add(js);
    1.80 +            ret.add(brwsr);
    1.81 +            
    1.82 +            ret.add(new CompareCase(m, real, js));
    1.83 +            ret.add(new CompareCase(m, real, brwsr));
    1.84 +        }
    1.85 +        return ret.toArray();
    1.86 +    }
    1.87 +
    1.88 +    /** Test that compares the previous results.
    1.89 +     * @throws Throwable 
    1.90 +     */
    1.91 +    @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
    1.92 +        Object v1 = first.value;
    1.93 +        Object v2 = second.value;
    1.94 +        if (v1 != null) {
    1.95 +            v1 = v1.toString();
    1.96 +        } else {
    1.97 +            v1 = "null";
    1.98 +        }
    1.99 +        Assert.assertEquals(v2, v1, "Comparing results");
   1.100 +    }
   1.101 +    
   1.102 +    /** Test name.
   1.103 +     * @return name of the tested method followed by a suffix
   1.104 +     */
   1.105 +    @Override
   1.106 +    public String getTestName() {
   1.107 +        return m.getName() + "[Compare " + second.typeName() + "]";
   1.108 +    }
   1.109 +    
   1.110 +    static StringBuilder dumpJS(CharSequence sb) throws IOException {
   1.111 +        File f = File.createTempFile("execution", ".js");
   1.112 +        try (FileWriter w = new FileWriter(f)) {
   1.113 +            w.append(sb);
   1.114 +        }
   1.115 +        return new StringBuilder(f.getPath());
   1.116 +    }
   1.117 +}