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