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