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
     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             Compare c = m.getAnnotation(Compare.class);
    78             if (c == null) {
    79                 continue;
    80             }
    81             final Bck2BrwsrCase real = new Bck2BrwsrCase(m, "Java", null);
    82             final Bck2BrwsrCase js = new Bck2BrwsrCase(m, "JavaScript", l.javaScript());
    83             ret.add(real);
    84             ret.add(js);
    85             ret.add(new CompareCase(m, real, js));
    86 
    87             for (String b : brwsr) {
    88                 final Launcher s = l.brwsr(b);
    89                 ret.add(s);
    90                 final Bck2BrwsrCase cse = new Bck2BrwsrCase(m, b, s);
    91                 ret.add(cse);
    92                 ret.add(new CompareCase(m, real, cse));
    93             }
    94         }
    95         return ret.toArray();
    96     }
    97 
    98     /** Test that compares the previous results.
    99      * @throws Throwable 
   100      */
   101     @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
   102         Object v1 = first.value;
   103         Object v2 = second.value;
   104         if (v1 != null) {
   105             v1 = v1.toString();
   106         } else {
   107             v1 = "null";
   108         }
   109         try {
   110             Assert.assertEquals(v2, v1, "Comparing results");
   111         } catch (AssertionError e) {
   112             StringBuilder sb = new StringBuilder();
   113             sb.append(e.getMessage());
   114             Bck2BrwsrCase.dumpJS(sb, second);
   115             throw new AssertionError(sb.toString());
   116         }
   117     }
   118     
   119     /** Test name.
   120      * @return name of the tested method followed by a suffix
   121      */
   122     @Override
   123     public String getTestName() {
   124         return m.getName() + "[Compare " + second.typeName() + "]";
   125     }
   126 }