vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 22 Jan 2013 11:59:25 +0100
changeset 518 5df0a239ebeb
parent 393 bedc3b93a040
child 525 ec343ebe862e
permissions -rw-r--r--
@BrwsrTest to execute tests directly in browser
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@518
    77
            registerCompareCases(m, l, ret, brwsr);
jaroslav@518
    78
            registerBrwsrCases(m, l, ret, brwsr);
jaroslav@273
    79
        }
jaroslav@372
    80
        return ret.toArray();
jaroslav@273
    81
    }
jaroslav@346
    82
jaroslav@346
    83
    /** Test that compares the previous results.
jaroslav@346
    84
     * @throws Throwable 
jaroslav@346
    85
     */
jaroslav@280
    86
    @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
jaroslav@280
    87
        Object v1 = first.value;
jaroslav@280
    88
        Object v2 = second.value;
jaroslav@356
    89
        if (v1 != null) {
jaroslav@356
    90
            v1 = v1.toString();
jaroslav@356
    91
        } else {
jaroslav@356
    92
            v1 = "null";
jaroslav@280
    93
        }
jaroslav@393
    94
        try {
jaroslav@393
    95
            Assert.assertEquals(v2, v1, "Comparing results");
jaroslav@393
    96
        } catch (AssertionError e) {
jaroslav@393
    97
            StringBuilder sb = new StringBuilder();
jaroslav@393
    98
            sb.append(e.getMessage());
jaroslav@393
    99
            Bck2BrwsrCase.dumpJS(sb, second);
jaroslav@393
   100
            throw new AssertionError(sb.toString());
jaroslav@393
   101
        }
jaroslav@280
   102
    }
jaroslav@280
   103
    
jaroslav@346
   104
    /** Test name.
jaroslav@346
   105
     * @return name of the tested method followed by a suffix
jaroslav@346
   106
     */
jaroslav@280
   107
    @Override
jaroslav@280
   108
    public String getTestName() {
jaroslav@349
   109
        return m.getName() + "[Compare " + second.typeName() + "]";
jaroslav@280
   110
    }
jaroslav@518
   111
    
jaroslav@518
   112
    private static void registerCompareCases(Method m, final LaunchSetup l, List<Object> ret, String[] brwsr) {
jaroslav@518
   113
        Compare c = m.getAnnotation(Compare.class);
jaroslav@518
   114
        if (c == null) {
jaroslav@518
   115
            return;
jaroslav@518
   116
        }
jaroslav@518
   117
        final Bck2BrwsrCase real = new Bck2BrwsrCase(m, "Java", null, false);
jaroslav@518
   118
        final Bck2BrwsrCase js = new Bck2BrwsrCase(m, "JavaScript", l.javaScript(), false);
jaroslav@518
   119
        ret.add(real);
jaroslav@518
   120
        ret.add(js);
jaroslav@518
   121
        ret.add(new CompareCase(m, real, js));
jaroslav@518
   122
        for (String b : brwsr) {
jaroslav@518
   123
            final Launcher s = l.brwsr(b);
jaroslav@518
   124
            ret.add(s);
jaroslav@518
   125
            final Bck2BrwsrCase cse = new Bck2BrwsrCase(m, b, s, false);
jaroslav@518
   126
            ret.add(cse);
jaroslav@518
   127
            ret.add(new CompareCase(m, real, cse));
jaroslav@518
   128
        }
jaroslav@518
   129
    }
jaroslav@518
   130
    private static void registerBrwsrCases(Method m, final LaunchSetup l, List<Object> ret, String[] brwsr) {
jaroslav@518
   131
        BrwsrTest c = m.getAnnotation(BrwsrTest.class);
jaroslav@518
   132
        if (c == null) {
jaroslav@518
   133
            return;
jaroslav@518
   134
        }
jaroslav@518
   135
        if (brwsr.length == 0) {
jaroslav@518
   136
            final Launcher s = l.brwsr(null);
jaroslav@518
   137
            ret.add(s);
jaroslav@518
   138
            ret.add(new Bck2BrwsrCase(m, "Brwsr", s, true));
jaroslav@518
   139
        } else {
jaroslav@518
   140
            for (String b : brwsr) {
jaroslav@518
   141
                final Launcher s = l.brwsr(b);
jaroslav@518
   142
                ret.add(s);
jaroslav@518
   143
                ret.add(new Bck2BrwsrCase(m, b, s, true));
jaroslav@518
   144
            }
jaroslav@518
   145
        }
jaroslav@518
   146
    }
jaroslav@273
   147
}