Home | History | Annotate | Download | only in prefs
      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 libcore.java.util.prefs;
     18 
     19 import java.io.ByteArrayInputStream;
     20 import java.io.ByteArrayOutputStream;
     21 import java.io.IOException;
     22 import java.util.prefs.AbstractPreferences;
     23 import java.util.prefs.BackingStoreException;
     24 import java.util.prefs.InvalidPreferencesFormatException;
     25 import java.util.prefs.NodeChangeEvent;
     26 import java.util.prefs.NodeChangeListener;
     27 import java.util.prefs.PreferenceChangeEvent;
     28 import java.util.prefs.PreferenceChangeListener;
     29 import java.util.prefs.Preferences;
     30 import junit.framework.TestCase;
     31 
     32 public final class OldAbstractPreferencesTest extends TestCase {
     33 
     34     static final String nodeName = "mock";
     35 
     36     AbstractPreferences pref;
     37     AbstractPreferences root;
     38     AbstractPreferences parent = null;
     39 
     40     protected void setUp() throws Exception {
     41         super.setUp();
     42 
     43         root = (AbstractPreferences) Preferences.userRoot();
     44         for (String child : root.childrenNames()) {
     45             root.node(child).removeNode();
     46         }
     47         root.clear();
     48 
     49         parent = (AbstractPreferences) Preferences.userNodeForPackage(getClass());
     50         pref = (AbstractPreferences) parent.node(nodeName);
     51     }
     52 
     53     public void testToString() {
     54         assertTrue(pref.toString().contains(nodeName));
     55     }
     56 
     57     public void testPut() throws BackingStoreException {
     58         pref.put("Value", "String");
     59         pref.flush();
     60 
     61         assertEquals("String", pref.get("Value", ":"));
     62 
     63         try {
     64             pref.put(null, "Exception");
     65             fail("NullPointerException expected");
     66         } catch (NullPointerException e) {
     67             //expected
     68         }
     69 
     70         int i;
     71         StringBuffer sb = new StringBuffer();
     72 
     73         for (i = 0; i < Preferences.MAX_KEY_LENGTH + 1; i++) {
     74             sb.append('c');
     75         }
     76 
     77         try {
     78             pref.put(new String(sb), "Exception");
     79             fail("IllegalArgumentException expected");
     80         } catch (IllegalArgumentException e) {
     81             //expected
     82         }
     83 
     84         sb = new StringBuffer();
     85 
     86         for (i = 0; i < Preferences.MAX_VALUE_LENGTH + 1; i++) {
     87             sb.append('c');
     88         }
     89 
     90         try {
     91             pref.put("DoubleValue", new String(sb));
     92             fail("IllegalArgumentException expected");
     93         } catch (IllegalArgumentException e) {
     94             //expected
     95         }
     96 
     97         pref.removeNode();
     98 
     99         try {
    100             pref.put("DoubleValue", "Exception");
    101             fail("IllegalStateException expected");
    102         } catch (IllegalStateException e) {
    103             //expected
    104         }
    105     }
    106 
    107     public void testGet() throws BackingStoreException {
    108         pref.put("Value", "String");
    109         pref.putDouble("DoubleValue", new Double(9.10938188e-31));
    110         pref.putBoolean("BoolValue", true);
    111         pref.flush();
    112 
    113         assertEquals("String", pref.get("Value", ":"));
    114         assertEquals("true", pref.get("BoolValue", ":"));
    115         assertEquals("9.10938188E-31", pref.get("DoubleValue", null));
    116 
    117         try {
    118             pref.get(null, "Exception");
    119             fail("NullPointerException expected");
    120         } catch (NullPointerException e) {
    121             //expected
    122         }
    123 
    124         pref.removeNode();
    125 
    126         try {
    127             pref.get("DoubleValue", "Exception");
    128             fail("IllegalStateException expected");
    129         } catch (IllegalStateException e) {
    130             //expected
    131         }
    132     }
    133 
    134     public void testRemove() throws BackingStoreException {
    135         String[] keyArray = new String[]{"Value", "DoubleValue", "LongValue", "IntValue"};
    136         pref.put(keyArray[0], "String");
    137         pref.putDouble(keyArray[1], new Double(9.10938188e-31));
    138         pref.putLong(keyArray[2], new Long(Long.MIN_VALUE));
    139         pref.putInt(keyArray[3], 299792458);
    140         pref.node("New node");
    141         pref.flush();
    142 
    143         String[] str = pref.keys();
    144         assertEquals(keyArray.length, str.length);
    145         for(int i = 0; i < keyArray.length; i++) {
    146             pref.remove(keyArray[i]);
    147             str = pref.keys();
    148             assertEquals(keyArray.length - i - 1, str.length);
    149         }
    150         assertEquals(1, pref.childrenNames().length);
    151         pref.remove("New node");
    152         assertEquals(1, pref.childrenNames().length);
    153 
    154         pref.removeNode();
    155 
    156         try {
    157             pref.remove("New node");
    158             fail("IllegalStateException expected");
    159         } catch (IllegalStateException e) {
    160             //expected
    161         }
    162     }
    163 
    164     public void testClear() throws BackingStoreException {
    165         AbstractPreferences ap = (AbstractPreferences) pref.node("New node");
    166         pref.putInt("IntValue", 33);
    167         pref.putBoolean("BoolValue", true);
    168         pref.flush();
    169         assertTrue(pref.getBoolean("BoolValue", false));
    170         assertEquals(33, pref.getInt("IntValue", 22));
    171         assertEquals(1, pref.childrenNames().length);
    172         pref.clear();
    173         assertFalse(pref.getBoolean("BoolValue", false));
    174         assertEquals(22, pref.getInt("IntValue", 22));
    175         assertEquals(1, pref.childrenNames().length);
    176 
    177         pref.removeNode();
    178 
    179         try {
    180             pref.clear();
    181             fail("IllegalStateException expected");
    182         } catch (IllegalStateException e) {
    183             //expected
    184         }
    185 
    186         try {
    187             ap.clear();
    188             fail("IllegalStateException expected");
    189         } catch (IllegalStateException e) {
    190             //expected
    191         }
    192     }
    193 
    194     public void testPutInt() throws BackingStoreException {
    195         pref.putInt("IntValue", 299792458);
    196         pref.flush();
    197 
    198         assertEquals(299792458, pref.getInt("IntValue", new Integer(1)));
    199 
    200         try {
    201             pref.putInt(null, new Integer(1));
    202             fail("NullPointerException expected");
    203         } catch (NullPointerException e) {
    204             //expected
    205         }
    206 
    207         int i;
    208         StringBuffer sb = new StringBuffer();
    209 
    210         for (i = 0; i < Preferences.MAX_KEY_LENGTH + 1; i++) {
    211             sb.append('c');
    212         }
    213 
    214         try {
    215             pref.putInt(new String(sb), new Integer(1));
    216             fail("IllegalArgumentException expected");
    217         } catch (IllegalArgumentException e) {
    218             //expected
    219         }
    220 
    221         pref.removeNode();
    222 
    223         try {
    224             pref.putInt("IntValue", new Integer(1));
    225             fail("IllegalStateException expected");
    226         } catch (IllegalStateException e) {
    227             //expected
    228         }
    229     }
    230 
    231     public void testGetInt() throws BackingStoreException {
    232         pref.put("Value", "String");
    233         pref.putDouble("DoubleValue", new Double(9.10938188e-31));
    234         pref.putLong("LongValue", new Long(Long.MIN_VALUE));
    235         pref.putInt("IntValue", 299792458);
    236         pref.flush();
    237 
    238         assertEquals(1, pref.getInt("Value", new Integer(1)));
    239         assertEquals(1, pref.getInt("LongValue", new Integer(1)));
    240         assertEquals(1, pref.getInt("DoubleValue", new Integer(1)));
    241         assertEquals(299792458, pref.getInt("IntValue", new Integer(1)));
    242 
    243         try {
    244             pref.getInt(null, new Integer(1));
    245             fail("NullPointerException expected");
    246         } catch (NullPointerException e) {
    247             //expected
    248         }
    249 
    250         pref.removeNode();
    251 
    252         try {
    253             pref.getInt("IntValue", new Integer(1));
    254             fail("IllegalStateException expected");
    255         } catch (IllegalStateException e) {
    256             //expected
    257         }
    258     }
    259 
    260     public void testPutLong() throws BackingStoreException {
    261         pref.putLong("LongValue", new Long(299792458));
    262         pref.flush();
    263 
    264         assertEquals(299792458L, pref.getLong("LongValue", new Long(1)));
    265 
    266         try {
    267             pref.putLong(null, new Long(1));
    268             fail("NullPointerException expected");
    269         } catch (NullPointerException e) {
    270             //expected
    271         }
    272 
    273         int i;
    274         StringBuffer sb = new StringBuffer();
    275 
    276         for (i = 0; i < Preferences.MAX_KEY_LENGTH + 1; i++) {
    277             sb.append('c');
    278         }
    279 
    280         try {
    281             pref.putLong(new String(sb), new Long(1));
    282             fail("IllegalArgumentException expected");
    283         } catch (IllegalArgumentException e) {
    284             //expected
    285         }
    286 
    287         pref.removeNode();
    288 
    289         try {
    290             pref.putLong("LongValue", new Long(1));
    291             fail("IllegalStateException expected");
    292         } catch (IllegalStateException e) {
    293             //expected
    294         }
    295     }
    296 
    297     public void testGetLong() throws BackingStoreException {
    298         pref.put("Value", "String");
    299         pref.putDouble("DoubleValue", new Double(9.10938188e-31));
    300         pref.putLong("LongValue", new Long(Long.MIN_VALUE));
    301         pref.putInt("IntValue", 299792458);
    302         pref.flush();
    303 
    304         assertEquals(1L, pref.getLong("Value", new Long(1)));
    305         assertEquals(Long.MIN_VALUE, pref.getLong("LongValue", new Long(1)));
    306         assertEquals(1L, pref.getLong("DoubleValue", new Long(1)));
    307         assertEquals(299792458L, pref.getLong("IntValue", new Long(1)));
    308 
    309         try {
    310             pref.getLong(null, new Long(1));
    311             fail("NullPointerException expected");
    312         } catch (NullPointerException e) {
    313             //expected
    314         }
    315 
    316         pref.removeNode();
    317 
    318         try {
    319             pref.getLong("LongValue", new Long(1));
    320             fail("IllegalStateException expected");
    321         } catch (IllegalStateException e) {
    322             //expected
    323         }
    324     }
    325 
    326     public void testPutBoolean() throws BackingStoreException {
    327         pref.putBoolean("BoolValue", true);
    328         pref.flush();
    329 
    330         assertTrue(pref.getBoolean("BoolValue", false));
    331 
    332         try {
    333             pref.putBoolean(null, true);
    334             fail("NullPointerException expected");
    335         } catch (NullPointerException e) {
    336             //expected
    337         }
    338 
    339         int i;
    340         StringBuffer sb = new StringBuffer();
    341 
    342         for (i = 0; i < Preferences.MAX_KEY_LENGTH + 1; i++) {
    343             sb.append('c');
    344         }
    345 
    346         try {
    347             pref.putBoolean(new String(sb), true);
    348             fail("IllegalArgumentException expected");
    349         } catch (IllegalArgumentException e) {
    350             //expected
    351         }
    352 
    353         pref.removeNode();
    354 
    355         try {
    356             pref.putBoolean("DoubleValue", true);
    357             fail("IllegalStateException expected");
    358         } catch (IllegalStateException e) {
    359             //expected
    360         }
    361     }
    362 
    363     public void testGetBoolean() throws BackingStoreException {
    364         pref.put("Value", "String");
    365         pref.putDouble("DoubleValue", new Double(9.10938188e-31));
    366         pref.putBoolean("BoolValue", true);
    367         pref.flush();
    368 
    369         assertFalse(pref.getBoolean("Value", false));
    370         assertTrue(pref.getBoolean("BoolValue", false));
    371         assertFalse(pref.getBoolean("DoubleValue", false));
    372 
    373         try {
    374             pref.getBoolean(null, true);
    375             fail("NullPointerException expected");
    376         } catch (NullPointerException e) {
    377             //expected
    378         }
    379 
    380         pref.removeNode();
    381 
    382         try {
    383             pref.getBoolean("DoubleValue", true);
    384             fail("IllegalStateException expected");
    385         } catch (IllegalStateException e) {
    386             //expected
    387         }
    388     }
    389 
    390     public void testPutFloat() throws BackingStoreException {
    391         pref.putFloat("FloatValue", new Float(1.602e-19));
    392         pref.flush();
    393 
    394         assertEquals(new Float(1.602e-19), pref.getFloat("FloatValue", new Float(0.2)));
    395 
    396         try {
    397             pref.putFloat(null, new Float(0.1));
    398             fail("NullPointerException expected");
    399         } catch (NullPointerException e) {
    400             //expected
    401         }
    402 
    403         int i;
    404         StringBuffer sb = new StringBuffer();
    405 
    406         for (i = 0; i < Preferences.MAX_KEY_LENGTH + 1; i++) {
    407             sb.append('c');
    408         }
    409 
    410         try {
    411             pref.putFloat(new String(sb), new Float(0.1));
    412             fail("IllegalArgumentException expected");
    413         } catch (IllegalArgumentException e) {
    414             //expected
    415         }
    416 
    417         pref.removeNode();
    418 
    419         try {
    420             pref.putFloat("FloatValue", new Float(0.1));
    421             fail("IllegalStateException expected");
    422         } catch (IllegalStateException e) {
    423             //expected
    424         }
    425     }
    426 
    427     public void testGetFloat() throws BackingStoreException {
    428         pref.put("Value", "String");
    429         pref.putDouble("DoubleValue", new Double(9.10938188e-31));
    430         pref.putFloat("FloatValue", new Float(-0.123));
    431         pref.putInt("IntValue", 299792458);
    432         pref.flush();
    433 
    434         assertEquals(new Float(0.1), pref.getFloat("Value", new Float(0.1)));
    435         assertEquals(new Float(-0.123), pref.getFloat("FloatValue", new Float(0.2)));
    436         assertEquals(new Float(9.109382e-31), pref.getFloat("DoubleValue", new Float(2.14)));
    437         assertEquals(new Float(2.99792448e8), pref.getFloat("IntValue", new Float(5)));
    438 
    439         try {
    440             pref.getFloat(null, new Float(0.1));
    441             fail("NullPointerException expected");
    442         } catch (NullPointerException e) {
    443             //expected
    444         }
    445 
    446         pref.removeNode();
    447 
    448         try {
    449             pref.getFloat("FloatValue", new Float(0.1));
    450             fail("IllegalStateException expected");
    451         } catch (IllegalStateException e) {
    452             //expected
    453         }
    454     }
    455 
    456     public void testPutDouble() throws BackingStoreException {
    457         pref.putDouble("DoubleValue", new Double(9.10938188e-31));
    458         pref.flush();
    459 
    460         assertEquals(new Double(9.10938188e-31), pref.getDouble("DoubleValue", new Double(2.14)));
    461 
    462         try {
    463             pref.putDouble(null, new Double(0.1));
    464             fail("NullPointerException expected");
    465         } catch (NullPointerException e) {
    466             //expected
    467         }
    468 
    469         int i;
    470         StringBuffer sb = new StringBuffer();
    471 
    472         for (i = 0; i < Preferences.MAX_KEY_LENGTH + 1; i++) {
    473             sb.append('c');
    474         }
    475 
    476         try {
    477             pref.putDouble(new String(sb), new Double(0.1));
    478             fail("IllegalArgumentException expected");
    479         } catch (IllegalArgumentException e) {
    480             //expected
    481         }
    482 
    483         pref.removeNode();
    484 
    485         try {
    486             pref.putDouble("DoubleValue", new Double(0.1));
    487             fail("IllegalStateException expected");
    488         } catch (IllegalStateException e) {
    489             //expected
    490         }
    491     }
    492 
    493     public void testGetDouble() throws BackingStoreException {
    494         pref.put("Value", "String");
    495         pref.putDouble("DoubleValue", new Double(9.10938188e-31));
    496         pref.putBoolean("BoolValue", true);
    497         pref.putInt("IntValue", 299792458);
    498         pref.flush();
    499 
    500         assertEquals(new Double(0.1), pref.getDouble("Value", new Double(0.1)));
    501         assertEquals(new Double(0.2), pref.getDouble("BoolValue", new Double(0.2)));
    502         assertEquals(new Double(9.10938188e-31), pref.getDouble("DoubleValue", new Double(2.14)));
    503         assertEquals(new Double(2.99792458e8), pref.getDouble("IntValue", new Double(5)));
    504 
    505         try {
    506             pref.getDouble(null, new Double(0.1));
    507             fail("NullPointerException expected");
    508         } catch (NullPointerException e) {
    509             //expected
    510         }
    511 
    512         pref.removeNode();
    513 
    514         try {
    515             pref.getDouble("DoubleValue", new Double(0.1));
    516             fail("IllegalStateException expected");
    517         } catch (IllegalStateException e) {
    518             //expected
    519         }
    520     }
    521 
    522     public void testPutByteArray() throws BackingStoreException {
    523         byte[] bArray = new byte[]{1, 2, 3, 4, 5};
    524         byte[] array  = null;
    525         int i;
    526         pref.putByteArray("Array", bArray);
    527         pref.flush();
    528 
    529         array = pref.getByteArray("Array", null);
    530         assertEquals(bArray.length, array.length);
    531         for(i = 0; i < bArray.length; i++) {
    532             assertEquals(bArray[i], array[i]);
    533         }
    534 
    535         try {
    536             pref.putByteArray(null, bArray);
    537             fail("NullPointerException expected");
    538         } catch (NullPointerException e) {
    539             //expected
    540         }
    541 
    542         StringBuffer sb = new StringBuffer();
    543 
    544         for (i = 0; i < Preferences.MAX_KEY_LENGTH + 1; i++) {
    545             sb.append('c');
    546         }
    547 
    548         try {
    549             pref.putByteArray(new String(sb), bArray);
    550             fail("IllegalArgumentException expected");
    551         } catch (IllegalArgumentException e) {
    552             //expected
    553         }
    554 
    555         bArray = new byte[Preferences.MAX_VALUE_LENGTH * 3 / 4 + 1];
    556 
    557         try {
    558             pref.putByteArray("Big array", bArray);
    559             fail("IllegalArgumentException expected");
    560         } catch (IllegalArgumentException e) {
    561             //expected
    562         }
    563 
    564         pref.removeNode();
    565 
    566         try {
    567             pref.putByteArray("Array", new byte[10]);
    568             fail("IllegalStateException expected");
    569         } catch (IllegalStateException e) {
    570             //expected
    571         }
    572     }
    573 
    574     public void testGetByteArray() throws BackingStoreException {
    575         byte[] bArray = new byte[]{1, 2, 3, 4, 5};
    576         byte[] tmp    = new byte[]{5};
    577         byte[] array  = null;
    578         int i;
    579         pref.put("Value", "String");
    580         pref.putDouble("DoubleValue", new Double(9.10938188e-31));
    581         pref.putByteArray("Array", bArray);
    582         pref.flush();
    583 
    584         array = pref.getByteArray("Value", tmp);
    585         assertEquals(tmp.length, array.length);
    586         for(i = 0; i < tmp.length; i++) {
    587             assertEquals(tmp[i], array[i]);
    588         }
    589 
    590         array = pref.getByteArray("DoubleValue", tmp);
    591         assertEquals(tmp.length, array.length);
    592         for(i = 0; i < tmp.length; i++) {
    593             assertEquals(tmp[i], array[i]);
    594         }
    595 
    596         array = pref.getByteArray("Array", tmp);
    597         assertEquals(bArray.length, array.length);
    598         for(i = 0; i < bArray.length; i++) {
    599             assertEquals(bArray[i], array[i]);
    600         }
    601 
    602         try {
    603             pref.getByteArray(null, tmp);
    604             fail("NullPointerException expected");
    605         } catch (NullPointerException e) {
    606             //expected
    607         }
    608 
    609         pref.removeNode();
    610 
    611         try {
    612             pref.getByteArray("Array", tmp);
    613             fail("IllegalStateException expected");
    614         } catch (IllegalStateException e) {
    615             //expected
    616         }
    617     }
    618 
    619     public void testKeys() throws BackingStoreException {
    620         String[] keyArray = new String[]{"Value", "DoubleValue", "BoolValue", "IntValue"};
    621         String nodeStr = "New node";
    622         pref.node(nodeStr);
    623         pref.put(keyArray[0], "String");
    624         pref.putDouble(keyArray[1], new Double(9.10938188e-31));
    625         pref.putBoolean(keyArray[2], true);
    626         pref.putInt(keyArray[3], 299792458);
    627         pref.flush();
    628 
    629         String[] str = pref.keys();
    630         assertEquals(keyArray.length, str.length);
    631         for(int i = 0; i < str.length; i++) {
    632             boolean flag = false;
    633             for(int j = 0; j < keyArray.length; j++) {
    634                 if (str[i].compareTo(keyArray[j]) == 0) {
    635                     flag = true;
    636                     break;
    637                 }
    638             }
    639             assertTrue(str[i].compareTo(nodeStr) != 0);
    640             assertTrue(flag);
    641         }
    642 
    643         pref.removeNode();
    644 
    645         try {
    646             pref.keys();
    647             fail("IllegalStateException expected");
    648         } catch(IllegalStateException e) {
    649             //expected
    650         }
    651     }
    652 
    653     public void testChildrenNames() throws BackingStoreException {
    654         AbstractPreferences first = (AbstractPreferences) pref.node("First node");
    655         AbstractPreferences second = (AbstractPreferences) pref.node("Second node");
    656 
    657         assertEquals(2, pref.childrenNames().length);
    658         assertEquals(0, first.childrenNames().length);
    659         assertEquals(0, second.childrenNames().length);
    660 
    661         second.removeNode();
    662 
    663         try {
    664             second.childrenNames();
    665             fail("IllegalStateException expected");
    666         } catch (IllegalStateException e) {
    667             //expected
    668         }
    669 
    670         pref.removeNode();
    671 
    672         try {
    673             first.childrenNames();
    674             fail("IllegalStateException expected");
    675         } catch (IllegalStateException e) {
    676             //expected
    677         }
    678     }
    679 
    680     public void test_nodeExists() throws BackingStoreException {
    681         AbstractPreferences test = (AbstractPreferences) Preferences.userRoot()
    682                 .node("test");
    683         try {
    684             test.nodeExists(null);
    685             fail("should throw NullPointerException");
    686         } catch (NullPointerException e) {
    687             // Expected
    688         }
    689 
    690         test.removeNode();
    691         try {
    692             test.nodeExists(null);
    693             fail("should throw NullPointerException");
    694         } catch (NullPointerException e) {
    695             // Expected
    696         }
    697     }
    698 
    699     public void testParent() throws BackingStoreException {
    700         AbstractPreferences node = (AbstractPreferences) pref.node("First node/sub node");
    701 
    702         assertTrue(node.parent().name().compareTo("First node") == 0);
    703 
    704         pref.removeNode();
    705 
    706         try {
    707             node.parent();
    708             fail("IllegalStateException expected");
    709         } catch (IllegalStateException e) {
    710             //expected
    711         }
    712     }
    713 
    714     public void testNode() throws BackingStoreException {
    715         AbstractPreferences first = (AbstractPreferences) pref.node("First node");
    716         AbstractPreferences second = (AbstractPreferences) pref.node("Second node");
    717 
    718         try {
    719             first.node("blabla/");
    720             fail("IllegalArgumentException expected");
    721         } catch (IllegalArgumentException e) {
    722             //expected
    723         }
    724 
    725         try {
    726             first.node("///invalid");
    727             fail("IllegalArgumentException expected");
    728         } catch (IllegalArgumentException e) {
    729             //expected
    730         }
    731 
    732         StringBuffer sb = new StringBuffer();
    733 
    734         for (int i = 0; i < Preferences.MAX_NAME_LENGTH; i++) {
    735             sb.append('c');
    736         }
    737         first.node(new String(sb));
    738         sb.append('c');
    739 
    740         try {
    741             first.node(new String(sb));
    742             fail("IllegalArgumentException expected");
    743         } catch (IllegalArgumentException e) {
    744             //expected
    745         }
    746 
    747         second.removeNode();
    748 
    749         try {
    750             second.node("");
    751             fail("IllegalStateException expected");
    752         } catch (IllegalStateException e) {
    753             //expected
    754         }
    755         pref.removeNode();
    756         try {
    757             first.node("");
    758             fail("IllegalStateException expected");
    759         } catch (IllegalStateException e) {
    760             //expected
    761         }
    762     }
    763 
    764     public void testNodeExists() throws BackingStoreException {
    765         AbstractPreferences ap1 = (AbstractPreferences) pref.node("First node");
    766         AbstractPreferences ap2 = (AbstractPreferences) pref.node("Second node");
    767         pref.putInt("IntegerValue", 33);
    768         pref.putBoolean("BoolValue", true);
    769         pref.flush();
    770 
    771         assertTrue(pref.nodeExists("First node"));
    772         assertTrue(pref.nodeExists("Second node"));
    773         assertFalse(pref.nodeExists("IntegerValue"));
    774         assertFalse(pref.nodeExists("BoolValue"));
    775         assertFalse(pref.nodeExists("Value"));
    776         assertFalse(pref.nodeExists(nodeName));
    777 
    778         try {
    779             pref.nodeExists("///invalid");
    780             fail("IllegalArgumentException expected");
    781         } catch (IllegalArgumentException e) {
    782             //expected
    783         }
    784 
    785         pref.removeNode();
    786 
    787         try {
    788             pref.nodeExists("Exception");
    789             fail("IllegalStateException expected");
    790         } catch (IllegalStateException e) {
    791             //expected
    792         }
    793     }
    794 
    795     public void testRemoveNode() throws BackingStoreException {
    796         String[] nodeArray = new String[]{"First node", "Second node", "Last node"};
    797         int i;
    798         pref.put("Key", "String");
    799         for (i = 0; i < nodeArray.length; i++) {
    800             pref.node(nodeArray[i]);
    801         }
    802         pref.flush();
    803 
    804         String[] str = pref.childrenNames();
    805         assertEquals(nodeArray.length, str.length);
    806         for(i = 0; i < nodeArray.length; i++) {
    807             pref.node(nodeArray[i]).removeNode();
    808             str = pref.childrenNames();
    809             assertEquals(nodeArray.length - i - 1, str.length);
    810         }
    811         assertEquals(1, pref.keys().length);
    812         pref.node("Key").removeNode();
    813         assertEquals(1, pref.keys().length);
    814 
    815         pref.removeNode();
    816 
    817         try {
    818             pref.removeNode();
    819             fail("IllegalStateException expected");
    820         } catch (IllegalStateException e) {
    821             //expected
    822         }
    823 
    824         try {
    825             root.removeNode();
    826             fail("UnsupportedOperationException expected");
    827         } catch (UnsupportedOperationException e) {
    828             //expected
    829         }
    830     }
    831 
    832     public void testName() {
    833         AbstractPreferences first = (AbstractPreferences) pref.node("First node");
    834         AbstractPreferences second = (AbstractPreferences) pref.node("Second node/sub node");
    835 
    836         assertTrue(first.name().compareTo("First node") == 0);
    837         assertFalse(first.name().compareTo("Second node") == 0);
    838         assertTrue(second.name().compareTo("sub node") == 0);
    839     }
    840 
    841     public void testAbsolutePath() {
    842         assertEquals(parent.absolutePath() + "/" + nodeName, pref.absolutePath());
    843         assertEquals(parent.absolutePath() + "/" + "new node", parent.node("new node").absolutePath());
    844     }
    845 
    846     public void testIsUserNode() {
    847         assertTrue(parent.isUserNode());
    848         assertFalse(Preferences.systemRoot().isUserNode());
    849     }
    850 
    851     public void testSync() throws BackingStoreException {
    852         pref.node("new node/sub node");
    853         pref.sync();
    854 
    855         pref.removeNode();
    856 
    857         try {
    858             pref.sync();
    859             fail("IllegalStateException expected");
    860         } catch (IllegalStateException e) {
    861             //expected
    862         }
    863     }
    864 
    865     class MockPreferenceChangeListener implements PreferenceChangeListener {
    866         private boolean flagChange = false;
    867 
    868         public synchronized void preferenceChange(PreferenceChangeEvent arg0) {
    869             flagChange = true;
    870             notifyAll();
    871         }
    872 
    873         public synchronized void assertChanged(boolean expected) throws InterruptedException {
    874             wait(100);
    875             assertEquals(expected, flagChange);
    876             flagChange = false;
    877         }
    878     }
    879 
    880     public void testAddPreferenceChangeListener() throws Exception {
    881         MockPreferenceChangeListener mpcl = new MockPreferenceChangeListener();
    882         parent.addPreferenceChangeListener(mpcl);
    883         mpcl.assertChanged(false);
    884         pref.node("new node");
    885         mpcl.assertChanged(false);
    886         parent.node("new node");
    887         mpcl.assertChanged(false);
    888         parent.putInt("IntValue", 33);
    889         mpcl.assertChanged(true);
    890         assertEquals(33, parent.getInt("IntValue", 22));
    891         mpcl.assertChanged(false);
    892         assertEquals(22, parent.getInt("Missed Value", 22));
    893         mpcl.assertChanged(false);
    894     }
    895 
    896     public void testRemovePreferenceChangeListener() throws Exception {
    897         MockPreferenceChangeListener mpcl = new MockPreferenceChangeListener();
    898         parent.addPreferenceChangeListener(mpcl);
    899         mpcl.assertChanged(false);
    900         parent.putInt("IntValue", 33);
    901         mpcl.assertChanged(true);
    902         parent.removePreferenceChangeListener(mpcl);
    903         parent.putInt("IntValue", 33);
    904         mpcl.assertChanged(false);
    905     }
    906 
    907     class MockNodeChangeListener implements NodeChangeListener {
    908         private boolean flagAdded = false;
    909         private boolean flagRemoved = false;
    910 
    911         public synchronized void childAdded(NodeChangeEvent arg0) {
    912             flagAdded = true;
    913             notifyAll();
    914         }
    915 
    916         public synchronized void childRemoved(NodeChangeEvent arg0) {
    917             flagRemoved = true;
    918             notifyAll();
    919         }
    920 
    921         public synchronized void assertAdded(boolean expected) throws InterruptedException {
    922             wait(100);
    923             assertEquals(expected, flagAdded);
    924         }
    925 
    926         public synchronized void assertRemoved(boolean expected) throws InterruptedException {
    927             wait(100);
    928             assertEquals(expected, flagRemoved);
    929         }
    930     }
    931 
    932     public void testAddNodeChangeListener() throws Exception {
    933         MockNodeChangeListener mncl = new MockNodeChangeListener();
    934         parent.addNodeChangeListener(mncl);
    935         pref.node("test");
    936         mncl.assertAdded(false);
    937         mncl.assertRemoved(false);
    938         pref.removeNode();
    939         mncl.assertAdded(false);
    940         mncl.assertRemoved(true);
    941         parent.node("new node");
    942         mncl.assertAdded(true);
    943         mncl.assertRemoved(true);
    944     }
    945 
    946     public void testRemoveNodeChangeListener() throws BackingStoreException, InterruptedException {
    947         MockNodeChangeListener mncl = new MockNodeChangeListener();
    948         parent.addNodeChangeListener(mncl);
    949         pref.node("test");
    950         mncl.assertAdded(false);
    951         mncl.assertRemoved(false);
    952         parent.removeNodeChangeListener(mncl);
    953         pref.removeNode();
    954         mncl.assertAdded(false);
    955         mncl.assertRemoved(false);
    956         parent.node("new node");
    957         mncl.assertAdded(false);
    958         mncl.assertRemoved(false);
    959     }
    960 
    961     public void testExportNode() throws BackingStoreException, IOException, InvalidPreferencesFormatException {
    962         AbstractPreferences ap = (AbstractPreferences) pref.node("New node");
    963         pref.putInt("IntValue", 33);
    964         pref.putBoolean("BoolValue", true);
    965         pref.flush();
    966 
    967         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    968 
    969         pref.exportNode(baos);
    970         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    971 
    972         assertTrue(pref.getBoolean("BoolValue", false));
    973         assertEquals(33, pref.getInt("IntValue", 22));
    974         assertEquals(1, pref.childrenNames().length);
    975 
    976         String xmlData = new String(baos.toByteArray());
    977 
    978         assertTrue(xmlData.contains("IntValue"));
    979         assertTrue(xmlData.contains("BoolValue"));
    980         assertTrue(xmlData.contains("33"));
    981         assertTrue(xmlData.contains("true"));
    982 
    983         pref.removeNode();
    984 
    985         try {
    986             pref.exportNode(new ByteArrayOutputStream());
    987             fail("IllegalStateException expected");
    988         } catch (IllegalStateException e) {
    989             //expected
    990         }
    991 
    992         try {
    993             pref.getBoolean("BoolValue", false);
    994             fail("IllegalStateException expected");
    995         } catch (IllegalStateException e) {
    996             //expected
    997         }
    998         pref = (AbstractPreferences) parent.node(nodeName);
    999 
   1000         pref.importPreferences(bais);
   1001 
   1002         assertTrue(pref.getBoolean("BoolValue", false));
   1003         assertEquals(33, pref.getInt("IntValue", 22));
   1004         assertEquals(0, pref.childrenNames().length);
   1005     }
   1006 
   1007     public void testExportSubtree() throws BackingStoreException, IOException, InvalidPreferencesFormatException {
   1008         AbstractPreferences ap1 = (AbstractPreferences) pref.node("First node");
   1009         AbstractPreferences ap2 = (AbstractPreferences) pref.node("Second node");
   1010         pref.putInt("IntegerValue", 33);
   1011         pref.putBoolean("BoolValue", true);
   1012         pref.flush();
   1013 
   1014         ap1.putInt("FirstIntValue", 11);
   1015         ap2.putDouble("DoubleValue", new Double(6.626e-34));
   1016 
   1017         ByteArrayOutputStream baos = new ByteArrayOutputStream();
   1018 
   1019         pref.exportSubtree(baos);
   1020         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
   1021 
   1022         assertTrue(pref.getBoolean("BoolValue", false));
   1023         assertEquals(33, pref.getInt("IntegerValue", 22));
   1024         assertEquals(2, pref.childrenNames().length);
   1025         assertEquals(11, ap1.getInt("FirstIntValue", 22));
   1026         assertEquals(new Double(6.626e-34), ap2.getDouble("DoubleValue", new Double (3.14)));
   1027 
   1028         String xmlData = new String(baos.toByteArray());
   1029 
   1030         assertTrue(xmlData.contains("IntegerValue"));
   1031         assertTrue(xmlData.contains("BoolValue"));
   1032         assertTrue(xmlData.contains("FirstIntValue"));
   1033         assertTrue(xmlData.contains("DoubleValue"));
   1034         assertTrue(xmlData.contains("33"));
   1035         assertTrue(xmlData.contains("true"));
   1036         assertTrue(xmlData.contains("11"));
   1037         assertTrue(xmlData.contains("6.626E-34"));
   1038 
   1039         pref.removeNode();
   1040 
   1041         try {
   1042             pref.exportSubtree(new ByteArrayOutputStream());
   1043             fail("IllegalStateException expected");
   1044         } catch (IllegalStateException e) {
   1045             //expected
   1046         }
   1047 
   1048         try {
   1049             pref.getBoolean("BoolValue", false);
   1050             fail("IllegalStateException expected");
   1051         } catch (IllegalStateException e) {
   1052             //expected
   1053         }
   1054         pref = (AbstractPreferences) parent.node(nodeName);
   1055         pref.importPreferences(bais);
   1056 
   1057         ap1 = (AbstractPreferences) pref.node("First node");
   1058         ap2 = (AbstractPreferences) pref.node("Second node");
   1059 
   1060         assertTrue(pref.getBoolean("BoolValue", false));
   1061         assertEquals(33, pref.getInt("IntegerValue", 22));
   1062         assertEquals(2, pref.childrenNames().length);
   1063         assertEquals(11, ap1.getInt("FirstIntValue", 22));
   1064         assertEquals(new Double(6.626e-34), ap2.getDouble("DoubleValue", new Double (3.14)));
   1065     }
   1066 
   1067     class MockAbstractPreferences extends AbstractPreferences {
   1068         protected MockAbstractPreferences(AbstractPreferences parent, String name) {
   1069             super(parent, name);
   1070         }
   1071 
   1072         @Override
   1073         protected AbstractPreferences childSpi(String name) {
   1074             return null;
   1075         }
   1076 
   1077         @Override
   1078         protected String[] childrenNamesSpi() throws BackingStoreException {
   1079             return null;
   1080         }
   1081 
   1082         @Override
   1083         protected void flushSpi() throws BackingStoreException {
   1084         }
   1085 
   1086         @Override
   1087         protected String getSpi(String key) {
   1088             return null;
   1089         }
   1090 
   1091         @Override
   1092         protected String[] keysSpi() throws BackingStoreException {
   1093             return null;
   1094         }
   1095 
   1096         @Override
   1097         protected void putSpi(String key, String value) {
   1098         }
   1099 
   1100         @Override
   1101         protected void removeNodeSpi() throws BackingStoreException {
   1102         }
   1103 
   1104         @Override
   1105         protected void removeSpi(String key) {
   1106         }
   1107 
   1108         @Override
   1109         protected void syncSpi() throws BackingStoreException {
   1110         }
   1111     }
   1112 
   1113     public void testAbstractPreferences() {
   1114         assertNotNull(new MockAbstractPreferences(pref, "node name"));
   1115         try {
   1116             new MockAbstractPreferences(pref, "node/name");
   1117             fail("IllegalArgumentException expected");
   1118         } catch (IllegalArgumentException e) {
   1119             //expected
   1120         }
   1121 
   1122         try {
   1123             new MockAbstractPreferences(null, "node");
   1124             fail("IllegalArgumentException expected");
   1125         } catch (IllegalArgumentException e) {
   1126             //expected
   1127         }
   1128     }
   1129 
   1130     public void testCachedChildren() throws BackingStoreException {
   1131         pref.node("First node");
   1132         pref.node("Second node");
   1133 
   1134         assertEquals(2, pref.childrenNames().length);
   1135     }
   1136 }
   1137