vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 29 Dec 2012 19:42:47 +0100
changeset 393 bedc3b93a040
parent 384 269d99fd6421
child 518 5df0a239ebeb
permissions -rw-r--r--
Dump some information about executed code
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@384
    27
import org.apidesign.bck2brwsr.launcher.Launcher;
jaroslav@273
    28
import org.testng.Assert;
jaroslav@273
    29
import org.testng.ITest;
jaroslav@273
    30
import org.testng.annotations.Factory;
jaroslav@273
    31
import org.testng.annotations.Test;
jaroslav@273
    32
jaroslav@273
    33
/** A TestNG {@link Factory} that seeks for {@link Compare} annotations
jaroslav@273
    34
 * in provided class and builds set of tests that compare the computations
jaroslav@273
    35
 * in real as well as JavaScript virtual machines. Use as:<pre>
jaroslav@273
    36
 * {@code @}{@link Factory} public static create() {
jaroslav@346
    37
 *   return @{link VMTest}.{@link #create(YourClass.class);
jaroslav@273
    38
 * }</pre>
jaroslav@273
    39
 *
jaroslav@273
    40
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@273
    41
 */
jaroslav@372
    42
public final class CompareCase implements ITest {
jaroslav@372
    43
    private final Bck2BrwsrCase first, second;
jaroslav@273
    44
    private final Method m;
jaroslav@273
    45
    
jaroslav@372
    46
    private CompareCase(Method m, Bck2BrwsrCase first, Bck2BrwsrCase second) {
jaroslav@273
    47
        this.first = first;
jaroslav@273
    48
        this.second = second;
jaroslav@273
    49
        this.m = m;
jaroslav@273
    50
    }
jaroslav@273
    51
jaroslav@346
    52
    /** Inspects <code>clazz</code> and for each {@lik Compare} method creates
jaroslav@347
    53
     * instances of tests. Each instance runs the test in different virtual
jaroslav@346
    54
     * machine and at the end they compare the results.
jaroslav@346
    55
     * 
jaroslav@346
    56
     * @param clazz the class to inspect
jaroslav@346
    57
     * @return the set of created tests
jaroslav@346
    58
     */
jaroslav@273
    59
    public static Object[] create(Class<?> clazz) {
jaroslav@273
    60
        Method[] arr = clazz.getMethods();
jaroslav@372
    61
        List<Object> ret = new ArrayList<>();
jaroslav@372
    62
        
jaroslav@384
    63
        final LaunchSetup l = LaunchSetup.INSTANCE;
jaroslav@372
    64
        ret.add(l);
jaroslav@372
    65
        
jaroslav@383
    66
        String[] brwsr;
jaroslav@383
    67
        {
jaroslav@383
    68
            String p = System.getProperty("vmtest.brwsrs");
jaroslav@383
    69
            if (p != null) {
jaroslav@383
    70
                brwsr = p.split(",");
jaroslav@383
    71
            } else {
jaroslav@383
    72
                brwsr = new String[0];
jaroslav@383
    73
            }
jaroslav@383
    74
        }
jaroslav@383
    75
        
jaroslav@273
    76
        for (Method m : arr) {
jaroslav@273
    77
            Compare c = m.getAnnotation(Compare.class);
jaroslav@273
    78
            if (c == null) {
jaroslav@273
    79
                continue;
jaroslav@273
    80
            }
jaroslav@383
    81
            final Bck2BrwsrCase real = new Bck2BrwsrCase(m, "Java", null);
jaroslav@384
    82
            final Bck2BrwsrCase js = new Bck2BrwsrCase(m, "JavaScript", l.javaScript());
jaroslav@372
    83
            ret.add(real);
jaroslav@372
    84
            ret.add(js);
jaroslav@372
    85
            ret.add(new CompareCase(m, real, js));
jaroslav@383
    86
jaroslav@383
    87
            for (String b : brwsr) {
jaroslav@384
    88
                final Launcher s = l.brwsr(b);
jaroslav@383
    89
                ret.add(s);
jaroslav@383
    90
                final Bck2BrwsrCase cse = new Bck2BrwsrCase(m, b, s);
jaroslav@383
    91
                ret.add(cse);
jaroslav@383
    92
                ret.add(new CompareCase(m, real, cse));
jaroslav@383
    93
            }
jaroslav@273
    94
        }
jaroslav@372
    95
        return ret.toArray();
jaroslav@273
    96
    }
jaroslav@346
    97
jaroslav@346
    98
    /** Test that compares the previous results.
jaroslav@346
    99
     * @throws Throwable 
jaroslav@346
   100
     */
jaroslav@280
   101
    @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
jaroslav@280
   102
        Object v1 = first.value;
jaroslav@280
   103
        Object v2 = second.value;
jaroslav@356
   104
        if (v1 != null) {
jaroslav@356
   105
            v1 = v1.toString();
jaroslav@356
   106
        } else {
jaroslav@356
   107
            v1 = "null";
jaroslav@280
   108
        }
jaroslav@393
   109
        try {
jaroslav@393
   110
            Assert.assertEquals(v2, v1, "Comparing results");
jaroslav@393
   111
        } catch (AssertionError e) {
jaroslav@393
   112
            StringBuilder sb = new StringBuilder();
jaroslav@393
   113
            sb.append(e.getMessage());
jaroslav@393
   114
            Bck2BrwsrCase.dumpJS(sb, second);
jaroslav@393
   115
            throw new AssertionError(sb.toString());
jaroslav@393
   116
        }
jaroslav@280
   117
    }
jaroslav@280
   118
    
jaroslav@346
   119
    /** Test name.
jaroslav@346
   120
     * @return name of the tested method followed by a suffix
jaroslav@346
   121
     */
jaroslav@280
   122
    @Override
jaroslav@280
   123
    public String getTestName() {
jaroslav@349
   124
        return m.getName() + "[Compare " + second.typeName() + "]";
jaroslav@280
   125
    }
jaroslav@273
   126
}