test/jdk/java/io/Serializable/replaceStringArray/ReplaceStringArray.java
author rriggs
Fri, 11 Oct 2019 13:11:56 -0400
changeset 58565 baa5969ecf34
parent 47216 71c04702a3d5
permissions -rw-r--r--
8231427: Warning cleanup in tests of java.io.Serializable Reviewed-by: darcy, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
     2
 * Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4099013
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Enable substitution of String and Array by ObjectStreams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.Array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
class A implements Serializable {
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    33
    private static final long serialVersionUID = 1L;
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    34
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    String stringA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    String stringB;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    String stringC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    String[] arrayOfString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    A() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        stringA = "hello";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        stringB = "goodbye";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        stringC = stringA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        arrayOfString = new String[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        for (int i = 0; i < arrayOfString.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            arrayOfString[i] = new String("array element " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    void report() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            System.out.println("stringA = " + stringA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            System.out.println("stringB = " + stringB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            System.out.println("stringC = " + stringC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            System.out.println("length of arrayOfString = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                               arrayOfString.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            for (int i = 0; i < arrayOfString.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                System.out.println("arrayOfString[" + i + "]= " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                                   arrayOfString[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
class SubstituteObjectOutputStream extends ObjectOutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public int numStringsReplaced = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public int numArraysCounted = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public SubstituteObjectOutputStream(OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        super(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        enableReplaceObject(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    protected Object replaceObject(Object obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (obj instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            numStringsReplaced++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            return obj + "_WriteReplaced";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (obj.getClass().isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            Object[] array = (Object[]) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            /* Double the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
             * Initialize new array elements with original array. */
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    79
            Class<?> arrayComponentType = array.getClass().getComponentType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            Object[] newarray =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                (Object[])Array.newInstance(arrayComponentType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                                            array.length * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            for (int i = 0; i < array.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                newarray[i] = array[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            for (int ni = array.length; ni < 2* array.length; ni++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                newarray[ni] = array[ni - array.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            numArraysCounted++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            obj = newarray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
class SubstituteObjectInputStream extends ObjectInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public int numStringsReplaced = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public int numArraysCounted = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public SubstituteObjectInputStream(InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        super(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        enableResolveObject(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    protected Object resolveObject(Object obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (obj instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            numStringsReplaced++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            return obj + "_ReadResolved";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (obj.getClass().isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            Object[] array = (Object[])obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            /* Double the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
             * Initialize new array elements with original array. */
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
   113
            Class<?> arrayComponentType = array.getClass().getComponentType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            Object[] newarray =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                (Object[])Array.newInstance(arrayComponentType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                            array.length * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            for (int i = 0; i < array.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                newarray[i] = array[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            for (int ni = array.length; ni < 2* array.length; ni++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                newarray[ni] = array[ni - array.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            numArraysCounted++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            obj = newarray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
public class ReplaceStringArray {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public static void main(String args[]) throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        boolean verbose = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (args.length >= 1 && args[0].compareTo("verbose") == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            verbose = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        A a = new A();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            System.out.println("Value of Class A");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            a.report();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        /* Serialize object a to bytestream. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            System.out.println("Serialize A to SubstituteObjectOutputStream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        SubstituteObjectOutputStream out =   new SubstituteObjectOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        out.writeObject(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        a = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        /* Validate that writeReplace called by SubstituteObjectOutputStream.*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        boolean expectedResult = (out.numStringsReplaced == 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (!expectedResult)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            throw new Error("Expected " + 4 + " strings to be replaced during serialization;" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                            " only " + out.numStringsReplaced + " strings were replaced.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (out.numArraysCounted != 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            throw new Error("Expected 1 array during serialization; only " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                            out.numArraysCounted + " arrays");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            System.out.println("DeSerialize A from SubstituteObjectInputStream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        SubstituteObjectInputStream in =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            new SubstituteObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        a = (A)in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        /* Validate that readResolve called by SubstituteObjectInputStream.*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (in.numStringsReplaced != 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            throw new Error("Expected 4 strings to be resolved during deserialization;" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                            " only " + in.numStringsReplaced + " strings were resolved.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (in.numArraysCounted != 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            throw new Error("Expected 1 array during deserialization; only " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                            out.numArraysCounted + " arrays");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (a.arrayOfString.length != 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            throw new Error("Expected a.arrayOfString.length to be 8, observed " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                            a.arrayOfString.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            System.out.println("Value of Class A after serialize/deserialize with writeReplace/readResolve");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            a.report();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
}