2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.apache.harmony.prefs.tests.java.util.prefs;
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;
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;
37 @TestTargetClass(AbstractPreferences.class)
38 public class AbstractPreferencesTest extends TestCase {
40 AbstractPreferences pref;
42 static AbstractPreferences root;
44 static final String nodeName = "mock";
46 static AbstractPreferences parent = null;
48 String oldUserHome = System.getProperty("user.home");
49 String oldJavaHome = System.getProperty("java.home");
51 protected void setUp() throws Exception {
54 root = (AbstractPreferences) Preferences.userRoot();
55 parent = (AbstractPreferences) Preferences.userNodeForPackage(this.getClass());
57 pref = (AbstractPreferences) parent.node(nodeName);
60 protected void tearDown() throws Exception {
65 level = TestLevel.COMPLETE,
70 public void testToString() {
71 assertTrue(pref.toString().contains(nodeName));
76 level = TestLevel.COMPLETE,
77 notes = "Tests putSpi indirectly",
79 args = {java.lang.String.class, java.lang.String.class}
82 level = TestLevel.COMPLETE,
83 notes = "Tests putSpi indirectly",
85 args = {java.lang.String.class, java.lang.String.class}
88 public void testPut() throws BackingStoreException {
89 pref.put("Value", "String");
92 assertEquals("String", pref.get("Value", ":"));
95 pref.put(null, "Exception");
96 fail("NullPointerException expected");
97 } catch (NullPointerException e) {
102 StringBuffer sb = new StringBuffer();
104 for (i = 0; i < Preferences.MAX_KEY_LENGTH + 1; i++) {
109 pref.put(new String(sb), "Exception");
110 fail("IllegalArgumentException expected");
111 } catch (IllegalArgumentException e) {
115 sb = new StringBuffer();
117 for (i = 0; i < Preferences.MAX_VALUE_LENGTH + 1; i++) {
122 pref.put("DoubleValue", new String(sb));
123 fail("IllegalArgumentException expected");
124 } catch (IllegalArgumentException e) {
131 pref.put("DoubleValue", "Exception");
132 fail("IllegalStateException expected");
133 } catch (IllegalStateException e) {
140 level = TestLevel.COMPLETE,
141 notes = "getSpi tested indirectly.",
143 args = {java.lang.String.class, java.lang.String.class}
146 level = TestLevel.COMPLETE,
147 notes = "getSpi tested indirectly.",
149 args = {java.lang.String.class}
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);
158 assertEquals("String", pref.get("Value", ":"));
159 assertEquals("true", pref.get("BoolValue", ":"));
160 assertEquals("9.10938188E-31", pref.get("DoubleValue", null));
163 pref.get(null, "Exception");
164 fail("NullPointerException expected");
165 } catch (NullPointerException e) {
172 pref.get("DoubleValue", "Exception");
173 fail("IllegalStateException expected");
174 } catch (IllegalStateException e) {
181 level = TestLevel.COMPLETE,
182 notes = "Indirectly checks removeSpi",
184 args = {java.lang.String.class}
187 level = TestLevel.COMPLETE,
188 notes = "Indirectly checks removeSpi",
189 method = "removeSpi",
190 args = {java.lang.String.class}
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");
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]);
207 assertEquals(keyArray.length - i - 1, str.length);
209 assertEquals(1, pref.childrenNames().length);
210 pref.remove("New node");
211 assertEquals(1, pref.childrenNames().length);
216 pref.remove("New node");
217 fail("IllegalStateException expected");
218 } catch (IllegalStateException e) {
224 level = TestLevel.COMPLETE,
229 public void testClear() throws BackingStoreException {
230 AbstractPreferences ap = (AbstractPreferences) pref.node("New node");
231 pref.putInt("IntValue", 33);
232 pref.putBoolean("BoolValue", true);
234 assertTrue(pref.getBoolean("BoolValue", false));
235 assertEquals(33, pref.getInt("IntValue", 22));
236 assertEquals(1, pref.childrenNames().length);
238 assertFalse(pref.getBoolean("BoolValue", false));
239 assertEquals(22, pref.getInt("IntValue", 22));
240 assertEquals(1, pref.childrenNames().length);
246 fail("IllegalStateException expected");
247 } catch (IllegalStateException e) {
253 fail("IllegalStateException expected");
254 } catch (IllegalStateException e) {
260 level = TestLevel.COMPLETE,
263 args = {java.lang.String.class, int.class}
265 public void testPutInt() throws BackingStoreException {
266 pref.putInt("IntValue", 299792458);
269 assertEquals(299792458, pref.getInt("IntValue", new Integer(1)));
272 pref.putInt(null, new Integer(1));
273 fail("NullPointerException expected");
274 } catch (NullPointerException e) {
279 StringBuffer sb = new StringBuffer();
281 for (i = 0; i < Preferences.MAX_KEY_LENGTH + 1; i++) {
286 pref.putInt(new String(sb), new Integer(1));
287 fail("IllegalArgumentException expected");
288 } catch (IllegalArgumentException e) {
295 pref.putInt("IntValue", new Integer(1));
296 fail("IllegalStateException expected");
297 } catch (IllegalStateException e) {
303 level = TestLevel.COMPLETE,
306 args = {java.lang.String.class, int.class}
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);
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)));
321 pref.getInt(null, new Integer(1));
322 fail("NullPointerException expected");
323 } catch (NullPointerException e) {
330 pref.getInt("IntValue", new Integer(1));
331 fail("IllegalStateException expected");
332 } catch (IllegalStateException e) {
338 level = TestLevel.COMPLETE,
341 args = {java.lang.String.class, long.class}
343 public void testPutLong() throws BackingStoreException {
344 pref.putLong("LongValue", new Long(299792458));
347 assertEquals(299792458L, pref.getLong("LongValue", new Long(1)));
350 pref.putLong(null, new Long(1));
351 fail("NullPointerException expected");
352 } catch (NullPointerException e) {
357 StringBuffer sb = new StringBuffer();
359 for (i = 0; i < Preferences.MAX_KEY_LENGTH + 1; i++) {
364 pref.putLong(new String(sb), new Long(1));
365 fail("IllegalArgumentException expected");
366 } catch (IllegalArgumentException e) {
373 pref.putLong("LongValue", new Long(1));
374 fail("IllegalStateException expected");
375 } catch (IllegalStateException e) {
381 level = TestLevel.COMPLETE,
384 args = {java.lang.String.class, long.class}
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);
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)));
399 pref.getLong(null, new Long(1));
400 fail("NullPointerException expected");
401 } catch (NullPointerException e) {
408 pref.getLong("LongValue", new Long(1));
409 fail("IllegalStateException expected");
410 } catch (IllegalStateException e) {
416 level = TestLevel.COMPLETE,
418 method = "putBoolean",
419 args = {java.lang.String.class, boolean.class}
421 public void testPutBoolean() throws BackingStoreException {
422 pref.putBoolean("BoolValue", true);
425 assertTrue(pref.getBoolean("BoolValue", false));
428 pref.putBoolean(null, true);
429 fail("NullPointerException expected");
430 } catch (NullPointerException e) {
435 StringBuffer sb = new StringBuffer();
437 for (i = 0; i < Preferences.MAX_KEY_LENGTH + 1; i++) {
442 pref.putBoolean(new String(sb), true);
443 fail("IllegalArgumentException expected");
444 } catch (IllegalArgumentException e) {
451 pref.putBoolean("DoubleValue", true);
452 fail("IllegalStateException expected");
453 } catch (IllegalStateException e) {
459 level = TestLevel.COMPLETE,
461 method = "getBoolean",
462 args = {java.lang.String.class, boolean.class}
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);
470 assertFalse(pref.getBoolean("Value", false));
471 assertTrue(pref.getBoolean("BoolValue", false));
472 assertFalse(pref.getBoolean("DoubleValue", false));
475 pref.getBoolean(null, true);
476 fail("NullPointerException expected");
477 } catch (NullPointerException e) {
484 pref.getBoolean("DoubleValue", true);
485 fail("IllegalStateException expected");
486 } catch (IllegalStateException e) {
492 level = TestLevel.COMPLETE,
495 args = {java.lang.String.class, float.class}
497 public void testPutFloat() throws BackingStoreException {
498 pref.putFloat("FloatValue", new Float(1.602e-19));
501 assertEquals(new Float(1.602e-19), pref.getFloat("FloatValue", new Float(0.2)));
504 pref.putFloat(null, new Float(0.1));
505 fail("NullPointerException expected");
506 } catch (NullPointerException e) {
511 StringBuffer sb = new StringBuffer();
513 for (i = 0; i < Preferences.MAX_KEY_LENGTH + 1; i++) {
518 pref.putFloat(new String(sb), new Float(0.1));
519 fail("IllegalArgumentException expected");
520 } catch (IllegalArgumentException e) {
527 pref.putFloat("FloatValue", new Float(0.1));
528 fail("IllegalStateException expected");
529 } catch (IllegalStateException e) {
535 level = TestLevel.COMPLETE,
538 args = {java.lang.String.class, float.class}
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);
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)));
553 pref.getFloat(null, new Float(0.1));
554 fail("NullPointerException expected");
555 } catch (NullPointerException e) {
562 pref.getFloat("FloatValue", new Float(0.1));
563 fail("IllegalStateException expected");
564 } catch (IllegalStateException e) {
570 level = TestLevel.COMPLETE,
572 method = "putDouble",
573 args = {java.lang.String.class, double.class}
575 public void testPutDouble() throws BackingStoreException {
576 pref.putDouble("DoubleValue", new Double(9.10938188e-31));
579 assertEquals(new Double(9.10938188e-31), pref.getDouble("DoubleValue", new Double(2.14)));
582 pref.putDouble(null, new Double(0.1));
583 fail("NullPointerException expected");
584 } catch (NullPointerException e) {
589 StringBuffer sb = new StringBuffer();
591 for (i = 0; i < Preferences.MAX_KEY_LENGTH + 1; i++) {
596 pref.putDouble(new String(sb), new Double(0.1));
597 fail("IllegalArgumentException expected");
598 } catch (IllegalArgumentException e) {
605 pref.putDouble("DoubleValue", new Double(0.1));
606 fail("IllegalStateException expected");
607 } catch (IllegalStateException e) {
613 level = TestLevel.COMPLETE,
615 method = "getDouble",
616 args = {java.lang.String.class, double.class}
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);
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)));
631 pref.getDouble(null, new Double(0.1));
632 fail("NullPointerException expected");
633 } catch (NullPointerException e) {
640 pref.getDouble("DoubleValue", new Double(0.1));
641 fail("IllegalStateException expected");
642 } catch (IllegalStateException e) {
648 level = TestLevel.COMPLETE,
650 method = "putByteArray",
651 args = {java.lang.String.class, byte[].class}
653 public void testPutByteArray() throws BackingStoreException {
654 byte[] bArray = new byte[]{1, 2, 3, 4, 5};
657 pref.putByteArray("Array", bArray);
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]);
667 pref.putByteArray(null, bArray);
668 fail("NullPointerException expected");
669 } catch (NullPointerException e) {
673 StringBuffer sb = new StringBuffer();
675 for (i = 0; i < Preferences.MAX_KEY_LENGTH + 1; i++) {
680 pref.putByteArray(new String(sb), bArray);
681 fail("IllegalArgumentException expected");
682 } catch (IllegalArgumentException e) {
686 bArray = new byte[Preferences.MAX_VALUE_LENGTH * 3 / 4 + 1];
689 pref.putByteArray("Big array", bArray);
690 fail("IllegalArgumentException expected");
691 } catch (IllegalArgumentException e) {
698 pref.putByteArray("Array", new byte[10]);
699 fail("IllegalStateException expected");
700 } catch (IllegalStateException e) {
706 level = TestLevel.COMPLETE,
708 method = "getByteArray",
709 args = {java.lang.String.class, byte[].class}
711 public void testGetByteArray() throws BackingStoreException {
712 byte[] bArray = new byte[]{1, 2, 3, 4, 5};
713 byte[] tmp = new byte[]{5};
716 pref.put("Value", "String");
717 pref.putDouble("DoubleValue", new Double(9.10938188e-31));
718 pref.putByteArray("Array", bArray);
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]);
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]);
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]);
740 pref.getByteArray(null, tmp);
741 fail("NullPointerException expected");
742 } catch (NullPointerException e) {
749 pref.getByteArray("Array", tmp);
750 fail("IllegalStateException expected");
751 } catch (IllegalStateException e) {
758 level = TestLevel.COMPLETE,
759 notes = "keysSpi tested indirectly",
764 level = TestLevel.COMPLETE,
765 notes = "keysSpi tested indirectly",
770 public void testKeys() throws BackingStoreException {
771 String[] keyArray = new String[]{"Value", "DoubleValue", "BoolValue", "IntValue"};
772 String nodeStr = "New node";
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);
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) {
790 assertTrue(str[i].compareTo(nodeStr) != 0);
798 fail("IllegalStateException expected");
799 } catch(IllegalStateException e) {
806 level = TestLevel.COMPLETE,
807 notes = "BackingStoreException can not be checked. childrenNamesSpi checked indirectly.",
808 method = "childrenNames",
812 level = TestLevel.COMPLETE,
813 notes = "BackingStoreException can not be checked. childrenNamesSpi checked indirectly.",
814 method = "childrenNamesSpi",
818 public void testChildrenNames() throws BackingStoreException {
819 AbstractPreferences first = (AbstractPreferences) pref.node("First node");
820 AbstractPreferences second = (AbstractPreferences) pref.node("Second node");
822 assertEquals(2, pref.childrenNames().length);
823 assertEquals(0, first.childrenNames().length);
824 assertEquals(0, second.childrenNames().length);
829 second.childrenNames();
830 fail("IllegalStateException expected");
831 } catch (IllegalStateException e) {
838 first.childrenNames();
839 fail("IllegalStateException expected");
840 } catch (IllegalStateException e) {
846 level = TestLevel.PARTIAL_COMPLETE,
848 method = "nodeExists",
849 args = {String.class}
851 public void test_nodeExists() throws BackingStoreException {
852 AbstractPreferences test = (AbstractPreferences) Preferences.userRoot()
855 test.nodeExists(null);
856 fail("should throw NullPointerException");
857 } catch (NullPointerException e) {
863 test.nodeExists(null);
864 fail("should throw NullPointerException");
865 } catch (NullPointerException e) {
871 level = TestLevel.COMPLETE,
876 public void testParent() throws BackingStoreException {
877 AbstractPreferences node = (AbstractPreferences) pref.node("First node/sub node");
879 assertTrue(node.parent().name().compareTo("First node") == 0);
885 fail("IllegalStateException expected");
886 } catch (IllegalStateException e) {
893 level = TestLevel.COMPLETE,
894 notes = "Indirecly checks childSpi",
896 args = {java.lang.String.class}
899 level = TestLevel.COMPLETE,
900 notes = "Indirecly checks childSpi",
902 args = {java.lang.String.class}
905 public void testNode() throws BackingStoreException {
906 AbstractPreferences first = (AbstractPreferences) pref.node("First node");
907 AbstractPreferences second = (AbstractPreferences) pref.node("Second node");
910 first.node("blabla/");
911 fail("IllegalArgumentException expected");
912 } catch (IllegalArgumentException e) {
917 first.node("///invalid");
918 fail("IllegalArgumentException expected");
919 } catch (IllegalArgumentException e) {
923 StringBuffer sb = new StringBuffer();
925 for (int i = 0; i < Preferences.MAX_NAME_LENGTH; i++) {
928 first.node(new String(sb));
932 first.node(new String(sb));
933 fail("IllegalArgumentException expected");
934 } catch (IllegalArgumentException e) {
942 fail("IllegalStateException expected");
943 } catch (IllegalStateException e) {
949 fail("IllegalStateException expected");
950 } catch (IllegalStateException e) {
957 level = TestLevel.COMPLETE,
958 notes = "getChild tested indirectly",
959 method = "nodeExists",
960 args = {java.lang.String.class}
963 level = TestLevel.COMPLETE,
964 notes = "getChild tested indirectly",
966 args = {java.lang.String.class}
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);
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));
984 pref.nodeExists("///invalid");
985 fail("IllegalArgumentException expected");
986 } catch (IllegalArgumentException e) {
993 pref.nodeExists("Exception");
994 fail("IllegalStateException expected");
995 } catch (IllegalStateException e) {
1002 level = TestLevel.COMPLETE,
1004 method = "removeNode",
1008 level = TestLevel.COMPLETE,
1010 method = "removeNodeSpi",
1014 public void testRemoveNode() throws BackingStoreException {
1015 String[] nodeArray = new String[]{"First node", "Second node", "Last node"};
1017 pref.put("Key", "String");
1018 for (i = 0; i < nodeArray.length; i++) {
1019 pref.node(nodeArray[i]);
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);
1030 assertEquals(1, pref.keys().length);
1031 pref.node("Key").removeNode();
1032 assertEquals(1, pref.keys().length);
1038 fail("IllegalStateException expected");
1039 } catch (IllegalStateException e) {
1045 fail("UnsupportedOperationException expected");
1046 } catch (UnsupportedOperationException e) {
1052 level = TestLevel.COMPLETE,
1057 public void testName() {
1058 AbstractPreferences first = (AbstractPreferences) pref.node("First node");
1059 AbstractPreferences second = (AbstractPreferences) pref.node("Second node/sub node");
1061 assertTrue(first.name().compareTo("First node") == 0);
1062 assertFalse(first.name().compareTo("Second node") == 0);
1063 assertTrue(second.name().compareTo("sub node") == 0);
1067 level = TestLevel.COMPLETE,
1069 method = "absolutePath",
1072 public void testAbsolutePath() {
1073 assertEquals(parent.absolutePath() + "/" + nodeName, pref.absolutePath());
1074 assertEquals(parent.absolutePath() + "/" + "new node", parent.node("new node").absolutePath());
1078 level = TestLevel.COMPLETE,
1080 method = "isUserNode",
1083 public void testIsUserNode() {
1084 assertTrue(parent.isUserNode());
1085 assertFalse(Preferences.systemRoot().isUserNode());
1090 level = TestLevel.COMPLETE,
1091 notes = "Indirectly checks syncSpi",
1096 level = TestLevel.COMPLETE,
1097 notes = "Indirectly checks syncSpi",
1102 public void testSync() throws BackingStoreException {
1103 pref.node("new node/sub node");
1110 fail("IllegalStateException expected");
1111 } catch (IllegalStateException e) {
1116 class MockPreferenceChangeListener implements PreferenceChangeListener {
1117 private boolean flagChange = false;
1119 public void preferenceChange(PreferenceChangeEvent arg0) {
1123 public boolean isChanged () {
1124 boolean retVal = flagChange;
1131 level = TestLevel.COMPLETE,
1133 method = "addPreferenceChangeListener",
1134 args = {java.util.prefs.PreferenceChangeListener.class}
1136 public void testAddPreferenceChangeListener() throws BackingStoreException {
1137 MockPreferenceChangeListener mpcl = new MockPreferenceChangeListener();
1138 parent.addPreferenceChangeListener(mpcl);
1139 assertFalse(mpcl.isChanged());
1140 pref.node("new node");
1143 assertFalse(mpcl.isChanged());
1144 parent.node("new node");
1146 assertFalse(mpcl.isChanged());
1147 parent.putInt("IntValue", 33);
1150 assertTrue(mpcl.isChanged());
1151 assertEquals(33, parent.getInt("IntValue", 22));
1153 assertFalse(mpcl.isChanged());
1154 assertEquals(22, parent.getInt("Missed Value", 22));
1156 assertFalse(mpcl.isChanged());
1160 level = TestLevel.COMPLETE,
1162 method = "removePreferenceChangeListener",
1163 args = {java.util.prefs.PreferenceChangeListener.class}
1165 public void testRemovePreferenceChangeListener() throws BackingStoreException {
1166 MockPreferenceChangeListener mpcl = new MockPreferenceChangeListener();
1167 parent.addPreferenceChangeListener(mpcl);
1168 assertFalse(mpcl.isChanged());
1169 parent.putInt("IntValue", 33);
1171 assertTrue(mpcl.isChanged());
1172 parent.removePreferenceChangeListener(mpcl);
1173 parent.putInt("IntValue", 33);
1175 assertFalse(mpcl.isChanged());
1178 class MockNodeChangeListener implements NodeChangeListener {
1179 private boolean flagAdded = false;
1180 private boolean flagRemoved = false;
1182 public void childAdded(NodeChangeEvent arg0) {
1186 public void childRemoved(NodeChangeEvent arg0) {
1190 public boolean isAdded() {
1194 public boolean isRemoved() {
1200 level = TestLevel.COMPLETE,
1202 method = "addNodeChangeListener",
1203 args = {java.util.prefs.NodeChangeListener.class}
1205 public void testAddNodeChangeListener() throws BackingStoreException {
1206 MockNodeChangeListener mncl = new MockNodeChangeListener();
1207 parent.addNodeChangeListener(mncl);
1211 assertFalse(mncl.isAdded());
1212 assertFalse(mncl.isRemoved());
1215 assertFalse(mncl.isAdded());
1216 assertTrue(mncl.isRemoved());
1217 parent.node("new node");
1219 assertTrue(mncl.isAdded());
1220 assertTrue(mncl.isRemoved());
1224 level = TestLevel.COMPLETE,
1226 method = "removeNodeChangeListener",
1227 args = {java.util.prefs.NodeChangeListener.class}
1229 public void testRemoveNodeChangeListener() throws BackingStoreException {
1230 MockNodeChangeListener mncl = new MockNodeChangeListener();
1231 parent.addNodeChangeListener(mncl);
1235 assertFalse(mncl.isAdded());
1236 assertFalse(mncl.isRemoved());
1237 parent.removeNodeChangeListener(mncl);
1240 assertFalse(mncl.isAdded());
1241 assertFalse(mncl.isRemoved());
1242 parent.node("new node");
1244 assertFalse(mncl.isAdded());
1245 assertFalse(mncl.isRemoved());
1250 level = TestLevel.COMPLETE,
1251 notes = "BackingStoreException, IOException can not be checked.",
1252 method = "exportNode",
1253 args = {java.io.OutputStream.class}
1256 level = TestLevel.COMPLETE,
1257 notes = "BackingStoreException, IOException can not be checked.",
1262 level = TestLevel.COMPLETE,
1263 notes = "BackingStoreException, IOException can not be checked.",
1264 method = "flushSpi",
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);
1274 ByteArrayOutputStream baos = new ByteArrayOutputStream();
1276 pref.exportNode(baos);
1277 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
1279 assertTrue(pref.getBoolean("BoolValue", false));
1280 assertEquals(33, pref.getInt("IntValue", 22));
1281 assertEquals(1, pref.childrenNames().length);
1283 String xmlData = new String(baos.toByteArray());
1285 assertTrue(xmlData.contains("IntValue"));
1286 assertTrue(xmlData.contains("BoolValue"));
1287 assertTrue(xmlData.contains("33"));
1288 assertTrue(xmlData.contains("true"));
1293 pref.exportNode(new ByteArrayOutputStream());
1294 fail("IllegalStateException expected");
1295 } catch (IllegalStateException e) {
1300 pref.getBoolean("BoolValue", false);
1301 fail("IllegalStateException expected");
1302 } catch (IllegalStateException e) {
1305 pref = (AbstractPreferences) parent.node(nodeName);
1307 pref.importPreferences(bais);
1309 assertTrue(pref.getBoolean("BoolValue", false));
1310 assertEquals(33, pref.getInt("IntValue", 22));
1311 assertEquals(0, pref.childrenNames().length);
1316 level = TestLevel.COMPLETE,
1317 notes = "BackingStoreException, IOException can not be checked.",
1318 method = "exportSubtree",
1319 args = {java.io.OutputStream.class}
1322 level = TestLevel.COMPLETE,
1323 notes = "BackingStoreException, IOException can not be checked.",
1328 level = TestLevel.COMPLETE,
1329 notes = "BackingStoreException, IOException can not be checked.",
1330 method = "flushSpi",
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);
1341 ap1.putInt("FirstIntValue", 11);
1342 ap2.putDouble("DoubleValue", new Double(6.626e-34));
1344 ByteArrayOutputStream baos = new ByteArrayOutputStream();
1346 pref.exportSubtree(baos);
1347 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
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)));
1355 String xmlData = new String(baos.toByteArray());
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"));
1369 pref.exportSubtree(new ByteArrayOutputStream());
1370 fail("IllegalStateException expected");
1371 } catch (IllegalStateException e) {
1376 pref.getBoolean("BoolValue", false);
1377 fail("IllegalStateException expected");
1378 } catch (IllegalStateException e) {
1381 pref = (AbstractPreferences) parent.node(nodeName);
1382 pref.importPreferences(bais);
1384 ap1 = (AbstractPreferences) pref.node("First node");
1385 ap2 = (AbstractPreferences) pref.node("Second node");
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)));
1394 class MockAbstractPreferences extends AbstractPreferences {
1395 protected MockAbstractPreferences(AbstractPreferences parent, String name) {
1396 super(parent, name);
1400 protected AbstractPreferences childSpi(String name) {
1405 protected String[] childrenNamesSpi() throws BackingStoreException {
1410 protected void flushSpi() throws BackingStoreException {
1414 protected String getSpi(String key) {
1419 protected String[] keysSpi() throws BackingStoreException {
1424 protected void putSpi(String key, String value) {
1428 protected void removeNodeSpi() throws BackingStoreException {
1432 protected void removeSpi(String key) {
1436 protected void syncSpi() throws BackingStoreException {
1441 level = TestLevel.COMPLETE,
1443 method = "AbstractPreferences",
1444 args = {java.util.prefs.AbstractPreferences.class, java.lang.String.class}
1446 public void testAbstractPreferences() {
1447 assertNotNull(new MockAbstractPreferences(pref, "node name"));
1449 new MockAbstractPreferences(pref, "node/name");
1450 fail("IllegalArgumentException expected");
1451 } catch (IllegalArgumentException e) {
1456 new MockAbstractPreferences(null, "node");
1457 fail("IllegalArgumentException expected");
1458 } catch (IllegalArgumentException e) {
1464 level = TestLevel.COMPLETE,
1465 notes = "Tested indirectly",
1466 method = "cachedChildren",
1469 public void testCachedChildren() throws BackingStoreException {
1470 pref.node("First node");
1471 pref.node("Second node");
1473 assertEquals(2, pref.childrenNames().length);
1477 level = TestLevel.COMPLETE,
1478 notes = "No reason to check dummy implementation",
1479 method = "isRemoved",
1482 public void testIsRemoved() {