vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 25 Nov 2012 21:31:51 +0100
branchlazy
changeset 202 d059e6eccb80
parent 201 f180d72cc7a4
child 207 8247ed0e8ef5
permissions -rw-r--r--
More debug info
     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 javax.script.Invocable;
    23 import javax.script.ScriptContext;
    24 import javax.script.ScriptEngine;
    25 import javax.script.ScriptException;
    26 import org.testng.annotations.BeforeClass;
    27 import static org.testng.Assert.*;
    28 import org.testng.annotations.Test;
    29 
    30 /** Implements loading class by class.
    31  *
    32  * @author Jaroslav Tulach <jtulach@netbeans.org>
    33  */
    34 public class VMLazyTest {
    35 
    36     
    37     private static CharSequence codeSeq;
    38     private static Invocable code;
    39 
    40     @BeforeClass
    41     public void compileTheCode() throws Exception {
    42         StringBuilder sb = new StringBuilder();
    43         
    44         sb.append("\nfunction test(clazz, as, method) {");
    45         sb.append("\n  var l = new lazyVM();");
    46         sb.append("\n  var c = l.loadClass(clazz, as);");
    47         sb.append("\n  return c[method]();");
    48         sb.append("\n}");
    49         
    50         
    51         sb.append("\nfunction lazyVM() {");
    52         sb.append("\n  var self = this;");
    53         sb.append("\n  this.constructor.prototype.Identity = function(value) {");
    54         sb.append("\n    var self = this;");
    55         sb.append("\n    self.value = value;");
    56         sb.append("\n    self.call = function() { return self.value; };");
    57         sb.append("\n  };");
    58         sb.append("\n");
    59         sb.append("\n  this.constructor.prototype.loadClass = function(res, name) {");
    60         sb.append("\n    var script = org_apidesign_vm4brwsr_VMLazy_toJavaScriptLjava_lang_StringAB(loader.get(res + '.class'));");
    61         sb.append("\n    try {");
    62         sb.append("\n      new Function(");
    63         sb.append("\n        'arguments[0][arguments[1]]=new lazyVM.prototype.Identity(' + script + ').call'");
    64         sb.append("\n      )(self, name);");
    65         sb.append("\n    } catch (ex) {");
    66         sb.append("\n      throw 'Cannot compile ' + res + ' error: ' + ex + ' script:\\n' + script;");
    67         sb.append("\n    };");
    68         sb.append("\n  };");
    69         sb.append("\n");
    70         sb.append("\n}\n");
    71         
    72         ScriptEngine[] arr = { null };
    73         code = StaticMethodTest.compileClass(sb, arr,
    74             "org/apidesign/vm4brwsr/VMLazy"
    75         );
    76         arr[0].getContext().setAttribute("loader", new FindBytes(), ScriptContext.ENGINE_SCOPE);
    77         codeSeq = sb;
    78     }
    79     
    80     @Test public void invokeStaticMethod() throws Exception {
    81         assertExec("Trying to get -1", "test", Double.valueOf(-1),
    82             "org/apidesign/vm4brwsr/StaticMethod", "org_apidesign_vm4brwsr_StaticMethod", "minusOne"
    83         );
    84     }
    85     
    86 
    87     private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
    88         Object ret = null;
    89         try {
    90             ret = code.invokeFunction(methodName, args);
    91         } catch (ScriptException ex) {
    92             fail("Execution failed in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
    93         } catch (NoSuchMethodException ex) {
    94             fail("Cannot find method in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
    95         }
    96         if (ret == null && expRes == null) {
    97             return;
    98         }
    99         if (expRes.equals(ret)) {
   100             return;
   101         }
   102         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
   103     }
   104 
   105     public static final class FindBytes {
   106         public byte[] get(String name) throws IOException {
   107             InputStream is = VMLazyTest.class.getClassLoader().getResourceAsStream(name);
   108             if (is == null) {
   109                 throw new IOException("Can't find " + name);
   110             }
   111             byte[] arr = new byte[is.available()];
   112             int len = is.read(arr);
   113             if (len != arr.length) {
   114                 throw new IOException("Read only " + len + " wanting " + arr.length);
   115             }
   116             System.err.print("loader['" + name + "'] = [");
   117             for (int i = 0; i < arr.length; i++) {
   118                 if (i > 0) {
   119                     System.err.print(", ");
   120                 }
   121                 System.err.print(arr[i]);
   122             }
   123             System.err.println("]");
   124             return arr;
   125         }
   126     }
   127 }