This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 197594
Collapse All | Expand All

(-)a/core.startup/test/unit/src/org/netbeans/core/startup/preferences/TestPropertiesStorage.java (-32 / +140 lines)
Lines 50-56 Link Here
50
import java.util.Collections;
50
import java.util.Collections;
51
import java.util.TreeSet;
51
import java.util.TreeSet;
52
import java.util.prefs.BackingStoreException;
52
import java.util.prefs.BackingStoreException;
53
import java.util.prefs.NodeChangeEvent;
54
import java.util.prefs.NodeChangeListener;
55
import java.util.prefs.PreferenceChangeEvent;
56
import java.util.prefs.PreferenceChangeListener;
53
import java.util.prefs.Preferences;
57
import java.util.prefs.Preferences;
58
import org.openide.filesystems.FileLock;
54
import org.openide.filesystems.FileObject;
59
import org.openide.filesystems.FileObject;
55
import org.openide.filesystems.FileUtil;
60
import org.openide.filesystems.FileUtil;
56
61
Lines 61-66 Link Here
61
public class TestPropertiesStorage extends TestFileStorage {
66
public class TestPropertiesStorage extends TestFileStorage {
62
    private PropertiesStorage storage;
67
    private PropertiesStorage storage;
63
    private NbPreferences pref;
68
    private NbPreferences pref;
69
    private boolean nodeAddedEvent;
70
    private boolean nodeRemovedEvent;
71
    private boolean prefChangedEvent;
72
    private boolean prefRemovedEvent;
64
    
73
    
65
    public TestPropertiesStorage(String testName) {
74
    public TestPropertiesStorage(String testName) {
66
        super(testName);
75
        super(testName);
Lines 133-177 Link Here
133
        assertNull(storage.toPropertiesFile());                
142
        assertNull(storage.toPropertiesFile());                
134
    }
143
    }
135
144
136
      public void testNodeIsReloadedAfterChange() throws Exception {
145
    public void testNodeIsReloadedAfterChange() throws Exception {
137
         String key= "key";
146
        String key= "key";
138
         String value = "oldValue";
147
        String value = "oldValue";
139
         String newValue = "newValue";
148
        String newValue = "newValue";
140
         storeEntry(key, value);
149
        storeEntry(key, value);
141
         overrideStorageEntryWithNewValue(value, newValue);
150
        overrideStorageEntryWithNewData(constructNewEntryText(value, newValue));
142
151
143
         String reloadedValue = pref.get(key, null);
152
        String reloadedValue = pref.get(key, null);
144
153
145
         assertNotNull("Reloaded value must not be null", reloadedValue);
154
        assertNotNull("Reloaded value must not be null", reloadedValue);
146
         assertEquals("Reloaded value must equals to manually stored value", newValue, reloadedValue);
155
        assertEquals("Reloaded value must equals to manually stored value", newValue, reloadedValue);
147
         /*
156
        /*
148
          Still need to cope with a memory leak
157
         Still need to cope with a memory leak
149
158
150
         WeakReference weakPref = new WeakReference(pref);
159
        WeakReference weakPref = new WeakReference(pref);
151
         storage = null;
160
        storage = null;
152
         pref = null;
161
        pref = null;
153
         assertGC("NbPreferences is not GC", weakPref);
162
        assertGC("NbPreferences is not GC", weakPref);
154
         */
163
        */
155
164
156
     }
165
    }
157
166
158
     private void storeEntry(String keyName, String oldValue) throws BackingStoreException {
167
    private void storeEntry(String keyName, String oldValue) throws BackingStoreException {
159
         pref.put(keyName, oldValue);
168
        pref.put(keyName, oldValue);
160
         pref.flush();
169
        pref.flush();
161
     }
170
    }
162
171
163
     private void overrideStorageEntryWithNewValue(String oldValue, String newValue) throws IOException {
172
     private void overrideStorageEntryWithNewData(String newData) throws IOException {
164
         String newText = constructNewEntryText(oldValue, newValue);         
165
         OutputStream storageOutputStream = storage.toPropertiesFile().getOutputStream();
173
         OutputStream storageOutputStream = storage.toPropertiesFile().getOutputStream();
166
         storageOutputStream.write(newText.getBytes("ISO-8859-1"));
174
         storageOutputStream.write(newData.getBytes("ISO-8859-1"));
167
         storageOutputStream.close();                     
175
         storageOutputStream.close();
168
     }
176
    }
169
177
170
     private String constructNewEntryText(String oldValue, String newValue) throws IOException {
178
     // UNDER DEVELOPMENT
171
         String currentText = storage.toPropertiesFile().asText();
179
     //
172
         String newText = currentText.replace(oldValue, newValue);
180
     // NOTE: replacing the file should probably be a method on
173
         return newText;
181
     //       PropertiesStorage; or maybe a package method on
174
     }
182
     //       NbPreferences for synchronization purposes.
183
     //
184
     //       THIS METHOD should probably have an "subnode" argument,
185
     //                   which (if not null) operates on subnode
186
     //
187
    private void XXXoverrideStorageEntryWithNewData(String newData) throws IOException {
188
        FileObject foOrig = storage.toPropertiesFile();
189
        FileObject foNew = foOrig.getParent().createData("NewProperties");
190
        FileLock lock = foNew.lock();
191
        OutputStream os = foNew.getOutputStream();
192
        os.write(newData.getBytes("ISO-8859-1"));
193
        os.close();
194
        foOrig.rename(lock, foOrig.getName(), foOrig.getExt());
195
    }
196
197
    private String constructNewEntryText(String oldValue, String newValue) throws IOException {
198
        String currentText = storage.toPropertiesFile().asText();
199
        String newText = currentText.replace(oldValue, newValue);
200
        return newText;
201
    }
175
202
176
203
177
    public void testRemove() throws BackingStoreException {
204
    public void testRemove() throws BackingStoreException {
Lines 296-300 Link Here
296
        subnode.sync();
323
        subnode.sync();
297
        assertEquals(1, subnode.childrenNames().length);        
324
        assertEquals(1, subnode.childrenNames().length);        
298
        assertEquals("a", subnode.childrenNames()[0]);        
325
        assertEquals("a", subnode.childrenNames()[0]);        
299
    }    
326
    }
327
328
329
    public void testPreferencesEvents() throws Exception {
330
        pref.addNodeChangeListener(new NodeListener());
331
        pref.addPreferenceChangeListener(new PrefListener());
332
        Preferences child = pref.node("a");
333
        Thread.currentThread().yield();
334
        assertTrue("Missing node added event", nodeAddedEvent);
335
        pref.put("key","value");
336
        Thread.currentThread().yield();
337
        assertTrue("Missing preference change event", prefChangedEvent);
338
        pref.remove("key");
339
        Thread.currentThread().yield();
340
        assertTrue("Missing preference removed event", prefRemovedEvent);
341
        child.removeNode();
342
        Thread.currentThread().yield();
343
        assertTrue("Missing node removed event", nodeRemovedEvent);
344
    }
345
346
    // started with testNodeIsReloadedAfterChange (memory leak issue?)
347
    public void testPreferencesEventsAfterChange1() throws Exception {
348
        String key= "key";
349
        String value = "oldValue";
350
        String newValue = "newValue";
351
        storeEntry(key, value);
352
353
        pref.addPreferenceChangeListener(new PrefListener());
354
355
        overrideStorageEntryWithNewData(constructNewEntryText(value, newValue));
356
        Thread.currentThread().yield();
357
358
        assertEquals("Reloaded value must equals to manually stored value",
359
                     newValue, pref.get(key, null));
360
        assertTrue("Missing preference change event", prefChangedEvent);
361
        assertFalse("Unexpected preference removed event", prefRemovedEvent);
362
    }
363
364
    // started with testNodeIsReloadedAfterChange (memory leak issue?)
365
    public void testPreferencesEventsAfterChange2() throws Exception {
366
        pref.put("targetKey","targetValue");
367
        pref.flush();
368
        // targetPrefs will be loaded as a file.
369
        String targetPrefs = storage.toPropertiesFile().asText();
370
371
        pref.clear();
372
        pref.put("key","value");
373
        pref.flush();
374
375
        pref.addPreferenceChangeListener(new PrefListener());
376
377
        overrideStorageEntryWithNewData(targetPrefs);
378
        Thread.currentThread().yield();
379
380
        assertEquals("not reloaded", "targetValue", pref.get("targetKey", "NONE"));
381
        assertEquals("not unloaded", "NONE", pref.get("key", "NONE"));
382
        assertTrue("Missing preference change event", prefChangedEvent);
383
        assertTrue("Missing preference removed event", prefRemovedEvent);
384
    }
385
386
    private class NodeListener implements NodeChangeListener {
387
        @Override
388
        public void childAdded(NodeChangeEvent evt) {
389
            nodeAddedEvent = true;
390
        }
391
392
        @Override
393
        public void childRemoved(NodeChangeEvent evt) {
394
            nodeRemovedEvent = true;
395
        }
396
    }
397
398
    private class PrefListener implements PreferenceChangeListener {
399
        @Override
400
        public void preferenceChange(PreferenceChangeEvent evt) {
401
            if(evt.getNewValue() != null) {
402
                prefChangedEvent = true;
403
            } else {
404
                prefRemovedEvent = true;
405
            }
406
        }
407
    }
300
}
408
}

Return to bug 197594