1 19 20 package org.netbeans.core.startup.preferences; 21 22 import java.io.IOException ; 23 import java.util.Arrays ; 24 import java.util.Collections ; 25 import java.util.TreeSet ; 26 import java.util.prefs.BackingStoreException ; 27 import java.util.prefs.Preferences ; 28 29 33 public class TestPropertiesStorage extends TestFileStorage { 34 private PropertiesStorage storage; 35 private NbPreferences pref; 36 37 public TestPropertiesStorage(String testName) { 38 super(testName); 39 } 40 41 protected void setUp() throws Exception { 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()); } 53 54 void noFileRepresentationAssertion() throws IOException { 55 super.noFileRepresentationAssertion(); 56 assertNull(((PropertiesStorage)instance).toFolder()); 57 assertNull(((PropertiesStorage)instance).toPropertiesFile()); 58 } 59 60 void fileRepresentationAssertion() throws IOException { 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 { 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 { 80 testNode(); 81 pref.flush(); 82 assertNotNull(storage.toPropertiesFile()); 83 } 84 85 public void testNode3() throws BackingStoreException { 86 testNode(); 87 pref.flushTask.waitFinished(); 88 assertNotNull(storage.toPropertiesFile()); 89 } 90 91 public void testNode4() throws BackingStoreException { 92 pref.node("a"); 93 testNode(); 94 } 95 96 public void testNode5() throws BackingStoreException { 97 Preferences 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 { 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 { 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 { 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 { 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 ise) {} 166 } 167 168 public void testRemoveParentNode() throws BackingStoreException { 169 assertNull(storage.toFolder()); 170 assertNull(storage.toPropertiesFile()); 171 172 Preferences 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 { 187 Preferences 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 <String >(Arrays.asList("c1", "c2", "c3")), new TreeSet <String >(Arrays.asList(storage.childrenNames()))); 197 pref.node("c2").removeNode(); 198 assertEquals(new TreeSet <String >(Arrays.asList("c1", "c3")), new TreeSet <String >(Arrays.asList(storage.childrenNames()))); 199 pref.node("c3").removeNode(); 200 assertEquals(Collections.singleton("c1"), new TreeSet <String >(Arrays.asList(storage.childrenNames()))); 201 pref.node("c1").removeNode(); 202 assertEquals(Collections.emptySet(), new TreeSet <String >(Arrays.asList(storage.childrenNames()))); 203 } 204 205 } 206 | Popular Tags |