1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.prefs;
18
19 import java.util.prefs.BackingStoreException;
20 import java.util.prefs.Preferences;
21
22 import junit.framework.Test;
23 import junit.framework.TestSuite;
24
25 import org.apache.jetspeed.components.util.DatasourceEnabledSpringTestCase;
26
27 /***
28 * <p>
29 * Unit testing for {@link Preferences}.
30 * </p>
31 *
32 * @author <a href="dlestrat@yahoo.com">David Le Strat </a>
33 */
34 public class TestPreferences extends DatasourceEnabledSpringTestCase
35 {
36
37 /***
38 * @see junit.framework.TestCase#setUp()
39 */
40 public void setUp() throws Exception
41 {
42 super.setUp();
43
44
45 clearChildren(Preferences.userRoot());
46 clearChildren(Preferences.systemRoot());
47 }
48
49 /***
50 * @see junit.framework.TestCase#tearDown()
51 */
52 public void tearDown() throws Exception
53 {
54 super.tearDown();
55 }
56
57 /***
58 * @return The test suite.
59 */
60 public static Test suite()
61 {
62
63 return new TestSuite(TestPreferences.class);
64 }
65
66 /***
67 * <p>
68 * Test user root.
69 * </p>
70 */
71 public void testUserRoot()
72 {
73
74 Preferences prefs = Preferences.userRoot();
75 if (null != prefs)
76 {
77 assertTrue("expected user root == '/', " + prefs.absolutePath(), prefs.absolutePath().equals("/"));
78 }
79 else
80 {
81 assertTrue("expected user root == '/', " + prefs, false);
82 }
83 }
84
85 /***
86 * <p>
87 * Test system root.
88 * </p>
89 */
90 public void testSystemRoot()
91 {
92 Preferences prefs = Preferences.systemRoot();
93 if (null != prefs)
94 {
95 assertTrue("expected system root == '/', " + prefs.absolutePath(), prefs.absolutePath().equals("/"));
96 }
97 else
98 {
99 assertTrue("expected system root == '/', " + prefs, false);
100 }
101 }
102
103 /***
104 * <p>
105 * Test node and whether children exist under a given node.
106 * </p>
107 */
108 public void testNodeAndChildrenNames()
109 {
110 Preferences prefs = Preferences.userRoot();
111
112 try
113 {
114 String[] childrenNames = prefs.childrenNames();
115 if (childrenNames.length > 0)
116 {
117 assertTrue("expected no children, " + childrenNames.length + ", " + childrenNames[0],
118 childrenNames.length == 0);
119 }
120 }
121 catch (BackingStoreException bse)
122 {
123 assertTrue("backing store exception: " + bse, false);
124 }
125
126
127
128 Preferences prefs0 = Preferences.userRoot().node("/an1/san1");
129 assertNotNull("should not be null", prefs0);
130 assertTrue("expected node == /an1/san1, " + prefs0.absolutePath(), prefs0.absolutePath().equals("/an1/san1"));
131
132
133 Preferences prefs1 = Preferences.userRoot().node("/an1/san1");
134 assertNotNull("should not be null", prefs1);
135 assertTrue("expected node == /an1/san1, " + prefs1.absolutePath(), prefs1.absolutePath().equals("/an1/san1"));
136
137
138 Preferences prefs3 = Preferences.userRoot().node("/an1");
139 Preferences prefs4 = prefs3.node("rn1/srn1");
140 assertNotNull("should not be null", prefs4);
141 assertTrue("expected node == /an1/rn1/srn1, " + prefs4.absolutePath(), prefs4.absolutePath().equals(
142 "/an1/rn1/srn1"));
143
144 try
145 {
146 String[] childrenNames = prefs3.childrenNames();
147 assertEquals("should have 2 children", 2, childrenNames.length);
148 }
149 catch (BackingStoreException bse)
150 {
151 assertTrue("backing store exception: " + bse, false);
152 }
153
154
155 try
156 {
157 prefs3.removeNode();
158 }
159 catch (BackingStoreException bse)
160 {
161 assertTrue("backing store exception: " + bse, false);
162 }
163
164 }
165
166 /***
167 * <p>
168 * Test adding properties to a property set node and get property keys for a given node.
169 * </p>
170 *
171 * @throws Exception
172 */
173 public void testPropertyAndPropertyKeys() throws Exception
174 {
175
176
177
178
179 Preferences pref0 = Preferences.userRoot();
180 try
181 {
182 String[] propertyKeys = pref0.keys();
183 if (propertyKeys.length > 0)
184 {
185 assertTrue("expected no children, " + propertyKeys.length + ", " + propertyKeys[0],
186 propertyKeys.length == 0);
187 }
188 }
189 catch (BackingStoreException bse)
190 {
191 assertTrue("backing store exception: " + bse, false);
192 }
193
194 pref0.put("propertyName0", "true");
195 String prop = pref0.get("propertyName0", null);
196 assertTrue("should be prop == true.", prop.equals("true"));
197
198
199 Preferences pref1 = Preferences.userRoot().node("/user/principal1/propertyset1");
200 pref1.put("propertyName0", "true");
201 String prop1 = pref1.get("propertyName0", null);
202 assertTrue("expected prop1 == true, " + prop1, prop1.equals("true"));
203
204
205 try
206 {
207 String[] propertyKeys = pref1.keys();
208 assertEquals("expected 1 child, ", 1, propertyKeys.length);
209 }
210 catch (BackingStoreException bse)
211 {
212 assertTrue("backing store exception: " + bse, false);
213 }
214
215
216 pref1.remove("propertyName0");
217 prop1 = pref1.get("propertyName0", null);
218 assertNull("should be null.", prop1);
219
220
221 pref1.put("propertyName0", "true");
222 prop1 = pref1.get("propertyName0", null);
223 assertTrue("expected prop1 == true, " + prop1, prop1.equals("true"));
224
225 try
226 {
227 Preferences pref2 = Preferences.userRoot().node("/user");
228 pref2.removeNode();
229 }
230 catch (BackingStoreException bse)
231 {
232 assertTrue("backing store exception: " + bse, false);
233 }
234
235 }
236
237 public void testNodeRemoval() throws Exception
238 {
239 Preferences prefs = Preferences.userRoot();
240
241 final String test_node = "removeTest";
242
243 assertFalse(prefs.nodeExists(test_node));
244
245 Preferences removeNode = prefs.node(test_node);
246
247 assertNotNull(removeNode);
248
249
250
251 removeNode.removeNode();
252 assertFalse(prefs.nodeExists(test_node));
253
254 try
255 {
256 removeNode.childrenNames();
257 assertFalse("An IllegalStateException should have been thrown by the AbtractPreferences class", true);
258 }
259 catch (IllegalStateException e)
260 {
261
262 }
263 }
264
265 /***
266 * <p>
267 * Clear all the children.
268 * </p>
269 *
270 * @param node
271 * @throws Exception
272 */
273 protected void clearChildren(Preferences node) throws Exception
274 {
275 String[] names = node.childrenNames();
276 for (int i = 0; i < names.length; i++)
277 {
278 node.node(names[i]).removeNode();
279 }
280
281 String[] keys = node.keys();
282 for (int j = 0; j < keys.length; j++)
283 {
284 node.remove(keys[j]);
285 }
286 }
287
288 /***
289 * @see org.apache.jetspeed.components.test.AbstractSpringTestCase#getConfigurations()
290 */
291 protected String[] getConfigurations()
292 {
293 return new String[]
294 { "prefs.xml", "transaction.xml", "cache.xml" };
295 }
296 }