vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 07 Feb 2013 12:58:12 +0100
branchemul
changeset 694 0d277415ed02
parent 623 4af0d3dedb9d
permissions -rw-r--r--
Rebasing the Inflater support on jzlib which, unlike GNU ClassPath, has correct implementation of Huffman code. Making the implementation more easily testable by turning Inflater and ZipInputStream into pure delegates. Current implementation is going to need proper long support.
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@273
    21
import java.lang.reflect.Method;
jaroslav@372
    22
import java.util.ArrayList;
jaroslav@372
    23
import java.util.List;
jaroslav@384
    24
import org.apidesign.bck2brwsr.launcher.Launcher;
jaroslav@273
    25
import org.testng.Assert;
jaroslav@273
    26
import org.testng.ITest;
jaroslav@273
    27
import org.testng.annotations.Factory;
jaroslav@273
    28
import org.testng.annotations.Test;
jaroslav@273
    29
jaroslav@273
    30
/** A TestNG {@link Factory} that seeks for {@link Compare} annotations
jaroslav@273
    31
 * in provided class and builds set of tests that compare the computations
jaroslav@273
    32
 * in real as well as JavaScript virtual machines. Use as:<pre>
jaroslav@273
    33
 * {@code @}{@link Factory} public static create() {
jaroslav@346
    34
 *   return @{link VMTest}.{@link #create(YourClass.class);
jaroslav@273
    35
 * }</pre>
jaroslav@273
    36
 *
jaroslav@273
    37
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@273
    38
 */
jaroslav@372
    39
public final class CompareCase implements ITest {
jaroslav@372
    40
    private final Bck2BrwsrCase first, second;
jaroslav@273
    41
    private final Method m;
jaroslav@273
    42
    
jaroslav@372
    43
    private CompareCase(Method m, Bck2BrwsrCase first, Bck2BrwsrCase second) {
jaroslav@273
    44
        this.first = first;
jaroslav@273
    45
        this.second = second;
jaroslav@273
    46
        this.m = m;
jaroslav@273
    47
    }
jaroslav@273
    48
jaroslav@346
    49
    /** Inspects <code>clazz</code> and for each {@lik Compare} method creates
jaroslav@347
    50
     * instances of tests. Each instance runs the test in different virtual
jaroslav@346
    51
     * machine and at the end they compare the results.
jaroslav@346
    52
     * 
jaroslav@346
    53
     * @param clazz the class to inspect
jaroslav@346
    54
     * @return the set of created tests
jaroslav@346
    55
     */
jaroslav@273
    56
    public static Object[] create(Class<?> clazz) {
jaroslav@273
    57
        Method[] arr = clazz.getMethods();
jaroslav@372
    58
        List<Object> ret = new ArrayList<>();
jaroslav@372
    59
        
jaroslav@384
    60
        final LaunchSetup l = LaunchSetup.INSTANCE;
jaroslav@372
    61
        ret.add(l);
jaroslav@372
    62
        
jaroslav@383
    63
        String[] brwsr;
jaroslav@383
    64
        {
jaroslav@383
    65
            String p = System.getProperty("vmtest.brwsrs");
jaroslav@383
    66
            if (p != null) {
jaroslav@383
    67
                brwsr = p.split(",");
jaroslav@383
    68
            } else {
jaroslav@383
    69
                brwsr = new String[0];
jaroslav@383
    70
            }
jaroslav@383
    71
        }
jaroslav@383
    72
        
jaroslav@273
    73
        for (Method m : arr) {
jaroslav@518
    74
            registerCompareCases(m, l, ret, brwsr);
jaroslav@518
    75
            registerBrwsrCases(m, l, ret, brwsr);
jaroslav@273
    76
        }
jaroslav@372
    77
        return ret.toArray();
jaroslav@273
    78
    }
jaroslav@346
    79
jaroslav@346
    80
    /** Test that compares the previous results.
jaroslav@346
    81
     * @throws Throwable 
jaroslav@346
    82
     */
jaroslav@280
    83
    @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
jaroslav@280
    84
        Object v1 = first.value;
jaroslav@280
    85
        Object v2 = second.value;
jaroslav@356
    86
        if (v1 != null) {
jaroslav@356
    87
            v1 = v1.toString();
jaroslav@356
    88
        } else {
jaroslav@356
    89
            v1 = "null";
jaroslav@280
    90
        }
jaroslav@393
    91
        try {
jaroslav@393
    92
            Assert.assertEquals(v2, v1, "Comparing results");
jaroslav@393
    93
        } catch (AssertionError e) {
jaroslav@393
    94
            StringBuilder sb = new StringBuilder();
jaroslav@393
    95
            sb.append(e.getMessage());
jaroslav@393
    96
            Bck2BrwsrCase.dumpJS(sb, second);
jaroslav@393
    97
            throw new AssertionError(sb.toString());
jaroslav@393
    98
        }
jaroslav@280
    99
    }
jaroslav@280
   100
    
jaroslav@346
   101
    /** Test name.
jaroslav@346
   102
     * @return name of the tested method followed by a suffix
jaroslav@346
   103
     */
jaroslav@280
   104
    @Override
jaroslav@280
   105
    public String getTestName() {
jaroslav@349
   106
        return m.getName() + "[Compare " + second.typeName() + "]";
jaroslav@280
   107
    }
jaroslav@518
   108
    
jaroslav@518
   109
    private static void registerCompareCases(Method m, final LaunchSetup l, List<Object> ret, String[] brwsr) {
jaroslav@518
   110
        Compare c = m.getAnnotation(Compare.class);
jaroslav@518
   111
        if (c == null) {
jaroslav@518
   112
            return;
jaroslav@518
   113
        }
jaroslav@623
   114
        final Bck2BrwsrCase real = new Bck2BrwsrCase(m, "Java", null, false, null, null);
jaroslav@518
   115
        ret.add(real);
jaroslav@537
   116
        if (c.scripting()) {
jaroslav@623
   117
            final Bck2BrwsrCase js = new Bck2BrwsrCase(m, "JavaScript", l.javaScript(), false, null, null);
jaroslav@537
   118
            ret.add(js);
jaroslav@537
   119
            ret.add(new CompareCase(m, real, js));
jaroslav@537
   120
        }
jaroslav@518
   121
        for (String b : brwsr) {
jaroslav@518
   122
            final Launcher s = l.brwsr(b);
jaroslav@518
   123
            ret.add(s);
jaroslav@623
   124
            final Bck2BrwsrCase cse = new Bck2BrwsrCase(m, b, s, false, null, null);
jaroslav@518
   125
            ret.add(cse);
jaroslav@518
   126
            ret.add(new CompareCase(m, real, cse));
jaroslav@518
   127
        }
jaroslav@518
   128
    }
jaroslav@518
   129
    private static void registerBrwsrCases(Method m, final LaunchSetup l, List<Object> ret, String[] brwsr) {
jaroslav@518
   130
        BrwsrTest c = m.getAnnotation(BrwsrTest.class);
jaroslav@518
   131
        if (c == null) {
jaroslav@518
   132
            return;
jaroslav@518
   133
        }
jaroslav@526
   134
        HtmlFragment f = m.getAnnotation(HtmlFragment.class);
jaroslav@526
   135
        if (f == null) {
jaroslav@526
   136
            f = m.getDeclaringClass().getAnnotation(HtmlFragment.class);
jaroslav@526
   137
        }
jaroslav@667
   138
        Http.Resource[] r = {};
jaroslav@667
   139
        Http h = m.getAnnotation(Http.class);
jaroslav@667
   140
        if (h == null) {
jaroslav@667
   141
            h = m.getDeclaringClass().getAnnotation(Http.class);
jaroslav@667
   142
            if (h != null) {
jaroslav@667
   143
                r = h.value();
jaroslav@667
   144
            }
jaroslav@667
   145
        } else {
jaroslav@667
   146
            r = h.value();
jaroslav@623
   147
        }
jaroslav@518
   148
        if (brwsr.length == 0) {
jaroslav@518
   149
            final Launcher s = l.brwsr(null);
jaroslav@518
   150
            ret.add(s);
jaroslav@623
   151
            ret.add(new Bck2BrwsrCase(m, "Brwsr", s, true, f, r));
jaroslav@518
   152
        } else {
jaroslav@518
   153
            for (String b : brwsr) {
jaroslav@518
   154
                final Launcher s = l.brwsr(b);
jaroslav@518
   155
                ret.add(s);
jaroslav@623
   156
                ret.add(new Bck2BrwsrCase(m, b, s, true, f, r));
jaroslav@518
   157
            }
jaroslav@518
   158
        }
jaroslav@518
   159
    }
jaroslav@273
   160
}