Path: blob/master/test/jdk/java/util/Properties/EscapeSpace.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 421964426* @summary Escaping of spaces required only for leading spaces in the value27* part of the property.28*/2930import java.io.BufferedInputStream;31import java.io.BufferedOutputStream;32import java.io.ByteArrayInputStream;33import java.io.FileInputStream;34import java.io.FileOutputStream;35import java.io.IOException;36import java.util.Properties;3738public class EscapeSpace {3940static String props =41"key1=\\ \\ Value1, has leading and trailing spaces\\ \n" +42"key2=Value2,\\ does not have\\ leading or trailing\\ spaces\n" +43"key3=Value3,has,no,spaces\n" +44"key4=Value4, does not have leading spaces\\ \n" +45"key5=\\t\\ \\ Value5, has leading tab and no trailing spaces\n" +46"key6=\\ \\ Value6,doesnothaveembeddedspaces\\ \\ \n" +47"\\ key1\\ test\\ =key1, has leading and trailing spaces \n" +48"key2\\ test=key2, does not have leading or trailing spaces\n" +49"key3test=key3,has,no,spaces\n" +50"key4\\ test\\ =key4, does not have leading spaces \n" +51"\\t\\ key5\\ test=key5, has leading tab and no trailing spaces\n" +52"\\ \\ key6\\ \\ =\\ key6,doesnothaveembeddedspaces ";53static void load(Properties p, String file) throws Exception54{55FileInputStream fis = null;56BufferedInputStream bis = null;5758try {59fis = new FileInputStream(file);60bis = new BufferedInputStream( fis );6162p.load(bis);63}64catch (IOException e) {65throw new RuntimeException(e.getMessage());66} finally {67if (fis != null)68fis.close();69}70}7172static void store(Properties p, String file) throws Exception73{7475FileOutputStream fos = null;76BufferedOutputStream bos = null;7778try {79fos = new FileOutputStream(file);80bos = new BufferedOutputStream( fos );8182p.store( bos, "Omitting escape characters for non leading space \" \" in properties");83}84catch (IOException e) {85throw new RuntimeException(e.getMessage());86} finally {87if (fos != null)88fos.close();89}90}9192public static void main( String args[] ) throws Exception93{94ByteArrayInputStream bais = new ByteArrayInputStream(props.getBytes());95Properties props0 = new Properties();96// Load properties with escape character '\' before space characters97try {98props0.load(bais);99} catch (IOException e) {100throw new RuntimeException(e.getMessage());101}102103Properties props1 = new Properties();104/*105* Put the same properties, but without the escape char for space in106* value part.107*/108props1.put( "key1", " Value1, has leading and trailing spaces " );109props1.put( "key2",110"Value2, does not have leading or trailing spaces" );111props1.put( "key3", "Value3,has,no,spaces" );112props1.put( "key4", "Value4, does not have leading spaces " );113props1.put( "key5",114"\t Value5, has leading tab and no trailing spaces" );115props1.put( "key6", " Value6,doesnothaveembeddedspaces " );116props1.put( " key1 test ", "key1, has leading and trailing spaces " );117props1.put( "key2 test",118"key2, does not have leading or trailing spaces" );119props1.put( "key3test", "key3,has,no,spaces" );120props1.put( "key4 test ", "key4, does not have leading spaces " );121props1.put( "\t key5 test",122"key5, has leading tab and no trailing spaces" );123props1.put( " key6 ", " key6,doesnothaveembeddedspaces " );124125// Check if both the properties match126if (!props0.equals(props1))127throw new RuntimeException("Test failed");128129130// Also store the new properties to a file131store(props1, "out1.props");132133Properties props2 = new Properties();134// Reread the properties from the file135load(props2, "out1.props");136137// Make sure that the properties match138if (!props1.equals(props2))139throw new RuntimeException("Test failed");140}141}142143144