OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / org / apache / harmony / prefs / tests / java / util / prefs / AbstractPreferencesTest.java
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.apache.harmony.prefs.tests.java.util.prefs;
18
19 import dalvik.annotation.TestLevel;
20 import dalvik.annotation.TestTargetClass;
21 import dalvik.annotation.TestTargetNew;
22 import dalvik.annotation.TestTargets;
23 import junit.framework.TestCase;
24
25 import java.io.ByteArrayInputStream;
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.util.prefs.AbstractPreferences;
29 import java.util.prefs.BackingStoreException;
30 import java.util.prefs.InvalidPreferencesFormatException;
31 import java.util.prefs.NodeChangeEvent;
32 import java.util.prefs.NodeChangeListener;
33 import java.util.prefs.PreferenceChangeEvent;
34 import java.util.prefs.PreferenceChangeListener;
35 import java.util.prefs.Preferences;
36
37 @TestTargetClass(AbstractPreferences.class)
38 public class AbstractPreferencesTest extends TestCase {
39
40     AbstractPreferences pref;
41
42     static AbstractPreferences root;
43
44     static final String nodeName = "mock";
45
46     static AbstractPreferences parent = null;
47
48     String oldUserHome = System.getProperty("user.home");
49     String oldJavaHome = System.getProperty("java.home");
50
51     protected void setUp() throws Exception {
52         super.setUp();
53
54         root = (AbstractPreferences) Preferences.userRoot();
55         parent = (AbstractPreferences) Preferences.userNodeForPackage(this.getClass());
56
57         pref = (AbstractPreferences) parent.node(nodeName);
58     }
59
60     protected void tearDown() throws Exception {
61         super.tearDown();
62     }
63
64     @TestTargetNew(
65         level = TestLevel.COMPLETE,
66         notes = "",
67         method = "toString",
68         args = {}
69     )
70     public void testToString() {
71         assertTrue(pref.toString().contains(nodeName));
72     }
73
74     @TestTargets({
75         @TestTargetNew(
76             level = TestLevel.COMPLETE,
77             notes = "Tests putSpi indirectly",
78             method = "put",
79             args = {java.lang.String.class, java.lang.String.class}
80         ),
81         @TestTargetNew(
82             level = TestLevel.COMPLETE,
83             notes = "Tests putSpi indirectly",
84             method = "putSpi",
85             args = {java.lang.String.class, java.lang.String.class}
86         )
87     })
88     public void testPut() throws BackingStoreException {
89         pref.put("Value", "String");
90         pref.flush();
91
92         assertEquals("String", pref.get("Value", ":"));
93
94         try {
95             pref.put(null, "Exception");
96             fail("NullPointerException expected");
97         } catch (NullPointerException e) {
98             //expected
99         }
100
101         int i;
102         StringBuffer sb = new StringBuffer();
103
104         for (i = 0; i < Preferences.MAX_KEY_LENGTH + 1; i++) {
105             sb.append('c');
106         }
107
108         try {
109             pref.put(new String(sb), "Exception");
110             fail("IllegalArgumentException expected");
111         } catch (IllegalArgumentException e) {
112             //expected
113         }
114
115         sb = new StringBuffer();
116
117         for (i = 0; i < Preferences.MAX_VALUE_LENGTH + 1; i++) {
118             sb.append('c');
119         }
120
121         try {
122             pref.put("DoubleValue", new String(sb));
123             fail("IllegalArgumentException expected");
124         } catch (IllegalArgumentException e) {
125             //expected
126         }
127
128         pref.removeNode();
129
130         try {
131             pref.put("DoubleValue", "Exception");
132             fail("IllegalStateException expected");
133         } catch (IllegalStateException e) {
134             //expected
135         }
136     }
137
138     @TestTargets({
139         @TestTargetNew(
140             level = TestLevel.COMPLETE,
141             notes = "getSpi tested indirectly.",
142             method = "get",
143             args = {java.lang.String.class, java.lang.String.class}
144         ),
145         @TestTargetNew(
146             level = TestLevel.COMPLETE,
147             notes = "getSpi tested indirectly.",
148             method = "getSpi",
149             args = {java.lang.String.class}
150         )
151     })
152     public void testGet() throws BackingStoreException {
153         pref.put("Value", "String");
154         pref.putDouble("DoubleValue", new Double(9.10938188e-31));
155         pref.putBoolean("BoolValue", true);
156         pref.flush();
157
158         assertEquals("String", pref.get("Value", ":"));
159         assertEquals("true", pref.get("BoolValue", ":"));
160         assertEquals("9.10938188E-31", pref.get("DoubleValue", null));
161
162         try {
163             pref.get(null, "Exception");
164             fail("NullPointerException expected");
165         } catch (NullPointerException e) {
166             //expected
167         }
168
169         pref.removeNode();
170
171         try {
172             pref.get("DoubleValue", "Exception");
173             fail("IllegalStateException expected");
174         } catch (IllegalStateException e) {
175             //expected
176         }
177     }
178
179     @TestTargets({
180         @TestTargetNew(
181             level = TestLevel.COMPLETE,
182             notes = "Indirectly checks removeSpi",
183             method = "remove",
184             args = {java.lang.String.class}
185         ),
186         @TestTargetNew(
187             level = TestLevel.COMPLETE,
188             notes = "Indirectly checks removeSpi",
189             method = "removeSpi",
190             args = {java.lang.String.class}
191         )
192     })
193     public void testRemove() throws BackingStoreException {
194         String[] keyArray = new String[]{"Value", "DoubleValue", "LongValue", "IntValue"};
195         pref.put(keyArray[0], "String");
196         pref.putDouble(keyArray[1], new Double(9.10938188e-31));
197         pref.putLong(keyArray[2], new Long(Long.MIN_VALUE));
198         pref.putInt(keyArray[3], 299792458);
199         pref.node("New node");
200         pref.flush();
201
202         String[] str = pref.keys();
203         assertEquals(keyArray.length, str.length);
204         for(int i = 0; i < keyArray.length; i++) {
205             pref.remove(keyArray[i]);
206             str = pref.keys();
207             assertEquals(keyArray.length - i - 1, str.length);
208         }
209         assertEquals(1, pref.childrenNames().length);
210         pref.remove("New node");
211         assertEquals(1, pref.childrenNames().length);
212
213         pref.removeNode();
214
215         try {
216             pref.remove("New node");
217             fail("IllegalStateException expected");
218         } catch (IllegalStateException e) {
219             //expected
220         }
221     }
222
223     @TestTargetNew(
224         level = TestLevel.COMPLETE,
225         notes = "",
226         method = "clear",
227         args = {}
228     )
229     public void testClear() throws BackingStoreException {
230         AbstractPreferences ap = (AbstractPreferences) pref.node("New node");
231         pref.putInt("IntValue", 33);
232         pref.putBoolean("BoolValue", true);
233         pref.flush();
234         assertTrue(pref.getBoolean("BoolValue", false));
235         assertEquals(33, pref.getInt("IntValue", 22));
236         assertEquals(1, pref.childrenNames().length);
237         pref.clear();
238         assertFalse(pref.getBoolean("BoolValue", false));
239         assertEquals(22, pref.getInt("IntValue", 22));
240         assertEquals(1, pref.childrenNames().length);
241
242         pref.removeNode();
243
244         try {
245             pref.clear();
246             fail("IllegalStateException expected");
247         } catch (IllegalStateException e) {
248             //expected
249         }
250
251         try {
252             ap.clear();
253             fail("IllegalStateException expected");
254         } catch (IllegalStateException e) {
255             //expected
256         }
257     }
258
259     @TestTargetNew(
260         level = TestLevel.COMPLETE,
261         notes = "",
262         method = "putInt",
263         args = {java.lang.String.class, int.class}
264     )
265     public void testPutInt() throws BackingStoreException {
266         pref.putInt("IntValue", 299792458);
267         pref.flush();
268
269         assertEquals(299792458, pref.getInt("IntValue", new Integer(1)));
270
271         try {
272             pref.putInt(null, new Integer(1));
273             fail("NullPointerException expected");
274         } catch (NullPointerException e) {
275             //expected
276         }
277
278         int i;
279         StringBuffer sb = new StringBuffer();
280
281         for (i = 0; i < Preferences.MAX_KEY_LENGTH + 1; i++) {
282             sb.append('c');
283         }
284
285         try {
286             pref.putInt(new String(sb), new Integer(1));
287             fail("IllegalArgumentException expected");
288         } catch (IllegalArgumentException e) {
289             //expected
290         }
291
292         pref.removeNode();
293
294         try {
295             pref.putInt("IntValue", new Integer(1));
296             fail("IllegalStateException expected");
297         } catch (IllegalStateException e) {
298             //expected
299         }
300     }
301
302     @TestTargetNew(
303         level = TestLevel.COMPLETE,
304         notes = "",
305         method = "getInt",
306         args = {java.lang.String.class, int.class}
307     )
308     public void testGetInt() throws BackingStoreException {
309         pref.put("Value", "String");
310         pref.putDouble("DoubleValue", new Double(9.10938188e-31));
311         pref.putLong("LongValue", new Long(Long.MIN_VALUE));
312         pref.putInt("IntValue", 299792458);
313         pref.flush();
314
315         assertEquals(1, pref.getInt("Value", new Integer(1)));
316         assertEquals(1, pref.getInt("LongValue", new Integer(1)));
317         assertEquals(1, pref.getInt("DoubleValue", new Integer(1)));
318         assertEquals(299792458, pref.getInt("IntValue", new Integer(1)));
319
320         try {
321             pref.getInt(null, new Integer(1));
322             fail("NullPointerException expected");
323         } catch (NullPointerException e) {
324             //expected
325         }
326
327         pref.removeNode();
328
329         try {
330             pref.getInt("IntValue", new Integer(1));
331             fail("IllegalStateException expected");
332         } catch (IllegalStateException e) {
333             //expected
334         }
335     }
336
337     @TestTargetNew(
338         level = TestLevel.COMPLETE,
339         notes = "",
340         method = "putLong",
341         args = {java.lang.String.class, long.class}
342     )
343     public void testPutLong() throws BackingStoreException {
344         pref.putLong("LongValue", new Long(299792458));
345         pref.flush();
346
347         assertEquals(299792458L, pref.getLong("LongValue", new Long(1)));
348
349         try {
350             pref.putLong(null, new Long(1));
351             fail("NullPointerException expected");
352         } catch (NullPointerException e) {
353             //expected
354         }
355
356         int i;
357         StringBuffer sb = new StringBuffer();
358
359         for (i = 0; i < Preferences.MAX_KEY_LENGTH + 1; i++) {
360             sb.append('c');
361         }
362
363         try {
364             pref.putLong(new String(sb), new Long(1));
365             fail("IllegalArgumentException expected");
366         } catch (IllegalArgumentException e) {
367             //expected
368         }
369
370         pref.removeNode();
371
372         try {
373             pref.putLong("LongValue", new Long(1));
374             fail("IllegalStateException expected");
375         } catch (IllegalStateException e) {
376             //expected
377         }
378     }
379
380     @TestTargetNew(
381         level = TestLevel.COMPLETE,
382         notes = "",
383         method = "getLong",
384         args = {java.lang.String.class, long.class}
385     )
386     public void testGetLong() throws BackingStoreException {
387         pref.put("Value", "String");
388         pref.putDouble("DoubleValue", new Double(9.10938188e-31));
389         pref.putLong("LongValue", new Long(Long.MIN_VALUE));
390         pref.putInt("IntValue", 299792458);
391         pref.flush();
392
393         assertEquals(1L, pref.getLong("Value", new Long(1)));
394         assertEquals(Long.MIN_VALUE, pref.getLong("LongValue", new Long(1)));
395         assertEquals(1L, pref.getLong("DoubleValue", new Long(1)));
396         assertEquals(299792458L, pref.getLong("IntValue", new Long(1)));
397
398         try {
399             pref.getLong(null, new Long(1));
400             fail("NullPointerException expected");
401         } catch (NullPointerException e) {
402             //expected
403         }
404
405         pref.removeNode();
406
407         try {
408             pref.getLong("LongValue", new Long(1));
409             fail("IllegalStateException expected");
410         } catch (IllegalStateException e) {
411             //expected
412         }
413     }
414
415     @TestTargetNew(
416         level = TestLevel.COMPLETE,
417         notes = "",
418         method = "putBoolean",
419         args = {java.lang.String.class, boolean.class}
420     )
421     public void testPutBoolean() throws BackingStoreException {
422         pref.putBoolean("BoolValue", true);
423         pref.flush();
424
425         assertTrue(pref.getBoolean("BoolValue", false));
426
427         try {
428             pref.putBoolean(null, true);
429             fail("NullPointerException expected");
430         } catch (NullPointerException e) {
431             //expected
432         }
433
434         int i;
435         StringBuffer sb = new StringBuffer();
436
437         for (i = 0; i < Preferences.MAX_KEY_LENGTH + 1; i++) {
438             sb.append('c');
439         }
440
441         try {
442             pref.putBoolean(new String(sb), true);
443             fail("IllegalArgumentException expected");
444         } catch (IllegalArgumentException e) {
445             //expected
446         }
447
448         pref.removeNode();
449
450         try {
451             pref.putBoolean("DoubleValue", true);
452             fail("IllegalStateException expected");
453         } catch (IllegalStateException e) {
454             //expected
455         }
456     }
457
458     @TestTargetNew(
459         level = TestLevel.COMPLETE,
460         notes = "",
461         method = "getBoolean",
462         args = {java.lang.String.class, boolean.class}
463     )
464     public void testGetBoolean() throws BackingStoreException {
465         pref.put("Value", "String");
466         pref.putDouble("DoubleValue", new Double(9.10938188e-31));
467         pref.putBoolean("BoolValue", true);
468         pref.flush();
469
470         assertFalse(pref.getBoolean("Value", false));
471         assertTrue(pref.getBoolean("BoolValue", false));
472         assertFalse(pref.getBoolean("DoubleValue", false));
473
474         try {
475             pref.getBoolean(null, true);
476             fail("NullPointerException expected");
477         } catch (NullPointerException e) {
478             //expected
479         }
480
481         pref.removeNode();
482
483         try {
484             pref.getBoolean("DoubleValue", true);
485             fail("IllegalStateException expected");
486         } catch (IllegalStateException e) {
487             //expected
488         }
489     }
490
491     @TestTargetNew(
492         level = TestLevel.COMPLETE,
493         notes = "",
494         method = "putFloat",
495         args = {java.lang.String.class, float.class}
496     )
497     public void testPutFloat() throws BackingStoreException {
498         pref.putFloat("FloatValue", new Float(1.602e-19));
499         pref.flush();
500
501         assertEquals(new Float(1.602e-19), pref.getFloat("FloatValue", new Float(0.2)));
502
503         try {
504             pref.putFloat(null, new Float(0.1));
505             fail("NullPointerException expected");
506         } catch (NullPointerException e) {
507             //expected
508         }
509
510         int i;
511         StringBuffer sb = new StringBuffer();
512
513         for (i = 0; i < Preferences.MAX_KEY_LENGTH + 1; i++) {
514             sb.append('c');
515         }
516
517         try {
518             pref.putFloat(new String(sb), new Float(0.1));
519             fail("IllegalArgumentException expected");
520         } catch (IllegalArgumentException e) {
521             //expected
522         }
523
524         pref.removeNode();
525
526         try {
527             pref.putFloat("FloatValue", new Float(0.1));
528             fail("IllegalStateException expected");
529         } catch (IllegalStateException e) {
530             //expected
531         }
532     }
533
534     @TestTargetNew(
535         level = TestLevel.COMPLETE,
536         notes = "",
537         method = "getFloat",
538         args = {java.lang.String.class, float.class}
539     )
540     public void testGetFloat() throws BackingStoreException {
541         pref.put("Value", "String");
542         pref.putDouble("DoubleValue", new Double(9.10938188e-31));
543         pref.putFloat("FloatValue", new Float(-0.123));
544         pref.putInt("IntValue", 299792458);
545         pref.flush();
546
547         assertEquals(new Float(0.1), pref.getFloat("Value", new Float(0.1)));
548         assertEquals(new Float(-0.123), pref.getFloat("FloatValue", new Float(0.2)));
549         assertEquals(new Float(9.109382e-31), pref.getFloat("DoubleValue", new Float(2.14)));
550         assertEquals(new Float(2.99792448e8), pref.getFloat("IntValue", new Float(5)));
551
552         try {
553             pref.getFloat(null, new Float(0.1));
554             fail("NullPointerException expected");
555         } catch (NullPointerException e) {
556             //expected
557         }
558
559         pref.removeNode();
560
561         try {
562             pref.getFloat("FloatValue", new Float(0.1));
563             fail("IllegalStateException expected");
564         } catch (IllegalStateException e) {
565             //expected
566         }
567     }
568
569     @TestTargetNew(
570         level = TestLevel.COMPLETE,
571         notes = "",
572         method = "putDouble",
573         args = {java.lang.String.class, double.class}
574     )
575     public void testPutDouble() throws BackingStoreException {
576         pref.putDouble("DoubleValue", new Double(9.10938188e-31));
577         pref.flush();
578
579         assertEquals(new Double(9.10938188e-31), pref.getDouble("DoubleValue", new Double(2.14)));
580
581         try {
582             pref.putDouble(null, new Double(0.1));
583             fail("NullPointerException expected");
584         } catch (NullPointerException e) {
585             //expected
586         }
587
588         int i;
589         StringBuffer sb = new StringBuffer();
590
591         for (i = 0; i < Preferences.MAX_KEY_LENGTH + 1; i++) {
592             sb.append('c');
593         }
594
595         try {
596             pref.putDouble(new String(sb), new Double(0.1));
597             fail("IllegalArgumentException expected");
598         } catch (IllegalArgumentException e) {
599             //expected
600         }
601
602         pref.removeNode();
603
604         try {
605             pref.putDouble("DoubleValue", new Double(0.1));
606             fail("IllegalStateException expected");
607         } catch (IllegalStateException e) {
608             //expected
609         }
610     }
611
612     @TestTargetNew(
613         level = TestLevel.COMPLETE,
614         notes = "",
615         method = "getDouble",
616         args = {java.lang.String.class, double.class}
617     )
618     public void testGetDouble() throws BackingStoreException {
619         pref.put("Value", "String");
620         pref.putDouble("DoubleValue", new Double(9.10938188e-31));
621         pref.putBoolean("BoolValue", true);
622         pref.putInt("IntValue", 299792458);
623         pref.flush();
624
625         assertEquals(new Double(0.1), pref.getDouble("Value", new Double(0.1)));
626         assertEquals(new Double(0.2), pref.getDouble("BoolValue", new Double(0.2)));
627         assertEquals(new Double(9.10938188e-31), pref.getDouble("DoubleValue", new Double(2.14)));
628         assertEquals(new Double(2.99792458e8), pref.getDouble("IntValue", new Double(5)));
629
630         try {
631             pref.getDouble(null, new Double(0.1));
632             fail("NullPointerException expected");
633         } catch (NullPointerException e) {
634             //expected
635         }
636
637         pref.removeNode();
638
639         try {
640             pref.getDouble("DoubleValue", new Double(0.1));
641             fail("IllegalStateException expected");
642         } catch (IllegalStateException e) {
643             //expected
644         }
645     }
646
647     @TestTargetNew(
648         level = TestLevel.COMPLETE,
649         notes = "",
650         method = "putByteArray",
651         args = {java.lang.String.class, byte[].class}
652     )
653     public void testPutByteArray() throws BackingStoreException {
654         byte[] bArray = new byte[]{1, 2, 3, 4, 5};
655         byte[] array  = null;
656         int i;
657         pref.putByteArray("Array", bArray);
658         pref.flush();
659
660         array = pref.getByteArray("Array", null);
661         assertEquals(bArray.length, array.length);
662         for(i = 0; i < bArray.length; i++) {
663             assertEquals(bArray[i], array[i]);
664         }
665
666         try {
667             pref.putByteArray(null, bArray);
668             fail("NullPointerException expected");
669         } catch (NullPointerException e) {
670             //expected
671         }
672
673         StringBuffer sb = new StringBuffer();
674
675         for (i = 0; i < Preferences.MAX_KEY_LENGTH + 1; i++) {
676             sb.append('c');
677         }
678
679         try {
680             pref.putByteArray(new String(sb), bArray);
681             fail("IllegalArgumentException expected");
682         } catch (IllegalArgumentException e) {
683             //expected
684         }
685
686         bArray = new byte[Preferences.MAX_VALUE_LENGTH * 3 / 4 + 1];
687
688         try {
689             pref.putByteArray("Big array", bArray);
690             fail("IllegalArgumentException expected");
691         } catch (IllegalArgumentException e) {
692             //expected
693         }
694
695         pref.removeNode();
696
697         try {
698             pref.putByteArray("Array", new byte[10]);
699             fail("IllegalStateException expected");
700         } catch (IllegalStateException e) {
701             //expected
702         }
703     }
704
705     @TestTargetNew(
706         level = TestLevel.COMPLETE,
707         notes = "",
708         method = "getByteArray",
709         args = {java.lang.String.class, byte[].class}
710     )
711     public void testGetByteArray() throws BackingStoreException {
712         byte[] bArray = new byte[]{1, 2, 3, 4, 5};
713         byte[] tmp    = new byte[]{5};
714         byte[] array  = null;
715         int i;
716         pref.put("Value", "String");
717         pref.putDouble("DoubleValue", new Double(9.10938188e-31));
718         pref.putByteArray("Array", bArray);
719         pref.flush();
720
721         array = pref.getByteArray("Value", tmp);
722         assertEquals(tmp.length, array.length);
723         for(i = 0; i < tmp.length; i++) {
724             assertEquals(tmp[i], array[i]);
725         }
726
727         array = pref.getByteArray("DoubleValue", tmp);
728         assertEquals(tmp.length, array.length);
729         for(i = 0; i < tmp.length; i++) {
730             assertEquals(tmp[i], array[i]);
731         }
732
733         array = pref.getByteArray("Array", tmp);
734         assertEquals(bArray.length, array.length);
735         for(i = 0; i < bArray.length; i++) {
736             assertEquals(bArray[i], array[i]);
737         }
738
739         try {
740             pref.getByteArray(null, tmp);
741             fail("NullPointerException expected");
742         } catch (NullPointerException e) {
743             //expected
744         }
745
746         pref.removeNode();
747
748         try {
749             pref.getByteArray("Array", tmp);
750             fail("IllegalStateException expected");
751         } catch (IllegalStateException e) {
752             //expected
753         }
754     }
755
756     @TestTargets({
757         @TestTargetNew(
758             level = TestLevel.COMPLETE,
759             notes = "keysSpi tested indirectly",
760             method = "keys",
761             args = {}
762         ),
763         @TestTargetNew(
764             level = TestLevel.COMPLETE,
765             notes = "keysSpi tested indirectly",
766             method = "keysSpi",
767             args = {}
768         )
769     })
770     public void testKeys() throws BackingStoreException {
771         String[] keyArray = new String[]{"Value", "DoubleValue", "BoolValue", "IntValue"};
772         String nodeStr = "New node";
773         pref.node(nodeStr);
774         pref.put(keyArray[0], "String");
775         pref.putDouble(keyArray[1], new Double(9.10938188e-31));
776         pref.putBoolean(keyArray[2], true);
777         pref.putInt(keyArray[3], 299792458);
778         pref.flush();
779
780         String[] str = pref.keys();
781         assertEquals(keyArray.length, str.length);
782         for(int i = 0; i < str.length; i++) {
783             boolean flag = false;
784             for(int j = 0; j < keyArray.length; j++) {
785                 if (str[i].compareTo(keyArray[j]) == 0) {
786                     flag = true;
787                     break;
788                 }
789             }
790             assertTrue(str[i].compareTo(nodeStr) != 0);
791             assertTrue(flag);
792         }
793
794         pref.removeNode();
795
796         try {
797             pref.keys();
798             fail("IllegalStateException expected");
799         } catch(IllegalStateException e) {
800             //expected
801         }
802     }
803
804     @TestTargets({
805         @TestTargetNew(
806             level = TestLevel.COMPLETE,
807             notes = "BackingStoreException can not be checked. childrenNamesSpi checked indirectly.",
808             method = "childrenNames",
809             args = {}
810         ),
811         @TestTargetNew(
812             level = TestLevel.COMPLETE,
813             notes = "BackingStoreException can not be checked. childrenNamesSpi checked indirectly.",
814             method = "childrenNamesSpi",
815             args = {}
816         )
817     })
818     public void testChildrenNames() throws BackingStoreException {
819         AbstractPreferences first = (AbstractPreferences) pref.node("First node");
820         AbstractPreferences second = (AbstractPreferences) pref.node("Second node");
821
822         assertEquals(2, pref.childrenNames().length);
823         assertEquals(0, first.childrenNames().length);
824         assertEquals(0, second.childrenNames().length);
825
826         second.removeNode();
827
828         try {
829             second.childrenNames();
830             fail("IllegalStateException expected");
831         } catch (IllegalStateException e) {
832             //expected
833         }
834
835         pref.removeNode();
836
837         try {
838             first.childrenNames();
839             fail("IllegalStateException expected");
840         } catch (IllegalStateException e) {
841             //expected
842         }
843     }
844
845     @TestTargetNew(
846             level = TestLevel.PARTIAL_COMPLETE,
847             notes = "",
848             method = "nodeExists",
849             args = {String.class}
850     )
851     public void test_nodeExists() throws BackingStoreException {
852         AbstractPreferences test = (AbstractPreferences) Preferences.userRoot()
853                 .node("test");
854         try {
855             test.nodeExists(null);
856             fail("should throw NullPointerException");
857         } catch (NullPointerException e) {
858             // Expected
859         }
860
861         test.removeNode();
862         try {
863             test.nodeExists(null);
864             fail("should throw NullPointerException");
865         } catch (NullPointerException e) {
866             // Expected
867         }
868     }
869
870     @TestTargetNew(
871         level = TestLevel.COMPLETE,
872         notes = "",
873         method = "parent",
874         args = {}
875     )
876     public void testParent() throws BackingStoreException {
877         AbstractPreferences node = (AbstractPreferences) pref.node("First node/sub node");
878
879         assertTrue(node.parent().name().compareTo("First node") == 0);
880
881         pref.removeNode();
882
883         try {
884             node.parent();
885             fail("IllegalStateException expected");
886         } catch (IllegalStateException e) {
887             //expected
888         }
889     }
890
891     @TestTargets({
892         @TestTargetNew(
893             level = TestLevel.COMPLETE,
894             notes = "Indirecly checks childSpi",
895             method = "node",
896             args = {java.lang.String.class}
897         ),
898         @TestTargetNew(
899             level = TestLevel.COMPLETE,
900             notes = "Indirecly checks childSpi",
901             method = "childSpi",
902             args = {java.lang.String.class}
903         )
904     })
905     public void testNode() throws BackingStoreException {
906         AbstractPreferences first = (AbstractPreferences) pref.node("First node");
907         AbstractPreferences second = (AbstractPreferences) pref.node("Second node");
908
909         try {
910             first.node("blabla/");
911             fail("IllegalArgumentException expected");
912         } catch (IllegalArgumentException e) {
913             //expected
914         }
915
916         try {
917             first.node("///invalid");
918             fail("IllegalArgumentException expected");
919         } catch (IllegalArgumentException e) {
920             //expected
921         }
922
923         StringBuffer sb = new StringBuffer();
924
925         for (int i = 0; i < Preferences.MAX_NAME_LENGTH; i++) {
926             sb.append('c');
927         }
928         first.node(new String(sb));
929         sb.append('c');
930
931         try {
932             first.node(new String(sb));
933             fail("IllegalArgumentException expected");
934         } catch (IllegalArgumentException e) {
935             //expected
936         }
937
938         second.removeNode();
939
940         try {
941             second.node("");
942             fail("IllegalStateException expected");
943         } catch (IllegalStateException e) {
944             //expected
945         }
946         pref.removeNode();
947         try {
948             first.node("");
949             fail("IllegalStateException expected");
950         } catch (IllegalStateException e) {
951             //expected
952         }
953     }
954
955     @TestTargets({
956         @TestTargetNew(
957             level = TestLevel.COMPLETE,
958             notes = "getChild tested indirectly",
959             method = "nodeExists",
960             args = {java.lang.String.class}
961         ),
962         @TestTargetNew(
963             level = TestLevel.COMPLETE,
964             notes = "getChild tested indirectly",
965             method = "getChild",
966             args = {java.lang.String.class}
967         )
968     })
969     public void testNodeExists() throws BackingStoreException {
970         AbstractPreferences ap1 = (AbstractPreferences) pref.node("First node");
971         AbstractPreferences ap2 = (AbstractPreferences) pref.node("Second node");
972         pref.putInt("IntegerValue", 33);
973         pref.putBoolean("BoolValue", true);
974         pref.flush();
975
976         assertTrue(pref.nodeExists("First node"));
977         assertTrue(pref.nodeExists("Second node"));
978         assertFalse(pref.nodeExists("IntegerValue"));
979         assertFalse(pref.nodeExists("BoolValue"));
980         assertFalse(pref.nodeExists("Value"));
981         assertFalse(pref.nodeExists(nodeName));
982
983         try {
984             pref.nodeExists("///invalid");
985             fail("IllegalArgumentException expected");
986         } catch (IllegalArgumentException e) {
987             //expected
988         }
989
990         pref.removeNode();
991
992         try {
993             pref.nodeExists("Exception");
994             fail("IllegalStateException expected");
995         } catch (IllegalStateException e) {
996             //expected
997         }
998     }
999
1000     @TestTargets({
1001         @TestTargetNew(
1002             level = TestLevel.COMPLETE,
1003             notes = "",
1004             method = "removeNode",
1005             args = {}
1006         ),
1007         @TestTargetNew(
1008             level = TestLevel.COMPLETE,
1009             notes = "",
1010             method = "removeNodeSpi",
1011             args = {}
1012         )
1013     })
1014     public void testRemoveNode() throws BackingStoreException {
1015         String[] nodeArray = new String[]{"First node", "Second node", "Last node"};
1016         int i;
1017         pref.put("Key", "String");
1018         for (i = 0; i < nodeArray.length; i++) {
1019             pref.node(nodeArray[i]);
1020         }
1021         pref.flush();
1022
1023         String[] str = pref.childrenNames();
1024         assertEquals(nodeArray.length, str.length);
1025         for(i = 0; i < nodeArray.length; i++) {
1026             pref.node(nodeArray[i]).removeNode();
1027             str = pref.childrenNames();
1028             assertEquals(nodeArray.length - i - 1, str.length);
1029         }
1030         assertEquals(1, pref.keys().length);
1031         pref.node("Key").removeNode();
1032         assertEquals(1, pref.keys().length);
1033
1034         pref.removeNode();
1035
1036         try {
1037             pref.removeNode();
1038             fail("IllegalStateException expected");
1039         } catch (IllegalStateException e) {
1040             //expected
1041         }
1042
1043         try {
1044             root.removeNode();
1045             fail("UnsupportedOperationException expected");
1046         } catch (UnsupportedOperationException e) {
1047             //expected
1048         }
1049     }
1050
1051     @TestTargetNew(
1052         level = TestLevel.COMPLETE,
1053         notes = "",
1054         method = "name",
1055         args = {}
1056     )
1057     public void testName() {
1058         AbstractPreferences first = (AbstractPreferences) pref.node("First node");
1059         AbstractPreferences second = (AbstractPreferences) pref.node("Second node/sub node");
1060
1061         assertTrue(first.name().compareTo("First node") == 0);
1062         assertFalse(first.name().compareTo("Second node") == 0);
1063         assertTrue(second.name().compareTo("sub node") == 0);
1064     }
1065
1066     @TestTargetNew(
1067         level = TestLevel.COMPLETE,
1068         notes = "",
1069         method = "absolutePath",
1070         args = {}
1071     )
1072     public void testAbsolutePath() {
1073         assertEquals(parent.absolutePath() + "/" + nodeName, pref.absolutePath());
1074         assertEquals(parent.absolutePath() + "/" + "new node", parent.node("new node").absolutePath());
1075     }
1076
1077     @TestTargetNew(
1078         level = TestLevel.COMPLETE,
1079         notes = "",
1080         method = "isUserNode",
1081         args = {}
1082     )
1083     public void testIsUserNode() {
1084         assertTrue(parent.isUserNode());
1085         assertFalse(Preferences.systemRoot().isUserNode());
1086     }
1087
1088     @TestTargets({
1089         @TestTargetNew(
1090             level = TestLevel.COMPLETE,
1091             notes = "Indirectly checks syncSpi",
1092             method = "sync",
1093             args = {}
1094         ),
1095         @TestTargetNew(
1096             level = TestLevel.COMPLETE,
1097             notes = "Indirectly checks syncSpi",
1098             method = "syncSpi",
1099             args = {}
1100         )
1101     })
1102     public void testSync() throws BackingStoreException {
1103         pref.node("new node/sub node");
1104         pref.sync();
1105
1106         pref.removeNode();
1107
1108         try {
1109             pref.sync();
1110             fail("IllegalStateException expected");
1111         } catch (IllegalStateException e) {
1112             //expected
1113         }
1114     }
1115
1116     class MockPreferenceChangeListener implements PreferenceChangeListener {
1117         private boolean flagChange = false;
1118
1119         public void preferenceChange(PreferenceChangeEvent arg0) {
1120             flagChange = true;
1121         }
1122
1123         public boolean isChanged () {
1124             boolean retVal = flagChange;
1125             flagChange = false;
1126             return retVal;
1127         }
1128     }
1129
1130     @TestTargetNew(
1131         level = TestLevel.COMPLETE,
1132         notes = "",
1133         method = "addPreferenceChangeListener",
1134         args = {java.util.prefs.PreferenceChangeListener.class}
1135     )
1136     public void testAddPreferenceChangeListener() throws BackingStoreException {
1137         MockPreferenceChangeListener mpcl = new MockPreferenceChangeListener();
1138         parent.addPreferenceChangeListener(mpcl);
1139         assertFalse(mpcl.isChanged());
1140         pref.node("new node");
1141         pref.flush();
1142         parent.flush();
1143         assertFalse(mpcl.isChanged());
1144         parent.node("new node");
1145         parent.flush();
1146         assertFalse(mpcl.isChanged());
1147         parent.putInt("IntValue", 33);
1148         parent.flush();
1149         parent.flush();
1150         assertTrue(mpcl.isChanged());
1151         assertEquals(33, parent.getInt("IntValue", 22));
1152         parent.flush();
1153         assertFalse(mpcl.isChanged());
1154         assertEquals(22, parent.getInt("Missed Value", 22));
1155         parent.flush();
1156         assertFalse(mpcl.isChanged());
1157     }
1158
1159     @TestTargetNew(
1160         level = TestLevel.COMPLETE,
1161         notes = "",
1162         method = "removePreferenceChangeListener",
1163         args = {java.util.prefs.PreferenceChangeListener.class}
1164     )
1165     public void testRemovePreferenceChangeListener() throws BackingStoreException {
1166         MockPreferenceChangeListener mpcl = new MockPreferenceChangeListener();
1167         parent.addPreferenceChangeListener(mpcl);
1168         assertFalse(mpcl.isChanged());
1169         parent.putInt("IntValue", 33);
1170         parent.flush();
1171         assertTrue(mpcl.isChanged());
1172         parent.removePreferenceChangeListener(mpcl);
1173         parent.putInt("IntValue", 33);
1174         parent.flush();
1175         assertFalse(mpcl.isChanged());
1176     }
1177
1178     class MockNodeChangeListener implements NodeChangeListener {
1179         private boolean flagAdded = false;
1180         private boolean flagRemoved = false;
1181
1182         public void childAdded(NodeChangeEvent arg0) {
1183             flagAdded = true;
1184         }
1185
1186         public void childRemoved(NodeChangeEvent arg0) {
1187             flagRemoved = true;
1188         }
1189
1190         public boolean isAdded() {
1191             return flagAdded;
1192         }
1193
1194         public boolean isRemoved() {
1195             return flagRemoved;
1196         }
1197     }
1198
1199     @TestTargetNew(
1200         level = TestLevel.COMPLETE,
1201         notes = "",
1202         method = "addNodeChangeListener",
1203         args = {java.util.prefs.NodeChangeListener.class}
1204     )
1205     public void testAddNodeChangeListener() throws BackingStoreException {
1206         MockNodeChangeListener mncl = new MockNodeChangeListener();
1207         parent.addNodeChangeListener(mncl);
1208         pref.node("test");
1209         pref.flush();
1210         parent.flush();
1211         assertFalse(mncl.isAdded());
1212         assertFalse(mncl.isRemoved());
1213         pref.removeNode();
1214         parent.flush();
1215         assertFalse(mncl.isAdded());
1216         assertTrue(mncl.isRemoved());
1217         parent.node("new node");
1218         parent.flush();
1219         assertTrue(mncl.isAdded());
1220         assertTrue(mncl.isRemoved());
1221     }
1222
1223     @TestTargetNew(
1224         level = TestLevel.COMPLETE,
1225         notes = "",
1226         method = "removeNodeChangeListener",
1227         args = {java.util.prefs.NodeChangeListener.class}
1228     )
1229     public void testRemoveNodeChangeListener() throws BackingStoreException {
1230         MockNodeChangeListener mncl = new MockNodeChangeListener();
1231         parent.addNodeChangeListener(mncl);
1232         pref.node("test");
1233         pref.flush();
1234         parent.flush();
1235         assertFalse(mncl.isAdded());
1236         assertFalse(mncl.isRemoved());
1237         parent.removeNodeChangeListener(mncl);
1238         pref.removeNode();
1239         parent.flush();
1240         assertFalse(mncl.isAdded());
1241         assertFalse(mncl.isRemoved());
1242         parent.node("new node");
1243         parent.flush();
1244         assertFalse(mncl.isAdded());
1245         assertFalse(mncl.isRemoved());
1246     }
1247
1248     @TestTargets({
1249         @TestTargetNew(
1250             level = TestLevel.COMPLETE,
1251             notes = "BackingStoreException, IOException can not be checked.",
1252             method = "exportNode",
1253             args = {java.io.OutputStream.class}
1254         ),
1255         @TestTargetNew(
1256             level = TestLevel.COMPLETE,
1257             notes = "BackingStoreException, IOException can not be checked.",
1258             method = "flush",
1259             args = {}
1260         ),
1261         @TestTargetNew(
1262             level = TestLevel.COMPLETE,
1263             notes = "BackingStoreException, IOException can not be checked.",
1264             method = "flushSpi",
1265             args = {}
1266         )
1267     })
1268     public void testExportNode() throws BackingStoreException, IOException, InvalidPreferencesFormatException {
1269         AbstractPreferences ap = (AbstractPreferences) pref.node("New node");
1270         pref.putInt("IntValue", 33);
1271         pref.putBoolean("BoolValue", true);
1272         pref.flush();
1273
1274         ByteArrayOutputStream baos = new ByteArrayOutputStream();
1275
1276         pref.exportNode(baos);
1277         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
1278
1279         assertTrue(pref.getBoolean("BoolValue", false));
1280         assertEquals(33, pref.getInt("IntValue", 22));
1281         assertEquals(1, pref.childrenNames().length);
1282
1283         String xmlData = new String(baos.toByteArray());
1284
1285         assertTrue(xmlData.contains("IntValue"));
1286         assertTrue(xmlData.contains("BoolValue"));
1287         assertTrue(xmlData.contains("33"));
1288         assertTrue(xmlData.contains("true"));
1289
1290         pref.removeNode();
1291
1292         try {
1293             pref.exportNode(new ByteArrayOutputStream());
1294             fail("IllegalStateException expected");
1295         } catch (IllegalStateException e) {
1296             //expected
1297         }
1298
1299         try {
1300             pref.getBoolean("BoolValue", false);
1301             fail("IllegalStateException expected");
1302         } catch (IllegalStateException e) {
1303             //expected
1304         }
1305         pref = (AbstractPreferences) parent.node(nodeName);
1306
1307         pref.importPreferences(bais);
1308
1309         assertTrue(pref.getBoolean("BoolValue", false));
1310         assertEquals(33, pref.getInt("IntValue", 22));
1311         assertEquals(0, pref.childrenNames().length);
1312     }
1313
1314     @TestTargets({
1315         @TestTargetNew(
1316             level = TestLevel.COMPLETE,
1317             notes = "BackingStoreException, IOException can not be checked.",
1318             method = "exportSubtree",
1319             args = {java.io.OutputStream.class}
1320         ),
1321         @TestTargetNew(
1322             level = TestLevel.COMPLETE,
1323             notes = "BackingStoreException, IOException can not be checked.",
1324             method = "flush",
1325             args = {}
1326         ),
1327         @TestTargetNew(
1328             level = TestLevel.COMPLETE,
1329             notes = "BackingStoreException, IOException can not be checked.",
1330             method = "flushSpi",
1331             args = {}
1332         )
1333     })
1334     public void testExportSubtree() throws BackingStoreException, IOException, InvalidPreferencesFormatException {
1335         AbstractPreferences ap1 = (AbstractPreferences) pref.node("First node");
1336         AbstractPreferences ap2 = (AbstractPreferences) pref.node("Second node");
1337         pref.putInt("IntegerValue", 33);
1338         pref.putBoolean("BoolValue", true);
1339         pref.flush();
1340
1341         ap1.putInt("FirstIntValue", 11);
1342         ap2.putDouble("DoubleValue", new Double(6.626e-34));
1343
1344         ByteArrayOutputStream baos = new ByteArrayOutputStream();
1345
1346         pref.exportSubtree(baos);
1347         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
1348
1349         assertTrue(pref.getBoolean("BoolValue", false));
1350         assertEquals(33, pref.getInt("IntegerValue", 22));
1351         assertEquals(2, pref.childrenNames().length);
1352         assertEquals(11, ap1.getInt("FirstIntValue", 22));
1353         assertEquals(new Double(6.626e-34), ap2.getDouble("DoubleValue", new Double (3.14)));
1354
1355         String xmlData = new String(baos.toByteArray());
1356
1357         assertTrue(xmlData.contains("IntegerValue"));
1358         assertTrue(xmlData.contains("BoolValue"));
1359         assertTrue(xmlData.contains("FirstIntValue"));
1360         assertTrue(xmlData.contains("DoubleValue"));
1361         assertTrue(xmlData.contains("33"));
1362         assertTrue(xmlData.contains("true"));
1363         assertTrue(xmlData.contains("11"));
1364         assertTrue(xmlData.contains("6.626E-34"));
1365
1366         pref.removeNode();
1367
1368         try {
1369             pref.exportSubtree(new ByteArrayOutputStream());
1370             fail("IllegalStateException expected");
1371         } catch (IllegalStateException e) {
1372             //expected
1373         }
1374
1375         try {
1376             pref.getBoolean("BoolValue", false);
1377             fail("IllegalStateException expected");
1378         } catch (IllegalStateException e) {
1379             //expected
1380         }
1381         pref = (AbstractPreferences) parent.node(nodeName);
1382         pref.importPreferences(bais);
1383
1384         ap1 = (AbstractPreferences) pref.node("First node");
1385         ap2 = (AbstractPreferences) pref.node("Second node");
1386
1387         assertTrue(pref.getBoolean("BoolValue", false));
1388         assertEquals(33, pref.getInt("IntegerValue", 22));
1389         assertEquals(2, pref.childrenNames().length);
1390         assertEquals(11, ap1.getInt("FirstIntValue", 22));
1391         assertEquals(new Double(6.626e-34), ap2.getDouble("DoubleValue", new Double (3.14)));
1392     }
1393
1394     class MockAbstractPreferences extends AbstractPreferences {
1395         protected MockAbstractPreferences(AbstractPreferences parent, String name) {
1396             super(parent, name);
1397         }
1398
1399         @Override
1400         protected AbstractPreferences childSpi(String name) {
1401             return null;
1402         }
1403
1404         @Override
1405         protected String[] childrenNamesSpi() throws BackingStoreException {
1406             return null;
1407         }
1408
1409         @Override
1410         protected void flushSpi() throws BackingStoreException {
1411         }
1412
1413         @Override
1414         protected String getSpi(String key) {
1415             return null;
1416         }
1417
1418         @Override
1419         protected String[] keysSpi() throws BackingStoreException {
1420             return null;
1421         }
1422
1423         @Override
1424         protected void putSpi(String key, String value) {
1425         }
1426
1427         @Override
1428         protected void removeNodeSpi() throws BackingStoreException {
1429         }
1430
1431         @Override
1432         protected void removeSpi(String key) {
1433         }
1434
1435         @Override
1436         protected void syncSpi() throws BackingStoreException {
1437         }
1438     }
1439
1440     @TestTargetNew(
1441         level = TestLevel.COMPLETE,
1442         notes = "",
1443         method = "AbstractPreferences",
1444         args = {java.util.prefs.AbstractPreferences.class, java.lang.String.class}
1445     )
1446     public void testAbstractPreferences() {
1447         assertNotNull(new MockAbstractPreferences(pref, "node name"));
1448         try {
1449             new MockAbstractPreferences(pref, "node/name");
1450             fail("IllegalArgumentException expected");
1451         } catch (IllegalArgumentException e) {
1452             //expected
1453         }
1454
1455         try {
1456             new MockAbstractPreferences(null, "node");
1457             fail("IllegalArgumentException expected");
1458         } catch (IllegalArgumentException e) {
1459             //expected
1460         }
1461     }
1462
1463     @TestTargetNew(
1464         level = TestLevel.COMPLETE,
1465         notes = "Tested indirectly",
1466         method = "cachedChildren",
1467         args = {}
1468     )
1469     public void testCachedChildren() throws BackingStoreException {
1470         pref.node("First node");
1471         pref.node("Second node");
1472
1473         assertEquals(2, pref.childrenNames().length);
1474     }
1475
1476     @TestTargetNew(
1477         level = TestLevel.COMPLETE,
1478         notes = "No reason to check dummy implementation",
1479         method = "isRemoved",
1480         args = {}
1481     )
1482     public void testIsRemoved() {
1483     }
1484 }