vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 25 Dec 2012 15:31:58 +0100
changeset 383 88ed1f51eb22
parent 372 3485327d3080
child 384 269d99fd6421
permissions -rw-r--r--
One can specify list of browsers to test in via 'vmtest.brwsrs' property
jaroslav@273
     1
/**
jaroslav@273
     2
 * Back 2 Browser Bytecode Translator
jaroslav@273
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@273
     4
 *
jaroslav@273
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@273
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@273
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@273
     8
 *
jaroslav@273
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@273
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@273
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@273
    12
 * GNU General Public License for more details.
jaroslav@273
    13
 *
jaroslav@273
    14
 * You should have received a copy of the GNU General Public License
jaroslav@273
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@273
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@273
    17
 */
jaroslav@372
    18
package org.apidesign.bck2brwsr.vmtest.impl;
jaroslav@273
    19
jaroslav@372
    20
import org.apidesign.bck2brwsr.vmtest.*;
jaroslav@346
    21
import java.io.File;
jaroslav@346
    22
import java.io.FileWriter;
jaroslav@346
    23
import java.io.IOException;
jaroslav@273
    24
import java.lang.reflect.Method;
jaroslav@372
    25
import java.util.ArrayList;
jaroslav@372
    26
import java.util.List;
jaroslav@273
    27
import org.testng.Assert;
jaroslav@273
    28
import org.testng.ITest;
jaroslav@273
    29
import org.testng.annotations.Factory;
jaroslav@273
    30
import org.testng.annotations.Test;
jaroslav@273
    31
jaroslav@273
    32
/** A TestNG {@link Factory} that seeks for {@link Compare} annotations
jaroslav@273
    33
 * in provided class and builds set of tests that compare the computations
jaroslav@273
    34
 * in real as well as JavaScript virtual machines. Use as:<pre>
jaroslav@273
    35
 * {@code @}{@link Factory} public static create() {
jaroslav@346
    36
 *   return @{link VMTest}.{@link #create(YourClass.class);
jaroslav@273
    37
 * }</pre>
jaroslav@273
    38
 *
jaroslav@273
    39
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@273
    40
 */
jaroslav@372
    41
public final class CompareCase implements ITest {
jaroslav@372
    42
    private final Bck2BrwsrCase first, second;
jaroslav@273
    43
    private final Method m;
jaroslav@273
    44
    
jaroslav@372
    45
    private CompareCase(Method m, Bck2BrwsrCase first, Bck2BrwsrCase second) {
jaroslav@273
    46
        this.first = first;
jaroslav@273
    47
        this.second = second;
jaroslav@273
    48
        this.m = m;
jaroslav@273
    49
    }
jaroslav@273
    50
jaroslav@346
    51
    /** Inspects <code>clazz</code> and for each {@lik Compare} method creates
jaroslav@347
    52
     * instances of tests. Each instance runs the test in different virtual
jaroslav@346
    53
     * machine and at the end they compare the results.
jaroslav@346
    54
     * 
jaroslav@346
    55
     * @param clazz the class to inspect
jaroslav@346
    56
     * @return the set of created tests
jaroslav@346
    57
     */
jaroslav@273
    58
    public static Object[] create(Class<?> clazz) {
jaroslav@273
    59
        Method[] arr = clazz.getMethods();
jaroslav@372
    60
        List<Object> ret = new ArrayList<>();
jaroslav@372
    61
        
jaroslav@383
    62
        final LaunchSetup l = LaunchSetup.javaScript();
jaroslav@372
    63
        ret.add(l);
jaroslav@372
    64
        
jaroslav@383
    65
        String[] brwsr;
jaroslav@383
    66
        {
jaroslav@383
    67
            String p = System.getProperty("vmtest.brwsrs");
jaroslav@383
    68
            if (p != null) {
jaroslav@383
    69
                brwsr = p.split(",");
jaroslav@383
    70
            } else {
jaroslav@383
    71
                brwsr = new String[0];
jaroslav@383
    72
            }
jaroslav@383
    73
        }
jaroslav@383
    74
        
jaroslav@273
    75
        for (Method m : arr) {
jaroslav@273
    76
            Compare c = m.getAnnotation(Compare.class);
jaroslav@273
    77
            if (c == null) {
jaroslav@273
    78
                continue;
jaroslav@273
    79
            }
jaroslav@383
    80
            final Bck2BrwsrCase real = new Bck2BrwsrCase(m, "Java", null);
jaroslav@383
    81
            final Bck2BrwsrCase js = new Bck2BrwsrCase(m, "JavaScript", l);
jaroslav@372
    82
            ret.add(real);
jaroslav@372
    83
            ret.add(js);
jaroslav@372
    84
            ret.add(new CompareCase(m, real, js));
jaroslav@383
    85
jaroslav@383
    86
            for (String b : brwsr) {
jaroslav@383
    87
                final LaunchSetup s = LaunchSetup.brwsr(b);
jaroslav@383
    88
                ret.add(s);
jaroslav@383
    89
                final Bck2BrwsrCase cse = new Bck2BrwsrCase(m, b, s);
jaroslav@383
    90
                ret.add(cse);
jaroslav@383
    91
                ret.add(new CompareCase(m, real, cse));
jaroslav@383
    92
            }
jaroslav@273
    93
        }
jaroslav@372
    94
        return ret.toArray();
jaroslav@273
    95
    }
jaroslav@346
    96
jaroslav@346
    97
    /** Test that compares the previous results.
jaroslav@346
    98
     * @throws Throwable 
jaroslav@346
    99
     */
jaroslav@280
   100
    @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
jaroslav@280
   101
        Object v1 = first.value;
jaroslav@280
   102
        Object v2 = second.value;
jaroslav@356
   103
        if (v1 != null) {
jaroslav@356
   104
            v1 = v1.toString();
jaroslav@356
   105
        } else {
jaroslav@356
   106
            v1 = "null";
jaroslav@280
   107
        }
jaroslav@296
   108
        Assert.assertEquals(v2, v1, "Comparing results");
jaroslav@280
   109
    }
jaroslav@280
   110
    
jaroslav@346
   111
    /** Test name.
jaroslav@346
   112
     * @return name of the tested method followed by a suffix
jaroslav@346
   113
     */
jaroslav@280
   114
    @Override
jaroslav@280
   115
    public String getTestName() {
jaroslav@349
   116
        return m.getName() + "[Compare " + second.typeName() + "]";
jaroslav@280
   117
    }
jaroslav@346
   118
    
jaroslav@346
   119
    static StringBuilder dumpJS(CharSequence sb) throws IOException {
jaroslav@346
   120
        File f = File.createTempFile("execution", ".js");
jaroslav@347
   121
        try (FileWriter w = new FileWriter(f)) {
jaroslav@347
   122
            w.append(sb);
jaroslav@347
   123
        }
jaroslav@346
   124
        return new StringBuilder(f.getPath());
jaroslav@346
   125
    }
jaroslav@273
   126
}