Path: blob/master/test/jdk/java/util/Properties/PropertiesSerialization.java
41152 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.ByteArrayInputStream;24import java.io.ByteArrayOutputStream;25import java.io.IOException;26import java.io.ObjectInputStream;27import java.io.ObjectOutputStream;28import java.util.Base64;29import java.util.Properties;3031/**32* @test33* @bug 802989134* @summary tests the compatibility of Properties serial form35* @run main PropertiesSerialization read36*37* To update this test in case the serial form of Properties changes, run this38* test with the 'write' flag, and copy the resulting output back into this39* file, replacing the existing String declaration(s).40*/41public class PropertiesSerialization {42private static final Properties TEST_PROPS;43static {44TEST_PROPS = new Properties();45TEST_PROPS.setProperty("one", "two");46TEST_PROPS.setProperty("buckle", "shoe");47TEST_PROPS.setProperty("three", "four");48TEST_PROPS.setProperty("shut", "door");49}5051/**52* Base64 encoded string for Properties object53* Java version: 1.8.054**/55private static final String TEST_SER_BASE64 =56"rO0ABXNyABRqYXZhLnV0aWwuUHJvcGVydGllczkS0HpwNj6YAgABTAAIZGVmYXVs"57+ "dHN0ABZMamF2YS91dGlsL1Byb3BlcnRpZXM7eHIAE2phdmEudXRpbC5IYXNodGFi"58+ "bGUTuw8lIUrkuAMAAkYACmxvYWRGYWN0b3JJAAl0aHJlc2hvbGR4cD9AAAAAAAAI"59+ "dwgAAAALAAAABHQAA29uZXQAA3R3b3QABHNodXR0AARkb29ydAAGYnVja2xldAAE"60+ "c2hvZXQABXRocmVldAAEZm91cnhw";6162public static void main(String[] args) throws IOException,63ClassNotFoundException {64if (args.length == 0) {65System.err.println("Run with 'read' or 'write'");66System.err.println(" read mode: normal test mode.");67System.err.println(" Confirms that serial stream can");68System.err.println(" be deserialized as expected.");69System.err.println(" write mode: meant for updating the test,");70System.err.println(" should the serial form change.");71System.err.println(" Test output should be pasted");72System.err.println(" back into the test source.");73return;74}7576Properties deserializedObject;77if ("read".equals(args[0])) {78ByteArrayInputStream bais = new79ByteArrayInputStream(Base64.getDecoder().decode(TEST_SER_BASE64));80try (ObjectInputStream ois = new ObjectInputStream(bais)) {81deserializedObject = (Properties) ois.readObject();82}83if (!TEST_PROPS.equals(deserializedObject)) {84throw new RuntimeException("deserializedObject not equals()");85}86System.out.println("Test passed");87} else if ("write".equals(args[0])) {88System.out.println("\nTo update the test, paste the following back "89+ "into the test code:\n");90ByteArrayOutputStream baos = new ByteArrayOutputStream();91ObjectOutputStream oos = new ObjectOutputStream(baos);92oos.writeObject(TEST_PROPS);93oos.flush();94oos.close();9596byte[] byteArray = baos.toByteArray();97// Check that the Properties deserializes correctly98ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);99ObjectInputStream ois = new ObjectInputStream(bais);100Properties deser = (Properties)ois.readObject();101if (!TEST_PROPS.equals(deser)) {102throw new RuntimeException("write: Deserialized != original");103}104105// Now get a Base64 string representation of the serialized bytes.106final String base64 = Base64.getEncoder().encodeToString(byteArray);107// Check that we can deserialize the Base64 string we just computed.108ByteArrayInputStream bais2 =109new ByteArrayInputStream(Base64.getDecoder().decode(base64));110ObjectInputStream ois2 = new ObjectInputStream(bais2);111Properties deser2 = (Properties)ois2.readObject();112if (!TEST_PROPS.equals(deser2)) {113throw new RuntimeException("write: Deserialized base64 != "114+ "original");115}116System.out.println(dumpBase64SerialStream(base64));117}118}119120private static final String INDENT = " ";121/* Based on:122* java/util/logging/HigherResolutionTimeStamps/SerializeLogRecored.java123*/124private static String dumpBase64SerialStream(String base64) {125// Generates the Java Pseudo code that can be cut & pasted into126// this test (see Jdk8SerializedLog and Jdk9SerializedLog below)127final StringBuilder sb = new StringBuilder();128sb.append(INDENT).append(" /**").append('\n');129sb.append(INDENT).append(" * Base64 encoded string for Properties object\n");130sb.append(INDENT).append(" * Java version: ")131.append(System.getProperty("java.version")).append('\n');132sb.append(INDENT).append(" **/").append('\n');133sb.append(INDENT).append(" private static final String TEST_SER_BASE64 = ")134.append("\n").append(INDENT).append(" ");135final int last = base64.length() - 1;136for (int i=0; i<base64.length();i++) {137if (i%64 == 0) sb.append("\"");138sb.append(base64.charAt(i));139if (i%64 == 63 || i == last) {140sb.append("\"");141if (i == last) sb.append(";\n");142else sb.append("\n").append(INDENT).append(" + ");143}144}145return sb.toString();146}147}148149150