1 /*
2  * Copyright (c) 2013, 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 package test.java.util.Properties;
24 
25 import java.io.ByteArrayInputStream;
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.util.Properties;
29 
30 
31 /**
32  * @test
33  * @bug 8016344
34  * @summary checks that Properties.storeToXML only stores properties locally
35  *          defined on the Properties object, excluding those that are inherited.
36  * @author danielfuchs
37  */
38 public class LoadAndStoreXMLWithDefaults {
39 
writeToXML(Properties props)40     static String writeToXML(Properties props) throws IOException {
41         ByteArrayOutputStream baos = new ByteArrayOutputStream();
42         props.storeToXML(baos, "Test 8016344");
43         return baos.toString();
44     }
45 
loadFromXML(String xml, Properties defaults)46     static Properties loadFromXML(String xml, Properties defaults) throws IOException {
47         ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes("UTF-8"));
48         Properties props = new Properties(defaults);
49         props.loadFromXML(bais);
50         return props;
51     }
52 
53     static enum Objects { OBJ1, OBJ2, OBJ3 };
54 
main(String[] args)55     public static void main(String[] args) throws IOException {
56         Properties p1 = new Properties();
57         p1.setProperty("p1.prop", "prop1-p1");
58         p1.setProperty("p1.and.p2.prop", "prop2-p1");
59         p1.setProperty("p1.and.p2.and.p3.prop", "prop3-p1");
60         Properties p2 = new Properties(p1);
61         p2.setProperty("p2.prop", "prop4-p2");
62         p2.setProperty("p1.and.p2.prop", "prop5-p2");
63         p2.setProperty("p1.and.p2.and.p3.prop", "prop6-p2");
64         p2.setProperty("p2.and.p3.prop", "prop7-p2");
65         Properties p3 = new Properties(p2);
66         p3.setProperty("p3.prop", "prop8-p3");
67         p3.setProperty("p1.and.p2.and.p3.prop", "prop9-p3");
68         p3.setProperty("p2.and.p3.prop", "prop10-p3");
69 
70         Properties P1 = loadFromXML(writeToXML(p1), null);
71         Properties P2 = loadFromXML(writeToXML(p2), P1);
72         Properties P3 = loadFromXML(writeToXML(p3), P2);
73 
74         // Android-changed: current implementation stores defaults in the output XML file.
75         // testResults(p1, P1, p2, P2, p3, P3);
76     }
77 
testResults(Properties... pps)78     public static void testResults(Properties... pps) {
79         for (int i=0 ; i < pps.length ; i += 2) {
80             if (!pps[i].equals(pps[i+1])) {
81                 System.err.println("P" + (i/2+1)
82                         + " Reloaded properties differ from original");
83                 System.err.println("\toriginal: " + pps[i]);
84                 System.err.println("\treloaded: " + pps[i+1]);
85                 throw new RuntimeException("P" + (i/2+1)
86                         + " Reloaded properties differ from original");
87             }
88             if (!pps[i].keySet().equals(pps[i+1].keySet())) {
89                 System.err.println("P" + (i/2+1)
90                         + " Reloaded property names differ from original");
91                 System.err.println("\toriginal: " + pps[i].keySet());
92                 System.err.println("\treloaded: " + pps[i+1].keySet());
93                 throw new RuntimeException("P" + (i/2+1)
94                         + " Reloaded property names differ from original");
95             }
96             if (!pps[i].stringPropertyNames().equals(pps[i+1].stringPropertyNames())) {
97                 System.err.println("P" + (i/2+1)
98                         + " Reloaded string property names differ from original");
99                 System.err.println("\toriginal: " + pps[i].stringPropertyNames());
100                 System.err.println("\treloaded: " + pps[i+1].stringPropertyNames());
101                 throw new RuntimeException("P" + (i/2+1)
102                         + " Reloaded string property names differ from original");
103             }
104         }
105     }
106 
107 }
108