vm/src/test/java/org/apidesign/vm4brwsr/TestUtils.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 08 Feb 2013 11:12:02 +0100
changeset 704 4fe8b3dfc55f
parent 615 e3f671b50e93
permissions -rw-r--r--
Proper implementation of System.currentTimeMillis with so desired test
     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.vm4brwsr;
    19 
    20 import javax.script.Invocable;
    21 import javax.script.ScriptException;
    22 import static org.testng.Assert.*;
    23 
    24 class TestUtils {
    25 
    26     static Object execCode(Invocable code, CharSequence codeSeq,
    27         String msg, Class<?> clazz, String method, Object expRes, Object... args) 
    28             throws Exception
    29     {
    30         Object ret = null;
    31         try {
    32             ret = code.invokeFunction("bck2brwsr");
    33             ret = code.invokeMethod(ret, "loadClass", clazz.getName());
    34             ret = code.invokeMethod(ret, method, args);
    35         } catch (ScriptException ex) {
    36             fail("Execution failed in " + StaticMethodTest.dumpJS(codeSeq), ex);
    37         } catch (NoSuchMethodException ex) {
    38             fail("Cannot find method in " + StaticMethodTest.dumpJS(codeSeq), ex);
    39         }
    40         if (ret == null && expRes == null) {
    41             return null;
    42         }
    43         if (expRes != null && expRes.equals(ret)) {
    44             return null;
    45         }
    46         if (expRes instanceof Number) {
    47             // in case of Long it is necessary convert it to number
    48             // since the Long is represented by two numbers in JavaScript
    49             try {
    50                 ret = code.invokeMethod(ret, "toFP");
    51                 ret = code.invokeFunction("Number", ret);
    52             } catch (ScriptException ex) {
    53                 fail("Conversion to number failed in " + StaticMethodTest.dumpJS(codeSeq), ex);
    54             } catch (NoSuchMethodException ex) {
    55                 fail("Cannot find global Number(x) function in " + StaticMethodTest.dumpJS(codeSeq), ex);
    56             }
    57         }
    58         return ret;
    59     }
    60 }