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