test/jdk/java/util/Properties/CompatibilityTest.java
branchJDK-8200758-branch
changeset 57061 ac44f38300b2
parent 57060 5103d6d2e796
parent 52859 413c28945e0f
child 57062 044e7a644ee3
equal deleted inserted replaced
57060:5103d6d2e796 57061:ac44f38300b2
     1 /*
       
     2  * Copyright (c) 2012, 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 
       
    24 /*
       
    25  * @test
       
    26  * @bug 8005280 8004371
       
    27  * @summary Compatibility test
       
    28  */
       
    29 
       
    30 import java.io.FileInputStream;
       
    31 import java.io.IOException;
       
    32 import java.io.InputStream;
       
    33 import java.util.Properties;
       
    34 
       
    35 /**
       
    36  * This is a behavior compatibility test.
       
    37  * Although not defined by the properties.dtd, the constructs
       
    38  * in Compatibility.xml are supported by the regular JDK XML
       
    39  * Provider.
       
    40  *
       
    41  * @author: Joe Wang
       
    42  */
       
    43 public class CompatibilityTest {
       
    44 
       
    45     public static void main(String[] args) {
       
    46         testInternalDTD();
       
    47     }
       
    48 
       
    49     /*
       
    50      * Not in the spec, but the constructs work with the current JDK
       
    51      */
       
    52     static void testInternalDTD() {
       
    53         String src = System.getProperty("test.src");
       
    54         if (src == null) {
       
    55             src = ".";
       
    56         }
       
    57         loadPropertyFile(src + "/Compatibility.xml");
       
    58     }
       
    59 
       
    60     /*
       
    61      * 'Store' the populated 'Property' with the specified 'Encoding Type' as an
       
    62      * XML file. Retrieve the same XML file and 'load' onto a new 'Property' object.
       
    63      */
       
    64     static void loadPropertyFile(String filename) {
       
    65         try (InputStream in = new FileInputStream(filename)) {
       
    66             Properties prop = new Properties();
       
    67             prop.loadFromXML(in);
       
    68             verifyProperites(prop);
       
    69         } catch (IOException ex) {
       
    70             fail(ex.getMessage());
       
    71         }
       
    72     }
       
    73 
       
    74     /*
       
    75      * This method verifies the first key-value with the original string.
       
    76      */
       
    77     static void verifyProperites(Properties prop) {
       
    78         try {
       
    79             for (String key : prop.stringPropertyNames()) {
       
    80                 String val = prop.getProperty(key);
       
    81                 if (key.equals("Key1")) {
       
    82                     if (!val.equals("value1")) {
       
    83                         fail("Key:" + key + "'s value: \nExpected: value1\nFound: " + val);
       
    84                     }
       
    85                 } else if (key.equals("Key2")) {
       
    86                     if (!val.equals("<value2>")) {
       
    87                         fail("Key:" + key + "'s value: \nExpected: <value2>\nFound: " + val);
       
    88                     }
       
    89                 } else if (key.equals("Key3")) {
       
    90                     if (!val.equals("value3")) {
       
    91                         fail("Key:" + key + "'s value: \nExpected: value3\nFound: " + val);
       
    92                     }
       
    93                 }
       
    94             }
       
    95         } catch (Exception e) {
       
    96             fail(e.getMessage());
       
    97         }
       
    98 
       
    99     }
       
   100 
       
   101     static void fail(String err) {
       
   102         throw new RuntimeException(err);
       
   103     }
       
   104 
       
   105 }