private void fireNodeEvents(List<EventBag<NodeChangeListener, NodeChangeEvent>> events) { if (noEvents) { return; } for(EventBag<NodeChangeListener, NodeChangeEvent> bag : events) { for(NodeChangeEvent event : bag.getEvents()) { for(NodeChangeListener l : bag.getListeners()) { try { if ((event instanceof NodeChangeEventExt) && ((NodeChangeEventExt) event).isRemovalEvent()) { l.childRemoved(event); } else { l.childAdded(event); } } catch (Throwable t) { LOG.log(Level.WARNING, null, t); } } } } }
protected ScheduledFuture<?> testNodeAdded(final NodeChangeListener listener, final Preferences parent, final String childName, final int count) { final ArgumentCaptor<NodeChangeEvent> captor = ArgumentCaptor.forClass(NodeChangeEvent.class); final Runnable task = new Runnable() { @Override public void run() { verify(listener, times(count)).childAdded(captor.capture()); final NodeChangeEvent event = captor.getValue(); assertEquals(event.getParent(), parent); assertEquals(event.getChild().name(), childName); } }; return schedule(task); }
protected ScheduledFuture<?> testNodeRemoved(final NodeChangeListener listener, final Preferences parent, final String childName, final int count) { final ArgumentCaptor<NodeChangeEvent> captor = ArgumentCaptor.forClass(NodeChangeEvent.class); final Runnable task = new Runnable() { @Override public void run() { verify(listener, times(count)).childRemoved(captor.capture()); final NodeChangeEvent event = captor.getValue(); assertEquals(event.getParent(), parent); assertEquals(event.getChild().name(), childName); } }; return schedule(task); }
@Test(dataProvider = "chroot", groups = {"prefs", "hierarchy", "notifications"}, dependsOnGroups = "factory", timeOut = 1000L) public void shouldTriggerNodeAdded(Preferences prefs) throws Exception { final Preferences watched = prefs.node("watched"); final NodeChangeListener listener = nodeListener(watched); final Preferences n1 = watched.node("n1"); testNodeAdded(listener, watched, n1.name(), 1).get(); watched.node("n1"); // not a new node, so the invocation count should stay the same prefs.node("n0"); // shouldn't trigger anything for our listener, as it's a parent event testNodeAdded(listener, watched, n1.name(), 1).get(); final Preferences n2 = watched.node("n2"); testNodeAdded(listener, watched, n2.name(), 2).get(); watched.node("n3/nested"); testNodeAdded(listener, watched, "n3", 3).get(); }
@Test(dataProvider = "chroot", groups = {"prefs", "hierarchy", "notifications"}, dependsOnMethods = "shouldTriggerNodeAdded", timeOut = 1000L) public void shouldTriggerNodeRemoved(Preferences prefs) throws Exception { final Preferences watched = prefs.node("watched"); final NodeChangeListener listener = nodeListener(watched); watched.node("n1").removeNode(); testNodeRemoved(listener, watched, "n1", 1).get(); watched.node("n2").removeNode(); testNodeRemoved(listener, watched, "n2", 2).get(); watched.node("n3/nested").removeNode(); testNodeRemoved(listener, watched, "n2", 2).get(); watched.node("n3").removeNode(); testNodeRemoved(listener, watched, "n3", 3).get(); }
@TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "addNodeChangeListener", args = {java.util.prefs.NodeChangeListener.class} ) public void testAddNodeChangeListener() throws BackingStoreException { MockNodeChangeListener mncl = new MockNodeChangeListener(); parent.addNodeChangeListener(mncl); pref.node("test"); pref.flush(); parent.flush(); assertFalse(mncl.isAdded()); assertFalse(mncl.isRemoved()); pref.removeNode(); parent.flush(); assertFalse(mncl.isAdded()); assertTrue(mncl.isRemoved()); parent.node("new node"); parent.flush(); assertTrue(mncl.isAdded()); assertTrue(mncl.isRemoved()); }
@TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "removeNodeChangeListener", args = {java.util.prefs.NodeChangeListener.class} ) public void testRemoveNodeChangeListener() throws BackingStoreException { MockNodeChangeListener mncl = new MockNodeChangeListener(); parent.addNodeChangeListener(mncl); pref.node("test"); pref.flush(); parent.flush(); assertFalse(mncl.isAdded()); assertFalse(mncl.isRemoved()); parent.removeNodeChangeListener(mncl); pref.removeNode(); parent.flush(); assertFalse(mncl.isAdded()); assertFalse(mncl.isRemoved()); parent.node("new node"); parent.flush(); assertFalse(mncl.isAdded()); assertFalse(mncl.isRemoved()); }
@Override public void removeNodeChangeListener(NodeChangeListener ncl) { try { super.removeNodeChangeListener(ncl); } catch (IllegalArgumentException e) { // ignore, see #143581 } }
@Override public void sync() throws BackingStoreException { ArrayList<EventBag<PreferenceChangeListener, PreferenceChangeEvent>> prefEvents = new ArrayList<EventBag<PreferenceChangeListener, PreferenceChangeEvent>>(); ArrayList<EventBag<NodeChangeListener, NodeChangeEvent>> nodeEvents = new ArrayList<EventBag<NodeChangeListener, NodeChangeEvent>>(); synchronized (tree.treeLock()) { _sync(prefEvents, nodeEvents); } fireNodeEvents(nodeEvents); firePrefEvents(prefEvents); }
private ProxyPreferences(ProxyPreferences parent, MemoryPreferences memoryPref, Preferences delegate) { this.parent = parent; this.delegateRoot = memoryPref; this.delegate = delegate == null ? memoryPref.getPreferences() : delegate; weakPrefListener = WeakListeners.create(PreferenceChangeListener.class, this, delegate); this.delegate.addPreferenceChangeListener(weakPrefListener); weakNodeListener = WeakListeners.create(NodeChangeListener.class, this, delegate); this.delegate.addNodeChangeListener(weakNodeListener); }
@Test(dataProvider = "chroot", groups = {"prefs", "concurrency"}, dependsOnGroups = {"hierarchy", "kv"}, invocationCount = 5, threadPoolSize = 3, timeOut = 3000L) public void concurrencyTest(Preferences prefs) throws Exception { final Preferences child = prefs.node(UUID.randomUUID().toString()); final String key = UUID.randomUUID().toString(); final NodeChangeListener nodeListener = nodeListener(child); final PreferenceChangeListener prefListener = prefListener(child); prefs.putInt(key, 12); final Preferences grandson = child.node("grandson"); ScheduledFuture<?> nodeAdded = testNodeAdded(nodeListener, child, grandson.name(), 1); child.putBoolean(key, true); ScheduledFuture<?> prefSet = testPrefChange(prefListener, child, key, "true", 1); assertEquals(prefs.getInt(key, 0), 12); assertTrue(child.getBoolean(key, false)); grandson.removeNode(); ScheduledFuture<?> nodeRemoved = testNodeRemoved(nodeListener, child, grandson.name(), 1); prefSet.get(); child.remove(key); child.removeNode(); assertEquals(prefs.getInt(key, 0), 12); nodeAdded.get(); nodeRemoved.get(); testPrefChange(prefListener, child, key, null, 2).get(); }
@Test(dataProvider = "chroot", groups = {"prefs", "backing-store"}, dependsOnGroups = {"hierarchy", "kv"}) public void shouldSyncNodeChanges(Preferences prefs) throws Exception { assertFalse(prefs.nodeExists("x")); assertFalse(childExistsInBackingStore(prefs, "x")); final Preferences x = prefs.node("x"); // prefs.flush(); assertTrue(childExistsInBackingStore(prefs, "x")); final NodeChangeListener listener = nodeListener(x); createInBackingStore(x, "y"); assertTrue(childExistsInBackingStore(x, "y")); final ScheduledFuture<?> nodeAdded = testNodeAdded(listener, x, "y", 1); // prefs.sync(); assertTrue(x.nodeExists("y")); nodeAdded.get(); assertTrue(x.nodeExists("y")); assertTrue(childExistsInBackingStore(x, "y")); removeChildFromBackingStore(x, "y"); assertFalse(childExistsInBackingStore(x, "y")); final ScheduledFuture<?> nodeRemoved = testNodeRemoved(listener, x, "y", 1); // prefs.sync(); assertFalse(x.nodeExists("y")); nodeRemoved.get(); x.removeNode(); // prefs.flush(); assertFalse(childExistsInBackingStore(prefs, "x")); }
@Override public void addNodeChangeListener(NodeChangeListener ncl) { synchronized (tree.treeLock()) { nodeListeners.add(ncl); } }
@Override public void removeNodeChangeListener(NodeChangeListener ncl) { synchronized (tree.treeLock()) { nodeListeners.remove(ncl); } }
private ProxyPreferencesImpl node(String pathName, boolean create, List<EventBag<NodeChangeListener, NodeChangeEvent>> events) { if (pathName.length() > 0 && pathName.charAt(0) == '/') { //NOI18N // absolute path, if this is not the root then find the root // and pass the call to it if (parent != null) { Preferences root = this; while (root.parent() != null) { root = root.parent(); } return ((ProxyPreferencesImpl) root).node(pathName, create, events); } else { // this is the root, change the pathName to a relative path and proceed pathName = pathName.substring(1); } } if (pathName.length() > 0) { String childName; String pathFromChild; int idx = pathName.indexOf('/'); //NOI18N if (idx != -1) { childName = pathName.substring(0, idx); pathFromChild = pathName.substring(idx + 1); } else { childName = pathName; pathFromChild = null; } ProxyPreferencesImpl child = children.get(childName); if (child == null) { if (removedChildren.contains(childName) && !create) { // this child has been removed return null; } Preferences childDelegate = null; try { if (delegate != null && delegate.nodeExists(childName)) { childDelegate = delegate.node(childName); } } catch (BackingStoreException bse) { // ignore } if (childDelegate != null || create) { child = tree.get(this, childName, childDelegate); children.put(childName, child); removedChildren.remove(childName); // fire event if we really created the new child node if (childDelegate == null) { EventBag<NodeChangeListener, NodeChangeEvent> bag = new EventBag<NodeChangeListener, NodeChangeEvent>(); bag.addListeners(nodeListeners); bag.addEvent(new NodeChangeEventExt(this, child, false)); events.add(bag); } } else { // childDelegate == null && !create return null; } } else { assert !child.removed; } return pathFromChild != null ? child.node(pathFromChild, create, events) : child; } else { return this; } }
@Override public void addNodeChangeListener(NodeChangeListener ncl) { synchronized (this) { nodeListeners.add(ncl); } }
@Override public void removeNodeChangeListener(NodeChangeListener ncl) { synchronized (this) { nodeListeners.remove(ncl); } }
@Override public void addNodeChangeListener(@Nonnull final NodeChangeListener ncl) { }
@Override public void removeNodeChangeListener(@Nonnull final NodeChangeListener ncl) { }
@Override public void addNodeChangeListener(NodeChangeListener ncl) { this.nodeChangeListeners.add(ncl); }
@Override public void removeNodeChangeListener(NodeChangeListener ncl) { this.nodeChangeListeners.remove(ncl); }
@Override public void addNodeChangeListener( NodeChangeListener ncl ) { throw new UnsupportedOperationException( "Method 'addNodeChangeListener' has not been implemented yet." ); }
@Override public void removeNodeChangeListener( NodeChangeListener ncl ) { throw new UnsupportedOperationException( "Method 'removeNodeChangeListener' has not been implemented yet." ); }
protected NodeChangeListener nodeListener(Preferences prefs) { final NodeChangeListener listener = mock(NodeChangeListener.class); prefs.addNodeChangeListener(listener); return listener; }
@Override public void addNodeChangeListener(NodeChangeListener arg0){ /* TODO Auto-generated method stub */ }
@Override public void removeNodeChangeListener(NodeChangeListener arg0){ /* TODO Auto-generated method stub */ }
@Override public void addNodeChangeListener(NodeChangeListener aArg0) { pref.addNodeChangeListener(aArg0); }
@Override public void removeNodeChangeListener(NodeChangeListener aArg0) { pref.removeNodeChangeListener(aArg0); }
@Override public void addNodeChangeListener( NodeChangeListener ncl ) { this.wrapped.addNodeChangeListener(ncl); }
@Override public void removeNodeChangeListener( NodeChangeListener ncl ) { this.wrapped.removeNodeChangeListener(ncl); }
@Override public void addNodeChangeListener(NodeChangeListener ncl) { }
@Override public void removeNodeChangeListener(NodeChangeListener ncl) { }
public void addNodeChangeListener(NodeChangeListener ncl) { }
public void removeNodeChangeListener(NodeChangeListener ncl) { }