jdk/test/java/util/Properties/LoadAndStoreXML.java
author alanb
Wed, 17 Oct 2012 11:43:56 +0100
changeset 14187 9e193f8bab66
parent 14029 c684694164c2
child 14909 eded175efec2
permissions -rw-r--r--
8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported Reviewed-by: mchung, chegar
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
14187
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
    26
 * @bug 8000354 8000685
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
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    28
 */
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    29
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    30
import java.io.*;
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    31
import java.util.*;
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    32
import java.security.*;
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    33
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    34
public class LoadAndStoreXML {
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    35
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
     * 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
    38
     * all code sources and protection domains.
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
    static class SimplePolicy extends Policy {
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    41
        private final Permissions perms;
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
        public SimplePolicy(Permission...permissions) {
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    44
            perms = new Permissions();
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    45
            for (Permission permission : permissions)
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    46
                perms.add(permission);
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    47
        }
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    48
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    49
        @Override
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    50
        public PermissionCollection getPermissions(CodeSource cs) {
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    51
            return perms;
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    52
        }
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    53
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    54
        @Override
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    55
        public PermissionCollection getPermissions(ProtectionDomain pd) {
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    56
            return perms;
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    57
        }
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    58
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    59
        @Override
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    60
        public boolean implies(ProtectionDomain pd, Permission p) {
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    61
            return perms.implies(p);
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    62
        }
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    63
    }
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    64
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
     * 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
    67
     * read with Properties#loadFromXML.
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    68
     */
14187
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
    69
    static void testLoadAndStore(String encoding) throws IOException {
14029
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    70
        Properties props = new Properties();
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    71
        props.put("k1", "foo");
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    72
        props.put("k2", "bar");
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    73
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    74
        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
    75
        props.storeToXML(out, null, encoding);
14029
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    76
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    77
        Properties p = new Properties();
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    78
        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    79
        p.loadFromXML(in);
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    80
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    81
        if (!p.equals(props)) {
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    82
            System.err.println("stored: " + props);
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    83
            System.err.println("loaded: " + p);
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    84
            throw new RuntimeException("Test failed");
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
    }
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
    87
14187
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
    88
    /**
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
    89
     * 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
    90
     */
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
    91
    static void testLoadWithoutEncoding() throws IOException {
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
    92
        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
    93
        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
    94
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
    95
        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
    96
                   "<!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
    97
                   "<properties>" +
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
    98
                   "<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
    99
                   "</properties>";
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   100
        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
   101
        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
   102
        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
   103
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   104
        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
   105
            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
   106
            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
   107
        }
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   108
    }
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   109
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
      * Test loadFromXML 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
   112
      */
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   113
     static void testLoadWithBadEncoding() throws IOException {
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   114
        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
   115
                   "<!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
   116
                   "<properties>" +
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   117
                   "<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
   118
                   "</properties>";
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   119
        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
   120
        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
   121
        try {
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   122
            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
   123
            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
   124
        } 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
   125
    }
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   126
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   127
    /**
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   128
     * 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
   129
     */
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   130
    static void testStoreWithBadEncoding() throws IOException {
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   131
        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
   132
        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
   133
        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
   134
        try {
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   135
            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
   136
            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
   137
        } 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
   138
    }
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   139
14029
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   140
    public static void main(String[] args) throws IOException {
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   141
14187
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   142
        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
   143
        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
   144
        testLoadWithoutEncoding();
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   145
        testLoadWithBadEncoding();
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   146
        testStoreWithBadEncoding();
14029
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   147
14187
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   148
        // re-run sanity test with security manager
14029
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   149
        Policy orig = Policy.getPolicy();
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   150
        Policy p = new SimplePolicy(new RuntimePermission("setSecurityManager"),
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   151
                                    new PropertyPermission("line.separator", "read"));
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   152
        Policy.setPolicy(p);
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   153
        System.setSecurityManager(new SecurityManager());
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   154
        try {
14187
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   155
            testLoadAndStore("UTF-8");
14029
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   156
        } finally {
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   157
            // turn off security manager and restore policy
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   158
            System.setSecurityManager(null);
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   159
            Policy.setPolicy(orig);
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   160
        }
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   161
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   162
    }
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents:
diff changeset
   163
}