KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > preferences > TestPreferences


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.startup.preferences;
21
22 import java.lang.reflect.Field JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27 import java.util.prefs.AbstractPreferences JavaDoc;
28 import java.util.prefs.BackingStoreException JavaDoc;
29 import java.util.prefs.NodeChangeEvent JavaDoc;
30 import java.util.prefs.NodeChangeListener JavaDoc;
31 import java.util.prefs.PreferenceChangeEvent JavaDoc;
32 import java.util.prefs.PreferenceChangeListener JavaDoc;
33 import java.util.prefs.Preferences JavaDoc;
34
35 /**
36  *
37  * @author Radek Matous
38  */

39 public class TestPreferences extends NbPreferencesTest.TestBasicSetup {
40     public TestPreferences(String JavaDoc testName) {
41         super(testName);
42     }
43     
44     protected void setUp() throws Exception JavaDoc {
45         super.setUp();
46         clearWorkDir();
47         //if diabled - all tests should pass with default impl. of Preferences API
48
assertSame(new NbPreferencesFactory().userRoot(), Preferences.userRoot());
49         
50         Preferences.userRoot().flush();
51     }
52
53    
54     private Preferences JavaDoc getPreferencesNode() {
55         return getUserPreferencesNode();
56     }
57     
58     private Preferences JavaDoc getUserPreferencesNode() {
59         return Preferences.userNodeForPackage(NbPreferencesTest.class).node(getName());
60     }
61     
62     private Preferences JavaDoc getSystemPreferencesNode() {
63         return Preferences.systemNodeForPackage(NbPreferencesTest.class).node(getName());
64     }
65     
66     
67     public void testUserRoot() throws Exception JavaDoc {
68         try {
69             Preferences.userRoot().removeNode();
70             fail();
71         } catch (UnsupportedOperationException JavaDoc ex) {
72         }
73     }
74     
75
76     /* We do not really care about enforcing these limits:
77     public void testKeyExceededSize() throws Exception {
78         Preferences pref = getPreferencesNode();
79         StringBuffer sb = new StringBuffer();
80         for (; sb.toString().length() < Preferences.MAX_KEY_LENGTH + 1; ) {
81             sb.append("1234567890");
82         }
83         assertTrue(sb.toString().length() > Preferences.MAX_KEY_LENGTH);
84         try {
85             pref.put(sb.toString(),"sss");
86             fail();
87         } catch (IllegalArgumentException iax) {
88         }
89     }
90     
91     public void testValueExceededSize() throws Exception {
92         Preferences pref = getPreferencesNode();
93         StringBuffer sb = new StringBuffer();
94         for (; sb.toString().length() < Preferences.MAX_VALUE_LENGTH + 1; ) {
95             sb.append("1234567890");
96         }
97         assertTrue(sb.toString().length() > Preferences.MAX_VALUE_LENGTH);
98         try {
99             pref.put("sss", sb.toString());
100             fail();
101         } catch (IllegalArgumentException iax) {
102         }
103     }
104      */

105     
106     public void testNameExceededSize() throws Exception JavaDoc {
107         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
108         for (; sb.toString().length() < Preferences.MAX_NAME_LENGTH + 1; ) {
109             sb.append("1234567890");
110         }
111         
112         assertTrue(sb.toString().length() > Preferences.MAX_NAME_LENGTH);
113         try {
114             Preferences JavaDoc pref =
115                     getPreferencesNode().node(sb.toString());
116             fail();
117         } catch (IllegalArgumentException JavaDoc iax) {
118         }
119     }
120     
121     public void testNullParameter() throws Exception JavaDoc {
122         Preferences JavaDoc pref = getPreferencesNode();
123         try {
124             pref.get(null, "value");
125             fail();
126         } catch(NullPointerException JavaDoc npe) {
127         }
128         
129         try {
130             //null permited here
131
pref.get("key", null);
132         } catch(NullPointerException JavaDoc npe) {
133             fail();
134         }
135         
136         try {
137             pref.node(null);
138             fail();
139         } catch(NullPointerException JavaDoc npe) {
140         }
141         
142         try {
143             pref.node("node2/");
144             fail();
145         } catch(IllegalArgumentException JavaDoc iax) {
146         }
147         
148     }
149     
150     public void testIsUserNode() {
151         Preferences JavaDoc upref = getUserPreferencesNode();
152         Preferences JavaDoc spref = getSystemPreferencesNode();
153         assertTrue(upref.isUserNode());
154         assertFalse(spref.isUserNode());
155     }
156     
157     public void testNode() throws BackingStoreException JavaDoc {
158         Preferences JavaDoc pref = getPreferencesNode();
159         assertNotNull(pref);
160         assertTrue(pref.nodeExists(""));
161         assertFalse(pref.nodeExists("sub1"));
162         assertFalse(Arrays.asList(pref.childrenNames()).contains("sub1"));
163         
164         Preferences JavaDoc sub1 =pref.node("sub1");
165         assertTrue(pref.nodeExists("sub1"));
166         assertTrue(Arrays.asList(pref.childrenNames()).contains("sub1"));
167     }
168     
169     public void testChildrenNames() throws Exception JavaDoc {
170         Preferences JavaDoc pref = getPreferencesNode();
171         assertNotNull(pref);
172         assertEquals(0, pref.childrenNames().length);
173         
174         assertFalse(pref.nodeExists("sub1"));
175         Preferences JavaDoc sub1 =pref.node("sub1");
176         assertNotNull(sub1);
177         assertTrue(pref.nodeExists("sub1"));
178         assertEquals(1, pref.childrenNames().length);
179         
180         
181         assertFalse(pref.nodeExists("sub2"));
182         Preferences JavaDoc sub2 =pref.node("sub2");
183         assertNotNull(sub2);
184         assertTrue(pref.nodeExists("sub2"));
185         assertEquals(2, pref.childrenNames().length);
186         
187         sub1.removeNode();
188         assertEquals(1, pref.childrenNames().length);
189         sub2.removeNode();
190         assertEquals(0, pref.childrenNames().length);
191     }
192     
193     public void testPut() {
194         Preferences JavaDoc pref = getPreferencesNode();
195         assertNotNull(pref);
196         
197         assertNull(pref.get("key1", null));
198         pref.put("key1", "value1");
199         assertEquals("value1",pref.get("key1", null));
200     }
201     
202     public void testPut2() throws Exception JavaDoc {
203         final Object JavaDoc sync = getEventQueueSync();
204         Preferences JavaDoc pref = getPreferencesNode();
205         assertNotNull(pref);
206         final List JavaDoc l = new ArrayList JavaDoc();
207         assertNull(pref.get("key1", null));
208         PreferenceChangeListener JavaDoc pl = new PreferenceChangeListener JavaDoc(){
209             public void preferenceChange(PreferenceChangeEvent JavaDoc evt) {
210                 synchronized(sync) {
211                     l.add(evt.getNewValue());
212                     sync.notifyAll();
213                 }
214             }
215         };
216         pref.addPreferenceChangeListener(pl);
217         try {
218             pref.put("key1", "value1");
219             assertEquals("value1",pref.get("key1", null));
220             synchronized(sync) {
221                 sync.wait(5000);
222                 assertEquals(1, l.size());
223             }
224             assertEquals("value1",l.get(0));
225             l.clear();
226             
227             pref.put("key1", "value2");
228             assertEquals("value2",pref.get("key1", null));
229             synchronized(sync) {
230                 sync.wait(5000);
231                 assertEquals(1, l.size());
232             }
233             assertEquals("value2",l.get(0));
234             l.clear();
235             
236             pref.put("key1", "value2");
237             assertEquals("value2",pref.get("key1", null));
238             synchronized(sync) {
239                 sync.wait(5000);
240                 assertEquals(0, l.size());
241             }
242             l.clear();
243             
244             pref.put("key1", "value2");
245             assertEquals("value2",pref.get("key1", null));
246             synchronized(sync) {
247                 sync.wait(5000);
248                 assertEquals(0, l.size());
249             }
250             l.clear();
251             
252         } finally {
253             pref.removePreferenceChangeListener(pl);
254         }
255     }
256     
257     private Object JavaDoc getEventQueueSync() {
258         try {
259             Field JavaDoc f = AbstractPreferences JavaDoc.class.getDeclaredField("eventQueue");
260             f.setAccessible(true);
261             return f.get(null);
262             
263         } catch (Exception JavaDoc ex) {
264             Logger.getLogger("global").log(java.util.logging.Level.SEVERE,ex.getMessage(), ex);
265         }
266         return null;
267     }
268     
269     
270     
271     public void testRemove() {
272         testPut();
273         Preferences JavaDoc pref = getPreferencesNode();
274         assertEquals("value1",pref.get("key1", null));
275         pref.remove("key1");
276         assertNull(pref.get("key1", null));
277     }
278     
279     public void testClear() throws Exception JavaDoc {
280         testKeys();
281         Preferences JavaDoc pref = getPreferencesNode();
282         pref.clear();
283         assertEquals(0,pref.keys().length);
284         assertNull(pref.get("key1", null));
285         assertNull(pref.get("key2", null));
286     }
287     
288     public void testKeys() throws Exception JavaDoc {
289         Preferences JavaDoc pref = getPreferencesNode();
290         assertNotNull(pref);
291         assertEquals(0,pref.keys().length);
292         pref.put("key1", "value1");
293         pref.put("key2", "value2");
294         assertEquals(2,pref.keys().length);
295     }
296     
297     
298     public void testParent() {
299         Preferences JavaDoc pref = getPreferencesNode();
300         Preferences JavaDoc pref2 = pref.node("1/2/3");
301         assertNotSame(pref, pref2);
302         
303         for (int i = 0; i < 3; i++) {
304             pref2 = pref2.parent();
305         }
306         
307         assertSame(pref2.absolutePath(), pref, pref2);
308     }
309     
310     public void testNodeExists() throws Exception JavaDoc {
311         Preferences JavaDoc pref = getPreferencesNode();
312         Preferences JavaDoc pref2 = pref.node("a/b/c");
313         while(pref2 != Preferences.userRoot()) {
314             assertTrue(pref2.nodeExists(""));
315             Preferences JavaDoc parent = pref2.parent();
316             pref2.removeNode();
317             assertFalse(pref2.nodeExists(""));
318             pref2 = parent;
319         }
320         
321         assertNotNull(getPreferencesNode().node("a/b/c/d"));
322         assertTrue(getPreferencesNode().node("a/b/c/d").nodeExists(""));
323     }
324     
325     
326     public void testName() {
327         Preferences JavaDoc pref = getPreferencesNode();
328         assertEquals("myname",pref.node("myname").name());
329     }
330     
331     public void testAbsolutePath() {
332         String JavaDoc validPath = "/a/b/c/d";
333         Preferences JavaDoc pref = Preferences.userRoot().node(validPath);
334         assertEquals(validPath, pref.absolutePath());
335         
336         //relative path
337
assertSame(pref, pref.parent().node("d"));
338         
339         String JavaDoc invalidPath = "/a/b/c/d/";
340         try {
341             pref = Preferences.userRoot().node(invalidPath);
342             fail();
343         } catch(IllegalArgumentException JavaDoc iax) {}
344         
345     }
346     
347     public void testAddPreferenceChangeListener() throws BackingStoreException JavaDoc, InterruptedException JavaDoc {
348         final Preferences JavaDoc pref = getPreferencesNode();
349         PreferenceChangeListener JavaDoc l = new PreferenceChangeListener JavaDoc() {
350             public void preferenceChange(PreferenceChangeEvent JavaDoc evt) {
351                 synchronized (TestPreferences.class) {
352                     //assertionerrors cause deadlock here
353
assertSame(pref, evt.getNode());
354                     assertEquals("key", evt.getKey());
355                     assertEquals(evt.getNewValue(),pref.get(evt.getKey(),null), evt.getNewValue());
356                     TestPreferences.class.notifyAll();
357                 }
358             }
359         };
360         pref.addPreferenceChangeListener(l);
361         try {
362             synchronized (TestPreferences.class) {
363                 pref.put("key","AddPreferenceChangeListener");
364                 pref.flush();
365                 TestPreferences.class.wait();
366             }
367             
368             synchronized (TestPreferences.class) {
369                 pref.remove("key");
370                 pref.flush();
371                 TestPreferences.class.wait();
372             }
373             
374             synchronized (TestPreferences.class) {
375                 pref.put("key","AddPreferenceChangeListener2");
376                 pref.flush();
377                 TestPreferences.class.wait();
378             }
379         } finally {
380             pref.removePreferenceChangeListener(l);
381         }
382     }
383     
384     public void testAddNodeChangeListener() throws BackingStoreException JavaDoc, InterruptedException JavaDoc {
385         final Preferences JavaDoc pref = getPreferencesNode();
386         NodeChangeListener JavaDoc l = new NodeChangeListener JavaDoc() {
387             public void childAdded(NodeChangeEvent JavaDoc evt) {
388                 synchronized (TestPreferences.class){
389                     //assertionerrors cause deadlock here
390
assertSame(pref, evt.getParent());
391                     assertEquals("added",evt.getChild().name());
392                     TestPreferences.class.notifyAll();
393                 }
394             }
395             
396             public void childRemoved(NodeChangeEvent JavaDoc evt) {
397                 synchronized (TestPreferences.class) {
398                     //assertionerrors cause deadlock here
399
assertSame(pref, evt.getParent());
400                     assertEquals("added",evt.getChild().name());
401                     TestPreferences.class.notifyAll();
402                 }
403             }
404         };
405         pref.addNodeChangeListener(l);
406         try {
407             Preferences JavaDoc added;
408             synchronized (TestPreferences.class) {
409                 added = pref.node("added");
410                 TestPreferences.class.wait();
411             }
412             
413             synchronized (TestPreferences.class) {
414                 added.removeNode();
415                 TestPreferences.class.wait();
416             }
417             
418         } finally {
419             pref.removeNodeChangeListener(l);
420         }
421     }
422     
423     public void testIsPersistent() throws BackingStoreException JavaDoc,InterruptedException JavaDoc {
424         NbPreferences pref = (NbPreferences)getPreferencesNode();
425         assertNotNull(pref);
426         assertEquals(NbPreferences.UserPreferences.class, pref.getClass());
427         pref.put("key", "value");
428         assertEquals("value", pref.get("key", null));
429         pref.sync();
430         assertEquals("value", pref.get("key", null));
431     }
432     
433     protected int timeOut() {
434         return 20000;
435     }
436
437
438 }
439
Popular Tags