vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 07 Dec 2012 06:29:54 +0100
branchlazyvm
changeset 277 e4b9eee9be83
parent 248 0bfcb6585290
child 298 885acca2fa0b
permissions -rw-r--r--
Lazy loading is now part of GenJS
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 java.io.IOException;
jaroslav@201
    21
import java.io.InputStream;
jaroslav@277
    22
import java.util.Set;
jaroslav@277
    23
import java.util.TreeSet;
jaroslav@201
    24
import javax.script.Invocable;
jaroslav@201
    25
import javax.script.ScriptContext;
jaroslav@201
    26
import javax.script.ScriptEngine;
jaroslav@201
    27
import javax.script.ScriptException;
jaroslav@201
    28
import org.testng.annotations.BeforeClass;
jaroslav@201
    29
import static org.testng.Assert.*;
jaroslav@201
    30
import org.testng.annotations.Test;
jaroslav@201
    31
jaroslav@201
    32
/** Implements loading class by class.
jaroslav@201
    33
 *
jaroslav@201
    34
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@201
    35
 */
jaroslav@201
    36
public class VMLazyTest {
jaroslav@201
    37
jaroslav@201
    38
    
jaroslav@201
    39
    private static CharSequence codeSeq;
jaroslav@201
    40
    private static Invocable code;
jaroslav@201
    41
jaroslav@201
    42
    @BeforeClass
jaroslav@201
    43
    public void compileTheCode() throws Exception {
jaroslav@201
    44
        StringBuilder sb = new StringBuilder();
jaroslav@277
    45
        sb.append("\nvar data = {};");
jaroslav@277
    46
        sb.append("\nfunction test(clazz, method) {");
jaroslav@277
    47
        sb.append("\n  if (!data.bck2brwsr) data.bck2brwsr = bck2brwsr(function(name) { return loader.get(name); });");
jaroslav@277
    48
        sb.append("\n  var c = data.bck2brwsr.loadClass(clazz);");
jaroslav@201
    49
        sb.append("\n  return c[method]();");
jaroslav@201
    50
        sb.append("\n}");
jaroslav@201
    51
        
jaroslav@201
    52
        
jaroslav@277
    53
       
jaroslav@201
    54
        ScriptEngine[] arr = { null };
jaroslav@201
    55
        code = StaticMethodTest.compileClass(sb, arr,
jaroslav@277
    56
            "org/apidesign/vm4brwsr/GenJS"
jaroslav@201
    57
        );
jaroslav@201
    58
        arr[0].getContext().setAttribute("loader", new FindBytes(), ScriptContext.ENGINE_SCOPE);
jaroslav@201
    59
        codeSeq = sb;
jaroslav@201
    60
    }
jaroslav@201
    61
    
jaroslav@201
    62
    @Test public void invokeStaticMethod() throws Exception {
jaroslav@201
    63
        assertExec("Trying to get -1", "test", Double.valueOf(-1),
jaroslav@277
    64
            StaticMethod.class.getName(), "minusOne__I"
jaroslav@201
    65
        );
jaroslav@201
    66
    }
jaroslav@214
    67
jaroslav@214
    68
    @Test public void loadDependantClass() throws Exception {
jaroslav@277
    69
        assertExec("Expecting zero", "test", Double.valueOf(0),
jaroslav@277
    70
            InstanceSub.class.getName(), "recallDbl__D"
jaroslav@214
    71
        );
jaroslav@214
    72
    }
jaroslav@201
    73
jaroslav@201
    74
    private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
jaroslav@201
    75
        Object ret = null;
jaroslav@201
    76
        try {
jaroslav@201
    77
            ret = code.invokeFunction(methodName, args);
jaroslav@201
    78
        } catch (ScriptException ex) {
jaroslav@201
    79
            fail("Execution failed in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
jaroslav@201
    80
        } catch (NoSuchMethodException ex) {
jaroslav@201
    81
            fail("Cannot find method in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
jaroslav@201
    82
        }
jaroslav@201
    83
        if (ret == null && expRes == null) {
jaroslav@201
    84
            return;
jaroslav@201
    85
        }
jaroslav@201
    86
        if (expRes.equals(ret)) {
jaroslav@201
    87
            return;
jaroslav@201
    88
        }
jaroslav@215
    89
        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + StaticMethodTest.dumpJS(codeSeq));
jaroslav@201
    90
    }
jaroslav@201
    91
jaroslav@201
    92
    public static final class FindBytes {
jaroslav@277
    93
        private static Set<String> requested = new TreeSet<String>();
jaroslav@277
    94
        
jaroslav@201
    95
        public byte[] get(String name) throws IOException {
jaroslav@277
    96
            if (!requested.add(name)) {
jaroslav@277
    97
                throw new IllegalStateException("Requested for second time: " + name);
jaroslav@277
    98
            }
jaroslav@277
    99
            
jaroslav@201
   100
            InputStream is = VMLazyTest.class.getClassLoader().getResourceAsStream(name);
jaroslav@201
   101
            if (is == null) {
jaroslav@201
   102
                throw new IOException("Can't find " + name);
jaroslav@201
   103
            }
jaroslav@201
   104
            byte[] arr = new byte[is.available()];
jaroslav@201
   105
            int len = is.read(arr);
jaroslav@201
   106
            if (len != arr.length) {
jaroslav@201
   107
                throw new IOException("Read only " + len + " wanting " + arr.length);
jaroslav@201
   108
            }
jaroslav@215
   109
            /*
jaroslav@201
   110
            System.err.print("loader['" + name + "'] = [");
jaroslav@201
   111
            for (int i = 0; i < arr.length; i++) {
jaroslav@201
   112
                if (i > 0) {
jaroslav@201
   113
                    System.err.print(", ");
jaroslav@201
   114
                }
jaroslav@201
   115
                System.err.print(arr[i]);
jaroslav@201
   116
            }
jaroslav@201
   117
            System.err.println("]");
jaroslav@215
   118
            */
jaroslav@201
   119
            return arr;
jaroslav@201
   120
        }
jaroslav@201
   121
    }
jaroslav@201
   122
}