src/test/java/org/apidesign/java4browser/StaticMethodTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 27 Aug 2012 05:08:30 +0200
changeset 0 71aab30ab2b7
child 1 48b1dce93691
permissions -rw-r--r--
Initial version of the Bytecode to JavaScript translator
jaroslav@0
     1
/*
jaroslav@0
     2
Java 4 Browser Bytecode Translator
jaroslav@0
     3
Copyright (C) 2012-2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@0
     4
jaroslav@0
     5
This program is free software: you can redistribute it and/or modify
jaroslav@0
     6
it under the terms of the GNU General Public License as published by
jaroslav@0
     7
the Free Software Foundation, version 2 of the License.
jaroslav@0
     8
jaroslav@0
     9
This program is distributed in the hope that it will be useful,
jaroslav@0
    10
but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@0
    11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@0
    12
GNU General Public License for more details.
jaroslav@0
    13
jaroslav@0
    14
You should have received a copy of the GNU General Public License
jaroslav@0
    15
along with this program. Look for COPYING file in the top folder.
jaroslav@0
    16
If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@0
    17
*/
jaroslav@0
    18
package org.apidesign.java4browser;
jaroslav@0
    19
jaroslav@0
    20
import java.io.IOException;
jaroslav@0
    21
import java.io.InputStream;
jaroslav@0
    22
import javax.script.Invocable;
jaroslav@0
    23
import javax.script.ScriptEngine;
jaroslav@0
    24
import javax.script.ScriptEngineManager;
jaroslav@0
    25
import javax.script.ScriptException;
jaroslav@0
    26
import static org.testng.Assert.*;
jaroslav@0
    27
import org.testng.annotations.Test;
jaroslav@0
    28
jaroslav@0
    29
/** Checks the basic behavior of the translator.
jaroslav@0
    30
 *
jaroslav@0
    31
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@0
    32
 */
jaroslav@0
    33
public class StaticMethodTest {
jaroslav@0
    34
    @Test public void threePlusFour() throws Exception {
jaroslav@0
    35
        Invocable i = compileClass("StaticMethod.class");
jaroslav@0
    36
        
jaroslav@0
    37
        Object ret = i.invokeFunction("org_apidesign_java4browser_StaticMethod_sumIII", 3, 4);
jaroslav@0
    38
        assertEquals(ret, Double.valueOf(7), "Should be seven");
jaroslav@0
    39
    }
jaroslav@0
    40
jaroslav@0
    41
    static Invocable compileClass(String name) throws ScriptException, IOException {
jaroslav@0
    42
        InputStream is = StaticMethodTest.class.getResourceAsStream(name);
jaroslav@0
    43
        assertNotNull(is, "Class file found");
jaroslav@0
    44
        StringBuilder sb = new StringBuilder();
jaroslav@0
    45
        ByteCodeToJavaScript.compile(name, is, sb);
jaroslav@0
    46
        ScriptEngineManager sem = new ScriptEngineManager();
jaroslav@0
    47
        ScriptEngine js = sem.getEngineByExtension("js");
jaroslav@0
    48
        try {
jaroslav@0
    49
            Object res = js.eval(sb.toString());
jaroslav@0
    50
            assertTrue(js instanceof Invocable, "It is invocable object: " + res);
jaroslav@0
    51
            return (Invocable)js;
jaroslav@0
    52
        } catch (ScriptException ex) {
jaroslav@0
    53
            fail("Could not compile:\n" + sb, ex);
jaroslav@0
    54
            return null;
jaroslav@0
    55
        }
jaroslav@0
    56
    }
jaroslav@0
    57
}