changeset 7631:aa40e0a648e7

Tests for 'MethodHandle incorrectly reports as invokespecial' bug
author briangoetz
date Wed, 13 Mar 2013 18:30:29 -0400
parents 1749ca6a6b76
children a657ad5273ba
files test-ng/tests/lambda/InvokespecialMethodKindTest.java test-ng/tests/lambda/TestInnerCtorRef.java test-ng/tests/lambda/TestPrivateCtorRef.java test-ng/tests/org/openjdk/tests/javac/ArrayCtorRefTest.java
diffstat 4 files changed, 83 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-ng/tests/lambda/InvokespecialMethodKindTest.java	Wed Mar 13 18:30:29 2013 -0400
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package lambda;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandleInfo;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+import java.util.function.Function;
+
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertEquals;
+
+/**
+ * InvokespecialMethodKindTest
+ */
+@Test
+public class InvokespecialMethodKindTest {
+    public void testStringToUpper() throws ReflectiveOperationException {
+        MethodHandle mh = MethodHandles.lookup().findVirtual(String.class, "toUpperCase",
+                                                             MethodType.methodType(String.class));
+        MethodHandleInfo mhi = new MethodHandleInfo(mh);
+        assertEquals(mhi.getReferenceKind(), MethodHandleInfo.REF_invokeVirtual);
+    }
+
+    public void testSerializeStringToUpper() throws IOException, ClassNotFoundException {
+        Function<String, String> f = (Function<String, String> & Serializable) String::toUpperCase;
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        ObjectOutputStream oos = new ObjectOutputStream(bos);
+        oos.writeObject(f);
+        oos.flush();
+        byte[] bs = bos.toByteArray();
+        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bs));
+        @SuppressWarnings("unchecked")
+        Function<String, String> ff = (Function<String, String>) ois.readObject();
+        assertEquals("FOO", ff.apply("fOo"));
+    }
+}
--- a/test-ng/tests/lambda/TestInnerCtorRef.java	Tue Feb 26 11:05:26 2013 +0000
+++ b/test-ng/tests/lambda/TestInnerCtorRef.java	Wed Mar 13 18:30:29 2013 -0400
@@ -30,8 +30,6 @@
 
 /**
  * TestInnerCtorRef
- *
- * @author Brian Goetz
  */
 // @Test
 public class TestInnerCtorRef {
--- a/test-ng/tests/lambda/TestPrivateCtorRef.java	Tue Feb 26 11:05:26 2013 +0000
+++ b/test-ng/tests/lambda/TestPrivateCtorRef.java	Wed Mar 13 18:30:29 2013 -0400
@@ -31,10 +31,8 @@
 
 /**
  * TestPrivateCtorRef
- *
- * @author Brian Goetz
  */
-// @Test
+@Test
 public class TestPrivateCtorRef {
     // @@@ Really, this needs to be a combo test:
     //     target class = nested static, nested instance, auxilliary
--- a/test-ng/tests/org/openjdk/tests/javac/ArrayCtorRefTest.java	Tue Feb 26 11:05:26 2013 +0000
+++ b/test-ng/tests/org/openjdk/tests/javac/ArrayCtorRefTest.java	Wed Mar 13 18:30:29 2013 -0400
@@ -26,6 +26,7 @@
 
 import org.testng.annotations.Test;
 
+import java.util.function.IntFunction;
 import java.util.function.Supplier;
 
 import static org.testng.Assert.assertTrue;
@@ -53,12 +54,12 @@
         assertTrue(arr.length == 3);
     }
 
-//    public void testIntCtorRef() {
-//        TIFunction<int[]> factory = int[]::new;
-//        int[] arr = factory.apply(6);
-//        assertTrue(arr.length == 6);
-//    }
-//
+    public void testIntCtorRef() {
+        IntFunction<int[]> factory = int[]::new;
+        int[] arr = factory.apply(6);
+        assertTrue(arr.length == 6);
+    }
+
     public void testLambdaInference() {
         Supplier<Object[]> oF = emptyArrayFactory(i -> new Object[i]);
         Supplier<String[]> sF = emptyArrayFactory(i -> new String[i]);
@@ -66,12 +67,11 @@
         assertTrue(sF.get() instanceof String[]);
     }
 
-    // @@@ Uncomment when compiler support for array ctor refs is turned on
-//    public void testCtorRef() {
-//        ArrayMaker<String> am = String[]::new;
-//        String[] arr = am.make(3);
-//        arr[0] = "Foo";
-//        assertTrue(arr instanceof String[]);
-//        assertTrue(arr.length == 3);
-//    }
+    public void testCtorRef() {
+        ArrayMaker<String> am = String[]::new;
+        String[] arr = am.make(3);
+        arr[0] = "Foo";
+        assertTrue(arr instanceof String[]);
+        assertTrue(arr.length == 3);
+    }
 }