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

File DefaultStringEntityReferenceResolverTest.java

 

Code metrics

0
340
14
1
611
450
14
0.04
24.29
14
1

Classes

Class Line # Actions
DefaultStringEntityReferenceResolverTest 56 340 0% 14 0
1.0100%
 

Contributing tests

This file is covered by 13 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.model.internal.reference;
21   
22    import java.io.Serializable;
23    import java.util.Arrays;
24    import java.util.Collections;
25    import java.util.HashMap;
26    import java.util.Locale;
27    import java.util.Map;
28   
29    import org.junit.jupiter.api.BeforeEach;
30    import org.junit.jupiter.api.Test;
31    import org.xwiki.model.EntityType;
32    import org.xwiki.model.reference.ClassPropertyReference;
33    import org.xwiki.model.reference.DocumentReference;
34    import org.xwiki.model.reference.EntityReference;
35    import org.xwiki.model.reference.EntityReferenceProvider;
36    import org.xwiki.model.reference.ObjectPropertyReference;
37    import org.xwiki.model.reference.PageReference;
38    import org.xwiki.model.reference.SpaceReference;
39    import org.xwiki.model.reference.WikiReference;
40    import org.xwiki.model.reference.test.TestConstants;
41    import org.xwiki.test.annotation.ComponentList;
42    import org.xwiki.test.junit5.mockito.ComponentTest;
43    import org.xwiki.test.junit5.mockito.InjectMockComponents;
44    import org.xwiki.test.junit5.mockito.MockComponent;
45   
46    import static org.junit.jupiter.api.Assertions.assertEquals;
47    import static org.mockito.Mockito.when;
48   
49    /**
50    * Unit tests for {@link DefaultStringEntityReferenceResolver}.
51    *
52    * @version $Id: 6d86798f0c305e7e492bdfbf74591d8409e46a5e $
53    */
54    @ComponentTest
55    @ComponentList({ DefaultSymbolScheme.class })
 
56    public class DefaultStringEntityReferenceResolverTest implements TestConstants
57    {
58    @MockComponent
59    private EntityReferenceProvider referenceProvider;
60   
61    @InjectMockComponents
62    private DefaultStringEntityReferenceResolver resolver;
63   
 
64  13 toggle @BeforeEach
65    public void setUp() throws Exception
66    {
67  13 when(this.referenceProvider.getDefaultReference(EntityType.WIKI)).thenReturn(DEFAULT_WIKI_REFERENCE);
68  13 when(this.referenceProvider.getDefaultReference(EntityType.SPACE)).thenReturn(DEFAULT_SPACE_REFERENCE);
69  13 when(this.referenceProvider.getDefaultReference(EntityType.DOCUMENT)).thenReturn(DEFAULT_DOCUMENT_REFERENCE);
70  13 when(this.referenceProvider.getDefaultReference(EntityType.ATTACHMENT))
71    .thenReturn(DEFAULT_ATTACHMENT_REFERENCE);
72  13 when(this.referenceProvider.getDefaultReference(EntityType.OBJECT)).thenReturn(DEFAULT_OBJECT_REFERENCE);
73  13 when(this.referenceProvider.getDefaultReference(EntityType.OBJECT_PROPERTY))
74    .thenReturn(DEFAULT_OBJECT_PROPERTY_REFERENCE);
75  13 when(this.referenceProvider.getDefaultReference(EntityType.CLASS_PROPERTY))
76    .thenReturn(DEFAULT_CLASS_PROPERTY_REFERENCE);
77  13 when(this.referenceProvider.getDefaultReference(EntityType.PAGE)).thenReturn(DEFAULT_PAGE_REFERENCE);
78  13 when(this.referenceProvider.getDefaultReference(EntityType.PAGE_ATTACHMENT))
79    .thenReturn(DEFAULT_PAGE_ATTACHMENT_REFERENCE);
80  13 when(this.referenceProvider.getDefaultReference(EntityType.PAGE_OBJECT))
81    .thenReturn(DEFAULT_PAGE_OBJECT_REFERENCE);
82  13 when(this.referenceProvider.getDefaultReference(EntityType.PAGE_OBJECT_PROPERTY))
83    .thenReturn(DEFAULT_PAGE_OBJECT_PROPERTY_REFERENCE);
84  13 when(this.referenceProvider.getDefaultReference(EntityType.PAGE_CLASS_PROPERTY))
85    .thenReturn(DEFAULT_PAGE_CLASS_PROPERTY_REFERENCE);
86    }
87   
 
88  1 toggle @Test
89    public void testResolveDocumentReference() throws Exception
90    {
91  1 EntityReference reference = resolver.resolve("wiki:space.page", EntityType.DOCUMENT);
92  1 assertEquals("wiki", reference.extractReference(EntityType.WIKI).getName());
93  1 assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
94  1 assertEquals("page", reference.getName());
95   
96  1 reference = resolver.resolve("wiki:space.", EntityType.DOCUMENT);
97  1 assertEquals("wiki", reference.extractReference(EntityType.WIKI).getName());
98  1 assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
99  1 assertEquals(DEFAULT_DOCUMENT, reference.getName());
100   
101  1 reference = resolver.resolve("space.", EntityType.DOCUMENT);
102  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
103  1 assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
104  1 assertEquals(DEFAULT_DOCUMENT, reference.getName());
105   
106  1 reference = resolver.resolve("page", EntityType.DOCUMENT);
107  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
108  1 assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
109  1 assertEquals("page", reference.getName());
110   
111  1 reference = resolver.resolve(".", EntityType.DOCUMENT);
112  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
113  1 assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
114  1 assertEquals(DEFAULT_DOCUMENT, reference.getName());
115   
116  1 reference = resolver.resolve(null, EntityType.DOCUMENT);
117  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
118  1 assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
119  1 assertEquals(DEFAULT_DOCUMENT, reference.getName());
120   
121  1 reference = resolver.resolve("", EntityType.DOCUMENT);
122  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
123  1 assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
124  1 assertEquals(DEFAULT_DOCUMENT, reference.getName());
125   
126  1 reference = resolver.resolve("wiki1.wiki2:wiki3:some.space.page", EntityType.DOCUMENT);
127  1 assertEquals("wiki1.wiki2:wiki3", reference.extractReference(EntityType.WIKI).getName());
128  1 assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
129  1 assertEquals("some", reference.extractReference(EntityType.SPACE).getParent().getName());
130  1 assertEquals("page", reference.getName());
131   
132  1 reference = resolver.resolve("some.space.page", EntityType.DOCUMENT);
133  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
134  1 assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
135  1 assertEquals("some", reference.extractReference(EntityType.SPACE).getParent().getName());
136  1 assertEquals("page", reference.getName());
137   
138  1 reference = resolver.resolve("wiki:page", EntityType.DOCUMENT);
139  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
140  1 assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
141  1 assertEquals("wiki:page", reference.getName());
142   
143    // Test escapes
144   
145  1 reference = resolver.resolve("\\\\\\.:@\\.", EntityType.DOCUMENT);
146  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
147  1 assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
148  1 assertEquals("\\.:@.", reference.getName());
149   
150  1 reference = resolver.resolve("some\\.space.page", EntityType.DOCUMENT);
151  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
152  1 assertEquals("some.space", reference.extractReference(EntityType.SPACE).getName());
153  1 assertEquals("page", reference.getName());
154   
155    // Escaping characters are escaped
156  1 reference = resolver.resolve("\\\\:\\\\.\\\\", EntityType.DOCUMENT);
157  1 assertEquals("\\", reference.extractReference(EntityType.WIKI).getName());
158  1 assertEquals("\\", reference.extractReference(EntityType.SPACE).getName());
159  1 assertEquals("\\", reference.getName());
160   
161  1 reference = resolver.resolve("\\wiki:\\space.\\page", EntityType.DOCUMENT);
162  1 assertEquals("wiki", reference.extractReference(EntityType.WIKI).getName());
163  1 assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
164  1 assertEquals("page", reference.getName());
165    }
166   
 
167  1 toggle @Test
168    public void testResolveDocumentReferenceWithReferenceParameter() throws Exception
169    {
170  1 DocumentReference otherReference = new DocumentReference("otherwiki", "otherspace", "otherpage");
171  1 assertEquals(otherReference, resolver.resolve("", EntityType.DOCUMENT, otherReference));
172    }
173   
 
174  1 toggle @Test
175    public void testResolveAttachmentReference() throws Exception
176    {
177  1 EntityReference reference = resolver.resolve("wiki:space.page@filename.ext", EntityType.ATTACHMENT);
178  1 assertEquals("wiki", reference.extractReference(EntityType.WIKI).getName());
179  1 assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
180  1 assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
181  1 assertEquals("filename.ext", reference.getName());
182   
183  1 reference = resolver.resolve("", EntityType.ATTACHMENT);
184  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
185  1 assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
186  1 assertEquals(DEFAULT_DOCUMENT, reference.extractReference(EntityType.DOCUMENT).getName());
187  1 assertEquals(DEFAULT_ATTACHMENT, reference.getName());
188   
189  1 reference = resolver.resolve("wiki:space.page@my.png", EntityType.ATTACHMENT);
190  1 assertEquals("wiki", reference.extractReference(EntityType.WIKI).getName());
191  1 assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
192  1 assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
193  1 assertEquals("my.png", reference.getName());
194   
195  1 reference = resolver.resolve("some:file.name", EntityType.ATTACHMENT);
196  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
197  1 assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
198  1 assertEquals(DEFAULT_DOCUMENT, reference.extractReference(EntityType.DOCUMENT).getName());
199  1 assertEquals("some:file.name", reference.getName());
200   
201    // Test escapes
202   
203  1 reference = resolver.resolve(":.\\@", EntityType.ATTACHMENT);
204  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
205  1 assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
206  1 assertEquals(DEFAULT_DOCUMENT, reference.extractReference(EntityType.DOCUMENT).getName());
207  1 assertEquals(":.@", reference.getName());
208    }
209   
210    /**
211    * Tests resolving object references.
212    */
 
213  1 toggle @Test
214    public void testResolveObjectReference()
215    {
216  1 EntityReference reference = resolver.resolve("wiki:space.page^Object", EntityType.OBJECT);
217  1 assertEquals("wiki", reference.extractReference(EntityType.WIKI).getName());
218  1 assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
219  1 assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
220  1 assertEquals("Object", reference.getName());
221   
222    // default values
223  1 reference = resolver.resolve("", EntityType.OBJECT);
224  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
225  1 assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
226  1 assertEquals(DEFAULT_DOCUMENT, reference.extractReference(EntityType.DOCUMENT).getName());
227  1 assertEquals(DEFAULT_OBJECT, reference.getName());
228   
229    // without some of the parents
230  1 reference = resolver.resolve("space.page^Object", EntityType.OBJECT);
231  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
232  1 assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
233  1 assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
234  1 assertEquals("Object", reference.getName());
235   
236  1 reference = resolver.resolve("page^Object", EntityType.OBJECT);
237  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
238  1 assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
239  1 assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
240  1 assertEquals("Object", reference.getName());
241   
242  1 reference = resolver.resolve("XWiki.Class", EntityType.OBJECT);
243  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
244  1 assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
245  1 assertEquals(DEFAULT_DOCUMENT, reference.extractReference(EntityType.DOCUMENT).getName());
246  1 assertEquals("XWiki.Class", reference.getName());
247   
248    // property without object
249  1 reference = resolver.resolve("wiki:space.page.property", EntityType.OBJECT);
250  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
251  1 assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
252  1 assertEquals(DEFAULT_DOCUMENT, reference.extractReference(EntityType.DOCUMENT).getName());
253  1 assertEquals("wiki:space.page.property", reference.getName());
254   
255    // object with no name
256  1 reference = resolver.resolve("wiki:space.page^", EntityType.OBJECT);
257  1 assertEquals("wiki", reference.extractReference(EntityType.WIKI).getName());
258  1 assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
259  1 assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
260  1 assertEquals(DEFAULT_OBJECT, reference.getName());
261   
262    // test separator escape
263  1 reference = resolver.resolve("wiki:space.page^obje\\^ct", EntityType.OBJECT);
264  1 assertEquals("wiki", reference.extractReference(EntityType.WIKI).getName());
265  1 assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
266  1 assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
267  1 assertEquals("obje^ct", reference.getName());
268   
269    // and that separators don't need to be escaped other than in the object name
270  1 reference = resolver.resolve("wiki:spa^ce.page^Object", EntityType.OBJECT);
271  1 assertEquals("wiki", reference.extractReference(EntityType.WIKI).getName());
272  1 assertEquals("spa^ce", reference.extractReference(EntityType.SPACE).getName());
273  1 assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
274  1 assertEquals("Object", reference.getName());
275   
276  1 reference = resolver.resolve(":.\\^@", EntityType.OBJECT);
277  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
278  1 assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
279  1 assertEquals(DEFAULT_DOCUMENT, reference.extractReference(EntityType.DOCUMENT).getName());
280  1 assertEquals(":.^@", reference.getName());
281    }
282   
283    /**
284    * Tests resolving object references.
285    */
 
286  1 toggle @Test
287    public void testResolveObjectPropertyReference()
288    {
289  1 EntityReference reference =
290    new ObjectPropertyReference(resolver.resolve("wiki:space.page^object.prop", EntityType.OBJECT_PROPERTY));
291  1 assertEquals("wiki", reference.extractReference(EntityType.WIKI).getName());
292  1 assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
293  1 assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
294  1 assertEquals("object", reference.extractReference(EntityType.OBJECT).getName());
295  1 assertEquals("prop", reference.extractReference(EntityType.OBJECT_PROPERTY).getName());
296   
297    // default values
298  1 reference = new ObjectPropertyReference(resolver.resolve("", EntityType.OBJECT_PROPERTY));
299  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
300  1 assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
301  1 assertEquals(DEFAULT_DOCUMENT, reference.extractReference(EntityType.DOCUMENT).getName());
302  1 assertEquals(DEFAULT_OBJECT, reference.extractReference(EntityType.OBJECT).getName());
303  1 assertEquals(DEFAULT_OBJECT_PROPERTY, reference.extractReference(EntityType.OBJECT_PROPERTY).getName());
304   
305    // without some of the parents
306  1 reference = new ObjectPropertyReference(resolver.resolve("space.page^Object.prop", EntityType.OBJECT_PROPERTY));
307  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
308  1 assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
309  1 assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
310  1 assertEquals("Object", reference.extractReference(EntityType.OBJECT).getName());
311  1 assertEquals("prop", reference.extractReference(EntityType.OBJECT_PROPERTY).getName());
312   
313  1 reference = new ObjectPropertyReference(resolver.resolve("page^Object.prop", EntityType.OBJECT_PROPERTY));
314  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
315  1 assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
316  1 assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
317  1 assertEquals("Object", reference.extractReference(EntityType.OBJECT).getName());
318  1 assertEquals("prop", reference.extractReference(EntityType.OBJECT_PROPERTY).getName());
319   
320  1 reference = new ObjectPropertyReference(resolver.resolve("Object.prop", EntityType.OBJECT_PROPERTY));
321  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
322  1 assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
323  1 assertEquals(DEFAULT_DOCUMENT, reference.extractReference(EntityType.DOCUMENT).getName());
324  1 assertEquals("Object", reference.extractReference(EntityType.OBJECT).getName());
325  1 assertEquals("prop", reference.extractReference(EntityType.OBJECT_PROPERTY).getName());
326   
327  1 reference = new ObjectPropertyReference(resolver.resolve("FooBar", EntityType.OBJECT_PROPERTY));
328  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
329  1 assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
330  1 assertEquals(DEFAULT_DOCUMENT, reference.extractReference(EntityType.DOCUMENT).getName());
331  1 assertEquals(DEFAULT_OBJECT, reference.extractReference(EntityType.OBJECT).getName());
332  1 assertEquals("FooBar", reference.extractReference(EntityType.OBJECT_PROPERTY).getName());
333   
334    // object without property, parsed as property
335  1 reference = new ObjectPropertyReference(resolver.resolve("page^Object", EntityType.OBJECT_PROPERTY));
336  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
337  1 assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
338  1 assertEquals(DEFAULT_DOCUMENT, reference.extractReference(EntityType.DOCUMENT).getName());
339  1 assertEquals(DEFAULT_OBJECT, reference.extractReference(EntityType.OBJECT).getName());
340  1 assertEquals("page^Object", reference.extractReference(EntityType.OBJECT_PROPERTY).getName());
341   
342    // empty prop
343  1 reference =
344    new ObjectPropertyReference(resolver.resolve("wiki:space.page^Object.", EntityType.OBJECT_PROPERTY));
345  1 assertEquals("wiki", reference.extractReference(EntityType.WIKI).getName());
346  1 assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
347  1 assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
348  1 assertEquals("Object", reference.extractReference(EntityType.OBJECT).getName());
349  1 assertEquals(DEFAULT_OBJECT_PROPERTY, reference.extractReference(EntityType.OBJECT_PROPERTY).getName());
350   
351    // test separator escape
352  1 reference = new ObjectPropertyReference(
353    resolver.resolve("wiki:space.page^Object.prop\\.erty", EntityType.OBJECT_PROPERTY));
354  1 assertEquals("wiki", reference.extractReference(EntityType.WIKI).getName());
355  1 assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
356  1 assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
357  1 assertEquals("Object", reference.extractReference(EntityType.OBJECT).getName());
358  1 assertEquals("prop.erty", reference.extractReference(EntityType.OBJECT_PROPERTY).getName());
359   
360    // and that separators don't need to be escaped other than in the property name
361  1 reference = new ObjectPropertyReference(
362    resolver.resolve("wiki:space.page^x.wiki.class[0].prop", EntityType.OBJECT_PROPERTY));
363  1 assertEquals("wiki", reference.extractReference(EntityType.WIKI).getName());
364  1 assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
365  1 assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
366  1 assertEquals("x.wiki.class[0]", reference.extractReference(EntityType.OBJECT).getName());
367  1 assertEquals("prop", reference.extractReference(EntityType.OBJECT_PROPERTY).getName());
368   
369  1 reference = new ObjectPropertyReference(resolver.resolve(":^\\.@", EntityType.OBJECT_PROPERTY));
370  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
371  1 assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
372  1 assertEquals(DEFAULT_DOCUMENT, reference.extractReference(EntityType.DOCUMENT).getName());
373  1 assertEquals(DEFAULT_OBJECT, reference.extractReference(EntityType.OBJECT).getName());
374  1 assertEquals(":^.@", reference.extractReference(EntityType.OBJECT_PROPERTY).getName());
375    }
376   
377    /**
378    * Tests resolving object references.
379    */
 
380  1 toggle @Test
381    public void testResolveClassPropertyReference()
382    {
383  1 EntityReference reference =
384    new ClassPropertyReference(resolver.resolve("wiki:space.page^ClassProp", EntityType.CLASS_PROPERTY));
385  1 assertEquals("wiki", reference.extractReference(EntityType.WIKI).getName());
386  1 assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
387  1 assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
388  1 assertEquals("ClassProp", reference.extractReference(EntityType.CLASS_PROPERTY).getName());
389   
390    // default values
391  1 reference = new ClassPropertyReference(resolver.resolve("", EntityType.CLASS_PROPERTY));
392  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
393  1 assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
394  1 assertEquals(DEFAULT_DOCUMENT, reference.extractReference(EntityType.DOCUMENT).getName());
395  1 assertEquals(DEFAULT_CLASS_PROPERTY, reference.extractReference(EntityType.CLASS_PROPERTY).getName());
396   
397    // without some of the parents
398  1 reference = new ClassPropertyReference(resolver.resolve("space.page^ClassProp", EntityType.CLASS_PROPERTY));
399  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
400  1 assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
401  1 assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
402  1 assertEquals("ClassProp", reference.extractReference(EntityType.CLASS_PROPERTY).getName());
403   
404  1 reference = new ClassPropertyReference(resolver.resolve("page^ClassProp", EntityType.CLASS_PROPERTY));
405  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
406  1 assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
407  1 assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
408  1 assertEquals("ClassProp", reference.extractReference(EntityType.CLASS_PROPERTY).getName());
409   
410  1 reference = new ClassPropertyReference(resolver.resolve("XWiki.Class", EntityType.CLASS_PROPERTY));
411  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
412  1 assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
413  1 assertEquals(DEFAULT_DOCUMENT, reference.extractReference(EntityType.DOCUMENT).getName());
414  1 assertEquals("XWiki.Class", reference.extractReference(EntityType.CLASS_PROPERTY).getName());
415   
416    // property without object
417  1 reference = new ClassPropertyReference(resolver.resolve("wiki:space.page.property", EntityType.CLASS_PROPERTY));
418  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
419  1 assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
420  1 assertEquals(DEFAULT_DOCUMENT, reference.extractReference(EntityType.DOCUMENT).getName());
421  1 assertEquals("wiki:space.page.property", reference.extractReference(EntityType.CLASS_PROPERTY).getName());
422   
423    // object with no name
424  1 reference = new ClassPropertyReference(resolver.resolve("wiki:space.page^", EntityType.CLASS_PROPERTY));
425  1 assertEquals("wiki", reference.extractReference(EntityType.WIKI).getName());
426  1 assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
427  1 assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
428  1 assertEquals(DEFAULT_CLASS_PROPERTY, reference.extractReference(EntityType.CLASS_PROPERTY).getName());
429   
430    // test separator escape
431  1 reference =
432    new ClassPropertyReference(resolver.resolve("wiki:space.page^obje\\^ct", EntityType.CLASS_PROPERTY));
433  1 assertEquals("wiki", reference.extractReference(EntityType.WIKI).getName());
434  1 assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
435  1 assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
436  1 assertEquals("obje^ct", reference.extractReference(EntityType.CLASS_PROPERTY).getName());
437   
438    // and that separators don't need to be escaped other than in the object name
439  1 reference =
440    new ClassPropertyReference(resolver.resolve("wiki:spa^ce.page^ClassProp", EntityType.CLASS_PROPERTY));
441  1 assertEquals("wiki", reference.extractReference(EntityType.WIKI).getName());
442  1 assertEquals("spa^ce", reference.extractReference(EntityType.SPACE).getName());
443  1 assertEquals("page", reference.extractReference(EntityType.DOCUMENT).getName());
444  1 assertEquals("ClassProp", reference.extractReference(EntityType.CLASS_PROPERTY).getName());
445   
446  1 reference = new ClassPropertyReference(resolver.resolve(":.\\^@", EntityType.CLASS_PROPERTY));
447  1 assertEquals(DEFAULT_WIKI, reference.extractReference(EntityType.WIKI).getName());
448  1 assertEquals(DEFAULT_SPACE, reference.extractReference(EntityType.SPACE).getName());
449  1 assertEquals(DEFAULT_DOCUMENT, reference.extractReference(EntityType.DOCUMENT).getName());
450  1 assertEquals(":.^@", reference.extractReference(EntityType.CLASS_PROPERTY).getName());
451    }
452   
 
453  1 toggle @Test
454    public void testResolveDocumentReferenceWithExplicitReference()
455    {
456  1 EntityReference reference = resolver.resolve("page", EntityType.DOCUMENT,
457    new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI)));
458  1 assertEquals("wiki", reference.extractReference(EntityType.WIKI).getName());
459  1 assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
460  1 assertEquals("page", reference.getName());
461    }
462   
463    /**
464    * Tests resolving space references.
465    */
 
466  1 toggle @Test
467    public void testResolveSpaceReference()
468    {
469  1 SpaceReference reference = new SpaceReference(this.resolver.resolve("space", EntityType.SPACE));
470  1 assertEquals("space", reference.getName());
471   
472    // several spaces
473  1 reference = new SpaceReference(this.resolver.resolve("space1.space2", EntityType.SPACE));
474  1 assertEquals("space2", reference.getName());
475  1 assertEquals("space1", reference.getParent().getName());
476    }
477   
478    /**
479    * Tests resolving wiki references.
480    */
 
481  1 toggle @Test
482    public void testResolveWikiReference()
483    {
484  1 WikiReference reference = new WikiReference(resolver.resolve("dev", EntityType.WIKI));
485  1 assertEquals("dev", reference.getName());
486   
487    // default value
488  1 reference = new WikiReference(resolver.resolve("", EntityType.WIKI));
489  1 assertEquals(DEFAULT_WIKI, reference.getName());
490   
491    // with parameter
492  1 DocumentReference baseReference = new DocumentReference("math", "Theorems", "Pitagora");
493  1 reference = new WikiReference(resolver.resolve("", EntityType.WIKI, baseReference));
494  1 assertEquals(baseReference.getWikiReference(), reference);
495   
496    // special symbols
497  1 reference = new WikiReference(resolver.resolve("dev:lab.uix", EntityType.WIKI));
498  1 assertEquals("dev:lab.uix", reference.getName());
499   
500    // escaping
501  1 reference = new WikiReference(resolver.resolve("dev\\:lab\\.uix", EntityType.WIKI));
502  1 assertEquals("dev:lab.uix", reference.getName());
503    }
504   
 
505  1 toggle @Test
506    public void testResolvePageReference() throws Exception
507    {
508  1 EntityReference reference = this.resolver.resolve("wiki:space/page", EntityType.PAGE);
509  1 assertEquals(new PageReference("wiki", "space", "page"), reference);
510   
511  1 reference = this.resolver.resolve("page", EntityType.PAGE);
512  1 assertEquals(new PageReference(DEFAULT_WIKI, "page"), reference);
513   
514  1 reference = this.resolver.resolve("/", EntityType.PAGE);
515  1 assertEquals(new PageReference(DEFAULT_WIKI, DEFAULT_PAGE, DEFAULT_PAGE), reference);
516   
517  1 reference = this.resolver.resolve(null, EntityType.PAGE);
518  1 assertEquals(new PageReference(DEFAULT_WIKI, DEFAULT_PAGE), reference);
519   
520  1 reference = this.resolver.resolve("", EntityType.PAGE);
521  1 assertEquals(new PageReference(DEFAULT_WIKI, DEFAULT_PAGE), reference);
522   
523  1 reference = this.resolver.resolve("wiki1/wiki2:wiki3:some/space/page", EntityType.PAGE);
524  1 assertEquals(new PageReference("wiki1/wiki2:wiki3", "some", "space", "page"), reference);
525   
526  1 reference = this.resolver.resolve("some/space/page", EntityType.PAGE);
527  1 assertEquals(new PageReference(DEFAULT_WIKI, "some", "space", "page"), reference);
528   
529    // Test escapes
530   
531  1 reference = this.resolver.resolve("some\\/space/page", EntityType.PAGE);
532  1 assertEquals(new PageReference(DEFAULT_WIKI, "some/space", "page"), reference);
533   
534    // Escaping characters are escaped
535  1 reference = this.resolver.resolve("\\\\:\\\\/\\\\", EntityType.PAGE);
536  1 assertEquals(new PageReference("\\", "\\", "\\"), reference);
537   
538  1 reference = this.resolver.resolve("\\wiki:\\space/\\page", EntityType.PAGE);
539  1 assertEquals(new PageReference("wiki", "space", "page"), reference);
540    }
541   
 
542  1 toggle @Test
543    public void testResolvePageReferenceKeywords() throws Exception
544    {
545  1 EntityReference reference = this.resolver.resolve(".", EntityType.PAGE);
546  1 assertEquals(new PageReference(DEFAULT_WIKI, DEFAULT_PAGE), reference);
547   
548  1 reference = this.resolver.resolve("page/.", EntityType.PAGE);
549  1 assertEquals(new PageReference(DEFAULT_WIKI, "page"), reference);
550   
551  1 reference = this.resolver.resolve("page/./.", EntityType.PAGE);
552  1 assertEquals(new PageReference(DEFAULT_WIKI, "page"), reference);
553   
554  1 reference = this.resolver.resolve("..", EntityType.PAGE);
555  1 assertEquals(new WikiReference(DEFAULT_WIKI), reference);
556   
557  1 reference = this.resolver.resolve("page/..", EntityType.PAGE);
558  1 assertEquals(new WikiReference(DEFAULT_WIKI), reference);
559   
560  1 reference = this.resolver.resolve("page1/page2/..", EntityType.PAGE);
561  1 assertEquals(new PageReference(DEFAULT_WIKI, "page1"), reference);
562    }
563   
 
564  1 toggle @Test
565    public void testResolvePageReferenceKeywordsWithDocumentParameter() throws Exception
566    {
567  1 EntityReference reference = this.resolver.resolve(".", EntityType.PAGE,
568    new DocumentReference("wiki", Arrays.asList("space1", "space2"), DEFAULT_DOCUMENT));
569  1 assertEquals(new PageReference("wiki", "space1", "space2"), reference);
570   
571  1 reference = this.resolver.resolve("..", EntityType.PAGE,
572    new DocumentReference("wiki", Arrays.asList("space"), DEFAULT_DOCUMENT));
573  1 assertEquals(new WikiReference("wiki"), reference);
574  1 reference = this.resolver.resolve("..", EntityType.PAGE,
575    new DocumentReference("wiki", Arrays.asList("space1", "space2"), DEFAULT_DOCUMENT));
576  1 assertEquals(new PageReference("wiki", "space1"), reference);
577  1 reference = this.resolver.resolve("..", EntityType.PAGE,
578    new DocumentReference("wiki", Arrays.asList("space1", "space2", "space3"), DEFAULT_DOCUMENT));
579  1 assertEquals(new PageReference("wiki", "space1", "space2"), reference);
580    }
581   
 
582  1 toggle @Test
583    public void testResolvePageReferenceParameters() throws Exception
584    {
585  1 PageReference pageReference = new PageReference("wiki", "space", "page");
586  1 Map<String, Serializable> parameters = new HashMap<>();
587  1 parameters.put("key1", "value1");
588  1 parameters.put("key2", "value2");
589   
590  1 EntityReference reference;
591   
592  1 reference = new PageReference(this.resolver.resolve("wiki:space/page;en", EntityType.PAGE));
593  1 assertEquals(new PageReference(pageReference, Locale.ENGLISH), reference);
594   
595  1 reference = new PageReference(this.resolver.resolve("wiki:space/page;en;fr", EntityType.PAGE));
596  1 assertEquals(new PageReference(pageReference, Locale.FRENCH), reference);
597   
598  1 reference = new PageReference(this.resolver.resolve("wiki:space/page;locale=en", EntityType.PAGE));
599  1 assertEquals(new PageReference(pageReference, Locale.ENGLISH), reference);
600   
601  1 reference = this.resolver.resolve("wiki:space/page;key1=value1;key2=value2", EntityType.PAGE);
602  1 assertEquals(new EntityReference(pageReference, parameters), reference);
603   
604  1 reference = this.resolver.resolve("wiki:space/page;key=val=ue", EntityType.PAGE);
605  1 assertEquals(new EntityReference(pageReference, Collections.singletonMap("key", "val=ue")), reference);
606   
607  1 reference = this.resolver.resolve("wiki:space/page;key\\=novalue=nokey\\=value", EntityType.PAGE);
608  1 assertEquals(new EntityReference(pageReference, Collections.singletonMap("key=novalue", "nokey=value")),
609    reference);
610    }
611    }