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