vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 11 Feb 2013 12:46:43 +0100
branchemul
changeset 708 59d5596a9c6c
parent 594 035fcbd7a33c
permissions -rw-r--r--
Encapsulation. Moving shared code into TestVM instance.
jaroslav@201
     1
/**
jaroslav@201
     2
 * Back 2 Browser Bytecode Translator
jaroslav@201
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@201
     4
 *
jaroslav@201
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@201
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@201
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@201
     8
 *
jaroslav@201
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@201
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@201
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@201
    12
 * GNU General Public License for more details.
jaroslav@201
    13
 *
jaroslav@201
    14
 * You should have received a copy of the GNU General Public License
jaroslav@201
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@201
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@201
    17
 */
jaroslav@201
    18
package org.apidesign.vm4brwsr;
jaroslav@201
    19
jaroslav@201
    20
import javax.script.ScriptContext;
jaroslav@201
    21
import javax.script.ScriptEngine;
jaroslav@201
    22
import javax.script.ScriptException;
jaroslav@201
    23
import org.testng.annotations.BeforeClass;
jaroslav@201
    24
import static org.testng.Assert.*;
jaroslav@201
    25
import org.testng.annotations.Test;
jaroslav@201
    26
jaroslav@201
    27
/** Implements loading class by class.
jaroslav@201
    28
 *
jaroslav@201
    29
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@201
    30
 */
jaroslav@201
    31
public class VMLazyTest {
jaroslav@708
    32
    private static TestVM code;
jaroslav@201
    33
jaroslav@201
    34
    @BeforeClass
jaroslav@201
    35
    public void compileTheCode() throws Exception {
jaroslav@201
    36
        StringBuilder sb = new StringBuilder();
jaroslav@277
    37
        sb.append("\nvar data = {};");
jaroslav@277
    38
        sb.append("\nfunction test(clazz, method) {");
jaroslav@277
    39
        sb.append("\n  if (!data.bck2brwsr) data.bck2brwsr = bck2brwsr(function(name) { return loader.get(name); });");
jaroslav@277
    40
        sb.append("\n  var c = data.bck2brwsr.loadClass(clazz);");
jaroslav@201
    41
        sb.append("\n  return c[method]();");
jaroslav@201
    42
        sb.append("\n}");
jaroslav@201
    43
        
jaroslav@507
    44
        sb.append("\nfunction checkKO() {");
jaroslav@507
    45
        sb.append("\n  return ko !== null;");
jaroslav@507
    46
        sb.append("\n}");
jaroslav@277
    47
       
jaroslav@201
    48
        ScriptEngine[] arr = { null };
jaroslav@708
    49
        code = TestVM.compileClass(sb, arr,
Martin@594
    50
            new String[]{"org/apidesign/vm4brwsr/VM", "org/apidesign/vm4brwsr/StaticMethod"}
jaroslav@201
    51
        );
jaroslav@298
    52
        arr[0].getContext().setAttribute("loader", new BytesLoader(), ScriptContext.ENGINE_SCOPE);
jaroslav@201
    53
    }
jaroslav@201
    54
    
jaroslav@201
    55
    @Test public void invokeStaticMethod() throws Exception {
jaroslav@201
    56
        assertExec("Trying to get -1", "test", Double.valueOf(-1),
jaroslav@277
    57
            StaticMethod.class.getName(), "minusOne__I"
jaroslav@201
    58
        );
jaroslav@201
    59
    }
jaroslav@214
    60
jaroslav@214
    61
    @Test public void loadDependantClass() throws Exception {
jaroslav@277
    62
        assertExec("Expecting zero", "test", Double.valueOf(0),
jaroslav@277
    63
            InstanceSub.class.getName(), "recallDbl__D"
jaroslav@214
    64
        );
jaroslav@214
    65
    }
jaroslav@201
    66
jaroslav@503
    67
    @Test public void loadClassWithAssociatedScript() throws Exception {
jaroslav@503
    68
        assertExec("ko is defined", "test", true,
jaroslav@503
    69
            Script.class.getName(), "checkNotNull__Z"
jaroslav@503
    70
        );
jaroslav@507
    71
        
jaroslav@507
    72
        Object res = code.invokeFunction("checkKO");
jaroslav@507
    73
        assertEquals(res, true, "KO is defined on a global level");
jaroslav@503
    74
    }
jaroslav@503
    75
jaroslav@201
    76
    private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
jaroslav@201
    77
        Object ret = null;
jaroslav@201
    78
        try {
jaroslav@201
    79
            ret = code.invokeFunction(methodName, args);
jaroslav@201
    80
        } catch (ScriptException ex) {
jaroslav@708
    81
            fail("Execution failed in\n" + code.toString(), ex);
jaroslav@201
    82
        } catch (NoSuchMethodException ex) {
jaroslav@708
    83
            fail("Cannot find method in\n" + code.toString(), ex);
jaroslav@201
    84
        }
jaroslav@201
    85
        if (ret == null && expRes == null) {
jaroslav@201
    86
            return;
jaroslav@201
    87
        }
jaroslav@201
    88
        if (expRes.equals(ret)) {
jaroslav@201
    89
            return;
jaroslav@201
    90
        }
jaroslav@708
    91
        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + code.toString());
jaroslav@201
    92
    }
jaroslav@201
    93
}