Reading of exported preferences yields the same results if done via DOM as well as nanoXML parser
2 * Copyright 2002-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
27 import java.io.ByteArrayInputStream;
28 import sun.util.xml.PrefsXmlSupport;
30 import java.io.ByteArrayOutputStream;
31 import java.util.Arrays;
32 import java.util.prefs.Preferences;
34 /** Checks whether reading and writing via standard DOM and simplified API
35 * results in same results.
37 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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();
44 public static void main(String[] args) throws Exception {
45 XMLPreferencesTest test = new XMLPreferencesTest();
46 test.testCompareOutput();
47 test.testCompareInput();
51 public XMLPreferencesTest() {
54 public void testCompareOutput() throws Exception {
55 Preferences p = Preferences.userRoot().node("a/b/c");
56 p.put("ahoj", "simple");
58 p.putBoolean("multi", true);
59 p.node("child").putBoolean("data", false);
61 p.parent().putDouble("visible", 1.0);
63 ByteArrayOutputStream full = new ByteArrayOutputStream();
64 FULL.export(full, p, true);
66 ByteArrayOutputStream simple = new ByteArrayOutputStream();
67 SIMPLE.export(simple, p, true);
68 if (full.toString().equals(simple.toString())) {
70 System.err.println("OK: testCompareOutput");
73 "Full version differs from simplified. Full:\n" + full + "\nSimple:\n" + simple;
77 public void testCompareInput() throws Exception {
78 Preferences.userRoot().node("test1/b/c").removeNode();
79 Preferences.userRoot().node("test2/b/c").removeNode();
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" +
91 "<node name='child'>" +
93 "<entry key='data' value='false'/>" +
95 "</node><node name='empty'><map/>" +
104 ByteArrayInputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
105 FULL.importPreferences(is);
109 text = text.replace("'test1'", "'test2'");
110 ByteArrayInputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
111 SIMPLE.importPreferences(is);
114 assert Preferences.userRoot().nodeExists("test1/b/c") : "Node in user imported";
115 assert Preferences.userRoot().nodeExists("test2/b/c") : "Node in system imported";
117 Preferences u = Preferences.userRoot().node("test1/b/c");
118 Preferences s = Preferences.userRoot().node("test2/b/c");
120 assert Arrays.equals(u.keys(), s.keys()) : "Same keys in both nodes";
122 for (String k : u.keys()) {
123 assert u.get(k, "Neco").equals(s.get(k, "Cone")) : "Same values for " + k;
125 System.err.println("OK: testCompareInput");