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

File DefaultWikiDescriptorManagerTest.java

 

Code metrics

0
99
13
1
310
200
13
0.13
7.62
13
1

Classes

Class Line # Actions
DefaultWikiDescriptorManagerTest 65 99 0% 13 0
1.0100%
 

Contributing tests

This file is covered by 12 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.wiki.internal.descriptor;
21   
22    import java.util.Arrays;
23    import java.util.Collection;
24    import java.util.List;
25   
26    import javax.inject.Provider;
27   
28    import org.junit.Before;
29    import org.junit.Rule;
30    import org.junit.Test;
31    import org.xwiki.component.manager.ComponentLookupException;
32    import org.xwiki.model.reference.EntityReference;
33    import org.xwiki.test.mockito.MockitoComponentMockingRule;
34    import org.xwiki.wiki.descriptor.WikiDescriptor;
35    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
36    import org.xwiki.wiki.internal.descriptor.builder.WikiDescriptorBuilder;
37    import org.xwiki.wiki.internal.descriptor.document.WikiDescriptorDocumentHelper;
38    import org.xwiki.wiki.internal.manager.WikiDescriptorCache;
39    import org.xwiki.wiki.manager.WikiManagerException;
40    import org.xwiki.wiki.properties.WikiPropertyGroup;
41   
42    import com.xpn.xwiki.XWikiContext;
43    import com.xpn.xwiki.doc.XWikiDocument;
44    import com.xpn.xwiki.objects.BaseObject;
45   
46    import static org.junit.Assert.assertEquals;
47    import static org.junit.Assert.assertFalse;
48    import static org.junit.Assert.assertNull;
49    import static org.junit.Assert.assertTrue;
50    import static org.mockito.ArgumentMatchers.any;
51    import static org.mockito.ArgumentMatchers.anyListOf;
52    import static org.mockito.ArgumentMatchers.eq;
53    import static org.mockito.ArgumentMatchers.same;
54    import static org.mockito.Mockito.mock;
55    import static org.mockito.Mockito.never;
56    import static org.mockito.Mockito.verify;
57    import static org.mockito.Mockito.when;
58   
59    /**
60    * Unit tests for {@link org.xwiki.wiki.internal.descriptor.DefaultWikiDescriptorManager}.
61    *
62    * @version $Id: d3b13789fc6415e83307a8fa97cbe6b87b9c6fbe $
63    * @since 6.0M1
64    */
 
65    public class DefaultWikiDescriptorManagerTest
66    {
67    @Rule
68    public MockitoComponentMockingRule<WikiDescriptorManager> mocker =
69    new MockitoComponentMockingRule<WikiDescriptorManager>(DefaultWikiDescriptorManager.class);
70   
71    private Provider<XWikiContext> xcontextProvider;
72   
73    private WikiDescriptorCache cache;
74   
75    private WikiDescriptorDocumentHelper descriptorDocumentHelper;
76   
77    private WikiDescriptorBuilder wikiDescriptorBuilder;
78   
79    private XWikiContext xcontext;
80   
81    private com.xpn.xwiki.XWiki xwiki;
82   
 
83  12 toggle @Before
84    public void setUp() throws Exception
85    {
86    // Injection
87  12 xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER, "readonly");
88   
89  12 wikiDescriptorBuilder = mocker.getInstance(WikiDescriptorBuilder.class);
90  12 cache = this.mocker.getInstance(WikiDescriptorCache.class);
91  12 descriptorDocumentHelper = mocker.getInstance(WikiDescriptorDocumentHelper.class);
92   
93    // Cache is supposed to return null and nul empty list by default
94  12 when(cache.getWikiIds()).thenReturn(null);
95   
96    // Frequent uses
97  12 xcontext = mock(XWikiContext.class);
98  12 when(xcontextProvider.get()).thenReturn(xcontext);
99  12 xwiki = mock(com.xpn.xwiki.XWiki.class);
100  12 when(xcontext.getWiki()).thenReturn(xwiki);
101  12 when(xcontext.getMainXWiki()).thenReturn("xwiki");
102  12 when(xcontext.isMainWiki("xwiki")).thenReturn(true);
103    }
104   
 
105  1 toggle @Test
106    public void getByIdWhenNotInCacheButExists() throws Exception
107    {
108    // Not in cache
109  1 when(cache.getFromId("wikiid")).thenReturn(null);
110   
111    // But exists
112  1 XWikiDocument document = mock(XWikiDocument.class);
113  1 when(descriptorDocumentHelper.getDocumentFromWikiId("wikiid")).thenReturn(document);
114  1 when(document.isNew()).thenReturn(false);
115   
116    // Get all XWiki.XWikiServerClass XObjects to pass to the Wiki Descriptor Builder
117  1 List<BaseObject> baseObjects = Arrays.asList(mock(BaseObject.class));
118  1 when(document.getXObjects(any(EntityReference.class))).thenReturn(baseObjects);
119   
120    // Get a Wiki from the Wiki Descriptor Builder
121  1 WikiDescriptorBuilder wikiDescriptorBuilder = this.mocker.getInstance(WikiDescriptorBuilder.class);
122  1 DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("wikiid", "wikialias");
123  1 when(wikiDescriptorBuilder.buildDescriptorObject(anyListOf(BaseObject.class), any(XWikiDocument.class))).
124    thenReturn(descriptor);
125   
126  1 assertEquals(descriptor, this.mocker.getComponentUnderTest().getById("wikiid"));
127   
128    // Verify that calling getById() also sets the descriptor in the cache.
129  1 verify(cache).add(descriptor);
130    }
131   
 
132  1 toggle @Test
133    public void getByWikiIdWhenInCache() throws Exception
134    {
135    // Wiki id is in cache...
136  1 DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("wikiid", "wikialias");
137  1 when(cache.getFromId("wikiid")).thenReturn(descriptor);
138   
139  1 assertEquals(descriptor, this.mocker.getComponentUnderTest().getById("wikiid"));
140    }
141   
 
142  1 toggle @Test
143    public void getByWikiIdWhenNotInCacheAndItDoesntExist() throws Exception
144    {
145    // Get the XWikiDocument for the Document Reference but mark it as new (meaning that it doesn't exist)
146  1 XWikiDocument document = mock(XWikiDocument.class);
147  1 when(descriptorDocumentHelper.getDocumentFromWikiId("wikiid")).thenReturn(document);
148  1 when(document.isNew()).thenReturn(true);
149   
150  1 assertNull(this.mocker.getComponentUnderTest().getById("wikiid"));
151   
152  1 verify(cache).addFromId(eq("wikiid"), same(DefaultWikiDescriptor.VOID));
153    }
154   
 
155  1 toggle @Test
156    public void getByAliasWhenNotInCacheButExists() throws Exception
157    {
158    // Not in cache
159  1 when(cache.getFromId("wikiid")).thenReturn(null);
160   
161    // But exists
162  1 XWikiDocument document = mock(XWikiDocument.class);
163  1 when(descriptorDocumentHelper.findXWikiServerClassDocument("wikialias")).thenReturn(document);
164  1 when(document.isNew()).thenReturn(false);
165   
166    // Get all XWiki.XWikiServerClass XObjects to pass to the Wiki Descriptor Builder
167  1 List<BaseObject> baseObjects = Arrays.asList(mock(BaseObject.class));
168  1 when(document.getXObjects(any(EntityReference.class))).thenReturn(baseObjects);
169   
170    // Get a Wiki from the Wiki Descriptor Builder
171  1 DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("wikiid", "wikialias");
172  1 when(wikiDescriptorBuilder.buildDescriptorObject(anyListOf(BaseObject.class), any(XWikiDocument.class))).
173    thenReturn(descriptor);
174   
175  1 assertEquals(descriptor, this.mocker.getComponentUnderTest().getByAlias("wikialias"));
176   
177    // Verify that calling getByAlias() also sets the descriptor in the cache.
178  1 verify(cache).add(descriptor);
179    }
180   
 
181  1 toggle @Test
182    public void getByAliasWhenInCache() throws Exception
183    {
184    // Wiki alias is in cache...
185  1 DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("wikiid", "wikialias");
186  1 when(cache.getFromAlias("wikialias")).thenReturn(descriptor);
187   
188  1 assertEquals(descriptor, this.mocker.getComponentUnderTest().getByAlias("wikialias"));
189    }
190   
 
191  1 toggle @Test
192    public void getByAliasWhenNotInCacheAndItDoesntExist() throws Exception
193    {
194  1 assertNull(this.mocker.getComponentUnderTest().getByAlias("wikialias"));
195   
196  1 verify(cache).addFromAlias(eq("wikialias"), same(DefaultWikiDescriptor.VOID));
197    }
198   
 
199  1 toggle @Test
200    public void getAll() throws Exception
201    {
202    // Get the XWikiDocuments for the Document References
203  1 XWikiDocument document1 = mock(XWikiDocument.class);
204  1 XWikiDocument document2 = mock(XWikiDocument.class);
205  1 XWikiDocument maindocument = mock(XWikiDocument.class);
206   
207  1 DefaultWikiDescriptor descriptor3 = new DefaultWikiDescriptor("wikiid3", "wikialias3");
208   
209    // Get documents
210  1 when(descriptorDocumentHelper.getAllXWikiServerClassDocumentNames()).thenReturn(
211    Arrays.asList("XWiki.XWikiServerWikiid1", "XWiki.XWikiServerWikiid2", "XWiki.XWikiServerWikiid3"));
212  1 when(descriptorDocumentHelper.getWikiIdFromDocumentFullname("XWiki.XWikiServerWikiid1")).thenReturn("wikiid1");
213  1 when(descriptorDocumentHelper.getWikiIdFromDocumentFullname("XWiki.XWikiServerWikiid2")).thenReturn("wikiid2");
214  1 when(descriptorDocumentHelper.getWikiIdFromDocumentFullname("XWiki.XWikiServerWikiid3")).thenReturn("wikiid3");
215  1 when(cache.getFromId("wikiid3")).thenReturn(descriptor3);
216  1 when(descriptorDocumentHelper.getDocumentFromWikiId("wikiid1")).thenReturn(document1);
217  1 when(descriptorDocumentHelper.getDocumentFromWikiId("wikiid2")).thenReturn(document2);
218  1 when(descriptorDocumentHelper.getDocumentFromWikiId("xwiki")).thenReturn(maindocument);
219   
220  1 when(maindocument.isNew()).thenReturn(true);
221   
222    // Get all XWiki.XWikiServerClass XObjects to pass to the Wiki Descriptor Builder
223  1 List<BaseObject> baseObjects = Arrays.asList(mock(BaseObject.class));
224  1 when(document1.getXObjects(any(EntityReference.class))).thenReturn(baseObjects);
225  1 when(document2.getXObjects(any(EntityReference.class))).thenReturn(baseObjects);
226   
227    // Get a Wiki from the Wiki Descriptor Builder
228  1 DefaultWikiDescriptor descriptor1 = new DefaultWikiDescriptor("wikiid1", "wikialias1");
229  1 DefaultWikiDescriptor descriptor2 = new DefaultWikiDescriptor("wikiid2", "wikialias2");
230  1 when(wikiDescriptorBuilder.buildDescriptorObject(anyListOf(BaseObject.class), any(XWikiDocument.class))).
231    thenReturn(descriptor1, descriptor2);
232   
233  1 Collection<WikiDescriptor> descriptors = this.mocker.getComponentUnderTest().getAll();
234  1 assertEquals(4, descriptors.size());
235   
236    // Verify that XWiki.XWikiServerWikiid3 has not be loaded
237  1 verify(descriptorDocumentHelper, never()).getDocumentFromWikiId("wikiid3");
238   
239    // Verify all descriptors were put in cache except those which was already there
240  1 verify(cache).add(descriptor1);
241  1 verify(cache).add(descriptor2);
242  1 verify(cache, never()).add(descriptor3);
243    }
244   
 
245  1 toggle @Test
246    public void exists() throws Exception
247    {
248  1 when(cache.getWikiIds()).thenReturn(Arrays.asList("wikiid1"));
249   
250    // When the wiki exists
251  1 assertTrue(mocker.getComponentUnderTest().exists("wikiid1"));
252   
253    // When the wiki does not exists
254  1 assertFalse(mocker.getComponentUnderTest().exists("wikiid2"));
255    }
256   
 
257  1 toggle @Test
258    public void getMainWikiId() throws Exception
259    {
260  1 assertEquals("xwiki", this.mocker.getComponentUnderTest().getMainWikiId());
261    }
262   
 
263  1 toggle @Test
264    public void isMainWiki() throws Exception
265    {
266  1 assertTrue(this.mocker.getComponentUnderTest().isMainWiki("xwiki"));
267  1 assertFalse(this.mocker.getComponentUnderTest().isMainWiki("notmainwiki"));
268    }
269   
 
270  1 toggle @Test
271    public void getMainWikiDescriptor() throws Exception
272    {
273  1 DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("xwiki", "xwiki");
274  1 when(cache.getFromId("xwiki")).thenReturn(descriptor);
275   
276  1 assertEquals(descriptor, this.mocker.getComponentUnderTest().getMainWikiDescriptor());
277    }
278   
 
279  1 toggle @Test
280    public void testCacheProtection() throws WikiManagerException, ComponentLookupException
281    {
282  1 DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("xwiki", "xwiki");
283  1 descriptor.setPrettyName("pretty name");
284  1 WikiPropertyGroup propertyGroup = new WikiPropertyGroup("group");
285  1 propertyGroup.set("property", "value");
286  1 descriptor.addPropertyGroup(propertyGroup);
287  1 when(cache.getFromId("xwiki")).thenReturn(descriptor);
288  1 when(cache.getFromAlias("xwiki")).thenReturn(descriptor);
289   
290  1 WikiDescriptorManager wikiDescriptorManager = this.mocker.getComponentUnderTest();
291   
292    // Modify the descriptor without saving it
293  1 wikiDescriptorManager.getById("xwiki").setPrettyName("changed pretty name");
294  1 assertEquals("pretty name", wikiDescriptorManager.getById("xwiki").getPrettyName());
295  1 wikiDescriptorManager.getById("xwiki").getPropertyGroup("group").set("property", "modified value");
296  1 assertEquals("value", wikiDescriptorManager.getById("xwiki").getPropertyGroup("group").get("property"));
297   
298    // Modify the descriptor without saving it
299  1 wikiDescriptorManager.getByAlias("xwiki").setPrettyName("changed pretty name");
300  1 assertEquals("pretty name", wikiDescriptorManager.getByAlias("xwiki").getPrettyName());
301  1 wikiDescriptorManager.getByAlias("xwiki").getPropertyGroup("group").set("property", "modified value");
302  1 assertEquals("value", wikiDescriptorManager.getByAlias("xwiki").getPropertyGroup("group").get("property"));
303   
304    // Modify the descriptor without saving it
305  1 wikiDescriptorManager.getMainWikiDescriptor().setPrettyName("changed pretty name");
306  1 assertEquals("pretty name", wikiDescriptorManager.getMainWikiDescriptor().getPrettyName());
307  1 wikiDescriptorManager.getMainWikiDescriptor().getPropertyGroup("group").set("property", "modified value");
308  1 assertEquals("value", wikiDescriptorManager.getMainWikiDescriptor().getPropertyGroup("group").get("property"));
309    }
310    }