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