test/java/util/Properties/LoadAndStoreXMLWithDefaults.java

Print this page




  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 }


  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     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 }