Home | History | Annotate | Download | only in prefs
      1 /* Licensed to the Apache Software Foundation (ASF) under one or more
      2  * contributor license agreements.  See the NOTICE file distributed with
      3  * this work for additional information regarding copyright ownership.
      4  * The ASF licenses this file to You under the Apache License, Version 2.0
      5  * (the "License"); you may not use this file except in compliance with
      6  * the License.  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.tests.java.util.prefs;
     18 
     19 import java.io.File;
     20 import java.io.IOException;
     21 import java.util.prefs.BackingStoreException;
     22 import java.util.prefs.Preferences;
     23 import java.util.prefs.PreferencesFactory;
     24 import junit.framework.TestCase;
     25 import libcore.io.IoUtils;
     26 
     27 public class FilePreferencesImplTest extends TestCase {
     28 
     29     private Preferences uroot;
     30     private Preferences sroot;
     31 
     32     @Override
     33     protected void setUp() throws Exception {
     34         File tmpDir = IoUtils.createTemporaryDirectory("FilePreferencesImplTest");
     35         AbstractPreferencesTest.TestPreferencesFactory factory
     36                 = new AbstractPreferencesTest.TestPreferencesFactory(tmpDir.getAbsolutePath());
     37 
     38         uroot = factory.userRoot().node("harmony_test");
     39         sroot = factory.systemRoot().node("harmony_test");
     40     }
     41 
     42     @Override
     43     protected void tearDown() throws Exception {
     44         uroot.removeNode();
     45         sroot.removeNode();
     46         uroot = null;
     47         sroot = null;
     48     }
     49 
     50     public void testPutGet() throws IOException, BackingStoreException {
     51         uroot.put("ukey1", "value1");
     52         assertEquals("value1", uroot.get("ukey1", null));
     53         String[] names = uroot.keys();
     54         assertEquals(1, names.length);
     55 
     56         uroot.put("ukey2", "value3");
     57         assertEquals("value3", uroot.get("ukey2", null));
     58         uroot.put("\u4e2d key1", "\u4e2d value1");
     59         assertEquals("\u4e2d value1", uroot.get("\u4e2d key1", null));
     60         names = uroot.keys();
     61         assertEquals(3, names.length);
     62 
     63         uroot.flush();
     64         uroot.clear();
     65         names = uroot.keys();
     66         assertEquals(0, names.length);
     67 
     68         sroot.put("skey1", "value1");
     69         assertEquals("value1", sroot.get("skey1", null));
     70         sroot.put("\u4e2d key1", "\u4e2d value1");
     71         assertEquals("\u4e2d value1", sroot.get("\u4e2d key1", null));
     72     }
     73 
     74     public void testChildNodes() throws Exception {
     75         Preferences child1 = uroot.node("child1");
     76         Preferences child2 = uroot.node("\u4e2d child2");
     77         Preferences grandchild = child1.node("grand");
     78         assertNotNull(grandchild);
     79 
     80         String[] childNames = uroot.childrenNames();
     81         assertEquals(2, childNames.length);
     82 
     83         childNames = child1.childrenNames();
     84         assertEquals(1, childNames.length);
     85 
     86         childNames = child2.childrenNames();
     87         assertEquals(0, childNames.length);
     88 
     89         child1.removeNode();
     90         childNames = uroot.childrenNames();
     91         assertEquals(1, childNames.length);
     92 
     93         child2.removeNode();
     94         childNames = uroot.childrenNames();
     95         assertEquals(0, childNames.length);
     96 
     97         child1 = sroot.node("child1");
     98         child2 = sroot.node("child2");
     99         childNames = sroot.childrenNames();
    100 
    101         Preferences grandchild2 = child1.node("grand");
    102         assertEquals(2, childNames.length);
    103 
    104         childNames = child1.childrenNames();
    105         assertEquals(1, childNames.length);
    106 
    107         childNames = child2.childrenNames();
    108         assertEquals(0, childNames.length);
    109 
    110         child1.removeNode();
    111         assertNotSame(child1, sroot.node("child1"));
    112         assertSame(sroot.node("child1"), sroot.node("child1"));
    113         sroot.node("child1").removeNode();
    114         childNames = sroot.childrenNames();
    115         assertEquals(1, childNames.length);
    116         child2.removeNode();
    117         childNames = sroot.childrenNames();
    118         assertEquals(0, childNames.length);
    119     }
    120 }
    121