1 19 20 package org.netbeans.core.startup.preferences; 21 22 import java.lang.reflect.Field ; 23 import java.util.ArrayList ; 24 import java.util.Arrays ; 25 import java.util.List ; 26 import java.util.logging.Logger ; 27 import java.util.prefs.AbstractPreferences ; 28 import java.util.prefs.BackingStoreException ; 29 import java.util.prefs.NodeChangeEvent ; 30 import java.util.prefs.NodeChangeListener ; 31 import java.util.prefs.PreferenceChangeEvent ; 32 import java.util.prefs.PreferenceChangeListener ; 33 import java.util.prefs.Preferences ; 34 35 39 public class TestPreferences extends NbPreferencesTest.TestBasicSetup { 40 public TestPreferences(String testName) { 41 super(testName); 42 } 43 44 protected void setUp() throws Exception { 45 super.setUp(); 46 clearWorkDir(); 47 assertSame(new NbPreferencesFactory().userRoot(), Preferences.userRoot()); 49 50 Preferences.userRoot().flush(); 51 } 52 53 54 private Preferences getPreferencesNode() { 55 return getUserPreferencesNode(); 56 } 57 58 private Preferences getUserPreferencesNode() { 59 return Preferences.userNodeForPackage(NbPreferencesTest.class).node(getName()); 60 } 61 62 private Preferences getSystemPreferencesNode() { 63 return Preferences.systemNodeForPackage(NbPreferencesTest.class).node(getName()); 64 } 65 66 67 public void testUserRoot() throws Exception { 68 try { 69 Preferences.userRoot().removeNode(); 70 fail(); 71 } catch (UnsupportedOperationException ex) { 72 } 73 } 74 75 76 105 106 public void testNameExceededSize() throws Exception { 107 StringBuffer sb = new StringBuffer (); 108 for (; sb.toString().length() < Preferences.MAX_NAME_LENGTH + 1; ) { 109 sb.append("1234567890"); 110 } 111 112 assertTrue(sb.toString().length() > Preferences.MAX_NAME_LENGTH); 113 try { 114 Preferences pref = 115 getPreferencesNode().node(sb.toString()); 116 fail(); 117 } catch (IllegalArgumentException iax) { 118 } 119 } 120 121 public void testNullParameter() throws Exception { 122 Preferences pref = getPreferencesNode(); 123 try { 124 pref.get(null, "value"); 125 fail(); 126 } catch(NullPointerException npe) { 127 } 128 129 try { 130 pref.get("key", null); 132 } catch(NullPointerException npe) { 133 fail(); 134 } 135 136 try { 137 pref.node(null); 138 fail(); 139 } catch(NullPointerException npe) { 140 } 141 142 try { 143 pref.node("node2/"); 144 fail(); 145 } catch(IllegalArgumentException iax) { 146 } 147 148 } 149 150 public void testIsUserNode() { 151 Preferences upref = getUserPreferencesNode(); 152 Preferences spref = getSystemPreferencesNode(); 153 assertTrue(upref.isUserNode()); 154 assertFalse(spref.isUserNode()); 155 } 156 157 public void testNode() throws BackingStoreException { 158 Preferences pref = getPreferencesNode(); 159 assertNotNull(pref); 160 assertTrue(pref.nodeExists("")); 161 assertFalse(pref.nodeExists("sub1")); 162 assertFalse(Arrays.asList(pref.childrenNames()).contains("sub1")); 163 164 Preferences sub1 =pref.node("sub1"); 165 assertTrue(pref.nodeExists("sub1")); 166 assertTrue(Arrays.asList(pref.childrenNames()).contains("sub1")); 167 } 168 169 public void testChildrenNames() throws Exception { 170 Preferences pref = getPreferencesNode(); 171 assertNotNull(pref); 172 assertEquals(0, pref.childrenNames().length); 173 174 assertFalse(pref.nodeExists("sub1")); 175 Preferences sub1 =pref.node("sub1"); 176 assertNotNull(sub1); 177 assertTrue(pref.nodeExists("sub1")); 178 assertEquals(1, pref.childrenNames().length); 179 180 181 assertFalse(pref.nodeExists("sub2")); 182 Preferences sub2 =pref.node("sub2"); 183 assertNotNull(sub2); 184 assertTrue(pref.nodeExists("sub2")); 185 assertEquals(2, pref.childrenNames().length); 186 187 sub1.removeNode(); 188 assertEquals(1, pref.childrenNames().length); 189 sub2.removeNode(); 190 assertEquals(0, pref.childrenNames().length); 191 } 192 193 public void testPut() { 194 Preferences pref = getPreferencesNode(); 195 assertNotNull(pref); 196 197 assertNull(pref.get("key1", null)); 198 pref.put("key1", "value1"); 199 assertEquals("value1",pref.get("key1", null)); 200 } 201 202 public void testPut2() throws Exception { 203 final Object sync = getEventQueueSync(); 204 Preferences pref = getPreferencesNode(); 205 assertNotNull(pref); 206 final List l = new ArrayList (); 207 assertNull(pref.get("key1", null)); 208 PreferenceChangeListener pl = new PreferenceChangeListener (){ 209 public void preferenceChange(PreferenceChangeEvent evt) { 210 synchronized(sync) { 211 l.add(evt.getNewValue()); 212 sync.notifyAll(); 213 } 214 } 215 }; 216 pref.addPreferenceChangeListener(pl); 217 try { 218 pref.put("key1", "value1"); 219 assertEquals("value1",pref.get("key1", null)); 220 synchronized(sync) { 221 sync.wait(5000); 222 assertEquals(1, l.size()); 223 } 224 assertEquals("value1",l.get(0)); 225 l.clear(); 226 227 pref.put("key1", "value2"); 228 assertEquals("value2",pref.get("key1", null)); 229 synchronized(sync) { 230 sync.wait(5000); 231 assertEquals(1, l.size()); 232 } 233 assertEquals("value2",l.get(0)); 234 l.clear(); 235 236 pref.put("key1", "value2"); 237 assertEquals("value2",pref.get("key1", null)); 238 synchronized(sync) { 239 sync.wait(5000); 240 assertEquals(0, l.size()); 241 } 242 l.clear(); 243 244 pref.put("key1", "value2"); 245 assertEquals("value2",pref.get("key1", null)); 246 synchronized(sync) { 247 sync.wait(5000); 248 assertEquals(0, l.size()); 249 } 250 l.clear(); 251 252 } finally { 253 pref.removePreferenceChangeListener(pl); 254 } 255 } 256 257 private Object getEventQueueSync() { 258 try { 259 Field f = AbstractPreferences .class.getDeclaredField("eventQueue"); 260 f.setAccessible(true); 261 return f.get(null); 262 263 } catch (Exception ex) { 264 Logger.getLogger("global").log(java.util.logging.Level.SEVERE,ex.getMessage(), ex); 265 } 266 return null; 267 } 268 269 270 271 public void testRemove() { 272 testPut(); 273 Preferences pref = getPreferencesNode(); 274 assertEquals("value1",pref.get("key1", null)); 275 pref.remove("key1"); 276 assertNull(pref.get("key1", null)); 277 } 278 279 public void testClear() throws Exception { 280 testKeys(); 281 Preferences pref = getPreferencesNode(); 282 pref.clear(); 283 assertEquals(0,pref.keys().length); 284 assertNull(pref.get("key1", null)); 285 assertNull(pref.get("key2", null)); 286 } 287 288 public void testKeys() throws Exception { 289 Preferences pref = getPreferencesNode(); 290 assertNotNull(pref); 291 assertEquals(0,pref.keys().length); 292 pref.put("key1", "value1"); 293 pref.put("key2", "value2"); 294 assertEquals(2,pref.keys().length); 295 } 296 297 298 public void testParent() { 299 Preferences pref = getPreferencesNode(); 300 Preferences pref2 = pref.node("1/2/3"); 301 assertNotSame(pref, pref2); 302 303 for (int i = 0; i < 3; i++) { 304 pref2 = pref2.parent(); 305 } 306 307 assertSame(pref2.absolutePath(), pref, pref2); 308 } 309 310 public void testNodeExists() throws Exception { 311 Preferences pref = getPreferencesNode(); 312 Preferences pref2 = pref.node("a/b/c"); 313 while(pref2 != Preferences.userRoot()) { 314 assertTrue(pref2.nodeExists("")); 315 Preferences parent = pref2.parent(); 316 pref2.removeNode(); 317 assertFalse(pref2.nodeExists("")); 318 pref2 = parent; 319 } 320 321 assertNotNull(getPreferencesNode().node("a/b/c/d")); 322 assertTrue(getPreferencesNode().node("a/b/c/d").nodeExists("")); 323 } 324 325 326 public void testName() { 327 Preferences pref = getPreferencesNode(); 328 assertEquals("myname",pref.node("myname").name()); 329 } 330 331 public void testAbsolutePath() { 332 String validPath = "/a/b/c/d"; 333 Preferences pref = Preferences.userRoot().node(validPath); 334 assertEquals(validPath, pref.absolutePath()); 335 336 assertSame(pref, pref.parent().node("d")); 338 339 String invalidPath = "/a/b/c/d/"; 340 try { 341 pref = Preferences.userRoot().node(invalidPath); 342 fail(); 343 } catch(IllegalArgumentException iax) {} 344 345 } 346 347 public void testAddPreferenceChangeListener() throws BackingStoreException , InterruptedException { 348 final Preferences pref = getPreferencesNode(); 349 PreferenceChangeListener l = new PreferenceChangeListener () { 350 public void preferenceChange(PreferenceChangeEvent evt) { 351 synchronized (TestPreferences.class) { 352 assertSame(pref, evt.getNode()); 354 assertEquals("key", evt.getKey()); 355 assertEquals(evt.getNewValue(),pref.get(evt.getKey(),null), evt.getNewValue()); 356 TestPreferences.class.notifyAll(); 357 } 358 } 359 }; 360 pref.addPreferenceChangeListener(l); 361 try { 362 synchronized (TestPreferences.class) { 363 pref.put("key","AddPreferenceChangeListener"); 364 pref.flush(); 365 TestPreferences.class.wait(); 366 } 367 368 synchronized (TestPreferences.class) { 369 pref.remove("key"); 370 pref.flush(); 371 TestPreferences.class.wait(); 372 } 373 374 synchronized (TestPreferences.class) { 375 pref.put("key","AddPreferenceChangeListener2"); 376 pref.flush(); 377 TestPreferences.class.wait(); 378 } 379 } finally { 380 pref.removePreferenceChangeListener(l); 381 } 382 } 383 384 public void testAddNodeChangeListener() throws BackingStoreException , InterruptedException { 385 final Preferences pref = getPreferencesNode(); 386 NodeChangeListener l = new NodeChangeListener () { 387 public void childAdded(NodeChangeEvent evt) { 388 synchronized (TestPreferences.class){ 389 assertSame(pref, evt.getParent()); 391 assertEquals("added",evt.getChild().name()); 392 TestPreferences.class.notifyAll(); 393 } 394 } 395 396 public void childRemoved(NodeChangeEvent evt) { 397 synchronized (TestPreferences.class) { 398 assertSame(pref, evt.getParent()); 400 assertEquals("added",evt.getChild().name()); 401 TestPreferences.class.notifyAll(); 402 } 403 } 404 }; 405 pref.addNodeChangeListener(l); 406 try { 407 Preferences added; 408 synchronized (TestPreferences.class) { 409 added = pref.node("added"); 410 TestPreferences.class.wait(); 411 } 412 413 synchronized (TestPreferences.class) { 414 added.removeNode(); 415 TestPreferences.class.wait(); 416 } 417 418 } finally { 419 pref.removeNodeChangeListener(l); 420 } 421 } 422 423 public void testIsPersistent() throws BackingStoreException ,InterruptedException { 424 NbPreferences pref = (NbPreferences)getPreferencesNode(); 425 assertNotNull(pref); 426 assertEquals(NbPreferences.UserPreferences.class, pref.getClass()); 427 pref.put("key", "value"); 428 assertEquals("value", pref.get("key", null)); 429 pref.sync(); 430 assertEquals("value", pref.get("key", null)); 431 } 432 433 protected int timeOut() { 434 return 20000; 435 } 436 437 438 } 439 | Popular Tags |