Modifying the test to fail in bck2brwsr VM jdk8
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 09 Aug 2014 10:52:34 +0200
branchjdk8
changeset 16450101d10bd2e0
parent 1644 d02324a82d80
child 1646 c880a8a8803b
child 1647 5fb6acaa51b8
Modifying the test to fail in bck2brwsr VM
rt/vm/src/test/java/org/apidesign/vm4brwsr/InvokeDynamicTest.java
rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java
     1.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/InvokeDynamicTest.java	Sat Aug 09 10:42:22 2014 +0200
     1.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/InvokeDynamicTest.java	Sat Aug 09 10:52:34 2014 +0200
     1.3 @@ -17,11 +17,15 @@
     1.4   */
     1.5  package org.apidesign.vm4brwsr;
     1.6  
     1.7 +import java.io.ByteArrayInputStream;
     1.8 +import java.io.IOException;
     1.9  import java.io.InputStream;
    1.10  import java.lang.invoke.CallSite;
    1.11  import java.lang.invoke.MethodHandles;
    1.12  import java.lang.invoke.MethodType;
    1.13  import java.lang.reflect.Method;
    1.14 +import java.net.URL;
    1.15 +import java.util.Enumeration;
    1.16  import org.objectweb.asm.ClassReader;
    1.17  import org.objectweb.asm.ClassVisitor;
    1.18  import org.objectweb.asm.ClassWriter;
    1.19 @@ -31,6 +35,7 @@
    1.20  import static org.objectweb.asm.Opcodes.ASM4;
    1.21  import static org.objectweb.asm.Opcodes.INVOKESTATIC;
    1.22  import static org.testng.Assert.*;
    1.23 +import org.testng.annotations.AfterClass;
    1.24  import org.testng.annotations.BeforeClass;
    1.25  import org.testng.annotations.Test;
    1.26  
    1.27 @@ -40,6 +45,8 @@
    1.28   */
    1.29  public class InvokeDynamicTest {
    1.30      private static Class<?> invokeDynamicClass;
    1.31 +    private static byte[] invokeDynamicBytes;
    1.32 +    private static TestVM code;
    1.33      
    1.34      @Test public void simpleDynamicInJava() throws Exception {
    1.35          Method m = invokeDynamicClass.getMethod("dynamicSay");
    1.36 @@ -47,27 +54,20 @@
    1.37          assertEquals(ret, "Hello from Dynamic!");
    1.38      }
    1.39      
    1.40 +    @Test public void simpleDynamicInJS() throws Exception {
    1.41 +        code.assertExec(
    1.42 +            "Invoke dynamic can return a value", InvokeDynamic.class,
    1.43 +            "dynamic__Ljava_lang_String_2",
    1.44 +            "Hello from Dynamic!"
    1.45 +        );
    1.46 +    }
    1.47      
    1.48 -/*    
    1.49 -    private static TestVM code;
    1.50 -
    1.51 -    @BeforeClass
    1.52 -    public void compileTheCode() throws Exception {
    1.53 -        code = TestVM.compileClass(InvokeDynamic.class.getName());
    1.54 -    }
    1.55  
    1.56      @AfterClass
    1.57      public static void releaseTheCode() {
    1.58          code = null;
    1.59      }
    1.60  
    1.61 -    private void assertExec(
    1.62 -            String msg, Class clazz, String method, Object expRes, Object... args
    1.63 -    ) throws Exception {
    1.64 -        code.assertExec(msg, clazz, method, expRes, args);
    1.65 -    }
    1.66 - */
    1.67 -
    1.68      //
    1.69      // the following code is inspired by 
    1.70      // https://code.google.com/p/indy-maven-plugin/
    1.71 @@ -75,10 +75,10 @@
    1.72      //
    1.73      @BeforeClass 
    1.74      public static void prepareClass() throws Exception {
    1.75 -        InputStream input = InvokeDynamic.class.getResourceAsStream("InvokeDynamic.class");
    1.76 -        assertNotNull(input, "Class found");
    1.77 +        InputStream is = InvokeDynamic.class.getResourceAsStream("InvokeDynamic.class");
    1.78 +        assertNotNull(is, "Class found");
    1.79          
    1.80 -        ClassReader reader = new ClassReader(input);
    1.81 +        ClassReader reader = new ClassReader(is);
    1.82          ClassWriter writer = new ClassWriter(reader, 0);
    1.83  
    1.84          reader.accept(
    1.85 @@ -91,8 +91,8 @@
    1.86                      }
    1.87                  },
    1.88                  0);
    1.89 -        input.close();
    1.90 -        final byte[] invokeDynamicBytes = writer.toByteArray();
    1.91 +        is.close();
    1.92 +        invokeDynamicBytes = writer.toByteArray();
    1.93          ClassLoader l = new ClassLoader() {
    1.94              @Override
    1.95              public Class<?> loadClass(String name) throws ClassNotFoundException {
    1.96 @@ -103,6 +103,11 @@
    1.97              }
    1.98          };
    1.99          invokeDynamicClass = l.loadClass(InvokeDynamic.class.getName());
   1.100 +        
   1.101 +        code = TestVM.compileClass(
   1.102 +            null, null, new EmulationResourcesWithException(),
   1.103 +            InvokeDynamic.class.getName().replace('.', '/')
   1.104 +        );
   1.105      }
   1.106      
   1.107      
   1.108 @@ -133,5 +138,37 @@
   1.109          }
   1.110      }
   1.111      
   1.112 +    private static class EmulationResourcesWithException implements Bck2Brwsr.Resources {
   1.113 +        @Override
   1.114 +        public InputStream get(String name) throws IOException {
   1.115 +            if ("org/apidesign/vm4brwsr/InvokeDynamic.class".equals(name)) {
   1.116 +                return new ByteArrayInputStream(invokeDynamicBytes);
   1.117 +            }
   1.118 +            if ("java/net/URI.class".equals(name)) {
   1.119 +                // skip
   1.120 +                return null;
   1.121 +            }
   1.122 +            if ("java/net/URLConnection.class".equals(name)) {
   1.123 +                // skip
   1.124 +                return null;
   1.125 +            }
   1.126 +            if ("java/lang/System.class".equals(name)) {
   1.127 +                // skip
   1.128 +                return null;
   1.129 +            }
   1.130 +            Enumeration<URL> en = InvokeDynamicTest.class.getClassLoader().getResources(name);
   1.131 +            URL u = null;
   1.132 +            while (en.hasMoreElements()) {
   1.133 +                u = en.nextElement();
   1.134 +            }
   1.135 +            if (u == null) {
   1.136 +                throw new IOException("Can't find " + name);
   1.137 +            }
   1.138 +            if (u.toExternalForm().contains("rt.jar!")) {
   1.139 +                throw new IOException("No emulation for " + u);
   1.140 +            }
   1.141 +            return u.openStream();
   1.142 +        }
   1.143 +    }
   1.144      
   1.145  }
     2.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java	Sat Aug 09 10:42:22 2014 +0200
     2.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java	Sat Aug 09 10:52:34 2014 +0200
     2.3 @@ -123,10 +123,18 @@
     2.4      }
     2.5  
     2.6      static TestVM compileClass(StringBuilder sb, ScriptEngine[] eng, String... names) throws ScriptException, IOException {
     2.7 +        return compileClass(sb, eng, new EmulationResources(), names);
     2.8 +    }
     2.9 +    static TestVM compileClass(
    2.10 +        StringBuilder sb, 
    2.11 +        ScriptEngine[] eng, 
    2.12 +        Bck2Brwsr.Resources resources, 
    2.13 +        String... names
    2.14 +    ) throws ScriptException, IOException {
    2.15          if (sb == null) {
    2.16              sb = new StringBuilder();
    2.17          }
    2.18 -        Bck2Brwsr.generate(sb, new EmulationResources(), names);
    2.19 +        Bck2Brwsr.generate(sb, resources, names);
    2.20          ScriptEngineManager sem = new ScriptEngineManager();
    2.21          ScriptEngine js = sem.getEngineByExtension("js");
    2.22          if (eng != null) {