Thu, 21 Feb 2013 14:43:51 -0800
8008405: Now that metafactory is in place, add javac lambda serialization tests
Summary: Tests part of original langtools serialization review.
Reviewed-by: mcimadamore
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/T8004969.java Thu Feb 21 14:43:51 2013 -0800 1.3 @@ -0,0 +1,103 @@ 1.4 +/* 1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 +@test 1.29 +@bug 8004969 1.30 +@summary Lambda deserialization 1.31 + 1.32 +*/ 1.33 + 1.34 +import java.io.*; 1.35 + 1.36 +public class T8004969 implements Serializable { 1.37 + 1.38 + static int assertionCount = 0; 1.39 + 1.40 + static void assertTrue(boolean cond) { 1.41 + assertionCount++; 1.42 + if (!cond) 1.43 + throw new AssertionError(); 1.44 + } 1.45 + 1.46 + static String mm(String s) { return "mref" + s; } 1.47 + 1.48 + String aField = "aF"; 1.49 + 1.50 + public static void main(String[] args) throws Exception { 1.51 + (new T8004969()).doit(); 1.52 + } 1.53 + 1.54 + public void doit() throws Exception { 1.55 + String aLocal = "aL"; 1.56 + int anInt = 99; 1.57 + 1.58 + try { 1.59 + // Write lambdas out 1.60 + ByteArrayOutputStream baos = new ByteArrayOutputStream(); 1.61 + ObjectOutput out = new ObjectOutputStream(baos); 1.62 + 1.63 + write(out, z -> "[" + z + "]" ); 1.64 + write(out, z -> { String x = z + z; return x + x; } ); 1.65 + write(out, T8004969::mm ); 1.66 + write(out, z -> (new LSI() { public String convert(String x) { return "*"+x; }} ).convert(z) ); 1.67 + write(out, z -> aField + z ); 1.68 + write(out, z -> aLocal + z ); 1.69 + write(out, z -> z + anInt ); 1.70 + out.flush(); 1.71 + out.close(); 1.72 + 1.73 + // Read them back 1.74 + ByteArrayInputStream bais = 1.75 + new ByteArrayInputStream(baos.toByteArray()); 1.76 + ObjectInputStream in = new ObjectInputStream(bais); 1.77 + readAssert(in, "[X]"); 1.78 + readAssert(in, "XXXX"); 1.79 + readAssert(in, "mrefX"); 1.80 + readAssert(in, "*X"); 1.81 + readAssert(in, "aFX"); 1.82 + readAssert(in, "aLX"); 1.83 + readAssert(in, "X99"); 1.84 + in.close(); 1.85 + } catch (IOException e) { 1.86 + e.printStackTrace(); 1.87 + throw e; 1.88 + } 1.89 + assertTrue(assertionCount == 7); 1.90 + } 1.91 + 1.92 + static void write(ObjectOutput out, LSI lamb) throws IOException { 1.93 + out.writeObject(lamb); 1.94 + } 1.95 + 1.96 + static void readAssert(ObjectInputStream in, String expected) throws IOException, ClassNotFoundException { 1.97 + LSI ls = (LSI) in.readObject(); 1.98 + String result = ls.convert("X"); 1.99 + System.out.printf("Result: %s\n", result); 1.100 + assertTrue(result.equals(expected)); 1.101 + } 1.102 +} 1.103 + 1.104 +interface LSI extends Serializable { 1.105 + String convert(String x); 1.106 +}
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/lambda/LambdaInnerTypeVarArgsSerialize.java Thu Feb 21 14:43:51 2013 -0800 2.3 @@ -0,0 +1,80 @@ 2.4 +/* 2.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.23 + * or visit www.oracle.com if you need additional information or have any 2.24 + * questions. 2.25 + */ 2.26 + 2.27 +/* 2.28 + * @test 2.29 + * @bug 8005653 2.30 + * @summary A serialized lambda containing an inner class referencing an external type var in class parameter type 2.31 + * @author Robert Field 2.32 + * @run main LambdaInnerTypeVarArgsSerialize 2.33 + */ 2.34 + 2.35 +import java.io.Serializable; 2.36 +import java.util.List; 2.37 +import java.util.ArrayList; 2.38 + 2.39 +public class LambdaInnerTypeVarArgsSerialize { 2.40 + 2.41 + static int assertionCount = 0; 2.42 + 2.43 + static void assertTrue(boolean cond) { 2.44 + assertionCount++; 2.45 + if (!cond) 2.46 + throw new AssertionError(); 2.47 + } 2.48 + 2.49 + interface I extends Serializable { 2.50 + C doit(); 2.51 + } 2.52 + 2.53 + abstract class C { 2.54 + abstract Object it(); 2.55 + } 2.56 + 2.57 + class TV { 2.58 + C go() { 2.59 + List<String> ls = new ArrayList<>(); 2.60 + ls.add("Oh"); 2.61 + ls.add("my"); 2.62 + return foo(ls).doit(); 2.63 + } 2.64 + 2.65 + <RRRRR> I foo(List<RRRRR> r) { 2.66 + return () -> new C() { 2.67 + List<RRRRR> xxxxx = r; 2.68 + @Override 2.69 + Object it() { return xxxxx; }; 2.70 + }; 2.71 + } 2.72 + } 2.73 + 2.74 + void test1() { 2.75 + assertTrue(((List<String>)(new TV().go().it())).get(0).equals("Oh")); 2.76 + } 2.77 + 2.78 + public static void main(String[] args) { 2.79 + LambdaInnerTypeVarArgsSerialize t = new LambdaInnerTypeVarArgsSerialize(); 2.80 + t.test1(); 2.81 + assertTrue(assertionCount == 1); 2.82 + } 2.83 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/lambda/LambdaInnerTypeVarSerialize.java Thu Feb 21 14:43:51 2013 -0800 3.3 @@ -0,0 +1,75 @@ 3.4 +/* 3.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 + * 3.8 + * This code is free software; you can redistribute it and/or modify it 3.9 + * under the terms of the GNU General Public License version 2 only, as 3.10 + * published by the Free Software Foundation. 3.11 + * 3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.15 + * version 2 for more details (a copy is included in the LICENSE file that 3.16 + * accompanied this code). 3.17 + * 3.18 + * You should have received a copy of the GNU General Public License version 3.19 + * 2 along with this work; if not, write to the Free Software Foundation, 3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.21 + * 3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 3.23 + * or visit www.oracle.com if you need additional information or have any 3.24 + * questions. 3.25 + */ 3.26 + 3.27 +/* 3.28 + * @test 3.29 + * @bug 8005653 3.30 + * @summary A serialized lambda containing an inner class referencing an external type var 3.31 + * @author Robert Field 3.32 + * @run main LambdaInnerTypeVarSerialize 3.33 + */ 3.34 + 3.35 +import java.io.Serializable; 3.36 + 3.37 +public class LambdaInnerTypeVarSerialize { 3.38 + 3.39 + static int assertionCount = 0; 3.40 + 3.41 + static void assertTrue(boolean cond) { 3.42 + assertionCount++; 3.43 + if (!cond) 3.44 + throw new AssertionError(); 3.45 + } 3.46 + 3.47 + interface I extends Serializable { 3.48 + C doit(); 3.49 + } 3.50 + 3.51 + abstract class C { 3.52 + abstract Object it(); 3.53 + } 3.54 + 3.55 + class TV { 3.56 + C go() { 3.57 + return foo("Frump").doit(); 3.58 + } 3.59 + 3.60 + <RRRRR> I foo(RRRRR r) { 3.61 + return () -> new C() { 3.62 + RRRRR xxxxx = r; 3.63 + @Override 3.64 + Object it() { return xxxxx; }; 3.65 + }; 3.66 + } 3.67 + } 3.68 + 3.69 + void test1() { 3.70 + assertTrue(new TV().go().it().equals("Frump")); 3.71 + } 3.72 + 3.73 + public static void main(String[] args) { 3.74 + LambdaInnerTypeVarSerialize t = new LambdaInnerTypeVarSerialize(); 3.75 + t.test1(); 3.76 + assertTrue(assertionCount == 1); 3.77 + } 3.78 +}