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

File DefaultWikiDescriptorManagerTest.java

 

Code metrics

0
96
12
1
302
193
12
0.12
8
12
1

Classes

Class Line # Actions
DefaultWikiDescriptorManagerTest 65 96 0% 12 0
1.0100%
 

Contributing tests

This file is covered by 11 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: a4ff7c7edd50e7072953b0fd912dbd1041cd8b36 $
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  11 toggle @Before
84    public void setUp() throws Exception
85    {
86    // Injection
87  11 xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER, "readonly");
88   
89  11 wikiDescriptorBuilder = mocker.getInstance(WikiDescriptorBuilder.class);
90  11 cache = this.mocker.getInstance(WikiDescriptorCache.class);
91  11 descriptorDocumentHelper = mocker.getInstance(WikiDescriptorDocumentHelper.class);
92   
93    // Cache is supposed to return null and nul empty list by default
94  11 when(cache.getWikiIds()).thenReturn(null);
95   
96    // Frequent uses
97  11 xcontext = mock(XWikiContext.class);
98  11 when(xcontextProvider.get()).thenReturn(xcontext);
99  11 xwiki = mock(com.xpn.xwiki.XWiki.class);
100  11 when(xcontext.getWiki()).thenReturn(xwiki);
101  11 when(xcontext.getMainXWiki()).thenReturn("xwiki");
102    }
103   
 
104  1 toggle @Test
105    public void getByIdWhenNotInCacheButExists() throws Exception
106    {
107    // Not in cache
108  1 when(cache.getFromId("wikiid")).thenReturn(null);
109   
110    // But exists
111  1 XWikiDocument document = mock(XWikiDocument.class);
112  1 when(descriptorDocumentHelper.getDocumentFromWikiId("wikiid")).thenReturn(document);
113  1 when(document.isNew()).thenReturn(false);
114   
115    // Get all XWiki.XWikiServerClass XObjects to pass to the Wiki Descriptor Builder
116  1 List<BaseObject> baseObjects = Arrays.asList(mock(BaseObject.class));
117  1 when(document.getXObjects(any(EntityReference.class))).thenReturn(baseObjects);
118   
119    // Get a Wiki from the Wiki Descriptor Builder
120  1 WikiDescriptorBuilder wikiDescriptorBuilder = this.mocker.getInstance(WikiDescriptorBuilder.class);
121  1 DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("wikiid", "wikialias");
122  1 when(wikiDescriptorBuilder.buildDescriptorObject(anyListOf(BaseObject.class), any(XWikiDocument.class))).
123    thenReturn(descriptor);
124   
125  1 assertEquals(descriptor, this.mocker.getComponentUnderTest().getById("wikiid"));
126   
127    // Verify that calling getById() also sets the descriptor in the cache.
128  1 verify(cache).add(descriptor);
129    }
130   
 
131  1 toggle @Test
132    public void getByWikiIdWhenInCache() throws Exception
133    {
134    // Wiki id is in cache...
135  1 DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("wikiid", "wikialias");
136  1 when(cache.getFromId("wikiid")).thenReturn(descriptor);
137   
138  1 assertEquals(descriptor, this.mocker.getComponentUnderTest().getById("wikiid"));
139    }
140   
 
141  1 toggle @Test
142    public void getByWikiIdWhenNotInCacheAndItDoesntExist() throws Exception
143    {
144    // Get the XWikiDocument for the Document Reference but mark it as new (meaning that it doesn't exist)
145  1 XWikiDocument document = mock(XWikiDocument.class);
146  1 when(descriptorDocumentHelper.getDocumentFromWikiId("wikiid")).thenReturn(document);
147  1 when(document.isNew()).thenReturn(true);
148   
149  1 assertNull(this.mocker.getComponentUnderTest().getById("wikiid"));
150   
151  1 verify(cache).addFromId(eq("wikiid"), same(DefaultWikiDescriptor.VOID));
152    }
153   
 
154  1 toggle @Test
155    public void getByAliasWhenNotInCacheButExists() throws Exception
156    {
157    // Not in cache
158  1 when(cache.getFromId("wikiid")).thenReturn(null);
159   
160    // But exists
161  1 XWikiDocument document = mock(XWikiDocument.class);
162  1 when(descriptorDocumentHelper.findXWikiServerClassDocument("wikialias")).thenReturn(document);
163  1 when(document.isNew()).thenReturn(false);
164   
165    // Get all XWiki.XWikiServerClass XObjects to pass to the Wiki Descriptor Builder
166  1 List<BaseObject> baseObjects = Arrays.asList(mock(BaseObject.class));
167  1 when(document.getXObjects(any(EntityReference.class))).thenReturn(baseObjects);
168   
169    // Get a Wiki from the Wiki Descriptor Builder
170  1 DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("wikiid", "wikialias");
171  1 when(wikiDescriptorBuilder.buildDescriptorObject(anyListOf(BaseObject.class), any(XWikiDocument.class))).
172    thenReturn(descriptor);
173   
174  1 assertEquals(descriptor, this.mocker.getComponentUnderTest().getByAlias("wikialias"));
175   
176    // Verify that calling getByAlias() also sets the descriptor in the cache.
177  1 verify(cache).add(descriptor);
178    }
179   
 
180  1 toggle @Test
181    public void getByAliasWhenInCache() throws Exception
182    {
183    // Wiki alias is in cache...
184  1 DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("wikiid", "wikialias");
185  1 when(cache.getFromAlias("wikialias")).thenReturn(descriptor);
186   
187  1 assertEquals(descriptor, this.mocker.getComponentUnderTest().getByAlias("wikialias"));
188    }
189   
 
190  1 toggle @Test
191    public void getByAliasWhenNotInCacheAndItDoesntExist() throws Exception
192    {
193  1 assertNull(this.mocker.getComponentUnderTest().getByAlias("wikialias"));
194   
195  1 verify(cache).addFromAlias(eq("wikialias"), same(DefaultWikiDescriptor.VOID));
196    }
197   
 
198  1 toggle @Test
199    public void getAll() throws Exception
200    {
201    // Get the XWikiDocuments for the Document References
202  1 XWikiDocument document1 = mock(XWikiDocument.class);
203  1 XWikiDocument document2 = mock(XWikiDocument.class);
204  1 XWikiDocument maindocument = mock(XWikiDocument.class);
205   
206  1 DefaultWikiDescriptor descriptor3 = new DefaultWikiDescriptor("wikiid3", "wikialias3");
207   
208    // Get documents
209  1 when(descriptorDocumentHelper.getAllXWikiServerClassDocumentNames()).thenReturn(
210    Arrays.asList("XWiki.XWikiServerWikiid1", "XWiki.XWikiServerWikiid2", "XWiki.XWikiServerWikiid3"));
211  1 when(descriptorDocumentHelper.getWikiIdFromDocumentFullname("XWiki.XWikiServerWikiid1")).thenReturn("wikiid1");
212  1 when(descriptorDocumentHelper.getWikiIdFromDocumentFullname("XWiki.XWikiServerWikiid2")).thenReturn("wikiid2");
213  1 when(descriptorDocumentHelper.getWikiIdFromDocumentFullname("XWiki.XWikiServerWikiid3")).thenReturn("wikiid3");
214  1 when(cache.getFromId("wikiid3")).thenReturn(descriptor3);
215  1 when(descriptorDocumentHelper.getDocumentFromWikiId("wikiid1")).thenReturn(document1);
216  1 when(descriptorDocumentHelper.getDocumentFromWikiId("wikiid2")).thenReturn(document2);
217  1 when(descriptorDocumentHelper.getDocumentFromWikiId("xwiki")).thenReturn(maindocument);
218   
219  1 when(maindocument.isNew()).thenReturn(true);
220   
221    // Get all XWiki.XWikiServerClass XObjects to pass to the Wiki Descriptor Builder
222  1 List<BaseObject> baseObjects = Arrays.asList(mock(BaseObject.class));
223  1 when(document1.getXObjects(any(EntityReference.class))).thenReturn(baseObjects);
224  1 when(document2.getXObjects(any(EntityReference.class))).thenReturn(baseObjects);
225   
226    // Get a Wiki from the Wiki Descriptor Builder
227  1 DefaultWikiDescriptor descriptor1 = new DefaultWikiDescriptor("wikiid1", "wikialias1");
228  1 DefaultWikiDescriptor descriptor2 = new DefaultWikiDescriptor("wikiid2", "wikialias2");
229  1 when(wikiDescriptorBuilder.buildDescriptorObject(anyListOf(BaseObject.class), any(XWikiDocument.class))).
230    thenReturn(descriptor1, descriptor2);
231   
232  1 Collection<WikiDescriptor> descriptors = this.mocker.getComponentUnderTest().getAll();
233  1 assertEquals(4, descriptors.size());
234   
235    // Verify that XWiki.XWikiServerWikiid3 has not be loaded
236  1 verify(descriptorDocumentHelper, never()).getDocumentFromWikiId("wikiid3");
237   
238    // Verify all descriptors were put in cache except those which was already there
239  1 verify(cache).add(descriptor1);
240  1 verify(cache).add(descriptor2);
241  1 verify(cache, never()).add(descriptor3);
242    }
243   
 
244  1 toggle @Test
245    public void exists() throws Exception
246    {
247  1 when(cache.getWikiIds()).thenReturn(Arrays.asList("wikiid1"));
248   
249    // When the wiki exists
250  1 assertTrue(mocker.getComponentUnderTest().exists("wikiid1"));
251   
252    // When the wiki does not exists
253  1 assertFalse(mocker.getComponentUnderTest().exists("wikiid2"));
254    }
255   
 
256  1 toggle @Test
257    public void getMainWikiId() throws Exception
258    {
259  1 assertEquals("xwiki", this.mocker.getComponentUnderTest().getMainWikiId());
260    }
261   
 
262  1 toggle @Test
263    public void getMainWikiDescriptor() throws Exception
264    {
265  1 DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("xwiki", "xwiki");
266  1 when(cache.getFromId("xwiki")).thenReturn(descriptor);
267   
268  1 assertEquals(descriptor, this.mocker.getComponentUnderTest().getMainWikiDescriptor());
269    }
270   
 
271  1 toggle @Test
272    public void testCacheProtection() throws WikiManagerException, ComponentLookupException
273    {
274  1 DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("xwiki", "xwiki");
275  1 descriptor.setPrettyName("pretty name");
276  1 WikiPropertyGroup propertyGroup = new WikiPropertyGroup("group");
277  1 propertyGroup.set("property", "value");
278  1 descriptor.addPropertyGroup(propertyGroup);
279  1 when(cache.getFromId("xwiki")).thenReturn(descriptor);
280  1 when(cache.getFromAlias("xwiki")).thenReturn(descriptor);
281   
282  1 WikiDescriptorManager wikiDescriptorManager = this.mocker.getComponentUnderTest();
283   
284    // Modify the descriptor without saving it
285  1 wikiDescriptorManager.getById("xwiki").setPrettyName("changed pretty name");
286  1 assertEquals("pretty name", wikiDescriptorManager.getById("xwiki").getPrettyName());
287  1 wikiDescriptorManager.getById("xwiki").getPropertyGroup("group").set("property", "modified value");
288  1 assertEquals("value", wikiDescriptorManager.getById("xwiki").getPropertyGroup("group").get("property"));
289   
290    // Modify the descriptor without saving it
291  1 wikiDescriptorManager.getByAlias("xwiki").setPrettyName("changed pretty name");
292  1 assertEquals("pretty name", wikiDescriptorManager.getByAlias("xwiki").getPrettyName());
293  1 wikiDescriptorManager.getByAlias("xwiki").getPropertyGroup("group").set("property", "modified value");
294  1 assertEquals("value", wikiDescriptorManager.getByAlias("xwiki").getPropertyGroup("group").get("property"));
295   
296    // Modify the descriptor without saving it
297  1 wikiDescriptorManager.getMainWikiDescriptor().setPrettyName("changed pretty name");
298  1 assertEquals("pretty name", wikiDescriptorManager.getMainWikiDescriptor().getPrettyName());
299  1 wikiDescriptorManager.getMainWikiDescriptor().getPropertyGroup("group").set("property", "modified value");
300  1 assertEquals("value", wikiDescriptorManager.getMainWikiDescriptor().getPropertyGroup("group").get("property"));
301    }
302    }