test/java/util/prefs/Preferences/XMLPreferencesTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Wed, 24 Jun 2009 17:29:29 +0200
branchxml-sax-and-dom-2
changeset 1263 24b6c30fbf71
child 1264 601d21ee9aa6
permissions -rw-r--r--
Simple output of Preferences is the same as the original one via DOM
     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.prefs.Preferences;
    32 
    33 /** Checks whether reading and writing via standard DOM and simplified API
    34  * results in same results.
    35  *
    36  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    37  */
    38 public class XMLPreferencesTest {
    39     private static PrefsXmlSupport FULL = new com.sun.xml.internal.PrefsXmlSupportImpl();
    40     private static PrefsXmlSupport SIMPLE = new sun.util.xml.DefaultPrefsXmlSupport();
    41 
    42 
    43     public static void main(String[] args) throws Exception {
    44         XMLPreferencesTest test = new XMLPreferencesTest();
    45         test.testCompareOutput();
    46         test.testCompareInput();
    47     }
    48 
    49 
    50     public XMLPreferencesTest() {
    51     }
    52 
    53     public void testCompareOutput() throws Exception {
    54         Preferences p = Preferences.userRoot().node("a/b/c");
    55         p.put("ahoj", "simple");
    56         p.putInt("kuk", 1);
    57         p.putBoolean("multi", true);
    58         p.node("child").putBoolean("data", false);
    59         p.node("empty");
    60         p.parent().putDouble("visible", 1.0);
    61 
    62         ByteArrayOutputStream full = new ByteArrayOutputStream();
    63         FULL.export(full, p, true);
    64 
    65         ByteArrayOutputStream simple = new ByteArrayOutputStream();
    66         SIMPLE.export(simple, p, true);
    67         if (full.toString().equals(simple.toString())) {
    68             // OK
    69             System.err.println("OK: testCompareOutput");
    70         } else {
    71             assert false :
    72                 "Full version differs from simplified. Full:\n" + full + "\nSimple:\n" + simple;
    73         }
    74     }
    75 
    76     public void testCompareInput() throws Exception {
    77         /*
    78         String text = "";
    79 
    80         Properties p1 = new Properties();
    81         {
    82             ByteArrayInputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
    83             FULL.load(p1, is);
    84         }
    85 
    86         Properties p2 = new Properties();
    87         {
    88             ByteArrayInputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
    89             SIMPLE.load(p2, is);
    90         }
    91 
    92         assert p1.equals(p2) : "P1: " + p1 + "\nP2: " + p2;
    93         System.err.println("OK: testCompareInput");
    94          */
    95     }
    96 
    97 }