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@1264
|
31 |
import java.util.Arrays;
|
jtulach@1263
|
32 |
import java.util.prefs.Preferences;
|
jtulach@1263
|
33 |
|
jtulach@1263
|
34 |
/** Checks whether reading and writing via standard DOM and simplified API
|
jtulach@1263
|
35 |
* results in same results.
|
jtulach@1263
|
36 |
*
|
jtulach@1263
|
37 |
* @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jtulach@1263
|
38 |
*/
|
jtulach@1263
|
39 |
public class XMLPreferencesTest {
|
jtulach@1263
|
40 |
private static PrefsXmlSupport FULL = new com.sun.xml.internal.PrefsXmlSupportImpl();
|
jtulach@1263
|
41 |
private static PrefsXmlSupport SIMPLE = new sun.util.xml.DefaultPrefsXmlSupport();
|
jtulach@1263
|
42 |
|
jtulach@1263
|
43 |
|
jtulach@1263
|
44 |
public static void main(String[] args) throws Exception {
|
jtulach@1263
|
45 |
XMLPreferencesTest test = new XMLPreferencesTest();
|
jtulach@1263
|
46 |
test.testCompareOutput();
|
jtulach@1263
|
47 |
test.testCompareInput();
|
jtulach@1263
|
48 |
}
|
jtulach@1263
|
49 |
|
jtulach@1263
|
50 |
|
jtulach@1263
|
51 |
public XMLPreferencesTest() {
|
jtulach@1263
|
52 |
}
|
jtulach@1263
|
53 |
|
jtulach@1263
|
54 |
public void testCompareOutput() throws Exception {
|
jtulach@1263
|
55 |
Preferences p = Preferences.userRoot().node("a/b/c");
|
jtulach@1263
|
56 |
p.put("ahoj", "simple");
|
jtulach@1263
|
57 |
p.putInt("kuk", 1);
|
jtulach@1263
|
58 |
p.putBoolean("multi", true);
|
jtulach@1263
|
59 |
p.node("child").putBoolean("data", false);
|
jtulach@1263
|
60 |
p.node("empty");
|
jtulach@1263
|
61 |
p.parent().putDouble("visible", 1.0);
|
jtulach@1263
|
62 |
|
jtulach@1263
|
63 |
ByteArrayOutputStream full = new ByteArrayOutputStream();
|
jtulach@1263
|
64 |
FULL.export(full, p, true);
|
jtulach@1263
|
65 |
|
jtulach@1263
|
66 |
ByteArrayOutputStream simple = new ByteArrayOutputStream();
|
jtulach@1263
|
67 |
SIMPLE.export(simple, p, true);
|
jtulach@1263
|
68 |
if (full.toString().equals(simple.toString())) {
|
jtulach@1263
|
69 |
// OK
|
jtulach@1263
|
70 |
System.err.println("OK: testCompareOutput");
|
jtulach@1263
|
71 |
} else {
|
jtulach@1263
|
72 |
assert false :
|
jtulach@1263
|
73 |
"Full version differs from simplified. Full:\n" + full + "\nSimple:\n" + simple;
|
jtulach@1263
|
74 |
}
|
jtulach@1263
|
75 |
}
|
jtulach@1263
|
76 |
|
jtulach@1263
|
77 |
public void testCompareInput() throws Exception {
|
jtulach@1264
|
78 |
Preferences.userRoot().node("test1/b/c").removeNode();
|
jtulach@1264
|
79 |
Preferences.userRoot().node("test2/b/c").removeNode();
|
jtulach@1263
|
80 |
|
jtulach@1264
|
81 |
String text = "<?xml version='1.0' encoding='UTF-8' standalone='no'?>" +
|
jtulach@1264
|
82 |
"<!DOCTYPE preferences SYSTEM 'http://java.sun.com/dtd/preferences.dtd'>" +
|
jtulach@1264
|
83 |
" <preferences EXTERNAL_XML_VERSION='1.0'>" +
|
jtulach@1264
|
84 |
" <root type='user'>\n\n\n" +
|
jtulach@1264
|
85 |
"<map/><node name='test1'><map/><node name='b'>" +
|
jtulach@1264
|
86 |
"<map/><node name='c'><map>" +
|
jtulach@1264
|
87 |
" <entry key='ahoj' value='simple'/>" +
|
jtulach@1264
|
88 |
" <entry key='kuk' value='1'/>" +
|
jtulach@1264
|
89 |
" <entry key='multi' value='true'/> \n" +
|
jtulach@1264
|
90 |
"</map>" +
|
jtulach@1264
|
91 |
"<node name='child'>" +
|
jtulach@1264
|
92 |
"<map>" +
|
jtulach@1264
|
93 |
"<entry key='data' value='false'/>" +
|
jtulach@1264
|
94 |
"</map>" +
|
jtulach@1264
|
95 |
"</node><node name='empty'><map/>" +
|
jtulach@1264
|
96 |
"</node>" +
|
jtulach@1264
|
97 |
"</node>" +
|
jtulach@1264
|
98 |
"</node>" +
|
jtulach@1264
|
99 |
"</node>" +
|
jtulach@1264
|
100 |
"</root>" +
|
jtulach@1264
|
101 |
"</preferences>";
|
jtulach@1264
|
102 |
|
jtulach@1263
|
103 |
{
|
jtulach@1263
|
104 |
ByteArrayInputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
|
jtulach@1264
|
105 |
FULL.importPreferences(is);
|
jtulach@1263
|
106 |
}
|
jtulach@1263
|
107 |
|
jtulach@1263
|
108 |
{
|
jtulach@1264
|
109 |
text = text.replace("'test1'", "'test2'");
|
jtulach@1263
|
110 |
ByteArrayInputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
|
jtulach@1264
|
111 |
SIMPLE.importPreferences(is);
|
jtulach@1263
|
112 |
}
|
jtulach@1263
|
113 |
|
jtulach@1264
|
114 |
assert Preferences.userRoot().nodeExists("test1/b/c") : "Node in user imported";
|
jtulach@1264
|
115 |
assert Preferences.userRoot().nodeExists("test2/b/c") : "Node in system imported";
|
jtulach@1264
|
116 |
|
jtulach@1264
|
117 |
Preferences u = Preferences.userRoot().node("test1/b/c");
|
jtulach@1264
|
118 |
Preferences s = Preferences.userRoot().node("test2/b/c");
|
jtulach@1264
|
119 |
|
jtulach@1264
|
120 |
assert Arrays.equals(u.keys(), s.keys()) : "Same keys in both nodes";
|
jtulach@1264
|
121 |
|
jtulach@1264
|
122 |
for (String k : u.keys()) {
|
jtulach@1264
|
123 |
assert u.get(k, "Neco").equals(s.get(k, "Cone")) : "Same values for " + k;
|
jtulach@1264
|
124 |
}
|
jtulach@1263
|
125 |
System.err.println("OK: testCompareInput");
|
jtulach@1263
|
126 |
}
|
jtulach@1263
|
127 |
|
jtulach@1263
|
128 |
} |