What surprised me was that none of my LookupListener instances attached to the proxy were being notified when changes happened in a delegate's Lookup?! Have you seen this, too?
Here's a JUnit test to demonstrate the puzzle, where I skip the formalities for brevity:
...
class EventCountingLookupListener implements LookupListener {
private int count_ = 0;
public EventCountingLookupListener(Lookup.Result result) {
result.addLookupListener(this);
}
public void resultChanged(LookupEvent ev) {
++count_;
}
public int count() {
return count_;
}
}
public void testProxyLookupNoEvents() {
InstanceContent ic = new InstanceContent();
AbstractLookup al = new AbstractLookup(ic);
Lookup.Result result0 = al.lookupResult(String.class);
EventCountingLookupListener ll0 = new EventCountingLookupListener(result0);
Lookup proxy = new ProxyLookup(al);
Lookup.Result result = proxy.lookupResult(String.class);
EventCountingLookupListener ll = new EventCountingLookupListener(result);
ic.add("hello1");
assertEquals(1, ll0.count()); // OKAY: 1 event encountered in the delegate as expected
assertEquals(1, ll.count()); // FAILURE: No events encountered in the proxy!!
}
...
Thanks to my kind colleague Tom Wheeler who also helped me to review the test code, we have now an open issue #136866 at the NetBeans Community website. Hopefully, some of the experts there will be able to shed a bit of light.
Luckily, I soon found a workaround (soon enough to prevent having to redesign my work :) As it turns out, simply calling proxy.lookupAll() before the events are expected, was sufficient to get the "correct" behavior:
...
public void testProxyLookupEventsWithLookupAllBeforeObtainingResult() {
InstanceContent ic = new InstanceContent();
AbstractLookup al = new AbstractLookup(ic);
Lookup proxy = new ProxyLookup(al);
// NOTE: proxy.lookupAll() does the "magic" here ...
// ... before any listeners have been attached to any of the
// proxy's Lookup.Result-s
assertEquals(0, proxy.lookupAll(String.class).size());
Lookup.Result result = proxy.lookupResult(String.class);
EventCountingLookupListener ll = new EventCountingLookupListener(result);
ic.add("hello1");
assertEquals(1, ll.count()); // OKAY: 1-st event encountered!
ic.add("hello2");
assertEquals(2, ll.count()); // OKAY: 2-nd event encountered!
}
...
Is this a feature or a bug?
No comments:
Post a Comment