rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 08 Mar 2013 23:43:01 +0100
changeset 824 97fdbed30f8b
parent 772 d382dacfd73f
child 825 0585605d4913
permissions -rw-r--r--
Only add the trailing '.0' if the number is known to be integer. Otherwise accept the string as provided by JavaScript
     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(Class<?> clazz) {
    57         Method[] arr = clazz.getMethods();
    58         List<Object> ret = new ArrayList<>();
    59         
    60         final LaunchSetup l = LaunchSetup.INSTANCE;
    61         ret.add(l);
    62         
    63         String[] brwsr;
    64         {
    65             String p = System.getProperty("vmtest.brwsrs");
    66             if (p != null) {
    67                 brwsr = p.split(",");
    68             } else {
    69                 brwsr = new String[0];
    70             }
    71         }
    72         
    73         for (Method m : arr) {
    74             registerCompareCases(m, l, ret, brwsr);
    75             registerBrwsrCases(m, l, ret, brwsr);
    76         }
    77         return ret.toArray();
    78     }
    79 
    80     /** Test that compares the previous results.
    81      * @throws Throwable 
    82      */
    83     @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
    84         Object v1 = first.value;
    85         Object v2 = second.value;
    86         if (v1 instanceof Number) {
    87             try {
    88                 v1 = Double.parseDouble(v1.toString());
    89             } catch (NumberFormatException nfe) {
    90                 v1 = "Can't parse " + v1.toString();
    91             }
    92             try {
    93                 v2 = Double.parseDouble(v2.toString());
    94             } catch (NumberFormatException nfe) {
    95                 v2 = "Can't parse " + v2.toString();
    96             }
    97         } else {
    98             if (v1 != null) {
    99                 v1 = v1.toString();
   100             } else {
   101                 v1 = "null";
   102             }
   103         }
   104         try {
   105             Assert.assertEquals(v2, v1, "Comparing results");
   106         } catch (AssertionError e) {
   107             StringBuilder sb = new StringBuilder();
   108             sb.append(e.getMessage());
   109             Bck2BrwsrCase.dumpJS(sb, second);
   110             throw new AssertionError(sb.toString());
   111         }
   112     }
   113     
   114     /** Test name.
   115      * @return name of the tested method followed by a suffix
   116      */
   117     @Override
   118     public String getTestName() {
   119         return m.getName() + "[Compare " + second.typeName() + "]";
   120     }
   121     
   122     private static void registerCompareCases(Method m, final LaunchSetup l, List<Object> ret, String[] brwsr) {
   123         Compare c = m.getAnnotation(Compare.class);
   124         if (c == null) {
   125             return;
   126         }
   127         final Bck2BrwsrCase real = new Bck2BrwsrCase(m, "Java", null, false, null, null);
   128         ret.add(real);
   129         if (c.scripting()) {
   130             final Bck2BrwsrCase js = new Bck2BrwsrCase(m, "JavaScript", l.javaScript(), false, null, null);
   131             ret.add(js);
   132             ret.add(new CompareCase(m, real, js));
   133         }
   134         for (String b : brwsr) {
   135             final Launcher s = l.brwsr(b);
   136             ret.add(s);
   137             final Bck2BrwsrCase cse = new Bck2BrwsrCase(m, b, s, false, null, null);
   138             ret.add(cse);
   139             ret.add(new CompareCase(m, real, cse));
   140         }
   141     }
   142     private static void registerBrwsrCases(Method m, final LaunchSetup l, List<Object> ret, String[] brwsr) {
   143         BrwsrTest c = m.getAnnotation(BrwsrTest.class);
   144         if (c == null) {
   145             return;
   146         }
   147         HtmlFragment f = m.getAnnotation(HtmlFragment.class);
   148         if (f == null) {
   149             f = m.getDeclaringClass().getAnnotation(HtmlFragment.class);
   150         }
   151         Http.Resource[] r = {};
   152         Http h = m.getAnnotation(Http.class);
   153         if (h == null) {
   154             h = m.getDeclaringClass().getAnnotation(Http.class);
   155             if (h != null) {
   156                 r = h.value();
   157             }
   158         } else {
   159             r = h.value();
   160         }
   161         if (brwsr.length == 0) {
   162             final Launcher s = l.brwsr(null);
   163             ret.add(s);
   164             ret.add(new Bck2BrwsrCase(m, "Brwsr", s, true, f, r));
   165         } else {
   166             for (String b : brwsr) {
   167                 final Launcher s = l.brwsr(b);
   168                 ret.add(s);
   169                 ret.add(new Bck2BrwsrCase(m, b, s, true, f, r));
   170             }
   171         }
   172     }
   173 }