use of com.iluwatar.eda.event.UserUpdatedEvent in project java-design-patterns by iluwatar.
the class EventDispatcherTest method testEventDriverPattern.
/**
* This unit test should register events and event handlers correctly with the event dispatcher
* and events should be dispatched accordingly.
*/
@Test
public void testEventDriverPattern() {
EventDispatcher dispatcher = spy(new EventDispatcher());
UserCreatedEventHandler userCreatedEventHandler = spy(new UserCreatedEventHandler());
UserUpdatedEventHandler userUpdatedEventHandler = spy(new UserUpdatedEventHandler());
dispatcher.registerHandler(UserCreatedEvent.class, userCreatedEventHandler);
dispatcher.registerHandler(UserUpdatedEvent.class, userUpdatedEventHandler);
User user = new User("iluwatar");
UserCreatedEvent userCreatedEvent = new UserCreatedEvent(user);
UserUpdatedEvent userUpdatedEvent = new UserUpdatedEvent(user);
// fire a userCreatedEvent and verify that userCreatedEventHandler has been invoked.
dispatcher.dispatch(userCreatedEvent);
verify(userCreatedEventHandler).onEvent(userCreatedEvent);
verify(dispatcher).dispatch(userCreatedEvent);
// fire a userCreatedEvent and verify that userUpdatedEventHandler has been invoked.
dispatcher.dispatch(userUpdatedEvent);
verify(userUpdatedEventHandler).onEvent(userUpdatedEvent);
verify(dispatcher).dispatch(userUpdatedEvent);
}
use of com.iluwatar.eda.event.UserUpdatedEvent in project java-design-patterns by iluwatar.
the class App method main.
/**
* Once the {@link EventDispatcher} is initialised, handlers related to specific events have to be
* made known to the dispatcher by registering them. In this case the {@link UserCreatedEvent} is
* bound to the UserCreatedEventHandler, whilst the {@link UserUpdatedEvent} is bound to the
* {@link UserUpdatedEventHandler}. The dispatcher can now be called to dispatch specific events.
* When a user is saved, the {@link UserCreatedEvent} can be dispatched.
* On the other hand, when a user is updated, {@link UserUpdatedEvent} can be dispatched.
*/
public static void main(String[] args) {
EventDispatcher dispatcher = new EventDispatcher();
dispatcher.registerHandler(UserCreatedEvent.class, new UserCreatedEventHandler());
dispatcher.registerHandler(UserUpdatedEvent.class, new UserUpdatedEventHandler());
User user = new User("iluwatar");
dispatcher.dispatch(new UserCreatedEvent(user));
dispatcher.dispatch(new UserUpdatedEvent(user));
}
Aggregations