Testing loading of resource from an extension and preparing for loading them from multiple extensions closure
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 28 Apr 2014 20:23:44 +0200
branchclosure
changeset 1497daa5f903b529
parent 1496 d3df935aff70
child 1498 9583bcc50db3
Testing loading of resource from an extension and preparing for loading them from multiple extensions
rt/vm/src/test/java/org/apidesign/vm4brwsr/NumbersAsExtensionTest.java
rt/vm/src/test/java/org/apidesign/vm4brwsr/Resources.java
rt/vm/src/test/java/org/apidesign/vm4brwsr/ResourcesWithExtensionsTest.java
rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java
     1.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/NumbersAsExtensionTest.java	Mon Apr 28 17:31:29 2014 +0200
     1.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/NumbersAsExtensionTest.java	Mon Apr 28 20:23:44 2014 +0200
     1.3 @@ -17,6 +17,7 @@
     1.4   */
     1.5  package org.apidesign.vm4brwsr;
     1.6  
     1.7 +import javax.script.ScriptEngine;
     1.8  import static org.testng.Assert.*;
     1.9  import org.testng.annotations.AfterClass;
    1.10  import org.testng.annotations.BeforeClass;
    1.11 @@ -209,7 +210,8 @@
    1.12  
    1.13      @BeforeClass
    1.14      public static void compileTheCode() throws Exception {
    1.15 -        code = TestVM.compileClassAsExtension(null, null, "org/apidesign/vm4brwsr/Numbers");
    1.16 +        ScriptEngine[] eng = { null };
    1.17 +        code = TestVM.compileClassAsExtension(null, eng, "org/apidesign/vm4brwsr/Numbers", null, null);
    1.18      }
    1.19      @AfterClass
    1.20      public static void releaseTheCode() {
     2.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/Resources.java	Mon Apr 28 17:31:29 2014 +0200
     2.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/Resources.java	Mon Apr 28 20:23:44 2014 +0200
     2.3 @@ -19,6 +19,8 @@
     2.4  
     2.5  import java.io.IOException;
     2.6  import java.io.InputStream;
     2.7 +import java.net.URL;
     2.8 +import java.util.Enumeration;
     2.9  
    2.10  /**
    2.11   *
    2.12 @@ -27,26 +29,26 @@
    2.13  public class Resources {
    2.14      public static String loadKO() throws IOException {
    2.15          InputStream is = Resources.class.getResourceAsStream("ko.js");
    2.16 +        return readIS(is, false);
    2.17 +    }
    2.18 +    
    2.19 +    static String loadClazz() throws IOException {
    2.20 +        InputStream is = Resources.class.getResourceAsStream("Bck2BrwsrToolkit.class");
    2.21 +        return readIS(is, false);
    2.22 +    }
    2.23 +
    2.24 +    private static String readIS(InputStream is, boolean asString) throws IOException {
    2.25          if (is == null) {
    2.26              return "No resource found!";
    2.27          }
    2.28          byte[] arr = new byte[4092];
    2.29          int len = is.read(arr);
    2.30 -        if (len == -1) {
    2.31 -            return "No data read!";
    2.32 +        if (len < 5) {
    2.33 +            return "No data read! Len: " + len;
    2.34          }
    2.35 -        final Object str = new String(arr, 0, len, "UTF-8");
    2.36 -        return str.toString();
    2.37 -    }
    2.38 -    static String loadClazz() throws IOException {
    2.39 -        InputStream is = Resources.class.getResourceAsStream("Bck2BrwsrToolkit.class");
    2.40 -        if (is == null) {
    2.41 -            return "No resource found!";
    2.42 -        }
    2.43 -        byte[] arr = new byte[4092];
    2.44 -        int len = is.read(arr);
    2.45 -        if (len < 10) {
    2.46 -            return "No data read! Len: " + len;
    2.47 +        
    2.48 +        if (asString) {
    2.49 +            return new String(arr, 0, len, "UTF-8").toString().toString();
    2.50          }
    2.51          
    2.52          StringBuilder sb = new StringBuilder();
    2.53 @@ -57,4 +59,25 @@
    2.54          
    2.55          return sb.toString().toString();
    2.56      }
    2.57 +
    2.58 +    static String loadHello() throws IOException {
    2.59 +        Enumeration<URL> en;
    2.60 +        try {
    2.61 +            en = Resources.class.getClassLoader().getResources("META-INF/ahoj");
    2.62 +        } catch (SecurityException ex) {
    2.63 +            return "SecurityException";
    2.64 +        }
    2.65 +        StringBuilder sb = new StringBuilder();
    2.66 +        while (en.hasMoreElements()) {
    2.67 +            URL url = en.nextElement();
    2.68 +            sb.append(readIS(url.openStream(), true));
    2.69 +        }
    2.70 +        return sb.toString().toString();
    2.71 +    }
    2.72 +    static String loadJustHello() throws IOException {
    2.73 +        URL url = Resources.class.getResource("/META-INF/ahoj");
    2.74 +        StringBuilder sb = new StringBuilder();
    2.75 +        sb.append(readIS(url.openStream(), true));
    2.76 +        return sb.toString().toString();
    2.77 +    }
    2.78  }
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/ResourcesWithExtensionsTest.java	Mon Apr 28 20:23:44 2014 +0200
     3.3 @@ -0,0 +1,86 @@
     3.4 +/**
     3.5 + * Back 2 Browser Bytecode Translator
     3.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, version 2 of the License.
    3.11 + *
    3.12 + * This program is distributed in the hope that it will be useful,
    3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.15 + * GNU General Public License for more details.
    3.16 + *
    3.17 + * You should have received a copy of the GNU General Public License
    3.18 + * along with this program. Look for COPYING file in the top folder.
    3.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    3.20 + */
    3.21 +package org.apidesign.vm4brwsr;
    3.22 +
    3.23 +import java.io.UnsupportedEncodingException;
    3.24 +import javax.script.ScriptEngine;
    3.25 +import org.testng.annotations.AfterClass;
    3.26 +import org.testng.annotations.BeforeClass;
    3.27 +import org.testng.annotations.Test;
    3.28 +
    3.29 +/** Tests related to loading resources from the VM.
    3.30 + *
    3.31 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.32 + */
    3.33 +public class ResourcesWithExtensionsTest {
    3.34 +    @Test public void checkHello() throws Exception {
    3.35 +        String exp = "Hello ";
    3.36 +        
    3.37 +        assertExec("Loading a precompiled resource:",
    3.38 +            Resources.class, "loadJustHello__Ljava_lang_String_2", 
    3.39 +            exp
    3.40 +        );
    3.41 +    }
    3.42 +    /*
    3.43 +    @Test public void checkHelloWorld() throws Exception {
    3.44 +        String exp = "Hello World!";
    3.45 +        
    3.46 +        assertExec("Loading precompiled resources:",
    3.47 +            Resources.class, "loadHello__Ljava_lang_String_2", 
    3.48 +            exp
    3.49 +        );
    3.50 +    }
    3.51 +    */
    3.52 +    
    3.53 +    private static TestVM code;
    3.54 +    
    3.55 +    @BeforeClass 
    3.56 +    public static void compileTheCode() throws Exception {
    3.57 +        StringBuilder sb = new StringBuilder();
    3.58 +        ScriptEngine[] eng = { null };
    3.59 +        code = TestVM.compileClassAsExtension(sb, eng, 
    3.60 +            "org/apidesign/vm4brwsr/Resources", 
    3.61 +            "META-INF/ahoj", "Hello "
    3.62 +        );
    3.63 +        code = TestVM.compileClassAsExtension(sb, eng, 
    3.64 +            "org/apidesign/vm4brwsr/Resources", 
    3.65 +            "META-INF/ahoj", "World!"
    3.66 +        );
    3.67 +    }
    3.68 +    @AfterClass
    3.69 +    public static void releaseTheCode() {
    3.70 +        code = null;
    3.71 +    }
    3.72 +
    3.73 +    private void assertExec(
    3.74 +        String msg, Class<?> clazz, String method, 
    3.75 +        Object ret, Object... args
    3.76 +    ) throws Exception {
    3.77 +        code.assertExec(msg, clazz, method, ret, args);
    3.78 +    }
    3.79 +    
    3.80 +    public static String parseBase64Binary(String s) throws UnsupportedEncodingException {
    3.81 +        final byte[] arr = javax.xml.bind.DatatypeConverter.parseBase64Binary(s);
    3.82 +        StringBuilder sb = new StringBuilder();
    3.83 +        for (int i = 0; i < arr.length; i++) {
    3.84 +            int ch = arr[i];
    3.85 +            sb.append((char)ch);
    3.86 +        }
    3.87 +        return sb.toString();
    3.88 +    }
    3.89 +}
     4.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java	Mon Apr 28 17:31:29 2014 +0200
     4.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java	Mon Apr 28 20:23:44 2014 +0200
     4.3 @@ -17,6 +17,7 @@
     4.4   */
     4.5  package org.apidesign.vm4brwsr;
     4.6  
     4.7 +import java.io.ByteArrayInputStream;
     4.8  import java.io.File;
     4.9  import java.io.FileWriter;
    4.10  import java.io.IOException;
    4.11 @@ -126,24 +127,39 @@
    4.12          }
    4.13      }
    4.14      
    4.15 -    static TestVM compileClassAsExtension(StringBuilder sb, ScriptEngine[] eng, String... names) throws ScriptException, IOException {
    4.16 +    static TestVM compileClassAsExtension(
    4.17 +        StringBuilder sb, ScriptEngine[] eng, 
    4.18 +        String name, final String resourceName, final String resourceContent
    4.19 +    ) throws ScriptException, IOException {
    4.20          if (sb == null) {
    4.21              sb = new StringBuilder();
    4.22          }
    4.23 -        Bck2Brwsr.generate(sb, new EmulationResources());
    4.24 +        if (eng[0] == null) {
    4.25 +            ScriptEngineManager sem = new ScriptEngineManager();
    4.26 +            ScriptEngine js = sem.getEngineByExtension("js");
    4.27 +            eng[0] = js;
    4.28 +            Bck2Brwsr.generate(sb, new EmulationResources());
    4.29 +        }
    4.30          Bck2Brwsr b2b = Bck2Brwsr.newCompiler().
    4.31 -            resources(new EmulationResources()).
    4.32 -            addRootClasses(names).library(true);
    4.33 +            resources(new EmulationResources() {
    4.34 +                @Override
    4.35 +                public InputStream get(String name) throws IOException {
    4.36 +                    if (name.equals(resourceName)) {
    4.37 +                        return new ByteArrayInputStream(resourceContent.getBytes("UTF-8"));
    4.38 +                    }
    4.39 +                    return super.get(name);
    4.40 +                }
    4.41 +            }).
    4.42 +            addRootClasses(name).library(true);
    4.43 +        if (resourceName != null) {
    4.44 +            b2b = b2b.addResources(resourceName);
    4.45 +        }
    4.46          b2b.generate(sb);
    4.47 -        ScriptEngineManager sem = new ScriptEngineManager();
    4.48 -        ScriptEngine js = sem.getEngineByExtension("js");
    4.49 -        if (eng != null) {
    4.50 -            eng[0] = js;
    4.51 -        }
    4.52          try {
    4.53 -            Object res = js.eval(sb.toString());
    4.54 -            assertTrue(js instanceof Invocable, "It is invocable object: " + res);
    4.55 -            return new TestVM((Invocable) js, sb);
    4.56 +            defineAtoB(eng[0]);
    4.57 +            Object res = eng[0].eval(sb.toString());
    4.58 +            assertTrue(eng[0] instanceof Invocable, "It is invocable object: " + res);
    4.59 +            return new TestVM((Invocable) eng[0], sb);
    4.60          } catch (Exception ex) {
    4.61              if (sb.length() > 2000) {
    4.62                  sb = dumpJS(sb);
    4.63 @@ -169,10 +185,7 @@
    4.64              eng[0] = js;
    4.65          }
    4.66          try {
    4.67 -            js.eval("atob = function(s) { return org.apidesign.vm4brwsr.ResourcesTest.parseBase64Binary(s); }");
    4.68 -            
    4.69 -            Object ahoj = js.eval("atob('QWhvag==')");
    4.70 -            assertEquals(ahoj, "Ahoj", "Verify the atob function behaves properly");
    4.71 +            defineAtoB(js);
    4.72              
    4.73              Object res = js.eval(sb.toString());
    4.74              assertTrue(js instanceof Invocable, "It is invocable object: " + res);
    4.75 @@ -186,6 +199,13 @@
    4.76          }
    4.77      }
    4.78  
    4.79 +    private static void defineAtoB(ScriptEngine js) throws ScriptException {
    4.80 +        js.eval("atob = function(s) { return org.apidesign.vm4brwsr.ResourcesTest.parseBase64Binary(s); }");
    4.81 +        
    4.82 +        Object ahoj = js.eval("atob('QWhvag==')");
    4.83 +        assertEquals(ahoj, "Ahoj", "Verify the atob function behaves properly");
    4.84 +    }
    4.85 +
    4.86      Object loadClass(String loadClass, String name) throws ScriptException, NoSuchMethodException {
    4.87          return code.invokeMethod(bck2brwsr, "loadClass", Exceptions.class.getName());
    4.88      }