jdk/test/java/lang/invoke/lambda/LambdaSerialization.java
author rfield
Thu, 21 Feb 2013 15:46:37 -0800
changeset 16018 3fe4876de509
parent 16001 fd4c8d3becf8
child 16034 cb5fbea1ecec
permissions -rw-r--r--
8008356: Test LambdaSerialization.java failing Summary: run in /othervm mode Reviewed-by: ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16001
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
     1
/*
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
     4
 *
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
     7
 * published by the Free Software Foundation.
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
     8
 *
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    13
 * accompanied this code).
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    14
 *
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    18
 *
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    21
 * questions.
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    22
 */
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    23
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    24
/*
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    25
@test
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    26
@bug 8004970
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    27
@summary Lambda serialization
16018
3fe4876de509 8008356: Test LambdaSerialization.java failing
rfield
parents: 16001
diff changeset
    28
@run main/othervm LambdaSerialization
16001
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    29
*/
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    30
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    31
import java.io.*;
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    32
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    33
public class LambdaSerialization {
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    34
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    35
    static int assertionCount = 0;
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    36
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    37
    static void assertTrue(boolean cond) {
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    38
        assertionCount++;
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    39
        if (!cond)
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    40
            throw new AssertionError();
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    41
    }
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    42
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    43
    public static void main(String[] args) throws Exception {
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    44
        try {
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    45
            // Write lambdas out
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    46
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    47
            ObjectOutput out = new ObjectOutputStream(baos);
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    48
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    49
            write(out, z -> "[" + z + "]" );
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    50
            write(out, z -> z + z );
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    51
            write(out, z -> "blah" );
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    52
            out.flush();
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    53
            out.close();
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    54
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    55
            // Read them back
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    56
            ByteArrayInputStream bais =
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    57
                new ByteArrayInputStream(baos.toByteArray());
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    58
            ObjectInputStream in = new ObjectInputStream(bais);
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    59
            readAssert(in, "[X]");
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    60
            readAssert(in, "XX");
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    61
            readAssert(in, "blah");
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    62
            in.close();
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    63
        } catch (IOException e) {
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    64
            e.printStackTrace();
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    65
            throw e;
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    66
        }
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    67
        assertTrue(assertionCount == 3);
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    68
    }
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    69
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    70
    static void write(ObjectOutput out, LSI lamb) throws IOException {
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    71
        out.writeObject(lamb);
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    72
    }
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    73
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    74
    static void readAssert(ObjectInputStream in, String expected)  throws IOException, ClassNotFoundException {
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    75
        LSI ls = (LSI) in.readObject();
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    76
        String result = ls.convert("X");
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    77
        System.out.printf("Result: %s\n", result);
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    78
        assertTrue(result.equals(expected));
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    79
    }
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    80
}
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    81
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    82
interface LSI extends Serializable {
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    83
    String convert(String x);
fd4c8d3becf8 8004970: Implement serialization in the lambda metafactory
rfield
parents:
diff changeset
    84
}