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

File DefaultResourceReferenceEntityReferenceResolverTest.java

 

Code metrics

0
90
14
2
431
301
14
0.16
6.43
7
1

Classes

Class Line # Actions
DefaultResourceReferenceEntityReferenceResolverTest 90 88 0% 12 0
1.0100%
DefaultResourceReferenceEntityReferenceResolverTest.VoidResourceReferenceEntityReferenceResolve 393 2 0% 2 2
0.550%
 

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.rendering.internal.resolver;
21   
22    import java.util.Arrays;
23    import java.util.HashSet;
24    import java.util.Set;
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.mockito.invocation.InvocationOnMock;
32    import org.mockito.stubbing.Answer;
33    import org.xwiki.bridge.DocumentAccessBridge;
34    import org.xwiki.component.internal.ContextComponentManagerProvider;
35    import org.xwiki.component.manager.ComponentLookupException;
36    import org.xwiki.component.util.DefaultParameterizedType;
37    import org.xwiki.model.EntityType;
38    import org.xwiki.model.internal.reference.DefaultReferenceAttachmentReferenceResolver;
39    import org.xwiki.model.internal.reference.DefaultReferenceDocumentReferenceResolver;
40    import org.xwiki.model.internal.reference.DefaultReferenceEntityReferenceResolver;
41    import org.xwiki.model.internal.reference.DefaultStringAttachmentReferenceResolver;
42    import org.xwiki.model.internal.reference.DefaultStringDocumentReferenceResolver;
43    import org.xwiki.model.internal.reference.DefaultStringEntityReferenceResolver;
44    import org.xwiki.model.internal.reference.DefaultStringSpaceReferenceResolver;
45    import org.xwiki.model.internal.reference.DefaultSymbolScheme;
46    import org.xwiki.model.internal.reference.RelativeStringEntityReferenceResolver;
47    import org.xwiki.model.reference.AttachmentReference;
48    import org.xwiki.model.reference.DocumentReference;
49    import org.xwiki.model.reference.EntityReference;
50    import org.xwiki.model.reference.EntityReferenceProvider;
51    import org.xwiki.model.reference.EntityReferenceResolver;
52    import org.xwiki.model.reference.SpaceReference;
53    import org.xwiki.rendering.listener.reference.AttachmentResourceReference;
54    import org.xwiki.rendering.listener.reference.DocumentResourceReference;
55    import org.xwiki.rendering.listener.reference.ResourceReference;
56    import org.xwiki.rendering.listener.reference.ResourceType;
57    import org.xwiki.rendering.listener.reference.SpaceResourceReference;
58    import org.xwiki.test.annotation.ComponentList;
59    import org.xwiki.test.mockito.MockitoComponentMockingRule;
60   
61    import static org.junit.Assert.assertEquals;
62    import static org.junit.Assert.assertFalse;
63    import static org.junit.Assert.assertNull;
64    import static org.junit.Assert.assertTrue;
65    import static org.mockito.ArgumentMatchers.any;
66    import static org.mockito.ArgumentMatchers.eq;
67    import static org.mockito.Mockito.when;
68   
69    /**
70    * Validate {@link DefaultResourceReferenceEntityReferenceResolver}.
71    *
72    * @version $Id: 3cf303df66609d4294768175f63097455cc525a1 $
73    */
74    @ComponentList({
75    DefaultResourceReferenceEntityReferenceResolver.class,
76    AttachmentResourceReferenceEntityReferenceResolver.class,
77    DocumentResourceReferenceEntityReferenceResolver.class,
78    DefaultStringAttachmentReferenceResolver.class,
79    DefaultStringDocumentReferenceResolver.class,
80    SpaceResourceReferenceEntityReferenceResolver.class,
81    DefaultReferenceEntityReferenceResolver.class,
82    DefaultStringEntityReferenceResolver.class,
83    RelativeStringEntityReferenceResolver.class,
84    DefaultReferenceAttachmentReferenceResolver.class,
85    DefaultReferenceDocumentReferenceResolver.class,
86    DefaultStringSpaceReferenceResolver.class,
87    ContextComponentManagerProvider.class,
88    DefaultSymbolScheme.class
89    })
 
90    public class DefaultResourceReferenceEntityReferenceResolverTest
91    {
92    private static final String DEFAULT_PAGE = "defaultpage";
93   
94    private static final String CURRENT_PAGE = "currentpage";
95   
96    private static final String CURRENT_SPACE = "currentspace";
97   
98    private static final String CURRENT_SUBSPACE = "currentsubspace";
99   
100    private static final String CURRENT_WIKI = "currentwiki";
101   
102    private static final String WIKI = "Wiki";
103   
104    private static final String SPACE = "Space";
105   
106    private static final String PAGE = "Page";
107   
108    private static final String ATTACHMENT = "file.ext";
109   
110    private static final DocumentReference CURRENT_DOCUMENT_REFERENCE =
111    new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, CURRENT_PAGE);
112   
113    @Rule
114    public MockitoComponentMockingRule<EntityReferenceResolver<ResourceReference>> mocker =
115    new MockitoComponentMockingRule<EntityReferenceResolver<ResourceReference>>(
116    DefaultResourceReferenceEntityReferenceResolver.class);
117   
118    private EntityReferenceResolver<String> currentEntityReferenceResolver;
119   
120    private Provider<DocumentReference> currentDocumentProvider;
121   
122    private DocumentAccessBridge bridge;
123   
124    private EntityReferenceProvider defaultEntityProvider;
125   
126    private final Set<DocumentReference> existingDocuments = new HashSet<>();
127   
 
128  7 toggle @Before
129    public void before() throws Exception
130    {
131  7 this.currentEntityReferenceResolver =
132    this.mocker.registerMockComponent(EntityReferenceResolver.TYPE_STRING, "current");
133   
134  7 this.currentDocumentProvider = this.mocker.registerMockComponent(
135    new DefaultParameterizedType(null, Provider.class, DocumentReference.class), "current");
136  7 when(this.currentDocumentProvider.get())
137    .thenReturn(new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, CURRENT_PAGE));
138   
139  7 this.bridge = this.mocker.registerMockComponent(DocumentAccessBridge.class);
140  7 when(this.bridge.exists(any(DocumentReference.class))).then(new Answer<Boolean>()
141    {
 
142  37 toggle @Override
143    public Boolean answer(InvocationOnMock invocation) throws Throwable
144    {
145  37 return existingDocuments.contains(invocation.getArguments()[0]);
146    }
147    });
148   
149  7 this.defaultEntityProvider = this.mocker.registerMockComponent(EntityReferenceProvider.class);
150  7 when(this.defaultEntityProvider.getDefaultReference(EntityType.DOCUMENT))
151    .thenReturn(new EntityReference(DEFAULT_PAGE, EntityType.DOCUMENT));
152    }
153   
 
154  29 toggle private DocumentResourceReference documentResource(String referenceString, boolean typed)
155    {
156  29 DocumentResourceReference reference = new DocumentResourceReference(referenceString);
157   
158  29 reference.setTyped(typed);
159   
160  29 return reference;
161    }
162   
 
163  5 toggle private SpaceResourceReference spaceResource(String referenceString, boolean typed)
164    {
165  5 SpaceResourceReference reference = new SpaceResourceReference(referenceString);
166   
167  5 reference.setTyped(typed);
168   
169  5 return reference;
170    }
171   
 
172  8 toggle private AttachmentResourceReference attachmentResource(String referenceString, boolean typed)
173    {
174  8 AttachmentResourceReference reference = new AttachmentResourceReference(referenceString);
175   
176  8 reference.setTyped(typed);
177   
178  8 return reference;
179    }
180    // Tests
181   
 
182  1 toggle @Test
183    public void resolve() throws ComponentLookupException
184    {
185  1 assertNull(this.mocker.getComponentUnderTest().resolve(null, null));
186  1 assertNull(this.mocker.getComponentUnderTest().resolve(new ResourceReference("path", ResourceType.PATH), null));
187    }
188   
 
189  1 toggle @Test
190    public void resolveTypeDocument() throws ComponentLookupException
191    {
192  1 assertEquals(new DocumentReference(WIKI, SPACE, PAGE),
193    this.mocker.getComponentUnderTest().resolve(documentResource(WIKI + ':' + SPACE + '.' + PAGE, true), null));
194   
195  1 assertEquals(new DocumentReference(CURRENT_WIKI, SPACE, PAGE),
196    this.mocker.getComponentUnderTest().resolve(documentResource(SPACE + '.' + PAGE, true), null));
197   
198  1 assertEquals(new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, PAGE),
199    this.mocker.getComponentUnderTest().resolve(documentResource(PAGE, true), null));
200   
201  1 assertEquals(new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, CURRENT_PAGE),
202    this.mocker.getComponentUnderTest().resolve(documentResource("", true), null));
203   
204  1 when(this.currentEntityReferenceResolver.resolve(eq(WIKI + ':' + SPACE + '.' + PAGE), eq(EntityType.DOCUMENT),
205    any())).thenReturn(new DocumentReference(WIKI, SPACE, PAGE));
206   
207  1 ResourceReference withBaseReference = documentResource("", true);
208  1 withBaseReference.addBaseReference(WIKI + ':' + SPACE + '.' + PAGE);
209  1 assertEquals(new DocumentReference(WIKI, SPACE, PAGE),
210    this.mocker.getComponentUnderTest().resolve(withBaseReference, null));
211   
212  1 assertEquals(new DocumentReference(WIKI, SPACE, PAGE), this.mocker.getComponentUnderTest()
213    .resolve(documentResource("", true), null, new DocumentReference(WIKI, SPACE, PAGE)));
214    }
215   
 
216  1 toggle @Test
217    public void resolveUntypeDocument() throws ComponentLookupException
218    {
219    // When the page does not exist
220   
221  1 assertEquals(new DocumentReference(WIKI, Arrays.asList(SPACE, PAGE), DEFAULT_PAGE), this.mocker
222    .getComponentUnderTest().resolve(documentResource(WIKI + ':' + SPACE + '.' + PAGE, false), null));
223  1 assertEquals(new DocumentReference(CURRENT_WIKI, Arrays.asList(SPACE, PAGE), DEFAULT_PAGE),
224    this.mocker.getComponentUnderTest().resolve(documentResource(SPACE + '.' + PAGE, false), null));
225  1 assertEquals(new DocumentReference(CURRENT_WIKI, Arrays.asList(CURRENT_SPACE, PAGE), DEFAULT_PAGE),
226    this.mocker.getComponentUnderTest().resolve(documentResource(PAGE, false), null));
227   
228    // Already ends with default page name
229   
230  1 assertEquals(new DocumentReference(WIKI, SPACE, DEFAULT_PAGE), this.mocker.getComponentUnderTest()
231    .resolve(documentResource(WIKI + ':' + SPACE + '.' + DEFAULT_PAGE, false), null));
232  1 assertEquals(new DocumentReference(CURRENT_WIKI, SPACE, DEFAULT_PAGE),
233    this.mocker.getComponentUnderTest().resolve(documentResource(SPACE + '.' + DEFAULT_PAGE, false), null));
234  1 assertEquals(new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, DEFAULT_PAGE),
235    this.mocker.getComponentUnderTest().resolve(documentResource(DEFAULT_PAGE, false), null));
236   
237    // When the page exist
238   
239  1 this.existingDocuments.add(new DocumentReference(WIKI, SPACE, PAGE));
240  1 assertEquals(new DocumentReference(WIKI, SPACE, PAGE), this.mocker.getComponentUnderTest()
241    .resolve(documentResource(WIKI + ':' + SPACE + '.' + PAGE, false), null));
242  1 this.existingDocuments.add(new DocumentReference(CURRENT_WIKI, SPACE, PAGE));
243  1 assertEquals(new DocumentReference(CURRENT_WIKI, SPACE, PAGE),
244    this.mocker.getComponentUnderTest().resolve(documentResource(SPACE + '.' + PAGE, false), null));
245  1 this.existingDocuments.add(new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, PAGE));
246  1 assertEquals(new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, PAGE),
247    this.mocker.getComponentUnderTest().resolve(documentResource(PAGE, false), null));
248   
249    // When the page is current page
250   
251  1 assertEquals(new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, CURRENT_PAGE),
252    this.mocker.getComponentUnderTest().resolve(documentResource(CURRENT_PAGE, false), null));
253   
254  1 assertEquals(new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, CURRENT_PAGE),
255    this.mocker.getComponentUnderTest().resolve(documentResource("", false), null));
256    }
257   
 
258  1 toggle @Test
259    public void resolveUntypeDocumentWhenCurrentPageIsSpace() throws ComponentLookupException
260    {
261    // Current is top level space
262   
263  1 when(this.currentDocumentProvider.get())
264    .thenReturn(new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, DEFAULT_PAGE));
265   
266  1 assertEquals(new DocumentReference(CURRENT_WIKI, Arrays.asList(CURRENT_SPACE, PAGE), DEFAULT_PAGE),
267    this.mocker.getComponentUnderTest().resolve(documentResource(PAGE, false), null));
268   
269  1 assertEquals(new DocumentReference(CURRENT_WIKI, Arrays.asList(SPACE, PAGE), DEFAULT_PAGE),
270    this.mocker.getComponentUnderTest().resolve(documentResource(SPACE + '.' + PAGE, false), null));
271   
272  1 assertEquals(new DocumentReference(CURRENT_WIKI, Arrays.asList(CURRENT_SPACE, PAGE), DEFAULT_PAGE),
273    this.mocker.getComponentUnderTest().resolve(documentResource('.' + PAGE, false), null));
274   
275  1 assertEquals(new DocumentReference(CURRENT_WIKI, Arrays.asList(CURRENT_SPACE, PAGE), DEFAULT_PAGE), this.mocker
276    .getComponentUnderTest().resolve(documentResource('.' + PAGE + '.' + DEFAULT_PAGE, false), null));
277   
278    // Current is subspace
279   
280    // When sibling page does not exist
281   
282  1 when(this.currentDocumentProvider.get()).thenReturn(
283    new DocumentReference(CURRENT_WIKI, Arrays.asList(CURRENT_SPACE, CURRENT_SUBSPACE), DEFAULT_PAGE));
284   
285  1 assertEquals(new DocumentReference(CURRENT_WIKI, Arrays.asList(CURRENT_SPACE, CURRENT_SUBSPACE, PAGE),
286    DEFAULT_PAGE),
287    this.mocker.getComponentUnderTest().resolve(documentResource(PAGE, false), null));
288   
289  1 assertEquals(new DocumentReference(CURRENT_WIKI, Arrays.asList(SPACE, PAGE), DEFAULT_PAGE),
290    this.mocker.getComponentUnderTest().resolve(documentResource(SPACE + '.' + PAGE, false), null));
291   
292  1 assertEquals(new DocumentReference(CURRENT_WIKI, Arrays.asList(CURRENT_SPACE, CURRENT_SUBSPACE, PAGE),
293    DEFAULT_PAGE),
294    this.mocker.getComponentUnderTest().resolve(documentResource('.' + PAGE, false), null));
295   
296  1 assertEquals(
297    new DocumentReference(CURRENT_WIKI, Arrays.asList(CURRENT_SPACE, CURRENT_SUBSPACE, PAGE), DEFAULT_PAGE),
298    this.mocker.getComponentUnderTest().resolve(documentResource('.' + PAGE + '.' + DEFAULT_PAGE, false),
299    null));
300   
301    // When sibling page exist
302   
303  1 this.existingDocuments.add(new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, PAGE));
304   
305  1 assertEquals(new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, PAGE),
306    this.mocker.getComponentUnderTest().resolve(documentResource(PAGE, false), null));
307   
308  1 assertEquals(new DocumentReference(CURRENT_WIKI, Arrays.asList(SPACE, PAGE), DEFAULT_PAGE),
309    this.mocker.getComponentUnderTest().resolve(documentResource(SPACE + '.' + PAGE, false), null));
310   
311    // FIXME: This should always be resolved to a child (terminal or non-terminal) page and never to a sibling.
312  1 assertEquals(new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, PAGE),
313    this.mocker.getComponentUnderTest().resolve(documentResource('.' + PAGE, false), null));
314   
315  1 assertEquals(
316    new DocumentReference(CURRENT_WIKI, Arrays.asList(CURRENT_SPACE, CURRENT_SUBSPACE, PAGE), DEFAULT_PAGE),
317    this.mocker.getComponentUnderTest().resolve(documentResource('.' + PAGE + '.' + DEFAULT_PAGE, false),
318    null));
319    }
320   
 
321  1 toggle @Test
322    public void resolveTypeSpace() throws ComponentLookupException
323    {
324  1 assertEquals(new SpaceReference(WIKI, SPACE),
325    this.mocker.getComponentUnderTest().resolve(spaceResource(WIKI + ':' + SPACE, true), null));
326   
327  1 assertEquals(new SpaceReference(CURRENT_WIKI, SPACE),
328    this.mocker.getComponentUnderTest().resolve(spaceResource(SPACE, true), null));
329   
330  1 assertEquals(new SpaceReference(CURRENT_WIKI, CURRENT_SPACE),
331    this.mocker.getComponentUnderTest().resolve(spaceResource("", true), null));
332   
333  1 when(this.currentEntityReferenceResolver.resolve(eq(WIKI + ':' + SPACE + '.' + PAGE), eq(EntityType.DOCUMENT),
334    any())).thenReturn(new DocumentReference(WIKI, SPACE, PAGE));
335   
336  1 ResourceReference withBaseReference = spaceResource("", true);
337  1 withBaseReference.addBaseReference(WIKI + ':' + SPACE + '.' + PAGE);
338  1 assertEquals(new SpaceReference(WIKI, SPACE),
339    this.mocker.getComponentUnderTest().resolve(withBaseReference, null));
340   
341  1 assertEquals(new SpaceReference(WIKI, SPACE), this.mocker.getComponentUnderTest()
342    .resolve(spaceResource("", true), null, new DocumentReference(WIKI, SPACE, PAGE)));
343    }
344   
 
345  1 toggle @Test
346    public void resolveTypeAttachment() throws ComponentLookupException
347    {
348    // When the page does not exist
349   
350  1 assertEquals(
351    new AttachmentReference(ATTACHMENT, new DocumentReference(WIKI, Arrays.asList(SPACE, PAGE), DEFAULT_PAGE)),
352    this.mocker.getComponentUnderTest()
353    .resolve(attachmentResource(WIKI + ':' + SPACE + '.' + PAGE + '@' + ATTACHMENT, true), null));
354   
355  1 assertEquals(
356    new AttachmentReference(ATTACHMENT,
357    new DocumentReference(CURRENT_WIKI, Arrays.asList(SPACE, PAGE), DEFAULT_PAGE)),
358    this.mocker.getComponentUnderTest().resolve(attachmentResource(SPACE + '.' + PAGE + '@' + ATTACHMENT, true),
359    null));
360   
361  1 assertEquals(
362    new AttachmentReference(ATTACHMENT,
363    new DocumentReference(CURRENT_WIKI, Arrays.asList(CURRENT_SPACE, PAGE), DEFAULT_PAGE)),
364    this.mocker.getComponentUnderTest().resolve(attachmentResource(PAGE + '@' + ATTACHMENT, true), null));
365   
366    // When the page exist
367   
368  1 this.existingDocuments.add(new DocumentReference(WIKI, SPACE, PAGE));
369  1 assertEquals(new AttachmentReference(ATTACHMENT, new DocumentReference(WIKI, SPACE, PAGE)),
370    this.mocker.getComponentUnderTest()
371    .resolve(attachmentResource(WIKI + ':' + SPACE + '.' + PAGE + '@' + ATTACHMENT, true), null));
372   
373  1 this.existingDocuments.add(new DocumentReference(CURRENT_WIKI, SPACE, PAGE));
374  1 assertEquals(new AttachmentReference(ATTACHMENT, new DocumentReference(CURRENT_WIKI, SPACE, PAGE)), this.mocker
375    .getComponentUnderTest().resolve(attachmentResource(SPACE + '.' + PAGE + '@' + ATTACHMENT, true), null));
376   
377  1 this.existingDocuments.add(new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, PAGE));
378  1 assertEquals(new AttachmentReference(ATTACHMENT, new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, PAGE)),
379    this.mocker.getComponentUnderTest().resolve(attachmentResource(PAGE + '@' + ATTACHMENT, true), null));
380   
381    // When page is current page
382   
383  1 assertEquals(
384    new AttachmentReference(ATTACHMENT, new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, CURRENT_PAGE)),
385    this.mocker.getComponentUnderTest().resolve(attachmentResource(ATTACHMENT, true), null));
386   
387  1 this.existingDocuments.add(new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, CURRENT_PAGE));
388  1 assertEquals(
389    new AttachmentReference(ATTACHMENT, new DocumentReference(CURRENT_WIKI, CURRENT_SPACE, CURRENT_PAGE)),
390    this.mocker.getComponentUnderTest().resolve(attachmentResource(ATTACHMENT, true), null));
391    }
392   
 
393    class VoidResourceReferenceEntityReferenceResolve extends AbstractResourceReferenceEntityReferenceResolver
394    {
 
395  1 toggle VoidResourceReferenceEntityReferenceResolve()
396    {
397  1 this.documentAccessBridge = DefaultResourceReferenceEntityReferenceResolverTest.this.bridge;
398    }
399   
 
400  0 toggle @Override
401    protected EntityReference resolveTyped(ResourceReference resourceReference, EntityReference baseReference)
402    {
403  0 return null;
404    }
405    }
406   
 
407  1 toggle @Test
408    public void trySpaceSiblingFallback()
409    {
410  1 VoidResourceReferenceEntityReferenceResolve resolver = new VoidResourceReferenceEntityReferenceResolve();
411   
412  1 String defaultDocumentName = "Foo";
413  1 EntityReference sourceReference = new EntityReference("Bar", EntityType.DOCUMENT);
414  1 DocumentReference finalReference = new DocumentReference("xwiki", "Bar",
415    "WebHome");
416  1 EntityReference baseReference = sourceReference;
417   
418  1 assertFalse(resolver.trySpaceSiblingFallback(sourceReference, finalReference, null,
419    defaultDocumentName));
420  1 assertFalse(resolver.trySpaceSiblingFallback(sourceReference, finalReference, baseReference,
421    defaultDocumentName));
422   
423  1 defaultDocumentName = "Bar";
424  1 assertTrue(resolver.trySpaceSiblingFallback(sourceReference, finalReference, baseReference,
425    defaultDocumentName));
426   
427  1 this.existingDocuments.add(finalReference);
428  1 assertFalse(resolver.trySpaceSiblingFallback(sourceReference, finalReference, baseReference,
429    defaultDocumentName));
430    }
431    }