test/java/util/prefs/Preferences/XMLPreferencesTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Wed, 24 Jun 2009 17:50:25 +0200
branchxml-sax-and-dom-2
changeset 1264 601d21ee9aa6
parent 1263 24b6c30fbf71
permissions -rw-r--r--
Reading of exported preferences yields the same results if done via DOM as well as nanoXML parser
     1 /*
     2  * Copyright 2002-2006 Sun Microsystems, Inc.  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.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    25 
    26 
    27 import java.io.ByteArrayInputStream;
    28 import sun.util.xml.PrefsXmlSupport;
    29 
    30 import java.io.ByteArrayOutputStream;
    31 import java.util.Arrays;
    32 import java.util.prefs.Preferences;
    33 
    34 /** Checks whether reading and writing via standard DOM and simplified API
    35  * results in same results.
    36  *
    37  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    38  */
    39 public class XMLPreferencesTest {
    40     private static PrefsXmlSupport FULL = new com.sun.xml.internal.PrefsXmlSupportImpl();
    41     private static PrefsXmlSupport SIMPLE = new sun.util.xml.DefaultPrefsXmlSupport();
    42 
    43 
    44     public static void main(String[] args) throws Exception {
    45         XMLPreferencesTest test = new XMLPreferencesTest();
    46         test.testCompareOutput();
    47         test.testCompareInput();
    48     }
    49 
    50 
    51     public XMLPreferencesTest() {
    52     }
    53 
    54     public void testCompareOutput() throws Exception {
    55         Preferences p = Preferences.userRoot().node("a/b/c");
    56         p.put("ahoj", "simple");
    57         p.putInt("kuk", 1);
    58         p.putBoolean("multi", true);
    59         p.node("child").putBoolean("data", false);
    60         p.node("empty");
    61         p.parent().putDouble("visible", 1.0);
    62 
    63         ByteArrayOutputStream full = new ByteArrayOutputStream();
    64         FULL.export(full, p, true);
    65 
    66         ByteArrayOutputStream simple = new ByteArrayOutputStream();
    67         SIMPLE.export(simple, p, true);
    68         if (full.toString().equals(simple.toString())) {
    69             // OK
    70             System.err.println("OK: testCompareOutput");
    71         } else {
    72             assert false :
    73                 "Full version differs from simplified. Full:\n" + full + "\nSimple:\n" + simple;
    74         }
    75     }
    76 
    77     public void testCompareInput() throws Exception {
    78         Preferences.userRoot().node("test1/b/c").removeNode();
    79         Preferences.userRoot().node("test2/b/c").removeNode();
    80 
    81         String text = "<?xml version='1.0' encoding='UTF-8' standalone='no'?>" +
    82                 "<!DOCTYPE preferences SYSTEM 'http://java.sun.com/dtd/preferences.dtd'>" +
    83                 "            <preferences EXTERNAL_XML_VERSION='1.0'>" +
    84                 "    <root type='user'>\n\n\n" +
    85                 "<map/><node name='test1'><map/><node name='b'>" +
    86                 "<map/><node name='c'><map>" +
    87                 "    <entry key='ahoj' value='simple'/>" +
    88                 "    <entry key='kuk' value='1'/>" +
    89                 "    <entry key='multi' value='true'/>        \n" +
    90                 "</map>" +
    91                 "<node name='child'>" +
    92                 "<map>" +
    93                 "<entry key='data' value='false'/>" +
    94                 "</map>" +
    95                 "</node><node name='empty'><map/>" +
    96                 "</node>" +
    97                 "</node>" +
    98                 "</node>" +
    99                 "</node>" +
   100                 "</root>" +
   101                 "</preferences>";
   102 
   103         {
   104             ByteArrayInputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
   105             FULL.importPreferences(is);
   106         }
   107 
   108         {
   109             text = text.replace("'test1'", "'test2'");
   110             ByteArrayInputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
   111             SIMPLE.importPreferences(is);
   112         }
   113 
   114         assert Preferences.userRoot().nodeExists("test1/b/c") : "Node in user imported";
   115         assert Preferences.userRoot().nodeExists("test2/b/c") : "Node in system imported";
   116 
   117         Preferences u = Preferences.userRoot().node("test1/b/c");
   118         Preferences s = Preferences.userRoot().node("test2/b/c");
   119 
   120         assert Arrays.equals(u.keys(), s.keys()) : "Same keys in both nodes";
   121 
   122         for (String k : u.keys()) {
   123             assert u.get(k, "Neco").equals(s.get(k, "Cone")) : "Same values for " + k;
   124         }
   125         System.err.println("OK: testCompareInput");
   126     }
   127 
   128 }