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

File DefaultWikiObjectComponentManagerEventListenerTest.java

 

Code metrics

0
55
11
1
230
161
11
0.2
5
11
1

Classes

Class Line # Actions
DefaultWikiObjectComponentManagerEventListenerTest 64 55 0% 11 0
1.0100%
 

Contributing tests

This file is covered by 8 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.Collections;
24    import java.util.HashMap;
25    import java.util.List;
26    import java.util.Map;
27   
28    import org.junit.Before;
29    import org.junit.Rule;
30    import org.junit.Test;
31    import org.xwiki.bridge.event.AbstractDocumentEvent;
32    import org.xwiki.bridge.event.DocumentCreatedEvent;
33    import org.xwiki.bridge.event.DocumentDeletedEvent;
34    import org.xwiki.bridge.event.DocumentUpdatedEvent;
35    import org.xwiki.bridge.event.WikiReadyEvent;
36    import org.xwiki.component.manager.ComponentManager;
37    import org.xwiki.component.wiki.internal.bridge.DefaultWikiObjectComponentManagerEventListener;
38    import org.xwiki.component.wiki.internal.bridge.WikiObjectComponentManagerEventListenerProxy;
39    import org.xwiki.model.reference.DocumentReference;
40    import org.xwiki.model.reference.EntityReference;
41    import org.xwiki.model.reference.EntityReferenceSerializer;
42    import org.xwiki.model.reference.LocalDocumentReference;
43    import org.xwiki.observation.event.Event;
44    import org.xwiki.test.mockito.MockitoComponentMockingRule;
45   
46    import com.xpn.xwiki.doc.XWikiDocument;
47    import com.xpn.xwiki.internal.event.XObjectAddedEvent;
48    import com.xpn.xwiki.objects.BaseObject;
49    import com.xpn.xwiki.objects.BaseObjectReference;
50   
51    import static org.junit.Assert.assertEquals;
52    import static org.mockito.ArgumentMatchers.any;
53    import static org.mockito.Mockito.mock;
54    import static org.mockito.Mockito.times;
55    import static org.mockito.Mockito.verify;
56    import static org.mockito.Mockito.when;
57   
58    /**
59    * Unit tests for {@link DefaultWikiObjectComponentManagerEventListener}.
60    *
61    * @version $Id: 9d5cca7e22c978510ce304a8a4d5efcc113f9482 $
62    * @since 9.5RC1
63    */
 
64    public class DefaultWikiObjectComponentManagerEventListenerTest
65    {
66    @Rule
67    public MockitoComponentMockingRule<DefaultWikiObjectComponentManagerEventListener> mocker =
68    new MockitoComponentMockingRule<>(DefaultWikiObjectComponentManagerEventListener.class);
69   
70    private WikiObjectComponentManagerEventListenerProxy wikiObjectComponentManagerEventListenerProxy;
71   
72    private EntityReferenceSerializer<String> localEntityReferenceSerializer;
73   
74    private EntityReferenceSerializer<String> defaultEntityReferenceSerializer;
75   
76    private List<EntityReference> xClassReferences;
77   
78    private ComponentManager componentManager;
79   
 
80  8 toggle @Before
81    public void setUp() throws Exception
82    {
83  8 this.wikiObjectComponentManagerEventListenerProxy =
84    this.mocker.registerMockComponent(WikiObjectComponentManagerEventListenerProxy.class);
85  8 this.localEntityReferenceSerializer =
86    this.mocker.registerMockComponent(EntityReferenceSerializer.TYPE_STRING, "local");
87  8 this.defaultEntityReferenceSerializer =
88    this.mocker.registerMockComponent(EntityReferenceSerializer.TYPE_STRING);
89  8 this.componentManager = this.mocker.getInstance(ComponentManager.class, "context");
90   
91  8 xClassReferences = Arrays.asList(
92    new DocumentReference("wiki1", "space1","xClass1"),
93    new DocumentReference("wiki1", "space1", "xClass2"));
94   
95  8 when(this.wikiObjectComponentManagerEventListenerProxy.getWikiObjectsList())
96    .thenReturn(Arrays.asList(
97    new LocalDocumentReference(this.xClassReferences.get(0)),
98    this.xClassReferences.get(1)));
99    }
100   
 
101  1 toggle @Test
102    public void supportedEvents() throws Exception
103    {
104  1 List<Event> events = this.mocker.getComponentUnderTest().getEvents();
105   
106  1 assertEquals(5, events.size());
107    }
108   
 
109  1 toggle @Test
110    public void correctListenerName() throws Exception
111    {
112  1 assertEquals("defaultWikiObjectComponentManagerEventListener",
113    this.mocker.getComponentUnderTest().getName());
114    }
115   
 
116  1 toggle @Test
117    public void testComponentInitializationOnApplicationReady() throws Exception
118    {
119  1 this.mocker.getComponentUnderTest().onEvent(new WikiReadyEvent(), null, null);
120   
121  1 verify(this.wikiObjectComponentManagerEventListenerProxy, times(1))
122    .registerAllObjectComponents();
123    }
124   
 
125  1 toggle @Test
126    public void testComponentInitializationOnWikiReady() throws Exception
127    {
128  1 this.mocker.getComponentUnderTest().onEvent(new WikiReadyEvent(), null, null);
129   
130  1 verify(this.wikiObjectComponentManagerEventListenerProxy, times(1))
131    .registerAllObjectComponents();
132    }
133   
 
134  1 toggle @Test
135    public void testOnEventWithUncompatibleEvent() throws Exception
136    {
137  1 this.mocker.getComponentUnderTest().onEvent(new XObjectAddedEvent(), null, null);
138   
139  1 verify(this.wikiObjectComponentManagerEventListenerProxy, times(0))
140    .registerAllObjectComponents();
141  1 verify(this.wikiObjectComponentManagerEventListenerProxy, times(0))
142    .unregisterObjectComponents(any());
143  1 verify(this.wikiObjectComponentManagerEventListenerProxy, times(0))
144    .registerObjectComponents(any(), any(), any());
145    }
146   
 
147  1 toggle @Test
148    public void testOnDocumentCreatedEvent() throws Exception
149    {
150  1 this.verifyXObjectAddOrUpdate(mock(DocumentCreatedEvent.class));
151    }
152   
 
153  1 toggle @Test
154    public void testOnDocumentUpdatedEvent() throws Exception
155    {
156  1 this.verifyXObjectAddOrUpdate(mock(DocumentUpdatedEvent.class));
157    }
158   
 
159  1 toggle @Test
160    public void testOnDocumentDeletedEvent() throws Exception
161    {
162  1 DocumentDeletedEvent event = mock(DocumentDeletedEvent.class);
163  1 XWikiDocument source = mock(XWikiDocument.class);
164  1 XWikiDocument oldXObjectDocument = mock(XWikiDocument.class);
165   
166  1 BaseObject xObject = mock(BaseObject.class);
167  1 BaseObjectReference xObjectReference = mock(BaseObjectReference.class);
168   
169  1 when(source.getOriginalDocument()).thenReturn(oldXObjectDocument);
170  1 when(xObject.getReference()).thenReturn(xObjectReference);
171   
172  1 DocumentReference xObjectClassReference = (DocumentReference) this.xClassReferences.get(0);
173  1 Map<DocumentReference, List<BaseObject>> fakeDocumentXObjects = new HashMap<>();
174  1 fakeDocumentXObjects.put(xObjectClassReference, Collections.singletonList(xObject));
175   
176  1 when(oldXObjectDocument.getXObjects()).thenReturn(fakeDocumentXObjects);
177  1 mockAssociatedComponentBuilderMethod(xObject);
178   
179  1 this.mocker.getComponentUnderTest().onEvent(event, source, null);
180   
181  1 verify(this.wikiObjectComponentManagerEventListenerProxy, times(1))
182    .unregisterObjectComponents(xObjectReference);
183    }
184   
 
185  3 toggle private WikiObjectComponentBuilder mockAssociatedComponentBuilderMethod(BaseObject xObject)
186    throws Exception
187    {
188  3 WikiObjectComponentBuilder builder = mock(WikiObjectComponentBuilder.class);
189   
190  3 DocumentReference xClassReference = (DocumentReference) this.xClassReferences.get(0);
191  3 when(xObject.getXClassReference()).thenReturn(xClassReference);
192   
193  3 when(this.defaultEntityReferenceSerializer.serialize(xClassReference)).thenReturn("xwiki1:space1.xClass1");
194  3 when(this.localEntityReferenceSerializer.serialize(xClassReference)).thenReturn("space1.xClass1");
195   
196  3 when(this.componentManager.hasComponent(WikiObjectComponentBuilder.class, "xwiki1:space1.xClass1"))
197    .thenReturn(false);
198  3 when(this.componentManager.hasComponent(WikiObjectComponentBuilder.class, "space1.xClass1"))
199    .thenReturn(true);
200  3 when(this.componentManager.getInstance(WikiObjectComponentBuilder.class, "space1.xClass1"))
201    .thenReturn(builder);
202   
203  3 return builder;
204    }
205   
 
206  2 toggle private void verifyXObjectAddOrUpdate(AbstractDocumentEvent event) throws Exception
207    {
208  2 XWikiDocument fakeSource = mock(XWikiDocument.class);
209  2 XWikiDocument fakeOldSource = mock(XWikiDocument.class);
210   
211  2 BaseObject xObject = mock(BaseObject.class);
212  2 BaseObjectReference xObjectReference = mock(BaseObjectReference.class);
213  2 DocumentReference xObjectClassReference = (DocumentReference) this.xClassReferences.get(0);
214   
215  2 when(xObject.getReference()).thenReturn(xObjectReference);
216   
217  2 Map<DocumentReference, List<BaseObject>> fakeDocumentXObjects = new HashMap<>();
218  2 fakeDocumentXObjects.put(xObjectClassReference, Collections.singletonList(xObject));
219   
220  2 when(fakeSource.getXObjects()).thenReturn(fakeDocumentXObjects);
221  2 when(fakeSource.getOriginalDocument()).thenReturn(fakeOldSource);
222   
223  2 WikiObjectComponentBuilder builder = mockAssociatedComponentBuilderMethod(xObject);
224   
225  2 this.mocker.getComponentUnderTest().onEvent(event, fakeSource, null);
226   
227  2 verify(this.wikiObjectComponentManagerEventListenerProxy, times(1))
228    .registerObjectComponents(xObjectReference, xObject, builder);
229    }
230    }