Moving the read method closer to the definition of the script launcher
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 20 Dec 2012 21:54:33 +0100
branchlauncher
changeset 36086f3ea771e24
parent 359 67fef1fda667
child 361 98eb1066dab1
Moving the read method closer to the definition of the script
launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java
launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java
vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java
     1.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Thu Dec 20 21:53:58 2012 +0100
     1.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Thu Dec 20 21:54:33 2012 +0100
     1.3 @@ -130,7 +130,7 @@
     1.4          ScriptEngine mach = sem.getEngineByExtension(sen);
     1.5  
     1.6          sb.append(
     1.7 -              "\nvar vm = bck2brwsr(org.apidesign.bck2brwsr.vmtest.VMTest.read);"
     1.8 +              "\nvar vm = new bck2brwsr(org.apidesign.bck2brwsr.launcher.Console.read);"
     1.9              + "\nfunction initVM() { return vm; };"
    1.10              + "\n");
    1.11  
     2.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java	Thu Dec 20 21:53:58 2012 +0100
     2.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java	Thu Dec 20 21:54:33 2012 +0100
     2.3 @@ -17,9 +17,12 @@
     2.4   */
     2.5  package org.apidesign.bck2brwsr.launcher;
     2.6  
     2.7 +import java.io.IOException;
     2.8 +import java.io.InputStream;
     2.9  import java.lang.reflect.InvocationTargetException;
    2.10  import java.lang.reflect.Method;
    2.11  import java.net.URL;
    2.12 +import java.util.Enumeration;
    2.13  import org.apidesign.bck2brwsr.core.JavaScriptBody;
    2.14  
    2.15  /**
    2.16 @@ -89,6 +92,37 @@
    2.17          return r == null ? "null" : r.toString().toString();
    2.18      }
    2.19  
    2.20 +    /** Helper method that inspects the classpath and loads given resource
    2.21 +     * (usually a class file). Used while running tests in Rhino.
    2.22 +     * 
    2.23 +     * @param name resource name to find
    2.24 +     * @return the array of bytes in the given resource
    2.25 +     * @throws IOException I/O in case something goes wrong
    2.26 +     */
    2.27 +    public static byte[] read(String name) throws IOException {
    2.28 +        URL u = null;
    2.29 +        Enumeration<URL> en = Console.class.getClassLoader().getResources(name);
    2.30 +        while (en.hasMoreElements()) {
    2.31 +            u = en.nextElement();
    2.32 +        }
    2.33 +        if (u == null) {
    2.34 +            throw new IOException("Can't find " + name);
    2.35 +        }
    2.36 +        try (InputStream is = u.openStream()) {
    2.37 +            byte[] arr;
    2.38 +            arr = new byte[is.available()];
    2.39 +            int offset = 0;
    2.40 +            while (offset < arr.length) {
    2.41 +                int len = is.read(arr, offset, arr.length - offset);
    2.42 +                if (len == -1) {
    2.43 +                    throw new IOException("Can't read " + name);
    2.44 +                }
    2.45 +                offset += len;
    2.46 +            }
    2.47 +            return arr;
    2.48 +        }
    2.49 +    }
    2.50 +   
    2.51      private static Object invokeMethod(String clazz, String method) 
    2.52      throws ClassNotFoundException, InvocationTargetException, 
    2.53      SecurityException, IllegalAccessException, IllegalArgumentException {
     3.1 --- a/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java	Thu Dec 20 21:53:58 2012 +0100
     3.2 +++ b/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java	Thu Dec 20 21:54:33 2012 +0100
     3.3 @@ -20,10 +20,7 @@
     3.4  import java.io.File;
     3.5  import java.io.FileWriter;
     3.6  import java.io.IOException;
     3.7 -import java.io.InputStream;
     3.8  import java.lang.reflect.Method;
     3.9 -import java.net.URL;
    3.10 -import java.util.Enumeration;
    3.11  import java.util.Map;
    3.12  import java.util.WeakHashMap;
    3.13  import java.util.logging.Level;
    3.14 @@ -111,37 +108,6 @@
    3.15          return m.getName() + "[Compare " + second.typeName() + "]";
    3.16      }
    3.17  
    3.18 -    /** Helper method that inspects the classpath and loads given resource
    3.19 -     * (usually a class file). Used while running tests in Rhino.
    3.20 -     * 
    3.21 -     * @param name resource name to find
    3.22 -     * @return the array of bytes in the given resource
    3.23 -     * @throws IOException I/O in case something goes wrong
    3.24 -     */
    3.25 -    public static byte[] read(String name) throws IOException {
    3.26 -        URL u = null;
    3.27 -        Enumeration<URL> en = VMTest.class.getClassLoader().getResources(name);
    3.28 -        while (en.hasMoreElements()) {
    3.29 -            u = en.nextElement();
    3.30 -        }
    3.31 -        if (u == null) {
    3.32 -            throw new IOException("Can't find " + name);
    3.33 -        }
    3.34 -        try (InputStream is = u.openStream()) {
    3.35 -            byte[] arr;
    3.36 -            arr = new byte[is.available()];
    3.37 -            int offset = 0;
    3.38 -            while (offset < arr.length) {
    3.39 -                int len = is.read(arr, offset, arr.length - offset);
    3.40 -                if (len == -1) {
    3.41 -                    throw new IOException("Can't read " + name);
    3.42 -                }
    3.43 -                offset += len;
    3.44 -            }
    3.45 -            return arr;
    3.46 -        }
    3.47 -    }
    3.48 -   
    3.49      public static final class Run implements ITest {
    3.50          private final Method m;
    3.51          private final int type;