Search in sources :

Example 1 with UserUpdatedEventHandler

use of com.iluwatar.eda.handler.UserUpdatedEventHandler 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);
}
Also used : UserCreatedEvent(com.iluwatar.eda.event.UserCreatedEvent) User(com.iluwatar.eda.model.User) UserCreatedEventHandler(com.iluwatar.eda.handler.UserCreatedEventHandler) UserUpdatedEvent(com.iluwatar.eda.event.UserUpdatedEvent) UserUpdatedEventHandler(com.iluwatar.eda.handler.UserUpdatedEventHandler) Test(org.junit.jupiter.api.Test)

Example 2 with UserUpdatedEventHandler

use of com.iluwatar.eda.handler.UserUpdatedEventHandler 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));
}
Also used : UserCreatedEvent(com.iluwatar.eda.event.UserCreatedEvent) EventDispatcher(com.iluwatar.eda.framework.EventDispatcher) User(com.iluwatar.eda.model.User) UserCreatedEventHandler(com.iluwatar.eda.handler.UserCreatedEventHandler) UserUpdatedEvent(com.iluwatar.eda.event.UserUpdatedEvent) UserUpdatedEventHandler(com.iluwatar.eda.handler.UserUpdatedEventHandler)

Aggregations

UserCreatedEvent (com.iluwatar.eda.event.UserCreatedEvent)2 UserUpdatedEvent (com.iluwatar.eda.event.UserUpdatedEvent)2 UserCreatedEventHandler (com.iluwatar.eda.handler.UserCreatedEventHandler)2 UserUpdatedEventHandler (com.iluwatar.eda.handler.UserUpdatedEventHandler)2 User (com.iluwatar.eda.model.User)2 EventDispatcher (com.iluwatar.eda.framework.EventDispatcher)1 Test (org.junit.jupiter.api.Test)1