1/*
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25@test
26@bug 8004969
27@summary Lambda deserialization
28
29*/
30
31import java.io.*;
32
33public class T8004969 implements Serializable {
34
35    static int assertionCount = 0;
36
37    static void assertTrue(boolean cond) {
38        assertionCount++;
39        if (!cond)
40            throw new AssertionError();
41    }
42
43    static String mm(String s) { return "mref" + s; }
44
45    String aField = "aF";
46
47    public static void main(String[] args) throws Exception {
48        (new T8004969()).doit();
49    }
50
51    public void doit() throws Exception {
52        String aLocal = "aL";
53        int anInt = 99;
54
55        try {
56            // Write lambdas out
57            ByteArrayOutputStream baos = new ByteArrayOutputStream();
58            ObjectOutput out = new ObjectOutputStream(baos);
59
60            write(out, z -> "[" + z + "]" );
61            write(out, z -> { String x = z + z; return x + x; } );
62            write(out, T8004969::mm );
63            write(out, z -> (new LSI() { public String convert(String x) { return "*"+x; }} ).convert(z) );
64            write(out, z -> aField + z );
65            write(out, z -> aLocal + z );
66            write(out, z -> z + anInt );
67            out.flush();
68            out.close();
69
70            // Read them back
71            ByteArrayInputStream bais =
72                new ByteArrayInputStream(baos.toByteArray());
73            ObjectInputStream in = new ObjectInputStream(bais);
74            readAssert(in, "[X]");
75            readAssert(in, "XXXX");
76            readAssert(in, "mrefX");
77            readAssert(in, "*X");
78            readAssert(in, "aFX");
79            readAssert(in, "aLX");
80            readAssert(in, "X99");
81            in.close();
82        } catch (IOException e) {
83            e.printStackTrace();
84            throw e;
85        }
86        assertTrue(assertionCount == 7);
87    }
88
89    static void write(ObjectOutput out, LSI lamb) throws IOException {
90        out.writeObject(lamb);
91    }
92
93    static void readAssert(ObjectInputStream in, String expected)  throws IOException, ClassNotFoundException {
94        LSI ls = (LSI) in.readObject();
95        String result = ls.convert("X");
96        System.out.printf("Result: %s\n", result);
97        assertTrue(result.equals(expected));
98    }
99}
100
101interface LSI extends Serializable {
102    String convert(String x);
103}
104