jdk/test/java/util/Properties/LoadAndStoreXML.java
author alanb
Wed, 19 Dec 2012 14:53:33 +0000
changeset 14909 eded175efec2
parent 14187 9e193f8bab66
child 14922 fdb5ce96f459
permissions -rw-r--r--
8005248: (props) Integrate small footprint parser into Properties Reviewed-by: joehw, mchung, psandoz, erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14029
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
     1
/*
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
     4
 *
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
     8
 *
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    13
 * accompanied this code).
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    14
 *
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    18
 *
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    21
 * questions.
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    22
 */
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    23
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    24
/*
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    25
 * @test
14909
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
    26
 * @bug 8000354 8000685 8004371
14029
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    27
 * @summary Basic test of storeToXML and loadToXML
14909
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
    28
 * @run main LoadAndStoreXML
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
    29
 * @run main/othervm -Dsun.util.spi.XmlPropertiesProvider=jdk.internal.util.xml.BasicXmlPropertiesProvider LoadAndStoreXML
14029
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    30
 */
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    31
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    32
import java.io.*;
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    33
import java.util.*;
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    34
import java.security.*;
14909
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
    35
import java.nio.file.*;
14029
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    36
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    37
public class LoadAndStoreXML {
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    38
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    39
    /**
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    40
     * Simple policy implementation that grants a set of permissions to
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    41
     * all code sources and protection domains.
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    42
     */
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    43
    static class SimplePolicy extends Policy {
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    44
        private final Permissions perms;
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    45
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    46
        public SimplePolicy(Permission...permissions) {
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    47
            perms = new Permissions();
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    48
            for (Permission permission : permissions)
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    49
                perms.add(permission);
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    50
        }
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    51
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    52
        @Override
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    53
        public PermissionCollection getPermissions(CodeSource cs) {
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    54
            return perms;
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    55
        }
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    56
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    57
        @Override
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    58
        public PermissionCollection getPermissions(ProtectionDomain pd) {
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    59
            return perms;
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    60
        }
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    61
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    62
        @Override
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    63
        public boolean implies(ProtectionDomain pd, Permission p) {
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    64
            return perms.implies(p);
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    65
        }
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    66
    }
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    67
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    68
    /**
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    69
     * Sanity test that properties saved with Properties#storeToXML can be
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    70
     * read with Properties#loadFromXML.
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    71
     */
14187
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
    72
    static void testLoadAndStore(String encoding) throws IOException {
14909
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
    73
        System.out.println("testLoadAndStore, encoding=" + encoding);
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
    74
14029
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    75
        Properties props = new Properties();
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    76
        props.put("k1", "foo");
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    77
        props.put("k2", "bar");
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    78
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    79
        ByteArrayOutputStream out = new ByteArrayOutputStream();
14187
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
    80
        props.storeToXML(out, null, encoding);
14029
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    81
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    82
        Properties p = new Properties();
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    83
        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    84
        p.loadFromXML(in);
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    85
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    86
        if (!p.equals(props)) {
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    87
            System.err.println("stored: " + props);
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    88
            System.err.println("loaded: " + p);
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    89
            throw new RuntimeException("Test failed");
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    90
        }
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    91
    }
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    92
14187
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
    93
    /**
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
    94
     * Test loadFromXML with a document that does not have an encoding declaration
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
    95
     */
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
    96
    static void testLoadWithoutEncoding() throws IOException {
14909
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
    97
        System.out.println("testLoadWithoutEncoding");
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
    98
14187
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
    99
        Properties expected = new Properties();
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   100
        expected.put("foo", "bar");
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   101
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   102
        String s = "<?xml version=\"1.0\"?>" +
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   103
                   "<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">" +
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   104
                   "<properties>" +
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   105
                   "<entry key=\"foo\">bar</entry>" +
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   106
                   "</properties>";
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   107
        ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes("UTF-8"));
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   108
        Properties props = new Properties();
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   109
        props.loadFromXML(in);
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   110
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   111
        if (!props.equals(expected)) {
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   112
            System.err.println("loaded: " + props + ", expected: " + expected);
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   113
            throw new RuntimeException("Test failed");
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   114
        }
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   115
    }
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   116
14909
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   117
    /**
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   118
     * Test loadFromXML with unsupported encoding
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   119
     */
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   120
    static void testLoadWithBadEncoding() throws IOException {
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   121
        System.out.println("testLoadWithBadEncoding");
14187
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   122
        String s = "<?xml version=\"1.0\" encoding=\"BAD\"?>" +
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   123
                   "<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">" +
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   124
                   "<properties>" +
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   125
                   "<entry key=\"foo\">bar</entry>" +
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   126
                   "</properties>";
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   127
        ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes("UTF-8"));
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   128
        Properties props = new Properties();
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   129
        try {
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   130
            props.loadFromXML(in);
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   131
            throw new RuntimeException("UnsupportedEncodingException expected");
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   132
        } catch (UnsupportedEncodingException expected) { }
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   133
    }
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   134
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   135
    /**
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   136
     * Test storeToXML with unsupported encoding
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   137
     */
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   138
    static void testStoreWithBadEncoding() throws IOException {
14909
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   139
        System.out.println("testStoreWithBadEncoding");
14187
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   140
        Properties props = new Properties();
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   141
        props.put("foo", "bar");
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   142
        ByteArrayOutputStream out = new ByteArrayOutputStream();
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   143
        try {
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   144
            props.storeToXML(out, null, "BAD");
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   145
            throw new RuntimeException("UnsupportedEncodingException expected");
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   146
        } catch (UnsupportedEncodingException expected) { }
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   147
    }
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   148
14909
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   149
    /**
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   150
     * Test loadFromXML with malformed documents
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   151
     */
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   152
    static void testLoadWithMalformedDoc(Path dir) throws IOException {
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   153
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.xml")) {
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   154
            for (Path file: stream) {
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   155
                System.out.println("testLoadWithMalformedDoc, file=" + file.getFileName());
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   156
                try (InputStream in = Files.newInputStream(file)) {
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   157
                    Properties props = new Properties();
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   158
                    try {
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   159
                        props.loadFromXML(in);
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   160
                        throw new RuntimeException("InvalidPropertiesFormatException not thrown");
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   161
                    } catch (InvalidPropertiesFormatException x) {
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   162
                        System.out.println(x);
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   163
                    }
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   164
                }
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   165
            }
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   166
        }
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   167
    }
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   168
14029
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   169
    public static void main(String[] args) throws IOException {
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   170
14187
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   171
        testLoadAndStore("UTF-8");
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   172
        testLoadAndStore("UTF-16");
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   173
        testLoadWithoutEncoding();
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   174
        testLoadWithBadEncoding();
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   175
        testStoreWithBadEncoding();
14029
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   176
14909
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   177
        // malformed documents
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   178
        String src = System.getProperty("test.src");
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   179
        String subdir = "invalidxml";
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   180
        Path dir = (src == null) ? Paths.get(subdir) : Paths.get(src, subdir);
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   181
        testLoadWithMalformedDoc(dir);
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
   182
14187
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   183
        // re-run sanity test with security manager
14029
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   184
        Policy orig = Policy.getPolicy();
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   185
        Policy p = new SimplePolicy(new RuntimePermission("setSecurityManager"),
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   186
                                    new PropertyPermission("line.separator", "read"));
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   187
        Policy.setPolicy(p);
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   188
        System.setSecurityManager(new SecurityManager());
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   189
        try {
14187
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   190
            testLoadAndStore("UTF-8");
14029
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   191
        } finally {
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   192
            // turn off security manager and restore policy
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   193
            System.setSecurityManager(null);
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   194
            Policy.setPolicy(orig);
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   195
        }
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   196
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   197
    }
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   198
}