Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Properties/LoadAndStoreXMLWithDefaults.java
47019 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 {3637public static enum StoreMethod {38// Note: this case will test the default provider when available,39// and the basic provider when it's not.40PROPERTIES {41@Override42public String writeToXML(Properties p) throws IOException {43final ByteArrayOutputStream baos = new ByteArrayOutputStream();44p.storeToXML(baos, "Test 8016344");45return baos.toString();46}47@Override48public Properties loadFromXML(String xml, Properties defaults)49throws IOException {50final ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes("UTF-8"));51Properties p = new Properties(defaults);52p.loadFromXML(bais);53return p;54}55},56// Note: this case always test the basic provider, which is always available.57// so sometimes it's just a dup with the previous case...58BASICPROVIDER {59@Override60public String writeToXML(Properties p) throws IOException {61final ByteArrayOutputStream baos = new ByteArrayOutputStream();62jdk.internal.util.xml.BasicXmlPropertiesProvider provider =63new jdk.internal.util.xml.BasicXmlPropertiesProvider();64provider.store(p, baos, "Test 8016344", "UTF-8");65return baos.toString();66}67@Override68public Properties loadFromXML(String xml, Properties defaults)69throws IOException {70final ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes("UTF-8"));71Properties p = new Properties(defaults);72jdk.internal.util.xml.BasicXmlPropertiesProvider provider =73new jdk.internal.util.xml.BasicXmlPropertiesProvider();74provider.load(p, bais);75return p;76}77};78public abstract String writeToXML(Properties p) throws IOException;79public abstract Properties loadFromXML(String xml, Properties defaults)80throws IOException;81public String displayName() {82switch(this) {83case PROPERTIES: return "Properties.storeToXML";84case BASICPROVIDER: return "BasicXmlPropertiesProvider.store";85default:86throw new UnsupportedOperationException(this.name());87}88}89}9091static enum Objects { OBJ1, OBJ2, OBJ3 };9293public static void main(String[] args) throws IOException {94Properties p1 = new Properties();95p1.setProperty("p1.prop", "prop1-p1");96p1.setProperty("p1.and.p2.prop", "prop2-p1");97p1.setProperty("p1.and.p2.and.p3.prop", "prop3-p1");98Properties p2 = new Properties(p1);99p2.setProperty("p2.prop", "prop4-p2");100p2.setProperty("p1.and.p2.prop", "prop5-p2");101p2.setProperty("p1.and.p2.and.p3.prop", "prop6-p2");102p2.setProperty("p2.and.p3.prop", "prop7-p2");103Properties p3 = new Properties(p2);104p3.setProperty("p3.prop", "prop8-p3");105p3.setProperty("p1.and.p2.and.p3.prop", "prop9-p3");106p3.setProperty("p2.and.p3.prop", "prop10-p3");107108for (StoreMethod m : StoreMethod.values()) {109System.out.println("Testing with " + m.displayName());110Properties P1 = m.loadFromXML(m.writeToXML(p1), null);111Properties P2 = m.loadFromXML(m.writeToXML(p2), P1);112Properties P3 = m.loadFromXML(m.writeToXML(p3), P2);113114testResults(m, p1, P1, p2, P2, p3, P3);115116// Now check that properties whose keys or values are objects117// are skipped.118119System.out.println("Testing with " + m.displayName() + " and Objects");120P1.put("p1.object.prop", Objects.OBJ1);121P1.put(Objects.OBJ1, "p1.object.prop");122P1.put("p2.object.prop", "p2.object.prop");123P2.put("p2.object.prop", Objects.OBJ2);124P2.put(Objects.OBJ2, "p2.object.prop");125P3.put("p3.object.prop", Objects.OBJ3);126P3.put(Objects.OBJ3, "p3.object.prop");127128Properties PP1 = m.loadFromXML(m.writeToXML(P1), null);129Properties PP2 = m.loadFromXML(m.writeToXML(P2), PP1);130Properties PP3 = m.loadFromXML(m.writeToXML(P3), PP2);131132p1.setProperty("p2.object.prop", "p2.object.prop");133try {134testResults(m, p1, PP1, p2, PP2, p3, PP3);135} finally {136p1.remove("p2.object.prop");137}138}139}140141public static void testResults(StoreMethod m, Properties... pps) {142for (int i=0 ; i < pps.length ; i += 2) {143if (!pps[i].equals(pps[i+1])) {144System.err.println(m.displayName() +": P" + (i/2+1)145+ " Reloaded properties differ from original");146System.err.println("\toriginal: " + pps[i]);147System.err.println("\treloaded: " + pps[i+1]);148throw new RuntimeException(m.displayName() +": P" + (i/2+1)149+ " Reloaded properties differ from original");150}151if (!pps[i].keySet().equals(pps[i+1].keySet())) {152System.err.println(m.displayName() +": P" + (i/2+1)153+ " Reloaded property names differ from original");154System.err.println("\toriginal: " + pps[i].keySet());155System.err.println("\treloaded: " + pps[i+1].keySet());156throw new RuntimeException(m.displayName() +": P" + (i/2+1)157+ " Reloaded property names differ from original");158}159if (!pps[i].stringPropertyNames().equals(pps[i+1].stringPropertyNames())) {160System.err.println(m.displayName() +": P" + (i/2+1)161+ " Reloaded string property names differ from original");162System.err.println("\toriginal: " + pps[i].stringPropertyNames());163System.err.println("\treloaded: " + pps[i+1].stringPropertyNames());164throw new RuntimeException(m.displayName() +": P" + (i/2+1)165+ " Reloaded string property names differ from original");166}167}168}169170}171172173