Search in sources :

Example 1 with EventListener

use of com.enonic.xp.event.EventListener in project xp by enonic.

the class EventMulticasterTest method testPublishOneListener.

@Test
public void testPublishOneListener() {
    final EventListener listener = mock(EventListener.class);
    this.multicaster.add(listener);
    final Event event = Event.create("test").build();
    this.multicaster.publish(event);
    this.multicaster.remove(listener);
    this.multicaster.publish(event);
    verify(listener, times(1)).onEvent(event);
}
Also used : Event(com.enonic.xp.event.Event) EventListener(com.enonic.xp.event.EventListener) Test(org.junit.jupiter.api.Test)

Example 2 with EventListener

use of com.enonic.xp.event.EventListener in project xp by enonic.

the class EventMulticasterTest method testListenerOrder.

@Test
public void testListenerOrder() {
    final EventListener listener1 = mock(EventListener.class);
    when(listener1.getOrder()).thenReturn(Integer.MIN_VALUE);
    when(listener1.toString()).thenReturn("listener1");
    final EventListener listener2 = mock(EventListener.class);
    when(listener2.getOrder()).thenReturn(200);
    when(listener2.toString()).thenReturn("listener2");
    final EventListener listener3 = mock(EventListener.class);
    when(listener3.getOrder()).thenReturn(Integer.MAX_VALUE);
    when(listener3.toString()).thenReturn("listener3");
    this.multicaster.add(listener2);
    this.multicaster.add(listener1);
    this.multicaster.add(listener3);
    final Event event = Event.create("test").build();
    this.multicaster.publish(event);
    InOrder inOrder = inOrder(listener1, listener2, listener3);
    inOrder.verify(listener1, times(1)).onEvent(event);
    inOrder.verify(listener2, times(1)).onEvent(event);
    inOrder.verify(listener3, times(1)).onEvent(event);
}
Also used : InOrder(org.mockito.InOrder) Event(com.enonic.xp.event.Event) EventListener(com.enonic.xp.event.EventListener) Test(org.junit.jupiter.api.Test)

Example 3 with EventListener

use of com.enonic.xp.event.EventListener in project xp by enonic.

the class EventMulticasterTest method testPublishExceptionOnListener.

@Test
public void testPublishExceptionOnListener() {
    final EventListener listener1 = mock(EventListener.class);
    this.multicaster.add(listener1);
    final EventListener listener2 = mock(EventListener.class);
    this.multicaster.add(listener2);
    final EventListener listener3 = mock(EventListener.class);
    this.multicaster.add(listener3);
    doThrow(new RuntimeException("Error")).when(listener2).onEvent(Mockito.any());
    final Event event = Event.create("test").build();
    this.multicaster.publish(event);
    verify(listener1, times(1)).onEvent(event);
    verify(listener2, times(1)).onEvent(event);
    verify(listener3, times(1)).onEvent(event);
}
Also used : Event(com.enonic.xp.event.Event) EventListener(com.enonic.xp.event.EventListener) Test(org.junit.jupiter.api.Test)

Example 4 with EventListener

use of com.enonic.xp.event.EventListener in project xp by enonic.

the class EventMulticasterTest method testPublishMultipleListeners.

@Test
public void testPublishMultipleListeners() {
    final EventListener listener1 = mock(EventListener.class);
    this.multicaster.add(listener1);
    final EventListener listener2 = mock(EventListener.class);
    this.multicaster.add(listener2);
    final EventListener listener3 = mock(EventListener.class);
    this.multicaster.add(listener3);
    final Event event = Event.create("test").build();
    this.multicaster.publish(event);
    verify(listener1, times(1)).onEvent(event);
    verify(listener2, times(1)).onEvent(event);
    verify(listener3, times(1)).onEvent(event);
}
Also used : Event(com.enonic.xp.event.Event) EventListener(com.enonic.xp.event.EventListener) Test(org.junit.jupiter.api.Test)

Example 5 with EventListener

use of com.enonic.xp.event.EventListener in project xp by enonic.

the class EventPublisherImplTest method testPublish_withListener.

@Test
public void testPublish_withListener() throws Exception {
    final EventListener listener = mock(EventListener.class);
    this.publisher.addListener(listener);
    final Event event = Event.create("test").build();
    for (int i = 0; i < 100; i++) {
        this.publisher.publish(event);
    }
    verify(listener, times(100)).onEvent(Mockito.any());
}
Also used : Event(com.enonic.xp.event.Event) EventListener(com.enonic.xp.event.EventListener) Test(org.junit.jupiter.api.Test)

Aggregations

Event (com.enonic.xp.event.Event)5 EventListener (com.enonic.xp.event.EventListener)5 Test (org.junit.jupiter.api.Test)5 InOrder (org.mockito.InOrder)1