1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
61 |
|
|
62 |
|
@version |
63 |
|
@since |
64 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (108) |
Complexity: 12 |
Complexity Density: 0.12 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
|
83 |
11 |
@Before... |
84 |
|
public void setUp() throws Exception |
85 |
|
{ |
86 |
|
|
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 |
|
|
94 |
11 |
when(cache.getWikiIds()).thenReturn(null); |
95 |
|
|
96 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
104 |
1 |
@Test... |
105 |
|
public void getByIdWhenNotInCacheButExists() throws Exception |
106 |
|
{ |
107 |
|
|
108 |
1 |
when(cache.getFromId("wikiid")).thenReturn(null); |
109 |
|
|
110 |
|
|
111 |
1 |
XWikiDocument document = mock(XWikiDocument.class); |
112 |
1 |
when(descriptorDocumentHelper.getDocumentFromWikiId("wikiid")).thenReturn(document); |
113 |
1 |
when(document.isNew()).thenReturn(false); |
114 |
|
|
115 |
|
|
116 |
1 |
List<BaseObject> baseObjects = Arrays.asList(mock(BaseObject.class)); |
117 |
1 |
when(document.getXObjects(any(EntityReference.class))).thenReturn(baseObjects); |
118 |
|
|
119 |
|
|
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 |
|
|
128 |
1 |
verify(cache).add(descriptor); |
129 |
|
} |
130 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
131 |
1 |
@Test... |
132 |
|
public void getByWikiIdWhenInCache() throws Exception |
133 |
|
{ |
134 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
141 |
1 |
@Test... |
142 |
|
public void getByWikiIdWhenNotInCacheAndItDoesntExist() throws Exception |
143 |
|
{ |
144 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
154 |
1 |
@Test... |
155 |
|
public void getByAliasWhenNotInCacheButExists() throws Exception |
156 |
|
{ |
157 |
|
|
158 |
1 |
when(cache.getFromId("wikiid")).thenReturn(null); |
159 |
|
|
160 |
|
|
161 |
1 |
XWikiDocument document = mock(XWikiDocument.class); |
162 |
1 |
when(descriptorDocumentHelper.findXWikiServerClassDocument("wikialias")).thenReturn(document); |
163 |
1 |
when(document.isNew()).thenReturn(false); |
164 |
|
|
165 |
|
|
166 |
1 |
List<BaseObject> baseObjects = Arrays.asList(mock(BaseObject.class)); |
167 |
1 |
when(document.getXObjects(any(EntityReference.class))).thenReturn(baseObjects); |
168 |
|
|
169 |
|
|
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 |
|
|
177 |
1 |
verify(cache).add(descriptor); |
178 |
|
} |
179 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
180 |
1 |
@Test... |
181 |
|
public void getByAliasWhenInCache() throws Exception |
182 |
|
{ |
183 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
190 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (25) |
Complexity: 1 |
Complexity Density: 0.04 |
1PASS
|
|
198 |
1 |
@Test... |
199 |
|
public void getAll() throws Exception |
200 |
|
{ |
201 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
236 |
1 |
verify(descriptorDocumentHelper, never()).getDocumentFromWikiId("wikiid3"); |
237 |
|
|
238 |
|
|
239 |
1 |
verify(cache).add(descriptor1); |
240 |
1 |
verify(cache).add(descriptor2); |
241 |
1 |
verify(cache, never()).add(descriptor3); |
242 |
|
} |
243 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
244 |
1 |
@Test... |
245 |
|
public void exists() throws Exception |
246 |
|
{ |
247 |
1 |
when(cache.getWikiIds()).thenReturn(Arrays.asList("wikiid1")); |
248 |
|
|
249 |
|
|
250 |
1 |
assertTrue(mocker.getComponentUnderTest().exists("wikiid1")); |
251 |
|
|
252 |
|
|
253 |
1 |
assertFalse(mocker.getComponentUnderTest().exists("wikiid2")); |
254 |
|
} |
255 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
256 |
1 |
@Test... |
257 |
|
public void getMainWikiId() throws Exception |
258 |
|
{ |
259 |
1 |
assertEquals("xwiki", this.mocker.getComponentUnderTest().getMainWikiId()); |
260 |
|
} |
261 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
262 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (20) |
Complexity: 1 |
Complexity Density: 0.05 |
1PASS
|
|
271 |
1 |
@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 |
|
|
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 |
|
|
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 |
|
|
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 |
|
} |