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
Martin@582
     1
/**
Martin@582
     2
 * Back 2 Browser Bytecode Translator
Martin@582
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Martin@582
     4
 *
Martin@582
     5
 * This program is free software: you can redistribute it and/or modify
Martin@582
     6
 * it under the terms of the GNU General Public License as published by
Martin@582
     7
 * the Free Software Foundation, version 2 of the License.
Martin@582
     8
 *
Martin@582
     9
 * This program is distributed in the hope that it will be useful,
Martin@582
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Martin@582
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Martin@582
    12
 * GNU General Public License for more details.
Martin@582
    13
 *
Martin@582
    14
 * You should have received a copy of the GNU General Public License
Martin@582
    15
 * along with this program. Look for COPYING file in the top folder.
Martin@582
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
Martin@582
    17
 */
Martin@582
    18
package org.apidesign.vm4brwsr;
Martin@582
    19
Martin@582
    20
import javax.script.Invocable;
Martin@582
    21
import javax.script.ScriptException;
Martin@582
    22
import static org.testng.Assert.*;
Martin@582
    23
Martin@582
    24
class TestUtils {
Martin@582
    25
Martin@582
    26
    static Object execCode(Invocable code, CharSequence codeSeq,
Martin@582
    27
        String msg, Class<?> clazz, String method, Object expRes, Object... args) 
Martin@582
    28
            throws Exception
Martin@582
    29
    {
Martin@582
    30
        Object ret = null;
Martin@582
    31
        try {
Martin@582
    32
            ret = code.invokeFunction("bck2brwsr");
Martin@582
    33
            ret = code.invokeMethod(ret, "loadClass", clazz.getName());
Martin@582
    34
            ret = code.invokeMethod(ret, method, args);
Martin@582
    35
        } catch (ScriptException ex) {
Martin@582
    36
            fail("Execution failed in " + StaticMethodTest.dumpJS(codeSeq), ex);
Martin@582
    37
        } catch (NoSuchMethodException ex) {
Martin@582
    38
            fail("Cannot find method in " + StaticMethodTest.dumpJS(codeSeq), ex);
Martin@582
    39
        }
Martin@582
    40
        if (ret == null && expRes == null) {
Martin@582
    41
            return null;
Martin@582
    42
        }
jaroslav@704
    43
        if (expRes != null && expRes.equals(ret)) {
Martin@582
    44
            return null;
Martin@582
    45
        }
Martin@582
    46
        if (expRes instanceof Number) {
Martin@582
    47
            // in case of Long it is necessary convert it to number
Martin@582
    48
            // since the Long is represented by two numbers in JavaScript
Martin@582
    49
            try {
Martin@615
    50
                ret = code.invokeMethod(ret, "toFP");
Martin@582
    51
                ret = code.invokeFunction("Number", ret);
Martin@582
    52
            } catch (ScriptException ex) {
Martin@582
    53
                fail("Conversion to number failed in " + StaticMethodTest.dumpJS(codeSeq), ex);
Martin@582
    54
            } catch (NoSuchMethodException ex) {
Martin@582
    55
                fail("Cannot find global Number(x) function in " + StaticMethodTest.dumpJS(codeSeq), ex);
Martin@582
    56
            }
Martin@582
    57
        }
Martin@582
    58
        return ret;
Martin@582
    59
    }
Martin@582
    60
}