rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java
changeset 772 d382dacfd73f
parent 667 5866e89ef568
child 824 97fdbed30f8b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,160 @@
     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.lang.reflect.Method;
    1.25 +import java.util.ArrayList;
    1.26 +import java.util.List;
    1.27 +import org.apidesign.bck2brwsr.launcher.Launcher;
    1.28 +import org.testng.Assert;
    1.29 +import org.testng.ITest;
    1.30 +import org.testng.annotations.Factory;
    1.31 +import org.testng.annotations.Test;
    1.32 +
    1.33 +/** A TestNG {@link Factory} that seeks for {@link Compare} annotations
    1.34 + * in provided class and builds set of tests that compare the computations
    1.35 + * in real as well as JavaScript virtual machines. Use as:<pre>
    1.36 + * {@code @}{@link Factory} public static create() {
    1.37 + *   return @{link VMTest}.{@link #create(YourClass.class);
    1.38 + * }</pre>
    1.39 + *
    1.40 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.41 + */
    1.42 +public final class CompareCase implements ITest {
    1.43 +    private final Bck2BrwsrCase first, second;
    1.44 +    private final Method m;
    1.45 +    
    1.46 +    private CompareCase(Method m, Bck2BrwsrCase first, Bck2BrwsrCase second) {
    1.47 +        this.first = first;
    1.48 +        this.second = second;
    1.49 +        this.m = m;
    1.50 +    }
    1.51 +
    1.52 +    /** Inspects <code>clazz</code> and for each {@lik Compare} method creates
    1.53 +     * instances of tests. Each instance runs the test in different virtual
    1.54 +     * machine and at the end they compare the results.
    1.55 +     * 
    1.56 +     * @param clazz the class to inspect
    1.57 +     * @return the set of created tests
    1.58 +     */
    1.59 +    public static Object[] create(Class<?> clazz) {
    1.60 +        Method[] arr = clazz.getMethods();
    1.61 +        List<Object> ret = new ArrayList<>();
    1.62 +        
    1.63 +        final LaunchSetup l = LaunchSetup.INSTANCE;
    1.64 +        ret.add(l);
    1.65 +        
    1.66 +        String[] brwsr;
    1.67 +        {
    1.68 +            String p = System.getProperty("vmtest.brwsrs");
    1.69 +            if (p != null) {
    1.70 +                brwsr = p.split(",");
    1.71 +            } else {
    1.72 +                brwsr = new String[0];
    1.73 +            }
    1.74 +        }
    1.75 +        
    1.76 +        for (Method m : arr) {
    1.77 +            registerCompareCases(m, l, ret, brwsr);
    1.78 +            registerBrwsrCases(m, l, ret, brwsr);
    1.79 +        }
    1.80 +        return ret.toArray();
    1.81 +    }
    1.82 +
    1.83 +    /** Test that compares the previous results.
    1.84 +     * @throws Throwable 
    1.85 +     */
    1.86 +    @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
    1.87 +        Object v1 = first.value;
    1.88 +        Object v2 = second.value;
    1.89 +        if (v1 != null) {
    1.90 +            v1 = v1.toString();
    1.91 +        } else {
    1.92 +            v1 = "null";
    1.93 +        }
    1.94 +        try {
    1.95 +            Assert.assertEquals(v2, v1, "Comparing results");
    1.96 +        } catch (AssertionError e) {
    1.97 +            StringBuilder sb = new StringBuilder();
    1.98 +            sb.append(e.getMessage());
    1.99 +            Bck2BrwsrCase.dumpJS(sb, second);
   1.100 +            throw new AssertionError(sb.toString());
   1.101 +        }
   1.102 +    }
   1.103 +    
   1.104 +    /** Test name.
   1.105 +     * @return name of the tested method followed by a suffix
   1.106 +     */
   1.107 +    @Override
   1.108 +    public String getTestName() {
   1.109 +        return m.getName() + "[Compare " + second.typeName() + "]";
   1.110 +    }
   1.111 +    
   1.112 +    private static void registerCompareCases(Method m, final LaunchSetup l, List<Object> ret, String[] brwsr) {
   1.113 +        Compare c = m.getAnnotation(Compare.class);
   1.114 +        if (c == null) {
   1.115 +            return;
   1.116 +        }
   1.117 +        final Bck2BrwsrCase real = new Bck2BrwsrCase(m, "Java", null, false, null, null);
   1.118 +        ret.add(real);
   1.119 +        if (c.scripting()) {
   1.120 +            final Bck2BrwsrCase js = new Bck2BrwsrCase(m, "JavaScript", l.javaScript(), false, null, null);
   1.121 +            ret.add(js);
   1.122 +            ret.add(new CompareCase(m, real, js));
   1.123 +        }
   1.124 +        for (String b : brwsr) {
   1.125 +            final Launcher s = l.brwsr(b);
   1.126 +            ret.add(s);
   1.127 +            final Bck2BrwsrCase cse = new Bck2BrwsrCase(m, b, s, false, null, null);
   1.128 +            ret.add(cse);
   1.129 +            ret.add(new CompareCase(m, real, cse));
   1.130 +        }
   1.131 +    }
   1.132 +    private static void registerBrwsrCases(Method m, final LaunchSetup l, List<Object> ret, String[] brwsr) {
   1.133 +        BrwsrTest c = m.getAnnotation(BrwsrTest.class);
   1.134 +        if (c == null) {
   1.135 +            return;
   1.136 +        }
   1.137 +        HtmlFragment f = m.getAnnotation(HtmlFragment.class);
   1.138 +        if (f == null) {
   1.139 +            f = m.getDeclaringClass().getAnnotation(HtmlFragment.class);
   1.140 +        }
   1.141 +        Http.Resource[] r = {};
   1.142 +        Http h = m.getAnnotation(Http.class);
   1.143 +        if (h == null) {
   1.144 +            h = m.getDeclaringClass().getAnnotation(Http.class);
   1.145 +            if (h != null) {
   1.146 +                r = h.value();
   1.147 +            }
   1.148 +        } else {
   1.149 +            r = h.value();
   1.150 +        }
   1.151 +        if (brwsr.length == 0) {
   1.152 +            final Launcher s = l.brwsr(null);
   1.153 +            ret.add(s);
   1.154 +            ret.add(new Bck2BrwsrCase(m, "Brwsr", s, true, f, r));
   1.155 +        } else {
   1.156 +            for (String b : brwsr) {
   1.157 +                final Launcher s = l.brwsr(b);
   1.158 +                ret.add(s);
   1.159 +                ret.add(new Bck2BrwsrCase(m, b, s, true, f, r));
   1.160 +            }
   1.161 +        }
   1.162 +    }
   1.163 +}