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 5087448
27  * @summary Verify that property.save saves comments correctly
28  */
29 
30 package test.java.util.Properties;
31 
32 import java.io.ByteArrayInputStream;
33 import java.io.ByteArrayOutputStream;
34 import java.io.IOException;
35 import java.util.Properties;
36 
37 public class SaveComments {
38 
main(String[] argv)39     public static void main(String[] argv) throws IOException {
40         String ls = System.getProperty("line.separator");
41         String[] input = new String[] {
42           "Comments with \u4e2d\u6587\u6c49\u5b57 included",
43           "Comments with \n Second comments line",
44           "Comments with \n# Second comments line",
45           "Comments with \n! Second comments line",
46           "Comments with last character is \n",
47           "Comments with last character is \r\n",
48           "Comments with last two characters are \n\n",
49           "Comments with last four characters are \r\n\r\n",
50           "Comments with \nkey4=value4",
51           "Comments with \n#key4=value4"};
52 
53         String[] output = new String[] {
54           "#Comments with \\u4E2D\\u6587\\u6C49\\u5B57 included" + ls,
55           "#Comments with " + ls + "# Second comments line" + ls,
56           "#Comments with " + ls + "# Second comments line" + ls,
57           "#Comments with " + ls + "! Second comments line" + ls,
58           "#Comments with last character is " + ls+"#"+ls,
59           "#Comments with last character is " + ls+"#"+ls,
60           "#Comments with last two characters are " + ls+"#"+ls+"#"+ls,
61           "#Comments with last four characters are " + ls+"#"+ls+"#"+ls};
62 
63         Properties props = new Properties();
64         boolean failed = false;
65         int i = 0;
66         for (i = 0; i < output.length; i++) {
67             ByteArrayOutputStream baos = new ByteArrayOutputStream(200);
68             props.store(baos, input[i]);
69             String result = baos.toString("iso8859-1");
70             if (result.indexOf(output[i]) == -1) {
71                 System.out.println("Wrong \"store()\" output:");
72                 System.out.println(result);
73                 failed = true;
74             }
75         }
76 
77         props.put("key1", "value1");
78         props.put("key2", "value2");
79         props.put("key3", "value3");
80         for (; i < input.length; i++) {
81             ByteArrayOutputStream baos = new ByteArrayOutputStream(200);
82             props.store(baos, input[i]);
83             Properties propsNew = new Properties();
84             propsNew.load(new ByteArrayInputStream(baos.toByteArray()));
85             /*
86             Set<Map.Entry<Object, Object>> kvsetNew = propsNew.entrySet();
87             Set<Map.Entry<Object, Object>> kvset = props.entrySet();
88             if (!kvsetNew.containsAll(kvset) || !kvset.containsAll(kvsetNew)) {
89             */
90             if (!props.equals (propsNew)) {
91                 System.out.println("Wrong output:");
92                 System.out.println(baos.toString("iso8859-1"));
93                 failed = true;
94             }
95         }
96         if (failed)
97             throw new RuntimeException("Incorrect Properties Comment Output.");
98     }
99 }
100