• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 4219644
27  * @summary Escaping of spaces required only for leading spaces in the value
28  *          part of the property.
29  */
30 
31 package test.java.util.Properties;
32 
33 import java.io.BufferedInputStream;
34 import java.io.BufferedOutputStream;
35 import java.io.ByteArrayInputStream;
36 import java.io.File;
37 import java.io.FileInputStream;
38 import java.io.FileOutputStream;
39 import java.io.IOException;
40 import java.util.Properties;
41 
42 public class EscapeSpace {
43 
44     static String props =
45         "key1=\\ \\ Value1, has leading and trailing spaces\\  \n" +
46         "key2=Value2,\\ does not have\\ leading or trailing\\ spaces\n" +
47         "key3=Value3,has,no,spaces\n" +
48         "key4=Value4, does not have leading spaces\\  \n" +
49         "key5=\\t\\ \\ Value5, has leading tab and no trailing spaces\n" +
50         "key6=\\ \\ Value6,doesnothaveembeddedspaces\\ \\ \n" +
51         "\\ key1\\ test\\ =key1, has leading and trailing spaces  \n" +
52         "key2\\ test=key2, does not have leading or trailing spaces\n" +
53         "key3test=key3,has,no,spaces\n" +
54         "key4\\ test\\ =key4, does not have leading spaces  \n" +
55         "\\t\\ key5\\ test=key5, has leading tab and no trailing spaces\n" +
56         "\\ \\ key6\\ \\ =\\  key6,doesnothaveembeddedspaces  ";
57     // Android-changed: pass File directly.
58     // static void load(Properties p, String file) throws Exception
load(Properties p, File file)59     static void load(Properties p, File file) throws Exception
60     {
61         FileInputStream     fis = null;
62         BufferedInputStream bis = null;
63 
64         try {
65             fis = new FileInputStream(file);
66             bis = new BufferedInputStream( fis );
67 
68             p.load(bis);
69         }
70         catch (IOException e) {
71             throw new RuntimeException(e.getMessage());
72         } finally {
73             if (fis != null)
74                 fis.close();
75         }
76     }
77 
78     // Android-changed: pass File directly.
79     // static void store(Properties p, String file) throws Exception
store(Properties p, File file)80     static void store(Properties p, File file) throws Exception
81     {
82 
83         FileOutputStream     fos = null;
84         BufferedOutputStream bos = null;
85 
86         try {
87             fos = new FileOutputStream(file);
88             bos = new BufferedOutputStream( fos );
89 
90             p.store( bos, "Omitting escape characters for non leading space \" \" in properties");
91         }
92         catch (IOException e) {
93             throw new RuntimeException(e.getMessage());
94         } finally {
95             if (fos != null)
96                 fos.close();
97         }
98     }
99 
main( String args[] )100     public static void main( String args[] ) throws Exception
101     {
102         ByteArrayInputStream bais = new ByteArrayInputStream(props.getBytes());
103         Properties props0 = new Properties();
104         // Load properties with escape character '\' before space characters
105         try {
106             props0.load(bais);
107         } catch (IOException e) {
108             throw new RuntimeException(e.getMessage());
109         }
110 
111         Properties props1 = new Properties();
112         /*
113          * Put the same properties, but without the escape char for space in
114          * value part.
115          */
116         props1.put( "key1", "  Value1, has leading and trailing spaces  " );
117         props1.put( "key2",
118             "Value2, does not have leading or trailing spaces" );
119         props1.put( "key3", "Value3,has,no,spaces" );
120         props1.put( "key4", "Value4, does not have leading spaces  " );
121         props1.put( "key5",
122             "\t  Value5, has leading tab and no trailing spaces" );
123         props1.put( "key6", "  Value6,doesnothaveembeddedspaces  " );
124         props1.put( " key1 test ", "key1, has leading and trailing spaces  " );
125         props1.put( "key2 test",
126             "key2, does not have leading or trailing spaces" );
127         props1.put( "key3test", "key3,has,no,spaces" );
128         props1.put( "key4 test ", "key4, does not have leading spaces  " );
129         props1.put( "\t key5 test",
130             "key5, has leading tab and no trailing spaces" );
131         props1.put( "  key6  ", "  key6,doesnothaveembeddedspaces  " );
132 
133         // Check if both the properties match
134         if (!props0.equals(props1))
135             throw new RuntimeException("Test failed");
136 
137 
138         // Android-added: create temp file upfront.
139         File out = File.createTempFile("out1", "props");
140         // Also store the new properties to a file
141         store(props1, out);
142 
143         Properties props2 = new Properties();
144         // Reread the properties from the file
145         load(props2, out);
146 
147         // Make sure that the properties match
148         if (!props1.equals(props2))
149             throw new RuntimeException("Test failed");
150     }
151 }
152