1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.observation

File ObservationManagerTest.java

 

Code metrics

0
92
9
1
244
159
9
0.1
10.22
9
1

Classes

Class Line # Actions
ObservationManagerTest 51 92 0% 9 0
1.0100%
 

Contributing tests

This file is covered by 9 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.observation;
21   
22    import java.util.Arrays;
23    import java.util.Collections;
24   
25    import org.junit.jupiter.api.Test;
26    import org.junit.jupiter.api.extension.RegisterExtension;
27    import org.xwiki.observation.event.ActionExecutionEvent;
28    import org.xwiki.observation.event.AllEvent;
29    import org.xwiki.observation.event.Event;
30    import org.xwiki.observation.internal.DefaultObservationManager;
31    import org.xwiki.test.LogLevel;
32    import org.xwiki.test.junit5.LogCaptureExtension;
33    import org.xwiki.test.junit5.mockito.ComponentTest;
34    import org.xwiki.test.junit5.mockito.InjectMockComponents;
35   
36    import static org.junit.jupiter.api.Assertions.assertSame;
37    import static org.junit.jupiter.api.Assertions.assertTrue;
38    import static org.mockito.ArgumentMatchers.any;
39    import static org.mockito.ArgumentMatchers.same;
40    import static org.mockito.Mockito.mock;
41    import static org.mockito.Mockito.never;
42    import static org.mockito.Mockito.verify;
43    import static org.mockito.Mockito.when;
44   
45    /**
46    * Unit tests for {@link ObservationManager}.
47    *
48    * @version $Id: b9825aebaae9a70ab2e45c7833fbb790f2b62667 $
49    */
50    @ComponentTest
 
51    public class ObservationManagerTest
52    {
53    @InjectMockComponents
54    private DefaultObservationManager manager;
55   
56    @RegisterExtension
57    LogCaptureExtension logCapture = new LogCaptureExtension(LogLevel.WARN);
58   
 
59  1 toggle @Test
60    public void notifyWhenMatching()
61    {
62  1 EventListener listener = mock(EventListener.class);
63  1 Event event = mock(Event.class);
64   
65  1 when(listener.getName()).thenReturn("mylistener");
66  1 when(listener.getEvents()).thenReturn(Arrays.asList(event));
67  1 when(event.matches(event)).thenReturn(true);
68   
69  1 this.manager.addListener(listener);
70  1 assertSame(listener, this.manager.getListener("mylistener"));
71  1 this.manager.notify(event, "some source", "some data");
72  1 verify(listener).onEvent(event, "some source", "some data");
73    }
74   
 
75  1 toggle @Test
76    public void removeListener()
77    {
78  1 EventListener listener = mock(EventListener.class);
79  1 Event event = mock(Event.class);
80   
81  1 when(listener.getName()).thenReturn("mylistener");
82  1 when(listener.getEvents()).thenReturn(Arrays.asList(event));
83   
84  1 this.manager.addListener(listener);
85  1 this.manager.removeListener("mylistener");
86  1 this.manager.notify(event, null);
87  1 verify(listener, never()).onEvent(any(Event.class), any(), any());
88    }
89   
 
90  1 toggle @Test
91    public void addEvent()
92    {
93  1 EventListener listener = mock(EventListener.class);
94  1 Event initialEvent = mock(Event.class, "initial");
95  1 Event afterEvent = mock(Event.class, "after");
96  1 Event notifyEvent = mock(Event.class, "notify");
97   
98  1 when(listener.getName()).thenReturn("mylistener");
99  1 when(listener.getEvents()).thenReturn(Arrays.asList(initialEvent));
100   
101    // Since the observation returns the first matching event, return false from initialEvent so that
102    // afterEvent is called.
103  1 when(initialEvent.matches(notifyEvent)).thenReturn(false);
104  1 when(afterEvent.matches(notifyEvent)).thenReturn(true);
105   
106  1 this.manager.addListener(listener);
107  1 this.manager.addEvent("mylistener", afterEvent);
108  1 this.manager.notify(notifyEvent, null);
109  1 verify(listener).onEvent(notifyEvent, null, null);
110    }
111   
 
112  1 toggle @Test
113    public void addEventWithNoInitialEvent()
114    {
115  1 EventListener listener = mock(EventListener.class);
116  1 Event afterEvent = mock(Event.class, "after");
117  1 Event notifyEvent = mock(Event.class, "notify");
118   
119  1 when(listener.getName()).thenReturn("mylistener");
120  1 when(listener.getEvents()).thenReturn(Collections.<Event>emptyList());
121   
122  1 when(afterEvent.matches(notifyEvent)).thenReturn(true);
123   
124  1 this.manager.addListener(listener);
125  1 this.manager.addEvent("mylistener", afterEvent);
126  1 this.manager.notify(notifyEvent, null);
127  1 verify(listener).onEvent(notifyEvent, null, null);
128    }
129   
 
130  1 toggle @Test
131    public void removeEvent()
132    {
133  1 EventListener listener = mock(EventListener.class);
134  1 Event initialEvent = mock(Event.class, "initial");
135  1 Event afterEvent = mock(Event.class, "after");
136  1 Event notifyEvent = mock(Event.class, "notify");
137   
138  1 when(listener.getName()).thenReturn("mylistener");
139  1 when(listener.getEvents()).thenReturn(Arrays.asList(initialEvent));
140   
141    // Since the observation returns the first matching event, return false from initialEvent so that
142    // the second event can be called (if there's a second event - in our case it'll be removed but
143    // we still want the test to fail if that doesn't work).
144  1 when(initialEvent.matches(same(notifyEvent))).thenReturn(false);
145   
146  1 this.manager.addListener(listener);
147  1 this.manager.addEvent("mylistener", afterEvent);
148  1 this.manager.removeEvent("mylistener", afterEvent);
149  1 this.manager.notify(notifyEvent, null);
150    // Ensure that the afterEvent is never called since we're adding it and removing it
151  1 verify(afterEvent, never()).matches(same(notifyEvent));
152  1 verify(listener, never()).onEvent(afterEvent, null, null);
153    }
154   
155    /**
156    * Verify that we can register two listeners on the same event and they'll both receive the event.
157    */
 
158  1 toggle @Test
159    public void registerSeveralListenersForSameEvent()
160    {
161  1 EventListener listener1 = mock(EventListener.class, "listener1");
162  1 EventListener listener2 = mock(EventListener.class, "listener2");
163  1 Event event = mock(Event.class, "event");
164  1 Event notifyEvent = mock(Event.class, "notify");
165   
166  1 when(listener1.getName()).thenReturn("listener 1");
167  1 when(listener2.getName()).thenReturn("listener 2");
168  1 when(listener1.getEvents()).thenReturn(Arrays.asList(event));
169  1 when(listener2.getEvents()).thenReturn(Arrays.asList(event));
170   
171  1 when(event.matches(notifyEvent)).thenReturn(true);
172   
173  1 this.manager.addListener(listener1);
174  1 this.manager.addListener(listener2);
175  1 this.manager.notify(notifyEvent, null);
176  1 verify(listener1).onEvent(notifyEvent, null, null);
177  1 verify(listener2).onEvent(notifyEvent, null, null);
178    }
179   
180    /**
181    * Verify that we can register a listener to receive any kind of event using AllEvent event type.
182    */
 
183  1 toggle @Test
184    public void registerListenerForAllEvents()
185    {
186  1 EventListener listener = mock(EventListener.class);
187  1 Event event = mock(Event.class);
188   
189  1 when(listener.getName()).thenReturn("mylistener");
190  1 when(listener.getEvents()).thenReturn(Arrays.asList(AllEvent.ALLEVENT));
191   
192  1 this.manager.addListener(listener);
193  1 assertSame(listener, this.manager.getListener("mylistener"));
194  1 this.manager.notify(event, "some source", "some data");
195  1 verify(listener).onEvent(event, "some source", "some data");
196    }
197   
198    /**
199    * Verify that a warning is logged is we try to register a listener with the same name.
200    */
 
201  1 toggle @Test
202    public void registerSameListenerSeveralTimes()
203    {
204  1 EventListener listener = mock(EventListener.class);
205   
206  1 when(listener.getName()).thenReturn("mylistener");
207  1 when(listener.getEvents()).thenReturn(Arrays.asList(AllEvent.ALLEVENT));
208    // The check is performed here, we verify that a warning is correctly logged
209   
210  1 this.manager.addListener(listener);
211    // Will raise log warning on the next line
212  1 this.manager.addListener(listener);
213   
214  1 assertTrue(this.logCapture.getMessage(0).matches("The \\[.*\\] listener is overwriting a previously registered "
215    + "listener \\[.*\\] since they both are registered under the same id \\[mylistener\\]\\. In the future "
216    + "consider removing a Listener first if you really want to register it again\\."));
217   
218    // Verify that no log is logged if we remove the listener before re-registering it
219  1 this.manager.removeListener("mylistener");
220    // Next line will not log any warning
221  1 this.manager.addListener(listener);
222    }
223   
224    /**
225    * Verify that we can register a listener with two events of the same type but with not matching the same thing and
226    * it will receive both events.
227    */
 
228  1 toggle @Test
229    public void registerListenerForTwoEventsOfSameType()
230    {
231  1 EventListener listener = mock(EventListener.class);
232  1 Event eventMatcher1 = new ActionExecutionEvent("action1");
233  1 Event eventMatcher2 = new ActionExecutionEvent("action2");
234   
235  1 when(listener.getName()).thenReturn("mylistener");
236  1 when(listener.getEvents()).thenReturn(Arrays.asList(eventMatcher1, eventMatcher2));
237   
238  1 this.manager.addListener(listener);
239  1 this.manager.notify(eventMatcher1, "some source", "some data");
240  1 this.manager.notify(eventMatcher2, "some source", "some data");
241  1 verify(listener).onEvent(eventMatcher1, "some source", "some data");
242  1 verify(listener).onEvent(eventMatcher2, "some source", "some data");
243    }
244    }