vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 25 Dec 2012 15:31:58 +0100
changeset 383 88ed1f51eb22
parent 372 3485327d3080
child 384 269d99fd6421
permissions -rw-r--r--
One can specify list of browsers to test in via 'vmtest.brwsrs' property
     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.testng.Assert;
    28 import org.testng.ITest;
    29 import org.testng.annotations.Factory;
    30 import org.testng.annotations.Test;
    31 
    32 /** A TestNG {@link Factory} that seeks for {@link Compare} annotations
    33  * in provided class and builds set of tests that compare the computations
    34  * in real as well as JavaScript virtual machines. Use as:<pre>
    35  * {@code @}{@link Factory} public static create() {
    36  *   return @{link VMTest}.{@link #create(YourClass.class);
    37  * }</pre>
    38  *
    39  * @author Jaroslav Tulach <jtulach@netbeans.org>
    40  */
    41 public final class CompareCase implements ITest {
    42     private final Bck2BrwsrCase first, second;
    43     private final Method m;
    44     
    45     private CompareCase(Method m, Bck2BrwsrCase first, Bck2BrwsrCase second) {
    46         this.first = first;
    47         this.second = second;
    48         this.m = m;
    49     }
    50 
    51     /** Inspects <code>clazz</code> and for each {@lik Compare} method creates
    52      * instances of tests. Each instance runs the test in different virtual
    53      * machine and at the end they compare the results.
    54      * 
    55      * @param clazz the class to inspect
    56      * @return the set of created tests
    57      */
    58     public static Object[] create(Class<?> clazz) {
    59         Method[] arr = clazz.getMethods();
    60         List<Object> ret = new ArrayList<>();
    61         
    62         final LaunchSetup l = LaunchSetup.javaScript();
    63         ret.add(l);
    64         
    65         String[] brwsr;
    66         {
    67             String p = System.getProperty("vmtest.brwsrs");
    68             if (p != null) {
    69                 brwsr = p.split(",");
    70             } else {
    71                 brwsr = new String[0];
    72             }
    73         }
    74         
    75         for (Method m : arr) {
    76             Compare c = m.getAnnotation(Compare.class);
    77             if (c == null) {
    78                 continue;
    79             }
    80             final Bck2BrwsrCase real = new Bck2BrwsrCase(m, "Java", null);
    81             final Bck2BrwsrCase js = new Bck2BrwsrCase(m, "JavaScript", l);
    82             ret.add(real);
    83             ret.add(js);
    84             ret.add(new CompareCase(m, real, js));
    85 
    86             for (String b : brwsr) {
    87                 final LaunchSetup s = LaunchSetup.brwsr(b);
    88                 ret.add(s);
    89                 final Bck2BrwsrCase cse = new Bck2BrwsrCase(m, b, s);
    90                 ret.add(cse);
    91                 ret.add(new CompareCase(m, real, cse));
    92             }
    93         }
    94         return ret.toArray();
    95     }
    96 
    97     /** Test that compares the previous results.
    98      * @throws Throwable 
    99      */
   100     @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
   101         Object v1 = first.value;
   102         Object v2 = second.value;
   103         if (v1 != null) {
   104             v1 = v1.toString();
   105         } else {
   106             v1 = "null";
   107         }
   108         Assert.assertEquals(v2, v1, "Comparing results");
   109     }
   110     
   111     /** Test name.
   112      * @return name of the tested method followed by a suffix
   113      */
   114     @Override
   115     public String getTestName() {
   116         return m.getName() + "[Compare " + second.typeName() + "]";
   117     }
   118     
   119     static StringBuilder dumpJS(CharSequence sb) throws IOException {
   120         File f = File.createTempFile("execution", ".js");
   121         try (FileWriter w = new FileWriter(f)) {
   122             w.append(sb);
   123         }
   124         return new StringBuilder(f.getPath());
   125     }
   126 }