The correct way to use / test the event service in Eclipse E4 RCP

Let me ask two related questions that can boil down to one good application design; -)

  • What is the best practice for using event-based communications in the e4 RCP application?
  • How can I write simple unit tests (using JUnit) for classes that send / receive events using dependency injection and IEventBroker?

Let's be more specific: let's say I'm developing an Eclipse e4 RCP application consisting of several plugins that need to exchange data. For communication, I want to use the event service provided by org.eclipse.e4.core.services.events.IEventBroker, so my plugins remain loosely coupled. I use dependency injection to inject an event broker into a class that dispatches events:

@Inject static IEventBroker broker; 
private void sendEvent() {
broker.post(MyEventConstants.SOME_EVENT, payload)
}

On the receiver side, I have a method like:

@Inject
@Optional
private void receiveEvent(@UIEventTopic(MyEventConstants.SOME_EVENT) Object payload) 

Now the questions are:

  • IEventBroker IEclipseContext. , , e4, , , ContextInjectionFactory.inject(myEventSendingObject, context); , , , . E4?

  • JUnit , ( , )? , , . , , . , IEventBroker?

IEclipseContext DI . , , JUnit Plug-in, , PDE unit test. , .

" IEventBroker". , ! , , .

, - , ? , ? / ?

+4
2

JUnit- . , , , - PDE . , , , Tycho. Surefire .

, , , . , , InjectorFactory.

: IEventBroker - ,

public class IEventBrokerMock implements IEventBroker {
    @Override
    // Implemented Methods
}

-

InjectorFactory.getDefault().addBinding(IEventBroker.class).implementedBy(IEventBrokerMock.class);
ClassUnderTest myObject = InjectorFactory.getDefault().make(ClassUnderTest.class, null);

,

IEclipseContext context = EclipseContextFactory.create();
context.set(IEventBroker.class, new IEventBrokerMock());
ClassUnderTest myObject = ContextInjectionFactory.make(ClassUnderTest.class, context);

JUnit, .

+4

, DI, "eventBroker = new org.eclipse.e4.ui.services.internal.events.EventBroker();" eventbroker ,

-1

All Articles