Path: blob/master/test/jdk/java/util/Properties/LoadAndStoreXMLWithDefaults.java
41152 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22import java.io.ByteArrayInputStream;23import java.io.ByteArrayOutputStream;24import java.io.IOException;25import java.util.Properties;262728/**29* @test30* @bug 801634431* @summary checks that Properties.storeToXML only stores properties locally32* defined on the Properties object, excluding those that are inherited.33* @author danielfuchs34*/35public class LoadAndStoreXMLWithDefaults {3637static String writeToXML(Properties props) throws IOException {38ByteArrayOutputStream baos = new ByteArrayOutputStream();39props.storeToXML(baos, "Test 8016344");40return baos.toString();41}4243static Properties loadFromXML(String xml, Properties defaults) throws IOException {44ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes("UTF-8"));45Properties props = new Properties(defaults);46props.loadFromXML(bais);47return props;48}4950static enum Objects { OBJ1, OBJ2, OBJ3 };5152public static void main(String[] args) throws IOException {53Properties p1 = new Properties();54p1.setProperty("p1.prop", "prop1-p1");55p1.setProperty("p1.and.p2.prop", "prop2-p1");56p1.setProperty("p1.and.p2.and.p3.prop", "prop3-p1");57Properties p2 = new Properties(p1);58p2.setProperty("p2.prop", "prop4-p2");59p2.setProperty("p1.and.p2.prop", "prop5-p2");60p2.setProperty("p1.and.p2.and.p3.prop", "prop6-p2");61p2.setProperty("p2.and.p3.prop", "prop7-p2");62Properties p3 = new Properties(p2);63p3.setProperty("p3.prop", "prop8-p3");64p3.setProperty("p1.and.p2.and.p3.prop", "prop9-p3");65p3.setProperty("p2.and.p3.prop", "prop10-p3");6667Properties P1 = loadFromXML(writeToXML(p1), null);68Properties P2 = loadFromXML(writeToXML(p2), P1);69Properties P3 = loadFromXML(writeToXML(p3), P2);7071testResults(p1, P1, p2, P2, p3, P3);72}7374public static void testResults(Properties... pps) {75for (int i=0 ; i < pps.length ; i += 2) {76if (!pps[i].equals(pps[i+1])) {77System.err.println("P" + (i/2+1)78+ " Reloaded properties differ from original");79System.err.println("\toriginal: " + pps[i]);80System.err.println("\treloaded: " + pps[i+1]);81throw new RuntimeException("P" + (i/2+1)82+ " Reloaded properties differ from original");83}84if (!pps[i].keySet().equals(pps[i+1].keySet())) {85System.err.println("P" + (i/2+1)86+ " Reloaded property names differ from original");87System.err.println("\toriginal: " + pps[i].keySet());88System.err.println("\treloaded: " + pps[i+1].keySet());89throw new RuntimeException("P" + (i/2+1)90+ " Reloaded property names differ from original");91}92if (!pps[i].stringPropertyNames().equals(pps[i+1].stringPropertyNames())) {93System.err.println("P" + (i/2+1)94+ " Reloaded string property names differ from original");95System.err.println("\toriginal: " + pps[i].stringPropertyNames());96System.err.println("\treloaded: " + pps[i+1].stringPropertyNames());97throw new RuntimeException("P" + (i/2+1)98+ " Reloaded string property names differ from original");99}100}101}102103}104105106