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

File DefaultWikiComponentManagerEventListenerTest.java

 

Code metrics

0
36
9
1
178
115
9
0.25
4
9
1

Classes

Class Line # Actions
DefaultWikiComponentManagerEventListenerTest 51 36 0% 9 0
1.0100%
 

Contributing tests

This file is covered by 7 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.component.wiki;
21   
22    import java.util.Arrays;
23    import java.util.List;
24   
25    import org.junit.Before;
26    import org.junit.Rule;
27    import org.junit.Test;
28    import org.xwiki.bridge.DocumentModelBridge;
29    import org.xwiki.bridge.event.ApplicationReadyEvent;
30    import org.xwiki.bridge.event.DocumentCreatedEvent;
31    import org.xwiki.bridge.event.DocumentDeletedEvent;
32    import org.xwiki.bridge.event.DocumentUpdatedEvent;
33    import org.xwiki.component.manager.ComponentManager;
34    import org.xwiki.component.wiki.internal.DefaultWikiComponentManagerEventListener;
35    import org.xwiki.component.wiki.internal.WikiComponentConstants;
36    import org.xwiki.component.wiki.internal.WikiComponentManagerEventListenerHelper;
37    import org.xwiki.model.reference.DocumentReference;
38    import org.xwiki.model.reference.EntityReference;
39    import org.xwiki.observation.EventListener;
40    import org.xwiki.observation.event.Event;
41    import org.xwiki.test.mockito.MockitoComponentMockingRule;
42   
43    import static org.mockito.ArgumentMatchers.*;
44    import static org.mockito.Mockito.*;
45   
46    /**
47    * Unit tests for {@link DefaultWikiComponentManagerEventListener}.
48    *
49    * @version $Id: 710be0d0d135e6d2981b8a2ba35e704f58ce3b34 $
50    */
 
51    public class DefaultWikiComponentManagerEventListenerTest implements WikiComponentConstants
52    {
53    private static final DocumentReference DOC_REFERENCE = new DocumentReference("xwiki", "XWiki", "MyComponent");
54   
55    @Rule
56    public MockitoComponentMockingRule<EventListener> mocker = new MockitoComponentMockingRule<EventListener>(
57    DefaultWikiComponentManagerEventListener.class);
58   
59    private WikiComponentManagerEventListenerHelper wikiComponentManagerEventListenerHelper;
60   
61    private ComponentManager componentManager;
62   
63    private WikiComponentBuilder wikiComponentBuilder;
64   
65    private WikiComponent wikiComponent;
66   
 
67  7 toggle @Before
68    public void setUp() throws Exception
69    {
70  7 this.wikiComponentManagerEventListenerHelper =
71    this.mocker.registerMockComponent(WikiComponentManagerEventListenerHelper.class);
72   
73  7 this.wikiComponent = mock(WikiComponent.class);
74   
75  7 this.wikiComponentBuilder = mock(WikiComponentBuilder.class);
76  7 when(this.wikiComponentBuilder.getDocumentReferences()).thenReturn(Arrays.asList(DOC_REFERENCE));
77  7 when(this.wikiComponentBuilder.buildComponents(DOC_REFERENCE)).thenReturn(Arrays.asList(this.wikiComponent));
78   
79  7 this.componentManager = this.mocker.registerMockComponent(ComponentManager.class);
80  7 when(this.componentManager.getInstanceList(WikiComponentBuilder.class))
81    .thenReturn(Arrays.asList(this.wikiComponentBuilder));
82    }
83   
 
84  1 toggle @Test
85    public void onEventWhenSourceIsNotAXWikiDocumentAndNoMatchingEvent() throws Exception
86    {
87  1 mocker.getComponentUnderTest().onEvent(null, null, null);
88   
89  1 verify(this.wikiComponentManagerEventListenerHelper, never()).registerComponentList(any());
90  1 verify(this.wikiComponentManagerEventListenerHelper, never()).unregisterComponents(any(EntityReference.class));
91    }
92   
 
93  1 toggle @Test
94    public void onEventWhenSourceDocumentButNoMatchingEvent() throws Exception
95    {
96  1 DocumentModelBridge componentDocument = mock(DocumentModelBridge.class);
97  1 when(componentDocument.getDocumentReference()).thenReturn(DOC_REFERENCE);
98   
99  1 mocker.getComponentUnderTest().onEvent(null, componentDocument, null);
100   
101  1 verify(this.wikiComponentManagerEventListenerHelper, never()).registerComponentList(any());
102  1 verify(this.wikiComponentManagerEventListenerHelper, never()).unregisterComponents(any(EntityReference.class));
103    }
104   
 
105  1 toggle @Test
106    public void onDocumentCreated() throws Exception
107    {
108  1 onDocumentCreatedOrUpdated(new DocumentCreatedEvent(DOC_REFERENCE));
109   
110  1 verify(this.wikiComponentManagerEventListenerHelper, times(2))
111    .registerComponentList(Arrays.asList(this.wikiComponent));
112  1 verify(this.wikiComponentManagerEventListenerHelper, times(1))
113    .unregisterComponents(any());
114    }
115   
 
116  1 toggle @Test
117    public void onDocumentUpdated() throws Exception
118    {
119  1 onDocumentCreatedOrUpdated(new DocumentUpdatedEvent(DOC_REFERENCE));
120   
121  1 verify(this.wikiComponentManagerEventListenerHelper, times(2))
122    .registerComponentList(Arrays.asList(this.wikiComponent));
123  1 verify(this.wikiComponentManagerEventListenerHelper, times(1))
124    .unregisterComponents(DOC_REFERENCE);
125    }
126   
 
127  1 toggle @Test
128    public void onDocumentUpdatedWhenTwoComponents() throws Exception
129    {
130    // We create a second WikiComponent and register it against our standard WikiComponentBuilder
131  1 WikiComponent secondWikiComponent = mock(WikiComponent.class);
132  1 when(this.wikiComponentBuilder.buildComponents(DOC_REFERENCE))
133    .thenReturn(Arrays.asList(this.wikiComponent, secondWikiComponent));
134   
135  1 this.onDocumentCreatedOrUpdated(new DocumentUpdatedEvent(DOC_REFERENCE));
136   
137  1 verify(this.wikiComponentManagerEventListenerHelper, times(2))
138    .registerComponentList(Arrays.asList(this.wikiComponent, secondWikiComponent));
139  1 verify(this.wikiComponentManagerEventListenerHelper, times(1))
140    .unregisterComponents(DOC_REFERENCE);
141    }
142   
 
143  1 toggle @Test
144    public void onDocumentDeleted() throws Exception
145    {
146  1 DocumentModelBridge componentDocument = mock(DocumentModelBridge.class);
147  1 when(componentDocument.getDocumentReference()).thenReturn(DOC_REFERENCE);
148   
149  1 mocker.getComponentUnderTest().onEvent(new DocumentDeletedEvent(DOC_REFERENCE), componentDocument, null);
150   
151  1 verify(this.wikiComponentManagerEventListenerHelper, times(1))
152    .unregisterComponents(DOC_REFERENCE);
153    }
154   
 
155  1 toggle @Test
156    public void onApplicationReady() throws Exception
157    {
158  1 mocker.getComponentUnderTest().onEvent(new ApplicationReadyEvent(), null, null);
159   
160  1 verify(this.wikiComponentManagerEventListenerHelper, times(1))
161    .registerComponentList(any());
162    }
163   
 
164  3 toggle private void onDocumentCreatedOrUpdated(Event event) throws Exception
165    {
166  3 DocumentModelBridge componentDocument = mock(DocumentModelBridge.class);
167  3 when(componentDocument.getDocumentReference()).thenReturn(DOC_REFERENCE);
168   
169    /**
170    * Here, {@link WikiComponentManagerEventListenerHelper#registerComponentList(List)} is called two times
171    * because we have to "initialize" the tested event listener with an ApplicationReadyEvent() before sending
172    * our custom event. Therefore, the tested WikiComponent will be registered two times.
173    */
174  3 mocker.getComponentUnderTest().onEvent(new ApplicationReadyEvent(), null, null);
175  3 mocker.getComponentUnderTest().onEvent(event, componentDocument, null);
176    }
177   
178    }