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

File ObservationManagerEventListenerTest.java

 

Code metrics

0
18
3
1
99
59
3
0.17
6
3
1

Classes

Class Line # Actions
ObservationManagerEventListenerTest 47 18 0% 3 0
1.0100%
 

Contributing tests

This file is covered by 2 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   
24    import org.junit.jupiter.api.BeforeEach;
25    import org.junit.jupiter.api.Test;
26    import org.xwiki.component.descriptor.DefaultComponentDescriptor;
27    import org.xwiki.component.internal.StackingComponentEventManager;
28    import org.xwiki.component.manager.ComponentManager;
29    import org.xwiki.component.manager.ComponentRepositoryException;
30    import org.xwiki.observation.event.Event;
31    import org.xwiki.observation.internal.DefaultObservationManager;
32    import org.xwiki.test.junit5.mockito.ComponentTest;
33    import org.xwiki.test.junit5.mockito.InjectComponentManager;
34    import org.xwiki.test.junit5.mockito.InjectMockComponents;
35   
36    import static org.junit.jupiter.api.Assertions.assertNull;
37    import static org.junit.jupiter.api.Assertions.assertSame;
38    import static org.mockito.Mockito.mock;
39    import static org.mockito.Mockito.when;
40   
41    /**
42    * Unit tests for {@link ObservationManager}.
43    *
44    * @version $Id: d0b2edc173654596ff681d8e773f109e58aac6df $
45    */
46    @ComponentTest
 
47    public class ObservationManagerEventListenerTest
48    {
49    @InjectMockComponents
50    private DefaultObservationManager manager;
51   
52    @InjectComponentManager
53    private ComponentManager componentManager;
54   
55    private EventListener eventListenerMock;
56   
57    private Event eventMock;
58   
59    private DefaultComponentDescriptor<EventListener> componentDescriptor;
60   
 
61  2 toggle @BeforeEach
62    public void setUp()
63    {
64  2 StackingComponentEventManager componentEventManager = new StackingComponentEventManager();
65  2 componentEventManager.shouldStack(false);
66  2 componentEventManager.setObservationManager(this.manager);
67  2 this.componentManager.setComponentEventManager(componentEventManager);
68   
69  2 this.eventListenerMock = mock(EventListener.class);
70  2 this.eventMock = mock(Event.class);
71   
72  2 this.componentDescriptor = new DefaultComponentDescriptor<>();
73  2 this.componentDescriptor.setImplementation(this.eventListenerMock.getClass());
74  2 this.componentDescriptor.setRoleType(EventListener.class);
75  2 this.componentDescriptor.setRoleHint("mylistener");
76   
77  2 when(this.eventMock.matches(this.eventMock)).thenReturn(true);
78  2 when(this.eventListenerMock.getName()).thenReturn("mylistener");
79  2 when(this.eventListenerMock.getEvents()).thenReturn(Arrays.asList(this.eventMock));
80    }
81   
 
82  1 toggle @Test
83    public void newListenerComponent() throws ComponentRepositoryException
84    {
85  1 this.componentManager.registerComponent(this.componentDescriptor, this.eventListenerMock);
86   
87  1 assertSame(this.eventListenerMock, this.manager.getListener("mylistener"));
88    }
89   
 
90  1 toggle @Test
91    public void removedListenerComponent() throws Exception
92    {
93  1 this.componentManager.registerComponent(this.componentDescriptor, this.eventListenerMock);
94  1 this.componentManager.unregisterComponent(this.componentDescriptor.getRoleType(),
95    this.componentDescriptor.getRoleHint());
96   
97  1 assertNull(this.manager.getListener("mylistener"));
98    }
99    }