1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 22:52:11 +02:00

[288672] Lacking dsf test for registering an event listener with a services filter

This commit is contained in:
John Cortell 2009-09-04 19:30:08 +00:00
parent 80326720b6
commit 5df35e00b1
5 changed files with 184 additions and 33 deletions

View file

@ -17,6 +17,7 @@ import org.eclipse.cdt.dsf.service.DsfServiceEventHandler;
import org.eclipse.cdt.dsf.service.DsfSession;
import org.eclipse.cdt.tests.dsf.DsfTestPlugin;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Filter;
/**
* Test service class used to test event behavior. It has three types of events
@ -29,7 +30,7 @@ abstract public class AbstractService extends AbstractDsfService
super(session);
}
@Override protected BundleContext getBundleContext() {
@Override protected final BundleContext getBundleContext() {
return DsfTestPlugin.getBundleContext();
}
@ -44,7 +45,7 @@ abstract public class AbstractService extends AbstractDsfService
}
private void doInitialize(RequestMonitor requestMonitor) {
getSession().addServiceEventListener(this, null);
getSession().addServiceEventListener(this, getEventServicesFilter());
requestMonitor.done();
}
@ -53,6 +54,12 @@ abstract public class AbstractService extends AbstractDsfService
super.shutdown(requestMonitor);
}
/**
* Subclass should override this if it wants events from only certain
* services.
*/
protected Filter getEventServicesFilter() { return null; }
///////////////////////////////////////////////////////////////////////////
// Test API
/** Records the number in the event 1 object when this service received the event. */
@ -66,12 +73,13 @@ abstract public class AbstractService extends AbstractDsfService
/** Simple event class 1 */
public class Event1 {
// 1-based counter for the recipient of the event.
/** 1-based counter for the recipient of the event. That is, whenever a handler is called with this event, the handler bumps this value */
int fRecipientNumberCounter = 1;
}
/** Simple event class 2. Note it doesn't have any relation to event 1 */
public class Event2 {
/** 1-based counter for the recipient of the event. That is, whenever a handler is called with this event, the handler bumps this value */
int fRecipientNumberCounter = 1;
}

View file

@ -30,6 +30,7 @@ public class EventTest {
Service1 fService1;
Service2 fService2;
Service3 fService3;
Service4 fService4;
@Before public void startServices() throws ExecutionException, InterruptedException {
fExecutor = new TestDsfExecutor();
@ -47,10 +48,12 @@ public class EventTest {
fService1 = fTracker.getService(Service1.class);
fService2 = fTracker.getService(Service2.class);
fService3 = fTracker.getService(Service3.class);
fService4 = fTracker.getService(Service4.class);
}}).get();
Assert.assertNotNull(fService1);
Assert.assertNotNull(fService2);
Assert.assertNotNull(fService3);
Assert.assertNotNull(fService4);
}
@After public void shutdownServices() throws ExecutionException, InterruptedException {
@ -62,6 +65,7 @@ public class EventTest {
fService1 = null;
fService2 = null;
fService3 = null;
fService4 = null;
fTracker.dispose();
fTracker = null;
DsfSession.endSession(fSession);
@ -82,22 +86,101 @@ public class EventTest {
@Test public void startStopTest() {
}
private void assertEventNotReceivedByAnyService(int eventNumber) {
switch (eventNumber) {
case 1:
Assert.assertTrue(0 == fService1.fEvent1RecipientNumber);
Assert.assertTrue(0 == fService2.fEvent1RecipientNumber);
Assert.assertTrue(0 == fService3.fEvent1RecipientNumber);
Assert.assertTrue(0 == fService4.fEvent1RecipientNumber);
break;
case 2:
Assert.assertTrue(0 == fService1.fEvent2RecipientNumber);
Assert.assertTrue(0 == fService2.fEvent2RecipientNumber);
Assert.assertTrue(0 == fService3.fEvent2RecipientNumber);
Assert.assertTrue(0 == fService4.fEvent2RecipientNumber);
break;
case 3:
Assert.assertTrue(0 == fService1.fEvent3RecipientNumber);
Assert.assertTrue(0 == fService2.fEvent3RecipientNumber);
Assert.assertTrue(0 == fService3.fEvent3RecipientNumber);
Assert.assertTrue(0 == fService4.fEvent3RecipientNumber);
break;
default:
Assert.fail("invalid event number specified.");
}
}
/**
* Tests dispatching event 1. The goal of the test is to make sure that
* recipients are called in the correct order.
*/
@Test public void event1Test() throws ExecutionException, InterruptedException {
// All the services should receive the event except service 4, which
// specifies a services filter when registering itself as a listener.
// The filter limits the listener to events originating from service 2.
fService1.dispatchEvent1();
fExecutor.submit(new DsfRunnable() { public void run() {
Assert.assertTrue(1 == fService1.fEvent1RecipientNumber);
Assert.assertTrue(2 == fService2.fEvent1RecipientNumber);
Assert.assertTrue(3 == fService3.fEvent1RecipientNumber);
Assert.assertTrue(0 == fService1.fEvent2RecipientNumber);
Assert.assertTrue(0 == fService2.fEvent2RecipientNumber);
Assert.assertTrue(0 == fService3.fEvent2RecipientNumber);
Assert.assertTrue(0 == fService1.fEvent3RecipientNumber);
Assert.assertTrue(0 == fService2.fEvent3RecipientNumber);
Assert.assertTrue(0 == fService3.fEvent3RecipientNumber);
Assert.assertTrue(0 == fService4.fEvent1RecipientNumber);
assertEventNotReceivedByAnyService(2);
assertEventNotReceivedByAnyService(3);
}}).get();
// Reset the counts for event 1. We're going to fire it again but this
// time from service 2. Now service 4 should receive the event since it
// is being sent from service 2 (meeting the filter criteria)
fService1.fEvent1RecipientNumber =
fService2.fEvent1RecipientNumber =
fService3.fEvent1RecipientNumber =
fService4.fEvent1RecipientNumber = 0;
fService2.dispatchEvent1();
fExecutor.submit(new DsfRunnable() { public void run() {
Assert.assertTrue(1 == fService1.fEvent1RecipientNumber);
Assert.assertTrue(2 == fService2.fEvent1RecipientNumber);
Assert.assertTrue(3 == fService3.fEvent1RecipientNumber);
Assert.assertTrue(4 == fService4.fEvent1RecipientNumber);
assertEventNotReceivedByAnyService(2);
assertEventNotReceivedByAnyService(3);
}}).get();
// Reset the counts for event 1. We're going to fire it again but this
// time from service 3. As in the first case, service 4 should not
// receive it.
fService1.fEvent1RecipientNumber =
fService2.fEvent1RecipientNumber =
fService3.fEvent1RecipientNumber =
fService4.fEvent1RecipientNumber = 0;
fService3.dispatchEvent1();
fExecutor.submit(new DsfRunnable() { public void run() {
Assert.assertTrue(1 == fService1.fEvent1RecipientNumber);
Assert.assertTrue(2 == fService2.fEvent1RecipientNumber);
Assert.assertTrue(3 == fService3.fEvent1RecipientNumber);
Assert.assertTrue(0 == fService4.fEvent1RecipientNumber);
assertEventNotReceivedByAnyService(2);
assertEventNotReceivedByAnyService(3);
}}).get();
// Ditto when firing the event from service 4
fService1.fEvent1RecipientNumber =
fService2.fEvent1RecipientNumber =
fService3.fEvent1RecipientNumber =
fService4.fEvent1RecipientNumber = 0;
fService4.dispatchEvent1();
fExecutor.submit(new DsfRunnable() { public void run() {
Assert.assertTrue(1 == fService1.fEvent1RecipientNumber);
Assert.assertTrue(2 == fService2.fEvent1RecipientNumber);
Assert.assertTrue(3 == fService3.fEvent1RecipientNumber);
Assert.assertTrue(0 == fService4.fEvent1RecipientNumber);
assertEventNotReceivedByAnyService(2);
assertEventNotReceivedByAnyService(3);
}}).get();
}
@ -109,39 +192,36 @@ public class EventTest {
@Test public void event2Test() throws ExecutionException, InterruptedException {
fService1.dispatchEvent2();
fExecutor.submit(new DsfRunnable() { public void run() {
Assert.assertTrue(0 == fService1.fEvent1RecipientNumber);
Assert.assertTrue(0 == fService2.fEvent1RecipientNumber);
Assert.assertTrue(0 == fService3.fEvent1RecipientNumber);
assertEventNotReceivedByAnyService(1);
Assert.assertTrue(1 == fService1.fEvent2RecipientNumber);
Assert.assertTrue(2 == fService2.fEvent2RecipientNumber);
Assert.assertTrue(3 == fService3.fEvent2RecipientNumber);
Assert.assertTrue(0 == fService1.fEvent3RecipientNumber);
Assert.assertTrue(0 == fService2.fEvent3RecipientNumber);
Assert.assertTrue(0 == fService3.fEvent3RecipientNumber);
Assert.assertTrue(0 == fService4.fEvent2RecipientNumber); // service 4 specified filter to receive events only from service 2
assertEventNotReceivedByAnyService(3);
}}).get();
}
/**
* Tests dispatching event 2. The goal of the test is to make sure that
* both event 2 and even 3 recipients are called for this event.
* <br>
* Note: When a single listener object has more than one method that that
* matches the event, both methods will be called. But there is currently
* no guaranteed order in which they should be called.
*/
/**
* Tests dispatching event 2. The goal of the test is to make sure that both
* event 2 and even 3 recipients are called for this event, since Event3
* derives from Event1. <br>
*
* Note: When a single listener object has more than one method that that
* matches the event, both methods will be called. But there is currently no
* guaranteed order in which they should be called.
*/
@Test public void event3Test() throws ExecutionException, InterruptedException {
fService1.dispatchEvent3();
fExecutor.submit(new DsfRunnable() { public void run() {
Assert.assertTrue(1 == fService1.fEvent1RecipientNumber || 2 == fService1.fEvent1RecipientNumber);
Assert.assertTrue(3 == fService2.fEvent1RecipientNumber || 4 == fService2.fEvent1RecipientNumber);
Assert.assertTrue(5 == fService3.fEvent1RecipientNumber || 6 == fService3.fEvent1RecipientNumber);
Assert.assertTrue(0 == fService1.fEvent2RecipientNumber);
Assert.assertTrue(0 == fService2.fEvent2RecipientNumber);
Assert.assertTrue(0 == fService3.fEvent2RecipientNumber);
Assert.assertTrue(0 == fService4.fEvent1RecipientNumber); // service 4 specified filter to receive events only from service 2
assertEventNotReceivedByAnyService(2);
Assert.assertTrue(1 == fService1.fEvent3RecipientNumber || 2 == fService1.fEvent3RecipientNumber);
Assert.assertTrue(3 == fService2.fEvent3RecipientNumber || 4 == fService2.fEvent3RecipientNumber);
Assert.assertTrue(5 == fService3.fEvent3RecipientNumber || 6 == fService3.fEvent3RecipientNumber);
Assert.assertTrue(0 == fService4.fEvent3RecipientNumber); // service 4 specified filter to receive events only from service 2
}}).get();
}
}

View file

@ -14,18 +14,12 @@ import java.util.Hashtable;
import org.eclipse.cdt.dsf.concurrent.RequestMonitor;
import org.eclipse.cdt.dsf.service.DsfSession;
import org.eclipse.cdt.tests.dsf.DsfTestPlugin;
import org.osgi.framework.BundleContext;
public class Service3 extends AbstractService {
Service3(DsfSession session) {
super(session);
}
@Override protected BundleContext getBundleContext() {
return DsfTestPlugin.getBundleContext();
}
@Override public void initialize(final RequestMonitor requestMonitor) {
super.initialize(
new RequestMonitor(getExecutor(), requestMonitor) {

View file

@ -0,0 +1,66 @@
/*******************************************************************************
* Copyright (c) 2009 Freescale Semiconductor and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Freescale Semiconductor - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.tests.dsf.events;
import java.util.Hashtable;
import org.eclipse.cdt.dsf.concurrent.RequestMonitor;
import org.eclipse.cdt.dsf.service.DsfSession;
import org.junit.Assert;
import org.osgi.framework.Filter;
import org.osgi.framework.InvalidSyntaxException;
/**
* This service differs from the other three in that when it registers itself as
* an event listener with the dsf session, it specifies a services filter.
*
*/
public class Service4 extends AbstractService {
Service4(DsfSession session) {
super(session);
}
@Override public void initialize(final RequestMonitor requestMonitor) {
super.initialize(
new RequestMonitor(getExecutor(), requestMonitor) {
@Override
public void handleSuccess() {
doInitialize(requestMonitor);
}
});
}
private void doInitialize(RequestMonitor requestMonitor) {
getServicesTracker().getService(Service1.class);
getServicesTracker().getService(Service2.class);
getServicesTracker().getService(Service3.class);
register(new String[]{Service4.class.getName()}, new Hashtable<String,String>());
requestMonitor.done();
}
@Override public void shutdown(RequestMonitor requestMonitor) {
unregister();
super.shutdown(requestMonitor);
}
/**
* We want to get events only from Service2.
* @see org.eclipse.cdt.tests.dsf.events.AbstractService#getEventServicesFilter()
*/
@Override protected Filter getEventServicesFilter() {
try {
return getBundleContext().createFilter("(objectClass=org.eclipse.cdt.tests.dsf.events.Service2)");
} catch (InvalidSyntaxException e) {
Assert.fail();
return null;
}
}
}

View file

@ -34,6 +34,9 @@ class StartupSequence extends Sequence {
}},
new Step() { @Override public void execute(RequestMonitor requestMonitor) {
new Service3(fSession).initialize(requestMonitor);
}},
new Step() { @Override public void execute(RequestMonitor requestMonitor) {
new Service4(fSession).initialize(requestMonitor);
}}
};
}