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 4026910 4011163 4077980 4096786 4213537
27   * @summary Test for saving and loading encoded keys and values
28   */
29 
30 package test.java.util.Properties;
31 
32 import java.io.BufferedReader;
33 import java.io.File;
34 import java.io.FileInputStream;
35 import java.io.FileOutputStream;
36 import java.io.IOException;
37 import java.io.InputStreamReader;
38 import java.util.Properties;
39 
40 /**
41  * This class tests to see if the properties object saves
42  * and loads keys and values properly
43  */
44 public class SaveEncoding {
45 
main(String argv[])46     public static void main(String argv[]) {
47         int testSucceeded=0;
48         FileOutputStream myOutput;
49 
50         // Create a properties object to save
51         Properties myProperties = new Properties();
52         myProperties.put("signal", "val\u0019");
53         myProperties.put("ABC 10", "value0");
54         myProperties.put("\uff10test", "value\u0020");
55         myProperties.put("key with spaces", "value with spaces");
56         myProperties.put(" special#=key ", "value3");
57 
58         try {
59             // Destroy old test file if any
60             // Android-changed: use temp file.
61             // File myFile = new File("testout");
62             File myFile = File.createTempFile("testout", "properties");
63             myFile.delete();
64 
65             // Save the object and check output
66             // Android-changed: read from the temp file.
67             // myOutput = new FileOutputStream("testout");
68             myOutput = new FileOutputStream(myFile);
69             myProperties.store(myOutput,"A test");
70             myOutput.close();
71 
72             // Read properties file and verify \u0019
73             // Android-changed: write to the temp file.
74             // FileInputStream inFile = new FileInputStream("testout");
75             FileInputStream inFile = new FileInputStream(myFile);
76             BufferedReader in = new BufferedReader(
77                                 new InputStreamReader(inFile));
78             String firstLine = "foo";
79             while (!firstLine.startsWith("signal"))
80                 firstLine = in.readLine();
81             inFile.close();
82             if (firstLine.length() != 16)
83                 throw new RuntimeException(
84                     "Incorrect storage of values < 32.");
85 
86             // Load the properties set
87             // Android-changed: read from the temp file.
88             // FileInputStream myIn = new FileInputStream("testout");
89             FileInputStream myIn = new FileInputStream(myFile);
90             Properties myNewProps = new Properties();
91             try {
92                 myNewProps.load(myIn);
93             } finally {
94                 myIn.close();
95             }
96 
97             // Check the results
98             if (!myNewProps.equals(myProperties))
99                 throw new RuntimeException(
100                     "Properties is not character encoding safe.");
101         } catch (IOException e) { // Do nothing
102         }
103     }
104 }
105