KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > preferences > TestPropertiesStorage


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.startup.preferences;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.TreeSet JavaDoc;
26 import java.util.prefs.BackingStoreException JavaDoc;
27 import java.util.prefs.Preferences JavaDoc;
28
29 /**
30  *
31  * @author Radek Matous
32  */

33 public class TestPropertiesStorage extends TestFileStorage {
34     private PropertiesStorage storage;
35     private NbPreferences pref;
36     
37     public TestPropertiesStorage(String JavaDoc testName) {
38         super(testName);
39     }
40     
41     protected void setUp() throws Exception JavaDoc {
42         super.setUp();
43         assertSame(new NbPreferencesFactory().userRoot(), Preferences.userRoot());
44         pref = getPreferencesNode();
45         assertNotNull(pref);
46         storage = (PropertiesStorage)pref.fileStorage;
47         assertNotNull(storage);
48     }
49     
50     protected NbPreferences.FileStorage getInstance() {
51         return PropertiesStorage.instanceReadOnly("/PropertiesStorageTest/" + getName());//NOI18N);
52
}
53     
54     void noFileRepresentationAssertion() throws IOException JavaDoc {
55         super.noFileRepresentationAssertion();
56         assertNull(((PropertiesStorage)instance).toFolder());
57         assertNull(((PropertiesStorage)instance).toPropertiesFile());
58     }
59     
60     void fileRepresentationAssertion() throws IOException JavaDoc {
61         super.fileRepresentationAssertion();
62         assertNotNull(((PropertiesStorage)instance).toFolder());
63         assertNotNull(((PropertiesStorage)instance).toPropertiesFile());
64     }
65     
66     private NbPreferences getPreferencesNode() {
67         return (NbPreferences)Preferences.userNodeForPackage(TestPropertiesStorage.class).node(getName());
68     }
69     
70     public void testNode() throws BackingStoreException JavaDoc {
71         assertNull(storage.toFolder());
72         assertNull(storage.toPropertiesFile());
73         pref.flush();
74         assertNull(storage.toFolder());
75         assertNull(storage.toPropertiesFile());
76         pref.put("key","value");
77     }
78     
79     public void testNode2() throws BackingStoreException JavaDoc {
80         testNode();
81         pref.flush();
82         assertNotNull(storage.toPropertiesFile());
83     }
84     
85     public void testNode3() throws BackingStoreException JavaDoc {
86         testNode();
87         pref.flushTask.waitFinished();
88         assertNotNull(storage.toPropertiesFile());
89     }
90
91     public void testNode4() throws BackingStoreException JavaDoc {
92         pref.node("a");
93         testNode();
94     }
95
96     public void testNode5() throws BackingStoreException JavaDoc {
97         Preferences JavaDoc child = pref.node("a");
98         child.put("key","value");
99         child.flush();
100         assertNotNull(storage.toFolder());
101         assertNull(storage.toPropertiesFile());
102     }
103
104     public void testRemove() throws BackingStoreException JavaDoc {
105         assertNull(storage.toFolder());
106         assertNull(storage.toPropertiesFile());
107         
108         pref.put("key","value");
109         pref.flush();
110         assertNotNull(storage.toPropertiesFile());
111         pref.remove("key");
112         assertTrue(pref.properties.isEmpty());
113         pref.flush();
114         assertNull(storage.toPropertiesFile());
115     }
116
117     public void testRemove2() throws BackingStoreException JavaDoc {
118         assertNull(storage.toFolder());
119         assertNull(storage.toPropertiesFile());
120
121         pref.put("key","value");
122         pref.put("key1","value1");
123
124         pref.flush();
125         assertNotNull(storage.toPropertiesFile());
126         pref.remove("key");
127         assertFalse(pref.properties.isEmpty());
128         pref.flush();
129         assertNotNull(storage.toPropertiesFile());
130     }
131
132     public void testClear() throws BackingStoreException JavaDoc {
133         assertNull(storage.toFolder());
134         assertNull(storage.toPropertiesFile());
135         pref.put("key","value");
136         pref.put("key1","value");
137         pref.put("key2","value");
138         pref.put("key3","value");
139         pref.put("key5","value");
140         pref.flush();
141         assertNotNull(storage.toPropertiesFile());
142
143         pref.clear();
144         pref.flush();
145         assertNull(storage.toPropertiesFile());
146     }
147
148     public void testRemoveNode() throws BackingStoreException JavaDoc {
149         assertNull(storage.toFolder());
150         assertNull(storage.toPropertiesFile());
151
152         pref.put("key","value");
153         pref.node("subnode").put("key","value");
154         pref.flush();
155         assertNotNull(storage.toPropertiesFile());
156         assertNotNull(storage.toFolder());
157         pref.removeNode();
158         pref.flush();
159         assertNull(storage.toPropertiesFile());
160         assertNull(storage.toFolder());
161         assertFalse(storage.existsNode());
162         try {
163             pref.sync();
164             fail();
165         } catch (IllegalStateException JavaDoc ise) {}
166     }
167
168     public void testRemoveParentNode() throws BackingStoreException JavaDoc {
169         assertNull(storage.toFolder());
170         assertNull(storage.toPropertiesFile());
171
172         Preferences JavaDoc subnode = pref.node("subnode");
173         assertNull(storage.toFolder());
174         assertNull(storage.toPropertiesFile());
175         subnode.put("key","value");
176         subnode.flush();
177         assertNotNull(storage.toFolder());
178         assertNull(storage.toPropertiesFile());
179         subnode.removeNode();
180         pref.flush();
181         assertNull(storage.toPropertiesFile());
182         assertNull(storage.toFolder());
183         assertFalse(storage.existsNode());
184     }
185
186     public void testChildrenNames() throws Exception JavaDoc {
187         Preferences JavaDoc subnode = pref.node("c1");
188         subnode.put("k","v");
189         subnode.flush();
190         subnode = pref.node("c2");
191         subnode.put("k","v");
192         subnode.flush();
193         subnode = pref.node("c3/c4");
194         subnode.put("k","v");
195         subnode.flush();
196         assertEquals(new TreeSet JavaDoc<String JavaDoc>(Arrays.asList("c1", "c2", "c3")), new TreeSet JavaDoc<String JavaDoc>(Arrays.asList(storage.childrenNames())));
197         pref.node("c2").removeNode();
198         assertEquals(new TreeSet JavaDoc<String JavaDoc>(Arrays.asList("c1", "c3")), new TreeSet JavaDoc<String JavaDoc>(Arrays.asList(storage.childrenNames())));
199         pref.node("c3").removeNode();
200         assertEquals(Collections.singleton("c1"), new TreeSet JavaDoc<String JavaDoc>(Arrays.asList(storage.childrenNames())));
201         pref.node("c1").removeNode();
202         assertEquals(Collections.emptySet(), new TreeSet JavaDoc<String JavaDoc>(Arrays.asList(storage.childrenNames())));
203     }
204
205 }
206
Popular Tags