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
jtulach@1263
     1
/*
jtulach@1263
     2
 * Copyright 2002-2006 Sun Microsystems, Inc.  All Rights Reserved.
jtulach@1263
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jtulach@1263
     4
 *
jtulach@1263
     5
 * This code is free software; you can redistribute it and/or modify it
jtulach@1263
     6
 * under the terms of the GNU General Public License version 2 only, as
jtulach@1263
     7
 * published by the Free Software Foundation.  Sun designates this
jtulach@1263
     8
 * particular file as subject to the "Classpath" exception as provided
jtulach@1263
     9
 * by Sun in the LICENSE file that accompanied this code.
jtulach@1263
    10
 *
jtulach@1263
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jtulach@1263
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jtulach@1263
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jtulach@1263
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jtulach@1263
    15
 * accompanied this code).
jtulach@1263
    16
 *
jtulach@1263
    17
 * You should have received a copy of the GNU General Public License version
jtulach@1263
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jtulach@1263
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jtulach@1263
    20
 *
jtulach@1263
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
jtulach@1263
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
jtulach@1263
    23
 * have any questions.
jtulach@1263
    24
 */
jtulach@1263
    25
jtulach@1263
    26
jtulach@1263
    27
import java.io.ByteArrayInputStream;
jtulach@1263
    28
import sun.util.xml.PrefsXmlSupport;
jtulach@1263
    29
jtulach@1263
    30
import java.io.ByteArrayOutputStream;
jtulach@1263
    31
import java.util.prefs.Preferences;
jtulach@1263
    32
jtulach@1263
    33
/** Checks whether reading and writing via standard DOM and simplified API
jtulach@1263
    34
 * results in same results.
jtulach@1263
    35
 *
jtulach@1263
    36
 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jtulach@1263
    37
 */
jtulach@1263
    38
public class XMLPreferencesTest {
jtulach@1263
    39
    private static PrefsXmlSupport FULL = new com.sun.xml.internal.PrefsXmlSupportImpl();
jtulach@1263
    40
    private static PrefsXmlSupport SIMPLE = new sun.util.xml.DefaultPrefsXmlSupport();
jtulach@1263
    41
jtulach@1263
    42
jtulach@1263
    43
    public static void main(String[] args) throws Exception {
jtulach@1263
    44
        XMLPreferencesTest test = new XMLPreferencesTest();
jtulach@1263
    45
        test.testCompareOutput();
jtulach@1263
    46
        test.testCompareInput();
jtulach@1263
    47
    }
jtulach@1263
    48
jtulach@1263
    49
jtulach@1263
    50
    public XMLPreferencesTest() {
jtulach@1263
    51
    }
jtulach@1263
    52
jtulach@1263
    53
    public void testCompareOutput() throws Exception {
jtulach@1263
    54
        Preferences p = Preferences.userRoot().node("a/b/c");
jtulach@1263
    55
        p.put("ahoj", "simple");
jtulach@1263
    56
        p.putInt("kuk", 1);
jtulach@1263
    57
        p.putBoolean("multi", true);
jtulach@1263
    58
        p.node("child").putBoolean("data", false);
jtulach@1263
    59
        p.node("empty");
jtulach@1263
    60
        p.parent().putDouble("visible", 1.0);
jtulach@1263
    61
jtulach@1263
    62
        ByteArrayOutputStream full = new ByteArrayOutputStream();
jtulach@1263
    63
        FULL.export(full, p, true);
jtulach@1263
    64
jtulach@1263
    65
        ByteArrayOutputStream simple = new ByteArrayOutputStream();
jtulach@1263
    66
        SIMPLE.export(simple, p, true);
jtulach@1263
    67
        if (full.toString().equals(simple.toString())) {
jtulach@1263
    68
            // OK
jtulach@1263
    69
            System.err.println("OK: testCompareOutput");
jtulach@1263
    70
        } else {
jtulach@1263
    71
            assert false :
jtulach@1263
    72
                "Full version differs from simplified. Full:\n" + full + "\nSimple:\n" + simple;
jtulach@1263
    73
        }
jtulach@1263
    74
    }
jtulach@1263
    75
jtulach@1263
    76
    public void testCompareInput() throws Exception {
jtulach@1263
    77
        /*
jtulach@1263
    78
        String text = "";
jtulach@1263
    79
jtulach@1263
    80
        Properties p1 = new Properties();
jtulach@1263
    81
        {
jtulach@1263
    82
            ByteArrayInputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
jtulach@1263
    83
            FULL.load(p1, is);
jtulach@1263
    84
        }
jtulach@1263
    85
jtulach@1263
    86
        Properties p2 = new Properties();
jtulach@1263
    87
        {
jtulach@1263
    88
            ByteArrayInputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
jtulach@1263
    89
            SIMPLE.load(p2, is);
jtulach@1263
    90
        }
jtulach@1263
    91
jtulach@1263
    92
        assert p1.equals(p2) : "P1: " + p1 + "\nP2: " + p2;
jtulach@1263
    93
        System.err.println("OK: testCompareInput");
jtulach@1263
    94
         */
jtulach@1263
    95
    }
jtulach@1263
    96
jtulach@1263
    97
}