1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.component.wiki

File DefaultWikiComponentManagerEventListenerTest.java

 

Code metrics

0
39
9
1
167
110
9
0.23
4.33
9
1

Classes

Class Line # Actions
DefaultWikiComponentManagerEventListenerTest 48 39 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   
24    import org.junit.Before;
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.xwiki.bridge.DocumentModelBridge;
28    import org.xwiki.bridge.event.ApplicationReadyEvent;
29    import org.xwiki.bridge.event.DocumentCreatedEvent;
30    import org.xwiki.bridge.event.DocumentDeletedEvent;
31    import org.xwiki.bridge.event.DocumentUpdatedEvent;
32    import org.xwiki.component.wiki.internal.DefaultWikiComponent;
33    import org.xwiki.component.wiki.internal.DefaultWikiComponentManagerEventListener;
34    import org.xwiki.component.wiki.internal.WikiComponentConstants;
35    import org.xwiki.model.reference.DocumentReference;
36    import org.xwiki.observation.EventListener;
37    import org.xwiki.observation.event.Event;
38    import org.xwiki.test.mockito.MockitoComponentMockingRule;
39   
40    import static org.mockito.ArgumentMatchers.*;
41    import static org.mockito.Mockito.*;
42   
43    /**
44    * Unit tests for {@link DefaultWikiComponentManagerEventListener}.
45    *
46    * @version $Id: 56a01d0bddd99b1fd30c1a093e3ecce855dd72bc $
47    */
 
48    public class DefaultWikiComponentManagerEventListenerTest implements WikiComponentConstants
49    {
50    private static final String ROLE_HINT = "roleHint";
51   
52    private static final DocumentReference DOC_REFERENCE = new DocumentReference("xwiki", "XWiki", "MyComponent");
53   
54    private static final DocumentReference AUTHOR_REFERENCE = new DocumentReference("xwiki", "XWiki", "Admin");
55   
56    @Rule
57    public MockitoComponentMockingRule<EventListener> mocker = new MockitoComponentMockingRule<EventListener>(
58    DefaultWikiComponentManagerEventListener.class);
59   
60    private WikiComponentBuilder wikiComponentBuilder;
61   
62    private WikiComponentManager wikiComponentManager;
63   
 
64  7 toggle @Before
65    public void setUp() throws Exception
66    {
67  7 wikiComponentBuilder = mocker.registerMockComponent(WikiComponentBuilder.class);
68  7 wikiComponentManager = mocker.getInstance(WikiComponentManager.class);
69    }
70   
 
71  1 toggle @Test
72    public void onEventWhenSourceIsNotAXWikiDocumentAndNoMatchingEvent() throws Exception
73    {
74  1 mocker.getComponentUnderTest().onEvent(null, null, null);
75   
76  1 verify(wikiComponentManager, never()).registerWikiComponent(any(WikiComponent.class));
77  1 verify(wikiComponentManager, never()).unregisterWikiComponents(any(DocumentReference.class));
78    }
79   
 
80  1 toggle @Test
81    public void onEventWhenSourceDocumentButNoMatchingEvent() throws Exception
82    {
83  1 DocumentModelBridge componentDocument = mock(DocumentModelBridge.class);
84  1 when(componentDocument.getDocumentReference()).thenReturn(DOC_REFERENCE);
85   
86  1 mocker.getComponentUnderTest().onEvent(null, componentDocument, null);
87   
88  1 verify(wikiComponentManager, never()).registerWikiComponent(any(WikiComponent.class));
89  1 verify(wikiComponentManager, never()).unregisterWikiComponents(any(DocumentReference.class));
90    }
91   
 
92  1 toggle @Test
93    public void onDocumentCreated() throws Exception
94    {
95  1 onDocumentCreatedOrUpdated(new DocumentCreatedEvent(DOC_REFERENCE));
96    }
97   
 
98  1 toggle @Test
99    public void onDocumentUpdated() throws Exception
100    {
101  1 onDocumentCreatedOrUpdated(new DocumentUpdatedEvent(DOC_REFERENCE));
102    }
103   
 
104  1 toggle @Test
105    public void onDocumentUpdatedWhenTwoComponents() throws Exception
106    {
107  1 DocumentModelBridge componentDocument = mock(DocumentModelBridge.class);
108  1 when(componentDocument.getDocumentReference()).thenReturn(DOC_REFERENCE);
109  1 when(wikiComponentBuilder.getDocumentReferences()).thenReturn(Arrays.asList(DOC_REFERENCE));
110   
111  1 WikiComponent component =
112    new DefaultWikiComponent(DOC_REFERENCE, AUTHOR_REFERENCE, TestRole.class, ROLE_HINT,
113    WikiComponentScope.WIKI);
114  1 when(wikiComponentBuilder.buildComponents(DOC_REFERENCE)).thenReturn(Arrays.asList(component, component));
115   
116  1 mocker.getComponentUnderTest().onEvent(new DocumentUpdatedEvent(DOC_REFERENCE), componentDocument, null);
117   
118  1 verify(wikiComponentManager, times(1)).unregisterWikiComponents(DOC_REFERENCE);
119  1 verify(wikiComponentManager, times(2)).registerWikiComponent(component);
120    }
121   
 
122  1 toggle @Test
123    public void onDocumentDeleted() throws Exception
124    {
125  1 DocumentModelBridge componentDocument = mock(DocumentModelBridge.class);
126  1 when(componentDocument.getDocumentReference()).thenReturn(DOC_REFERENCE);
127   
128  1 mocker.getComponentUnderTest().onEvent(new DocumentDeletedEvent(DOC_REFERENCE), componentDocument, null);
129   
130  1 verify(wikiComponentManager, times(1)).unregisterWikiComponents(DOC_REFERENCE);
131    }
132   
 
133  1 toggle @Test
134    public void onApplicationReady() throws Exception
135    {
136  1 DocumentModelBridge componentDocument = mock(DocumentModelBridge.class);
137  1 when(componentDocument.getDocumentReference()).thenReturn(DOC_REFERENCE);
138  1 when(wikiComponentBuilder.getDocumentReferences()).thenReturn(Arrays.asList(DOC_REFERENCE));
139   
140  1 WikiComponent component =
141    new DefaultWikiComponent(DOC_REFERENCE, AUTHOR_REFERENCE, TestRole.class, ROLE_HINT,
142    WikiComponentScope.WIKI);
143  1 when(wikiComponentBuilder.buildComponents(DOC_REFERENCE)).thenReturn(Arrays.asList(component));
144   
145  1 mocker.getComponentUnderTest().onEvent(new ApplicationReadyEvent(), null, null);
146   
147  1 verify(wikiComponentManager, times(1)).registerWikiComponent(component);
148    }
149   
 
150  2 toggle private void onDocumentCreatedOrUpdated(Event event) throws Exception
151    {
152  2 DocumentModelBridge componentDocument = mock(DocumentModelBridge.class);
153  2 when(componentDocument.getDocumentReference()).thenReturn(DOC_REFERENCE);
154  2 when(wikiComponentBuilder.getDocumentReferences()).thenReturn(Arrays.asList(DOC_REFERENCE));
155   
156  2 WikiComponent component =
157    new DefaultWikiComponent(DOC_REFERENCE, AUTHOR_REFERENCE, TestRole.class, ROLE_HINT,
158    WikiComponentScope.WIKI);
159  2 when(wikiComponentBuilder.buildComponents(DOC_REFERENCE)).thenReturn(Arrays.asList(component));
160   
161  2 mocker.getComponentUnderTest().onEvent(event, componentDocument, null);
162   
163  2 verify(wikiComponentManager, times(1)).unregisterWikiComponents(DOC_REFERENCE);
164  2 verify(wikiComponentManager, times(1)).registerWikiComponent(component);
165    }
166   
167    }