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