rt/vm/src/test/java/org/apidesign/vm4brwsr/InvokeDynamicTest.java
branchjdk8
changeset 1645 0101d10bd2e0
parent 1644 d02324a82d80
     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  }