Path: blob/master/test/jdk/java/util/Properties/SaveEncoding.java
41149 views
/*1* Copyright (c) 2018, 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*/2223/*24* @test25* @bug 4026910 4011163 4077980 4096786 421353726* @summary Test for saving and loading encoded keys and values27*/2829import java.io.BufferedReader;30import java.io.File;31import java.io.FileInputStream;32import java.io.FileOutputStream;33import java.io.IOException;34import java.io.InputStreamReader;35import java.util.Properties;3637/**38* This class tests to see if the properties object saves39* and loads keys and values properly40*/41public class SaveEncoding {4243public static void main(String argv[]) {44int testSucceeded=0;45FileOutputStream myOutput;4647// Create a properties object to save48Properties myProperties = new Properties();49myProperties.put("signal", "val\u0019");50myProperties.put("ABC 10", "value0");51myProperties.put("\uff10test", "value\u0020");52myProperties.put("key with spaces", "value with spaces");53myProperties.put(" special#=key ", "value3");5455try {56// Destroy old test file if any57File myFile = new File("testout");58myFile.delete();5960// Save the object and check output61myOutput = new FileOutputStream("testout");62myProperties.store(myOutput,"A test");63myOutput.close();6465// Read properties file and verify \u001966FileInputStream inFile = new FileInputStream("testout");67BufferedReader in = new BufferedReader(68new InputStreamReader(inFile));69String firstLine = "foo";70while (!firstLine.startsWith("signal"))71firstLine = in.readLine();72inFile.close();73if (firstLine.length() != 16)74throw new RuntimeException(75"Incorrect storage of values < 32.");7677// Load the properties set78FileInputStream myIn = new FileInputStream("testout");79Properties myNewProps = new Properties();80try {81myNewProps.load(myIn);82} finally {83myIn.close();84}8586// Check the results87if (!myNewProps.equals(myProperties))88throw new RuntimeException(89"Properties is not character encoding safe.");90} catch (IOException e) { // Do nothing91}92}93}949596