1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.observation; |
21 |
|
|
22 |
|
import java.util.Arrays; |
23 |
|
import java.util.Collections; |
24 |
|
|
25 |
|
import org.junit.Assert; |
26 |
|
import org.junit.Rule; |
27 |
|
import org.junit.Test; |
28 |
|
import org.mockito.Mockito; |
29 |
|
import org.slf4j.Logger; |
30 |
|
import org.xwiki.component.manager.ComponentLookupException; |
31 |
|
import org.xwiki.observation.event.ActionExecutionEvent; |
32 |
|
import org.xwiki.observation.event.AllEvent; |
33 |
|
import org.xwiki.observation.event.Event; |
34 |
|
import org.xwiki.observation.internal.DefaultObservationManager; |
35 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
36 |
|
|
37 |
|
import static org.mockito.ArgumentMatchers.any; |
38 |
|
import static org.mockito.ArgumentMatchers.contains; |
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 |
|
@link |
47 |
|
|
48 |
|
@version |
49 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (103) |
Complexity: 9 |
Complexity Density: 0.1 |
|
50 |
|
public class ObservationManagerTest |
51 |
|
{ |
52 |
|
@Rule |
53 |
|
public final MockitoComponentMockingRule<ObservationManager> mocker = |
54 |
|
new MockitoComponentMockingRule<ObservationManager>(DefaultObservationManager.class); |
55 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
56 |
1 |
@Test... |
57 |
|
public void testNotifyWhenMatching() throws ComponentLookupException |
58 |
|
{ |
59 |
1 |
final EventListener listener = mock(EventListener.class); |
60 |
1 |
final Event event = mock(Event.class); |
61 |
|
|
62 |
1 |
when(listener.getName()).thenReturn("mylistener"); |
63 |
1 |
when(listener.getEvents()).thenReturn(Arrays.asList(event)); |
64 |
1 |
when(event.matches(event)).thenReturn(true); |
65 |
|
|
66 |
1 |
this.mocker.getComponentUnderTest().addListener(listener); |
67 |
1 |
Assert.assertSame(listener, this.mocker.getComponentUnderTest().getListener("mylistener")); |
68 |
1 |
this.mocker.getComponentUnderTest().notify(event, "some source", "some data"); |
69 |
1 |
verify(listener).onEvent(event, "some source", "some data"); |
70 |
|
} |
71 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
72 |
1 |
@Test... |
73 |
|
public void testRemoveListener() throws ComponentLookupException |
74 |
|
{ |
75 |
1 |
final EventListener listener = mock(EventListener.class); |
76 |
1 |
final Event event = mock(Event.class); |
77 |
|
|
78 |
1 |
when(listener.getName()).thenReturn("mylistener"); |
79 |
1 |
when(listener.getEvents()).thenReturn(Arrays.asList(event)); |
80 |
|
|
81 |
1 |
this.mocker.getComponentUnderTest().addListener(listener); |
82 |
1 |
this.mocker.getComponentUnderTest().removeListener("mylistener"); |
83 |
1 |
this.mocker.getComponentUnderTest().notify(event, null); |
84 |
1 |
verify(listener, never()).onEvent(any(Event.class), any(), any()); |
85 |
|
} |
86 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
87 |
1 |
@Test... |
88 |
|
public void testAddEvent() throws Exception |
89 |
|
{ |
90 |
1 |
final EventListener listener = mock(EventListener.class); |
91 |
1 |
final Event initialEvent = mock(Event.class, "initial"); |
92 |
1 |
final Event afterEvent = mock(Event.class, "after"); |
93 |
1 |
final Event notifyEvent = mock(Event.class, "notify"); |
94 |
|
|
95 |
1 |
when(listener.getName()).thenReturn("mylistener"); |
96 |
1 |
when(listener.getEvents()).thenReturn(Arrays.asList(initialEvent)); |
97 |
|
|
98 |
|
|
99 |
|
|
100 |
1 |
when(initialEvent.matches(notifyEvent)).thenReturn(false); |
101 |
1 |
when(afterEvent.matches(notifyEvent)).thenReturn(true); |
102 |
|
|
103 |
1 |
this.mocker.getComponentUnderTest().addListener(listener); |
104 |
1 |
this.mocker.getComponentUnderTest().addEvent("mylistener", afterEvent); |
105 |
1 |
this.mocker.getComponentUnderTest().notify(notifyEvent, null); |
106 |
1 |
verify(listener).onEvent(notifyEvent, null, null); |
107 |
|
} |
108 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
109 |
1 |
@Test... |
110 |
|
public void testAddEventWithNoInitialEvent() throws Exception |
111 |
|
{ |
112 |
1 |
final EventListener listener = mock(EventListener.class); |
113 |
1 |
final Event afterEvent = mock(Event.class, "after"); |
114 |
1 |
final Event notifyEvent = mock(Event.class, "notify"); |
115 |
|
|
116 |
1 |
when(listener.getName()).thenReturn("mylistener"); |
117 |
1 |
when(listener.getEvents()).thenReturn(Collections.<Event>emptyList()); |
118 |
|
|
119 |
1 |
when(afterEvent.matches(notifyEvent)).thenReturn(true); |
120 |
|
|
121 |
1 |
this.mocker.getComponentUnderTest().addListener(listener); |
122 |
1 |
this.mocker.getComponentUnderTest().addEvent("mylistener", afterEvent); |
123 |
1 |
this.mocker.getComponentUnderTest().notify(notifyEvent, null); |
124 |
1 |
verify(listener).onEvent(notifyEvent, null, null); |
125 |
|
} |
126 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
127 |
1 |
@Test... |
128 |
|
public void testRemoveEvent() throws ComponentLookupException |
129 |
|
{ |
130 |
1 |
final EventListener listener = mock(EventListener.class); |
131 |
1 |
final Event initialEvent = mock(Event.class, "initial"); |
132 |
1 |
final Event afterEvent = mock(Event.class, "after"); |
133 |
1 |
final Event notifyEvent = mock(Event.class, "notify"); |
134 |
|
|
135 |
1 |
when(listener.getName()).thenReturn("mylistener"); |
136 |
1 |
when(listener.getEvents()).thenReturn(Arrays.asList(initialEvent)); |
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
1 |
when(initialEvent.matches(same(notifyEvent))).thenReturn(false); |
142 |
|
|
143 |
1 |
this.mocker.getComponentUnderTest().addListener(listener); |
144 |
1 |
this.mocker.getComponentUnderTest().addEvent("mylistener", afterEvent); |
145 |
1 |
this.mocker.getComponentUnderTest().removeEvent("mylistener", afterEvent); |
146 |
1 |
this.mocker.getComponentUnderTest().notify(notifyEvent, null); |
147 |
|
|
148 |
1 |
verify(afterEvent, never()).matches(same(notifyEvent)); |
149 |
1 |
verify(listener, never()).onEvent(afterEvent, null, null); |
150 |
|
} |
151 |
|
|
152 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
153 |
1 |
@Test... |
154 |
|
public void testRegisterSeveralListenersForSameEvent() throws ComponentLookupException |
155 |
|
{ |
156 |
1 |
final EventListener listener1 = mock(EventListener.class, "listener1"); |
157 |
1 |
final EventListener listener2 = mock(EventListener.class, "listener2"); |
158 |
1 |
final Event event = mock(Event.class, "event"); |
159 |
1 |
final Event notifyEvent = mock(Event.class, "notify"); |
160 |
|
|
161 |
1 |
when(listener1.getName()).thenReturn("listener 1"); |
162 |
1 |
when(listener2.getName()).thenReturn("listener 2"); |
163 |
1 |
when(listener1.getEvents()).thenReturn(Arrays.asList(event)); |
164 |
1 |
when(listener2.getEvents()).thenReturn(Arrays.asList(event)); |
165 |
|
|
166 |
1 |
when(event.matches(notifyEvent)).thenReturn(true); |
167 |
|
|
168 |
1 |
this.mocker.getComponentUnderTest().addListener(listener1); |
169 |
1 |
this.mocker.getComponentUnderTest().addListener(listener2); |
170 |
1 |
this.mocker.getComponentUnderTest().notify(notifyEvent, null); |
171 |
1 |
verify(listener1).onEvent(notifyEvent, null, null); |
172 |
1 |
verify(listener2).onEvent(notifyEvent, null, null); |
173 |
|
} |
174 |
|
|
175 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
176 |
1 |
@Test... |
177 |
|
public void testRegisterListenerForAllEvents() throws ComponentLookupException |
178 |
|
{ |
179 |
1 |
final EventListener listener = mock(EventListener.class); |
180 |
1 |
final Event event = mock(Event.class); |
181 |
|
|
182 |
1 |
when(listener.getName()).thenReturn("mylistener"); |
183 |
1 |
when(listener.getEvents()).thenReturn(Arrays.asList(AllEvent.ALLEVENT)); |
184 |
|
|
185 |
1 |
this.mocker.getComponentUnderTest().addListener(listener); |
186 |
1 |
Assert.assertSame(listener, this.mocker.getComponentUnderTest().getListener("mylistener")); |
187 |
1 |
this.mocker.getComponentUnderTest().notify(event, "some source", "some data"); |
188 |
1 |
verify(listener).onEvent(event, "some source", "some data"); |
189 |
|
} |
190 |
|
|
191 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
192 |
1 |
@Test... |
193 |
|
public void testRegisterSameListenerSeveralTimes() throws ComponentLookupException |
194 |
|
{ |
195 |
1 |
final EventListener listener = mock(EventListener.class); |
196 |
|
|
197 |
1 |
when(listener.getName()).thenReturn("mylistener"); |
198 |
1 |
when(listener.getEvents()).thenReturn(Arrays.asList(AllEvent.ALLEVENT)); |
199 |
|
|
200 |
|
|
201 |
1 |
this.mocker.getComponentUnderTest().addListener(listener); |
202 |
|
|
203 |
1 |
this.mocker.getComponentUnderTest().addListener(listener); |
204 |
|
|
205 |
1 |
final Logger logger = this.mocker.getMockedLogger(); |
206 |
1 |
verify(logger).warn(contains("listener is overwritting a previously registered listener"), any(), any(), any()); |
207 |
|
|
208 |
|
|
209 |
1 |
this.mocker.getComponentUnderTest().removeListener("mylistener"); |
210 |
|
|
211 |
1 |
this.mocker.getComponentUnderTest().addListener(listener); |
212 |
1 |
Mockito.verifyNoMoreInteractions(logger); |
213 |
|
} |
214 |
|
|
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
219 |
1 |
@Test... |
220 |
|
public void testRegisterListenerForTwoEventsOfSameType() throws ComponentLookupException |
221 |
|
{ |
222 |
1 |
final EventListener listener = mock(EventListener.class); |
223 |
1 |
@SuppressWarnings("deprecation") |
224 |
|
final Event eventMatcher1 = new ActionExecutionEvent("action1"); |
225 |
1 |
@SuppressWarnings("deprecation") |
226 |
|
final Event eventMatcher2 = new ActionExecutionEvent("action2"); |
227 |
|
|
228 |
1 |
when(listener.getName()).thenReturn("mylistener"); |
229 |
1 |
when(listener.getEvents()).thenReturn(Arrays.asList(eventMatcher1, eventMatcher2)); |
230 |
|
|
231 |
1 |
this.mocker.getComponentUnderTest().addListener(listener); |
232 |
1 |
this.mocker.getComponentUnderTest().notify(eventMatcher1, "some source", "some data"); |
233 |
1 |
this.mocker.getComponentUnderTest().notify(eventMatcher2, "some source", "some data"); |
234 |
1 |
verify(listener).onEvent(eventMatcher1, "some source", "some data"); |
235 |
1 |
verify(listener).onEvent(eventMatcher2, "some source", "some data"); |
236 |
|
} |
237 |
|
} |