rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 28 Apr 2013 22:38:29 +0200
branchmodel
changeset 1050 6415da7d00b6
parent 1018 49eb825c87b7
child 1185 be346bd5a46d
permissions -rw-r--r--
Turning VMTest into a builder pattern
     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.lang.reflect.Method;
    22 import java.util.ArrayList;
    23 import java.util.List;
    24 import org.apidesign.bck2brwsr.launcher.Launcher;
    25 import org.testng.Assert;
    26 import org.testng.ITest;
    27 import org.testng.annotations.Factory;
    28 import org.testng.annotations.Test;
    29 
    30 /** A TestNG {@link Factory} that seeks for {@link Compare} annotations
    31  * in provided class and builds set of tests that compare the computations
    32  * in real as well as JavaScript virtual machines. Use as:<pre>
    33  * {@code @}{@link Factory} public static create() {
    34  *   return @{link VMTest}.{@link #create(YourClass.class);
    35  * }</pre>
    36  *
    37  * @author Jaroslav Tulach <jtulach@netbeans.org>
    38  */
    39 public final class CompareCase implements ITest {
    40     private final Bck2BrwsrCase first, second;
    41     private final Method m;
    42     
    43     private CompareCase(Method m, Bck2BrwsrCase first, Bck2BrwsrCase second) {
    44         this.first = first;
    45         this.second = second;
    46         this.m = m;
    47     }
    48 
    49     /** Inspects <code>clazz</code> and for each {@lik Compare} method creates
    50      * instances of tests. Each instance runs the test in different virtual
    51      * machine and at the end they compare the results.
    52      * 
    53      * @param clazz the class to inspect
    54      * @return the set of created tests
    55      */
    56     public static Object[] create(String[] brwsr, Class[] classes) {
    57         List<Object> ret = new ArrayList<>();
    58         
    59         final LaunchSetup l = LaunchSetup.INSTANCE;
    60         ret.add(l);
    61         
    62         {
    63             String p = System.getProperty("vmtest.brwsrs");
    64             if (p != null) {
    65                 brwsr = p.split(",");
    66             }
    67         }
    68         
    69         for (Class clazz : classes) {
    70             Method[] arr = clazz.getMethods();
    71             for (Method m : arr) {
    72                 registerCompareCases(m, l, ret, brwsr);
    73                 registerBrwsrCases(m, l, ret, brwsr);
    74             }
    75         }
    76         return ret.toArray();
    77     }
    78 
    79     /** Test that compares the previous results.
    80      * @throws Throwable 
    81      */
    82     @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
    83         Object v1 = first.value;
    84         Object v2 = second.value;
    85         if (v1 instanceof Integer || v1 instanceof Long || v1 instanceof Byte || v1 instanceof Short) {
    86             try {
    87                 v1 = Long.parseLong(v1.toString());
    88             } catch (NumberFormatException nfe) {
    89                 v1 = "Can't parse " + v1.toString();
    90             }
    91             try {
    92                 v2 = Long.parseLong(v2.toString());
    93             } catch (NumberFormatException nfe) {
    94                 v2 = "Can't parse " + v2.toString();
    95             }
    96         } else if (v1 instanceof Number) {
    97             try {
    98                 v1 = Double.parseDouble(v1.toString());
    99             } catch (NumberFormatException nfe) {
   100                 v1 = "Can't parse " + v1.toString();
   101             }
   102             try {
   103                 v2 = Double.parseDouble(v2.toString());
   104             } catch (NumberFormatException nfe) {
   105                 v2 = "Can't parse " + v2.toString();
   106             }
   107         } else {
   108             if (v1 != null) {
   109                 v1 = v1.toString();
   110             } else {
   111                 v1 = "null";
   112             }
   113         }
   114         try {
   115             Assert.assertEquals(v2, v1, "Comparing results");
   116         } catch (AssertionError e) {
   117             StringBuilder sb = new StringBuilder();
   118             sb.append(e.getMessage());
   119             Bck2BrwsrCase.dumpJS(sb, second);
   120             throw new AssertionError(sb.toString());
   121         }
   122     }
   123     
   124     /** Test name.
   125      * @return name of the tested method followed by a suffix
   126      */
   127     @Override
   128     public String getTestName() {
   129         return m.getName() + "[Compare " + second.typeName() + "]";
   130     }
   131     
   132     private static void registerCompareCases(Method m, final LaunchSetup l, List<Object> ret, String[] brwsr) {
   133         Compare c = m.getAnnotation(Compare.class);
   134         if (c == null) {
   135             return;
   136         }
   137         final Bck2BrwsrCase real = new Bck2BrwsrCase(m, "Java", null, false, null, null);
   138         ret.add(real);
   139         if (c.scripting()) {
   140             final Bck2BrwsrCase js = new Bck2BrwsrCase(m, "JavaScript", l.javaScript(), false, null, null);
   141             ret.add(js);
   142             ret.add(new CompareCase(m, real, js));
   143         }
   144         for (String b : brwsr) {
   145             final Launcher s = l.brwsr(b);
   146             ret.add(s);
   147             final Bck2BrwsrCase cse = new Bck2BrwsrCase(m, b, s, false, null, null);
   148             ret.add(cse);
   149             ret.add(new CompareCase(m, real, cse));
   150         }
   151     }
   152     private static void registerBrwsrCases(Method m, final LaunchSetup l, List<Object> ret, String[] brwsr) {
   153         BrwsrTest c = m.getAnnotation(BrwsrTest.class);
   154         if (c == null) {
   155             return;
   156         }
   157         HtmlFragment f = m.getAnnotation(HtmlFragment.class);
   158         if (f == null) {
   159             f = m.getDeclaringClass().getAnnotation(HtmlFragment.class);
   160         }
   161         Http.Resource[] r = {};
   162         Http h = m.getAnnotation(Http.class);
   163         if (h == null) {
   164             h = m.getDeclaringClass().getAnnotation(Http.class);
   165             if (h != null) {
   166                 r = h.value();
   167             }
   168         } else {
   169             r = h.value();
   170         }
   171         if (brwsr.length == 0) {
   172             final Launcher s = l.brwsr(null);
   173             ret.add(s);
   174             ret.add(new Bck2BrwsrCase(m, "Brwsr", s, true, f, r));
   175         } else {
   176             for (String b : brwsr) {
   177                 final Launcher s = l.brwsr(b);
   178                 ret.add(s);
   179                 ret.add(new Bck2BrwsrCase(m, b, s, true, f, r));
   180             }
   181         }
   182     }
   183 }