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
17package org.apache.harmony.prefs.tests.java.util.prefs;
18
19import java.io.FilePermission;
20import java.io.IOException;
21import java.security.Permission;
22import java.util.prefs.BackingStoreException;
23import java.util.prefs.Preferences;
24
25import junit.framework.TestCase;
26
27public class FilePreferencesImplTest extends TestCase {
28
29    private String prevFactory;
30    private Preferences uroot;
31    private Preferences sroot;
32
33    public FilePreferencesImplTest() {
34        super();
35    }
36
37    @Override
38    protected void setUp() throws Exception {
39        prevFactory = System.getProperty("java.util.prefs.PreferencesFactory");
40        System.setProperty("java.util.prefs.PreferencesFactory", "java.util.prefs.FilePreferencesFactoryImpl");
41
42        uroot = Preferences.userRoot().node("harmony_test");
43        sroot = Preferences.systemRoot().node("harmony_test");
44    }
45
46    @Override
47    protected void tearDown() throws Exception {
48        if (prevFactory != null)
49            System.setProperty("java.util.prefs.PreferencesFactory", prevFactory);
50
51        uroot.removeNode();
52        sroot.removeNode();
53        uroot = null;
54        sroot = null;
55    }
56
57    public void testPutGet() throws IOException, BackingStoreException {
58        uroot.put("ukey1", "value1");
59        assertEquals("value1", uroot.get("ukey1", null));
60        String[] names = uroot.keys();
61        assertEquals(1, names.length);
62
63        uroot.put("ukey2", "value3");
64        assertEquals("value3", uroot.get("ukey2", null));
65        uroot.put("\u4e2d key1", "\u4e2d value1");
66        assertEquals("\u4e2d value1", uroot.get("\u4e2d key1", null));
67        names = uroot.keys();
68        assertEquals(3, names.length);
69
70        uroot.flush();
71        uroot.clear();
72        names = uroot.keys();
73        assertEquals(0, names.length);
74
75        sroot.put("skey1", "value1");
76        assertEquals("value1", sroot.get("skey1", null));
77        sroot.put("\u4e2d key1", "\u4e2d value1");
78        assertEquals("\u4e2d value1", sroot.get("\u4e2d key1", null));
79    }
80
81    public void testChildNodes() throws Exception {
82        Preferences child1 = uroot.node("child1");
83        Preferences child2 = uroot.node("\u4e2d child2");
84        Preferences grandchild = child1.node("grand");
85        assertNotNull(grandchild);
86
87        String[] childNames = uroot.childrenNames();
88        assertEquals(2, childNames.length);
89
90        childNames = child1.childrenNames();
91        assertEquals(1, childNames.length);
92
93        childNames = child2.childrenNames();
94        assertEquals(0, childNames.length);
95
96        child1.removeNode();
97        childNames = uroot.childrenNames();
98        assertEquals(1, childNames.length);
99
100        child2.removeNode();
101        childNames = uroot.childrenNames();
102        assertEquals(0, childNames.length);
103
104        child1 = sroot.node("child1");
105        child2 = sroot.node("child2");
106        grandchild = child1.node("grand");
107
108        childNames = sroot.childrenNames();
109
110        assertEquals(2, childNames.length);
111
112        childNames = child1.childrenNames();
113        assertEquals(1, childNames.length);
114
115        childNames = child2.childrenNames();
116        assertEquals(0, childNames.length);
117
118        child1.removeNode();
119        assertNotSame(child1, sroot.node("child1"));
120        assertSame(sroot.node("child1"), sroot.node("child1"));
121        sroot.node("child1").removeNode();
122        childNames = sroot.childrenNames();
123        assertEquals(1, childNames.length);
124        child2.removeNode();
125        childNames = sroot.childrenNames();
126        assertEquals(0, childNames.length);
127    }
128}
129