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

File DefaultReferenceEntityReferenceResolverTest.java

 

Code metrics

0
116
17
1
323
242
18
0.16
6.82
17
1.06

Classes

Class Line # Actions
DefaultReferenceEntityReferenceResolverTest 52 116 0% 18 1
0.9924812399.2%
 

Contributing tests

This file is covered by 16 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.util.Arrays;
23   
24    import org.junit.jupiter.api.Test;
25    import org.xwiki.model.EntityType;
26    import org.xwiki.model.reference.DocumentReference;
27    import org.xwiki.model.reference.EntityReference;
28    import org.xwiki.model.reference.EntityReferenceProvider;
29    import org.xwiki.model.reference.InvalidEntityReferenceException;
30    import org.xwiki.model.reference.PageReference;
31    import org.xwiki.model.reference.SpaceReference;
32    import org.xwiki.model.reference.test.TestConstants;
33    import org.xwiki.test.annotation.BeforeComponent;
34    import org.xwiki.test.annotation.ComponentList;
35    import org.xwiki.test.junit5.mockito.ComponentTest;
36    import org.xwiki.test.junit5.mockito.InjectMockComponents;
37    import org.xwiki.test.junit5.mockito.MockComponent;
38   
39    import static org.junit.jupiter.api.Assertions.assertEquals;
40    import static org.junit.jupiter.api.Assertions.assertNotSame;
41    import static org.junit.jupiter.api.Assertions.fail;
42    import static org.mockito.Mockito.when;
43   
44    /**
45    * Unit tests for {@link DefaultReferenceEntityReferenceResolver}.
46    *
47    * @version $Id: 9717c5dbdd91e7b13b730298db40c790ab8d9d14 $
48    * @since 2.2M1
49    */
50    @ComponentTest
51    @ComponentList({ DefaultSymbolScheme.class })
 
52    public class DefaultReferenceEntityReferenceResolverTest implements TestConstants
53    {
54    @MockComponent
55    private EntityReferenceProvider referenceProvider;
56   
57    @InjectMockComponents
58    private DefaultReferenceEntityReferenceResolver resolver;
59   
 
60  16 toggle @BeforeComponent
61    public void beforeComponent()
62    {
63  16 when(this.referenceProvider.getDefaultReference(EntityType.WIKI)).thenReturn(DEFAULT_WIKI_REFERENCE);
64  16 when(this.referenceProvider.getDefaultReference(EntityType.SPACE)).thenReturn(DEFAULT_SPACE_REFERENCE);
65  16 when(this.referenceProvider.getDefaultReference(EntityType.DOCUMENT)).thenReturn(DEFAULT_DOCUMENT_REFERENCE);
66  16 when(this.referenceProvider.getDefaultReference(EntityType.ATTACHMENT)).thenReturn(DEFAULT_ATTACHMENT_REFERENCE);
67  16 when(this.referenceProvider.getDefaultReference(EntityType.OBJECT)).thenReturn(DEFAULT_OBJECT_REFERENCE);
68  16 when(this.referenceProvider.getDefaultReference(EntityType.OBJECT_PROPERTY))
69    .thenReturn(DEFAULT_OBJECT_PROPERTY_REFERENCE);
70  16 when(this.referenceProvider.getDefaultReference(EntityType.PAGE)).thenReturn(DEFAULT_PAGE_REFERENCE);
71  16 when(this.referenceProvider.getDefaultReference(EntityType.PAGE_ATTACHMENT))
72    .thenReturn(DEFAULT_PAGE_ATTACHMENT_REFERENCE);
73  16 when(this.referenceProvider.getDefaultReference(EntityType.PAGE_OBJECT))
74    .thenReturn(DEFAULT_PAGE_OBJECT_REFERENCE);
75  16 when(this.referenceProvider.getDefaultReference(EntityType.PAGE_OBJECT_PROPERTY))
76    .thenReturn(DEFAULT_PAGE_OBJECT_PROPERTY_REFERENCE);
77    }
78   
 
79  1 toggle @Test
80    public void resolveDocumentReferenceWhenMissingParents()
81    {
82  1 EntityReference partialReference = new EntityReference("document", EntityType.DOCUMENT);
83   
84  1 EntityReference reference = this.resolver.resolve(partialReference, EntityType.DOCUMENT);
85   
86  1 assertNotSame(partialReference, reference);
87  1 assertEquals(DEFAULT_SPACE, reference.getParent().getName());
88  1 assertEquals(EntityType.SPACE, reference.getParent().getType());
89  1 assertEquals(DEFAULT_WIKI, reference.getParent().getParent().getName());
90  1 assertEquals(EntityType.WIKI, reference.getParent().getParent().getType());
91    }
92   
 
93  1 toggle @Test
94    public void resolveAttachmentReferenceWhenMissingParents()
95    {
96  1 EntityReference reference =
97    this.resolver.resolve(new EntityReference("filename", EntityType.ATTACHMENT), EntityType.ATTACHMENT);
98   
99  1 assertEquals(DEFAULT_DOCUMENT, reference.getParent().getName());
100  1 assertEquals(EntityType.DOCUMENT, reference.getParent().getType());
101  1 assertEquals(DEFAULT_SPACE, reference.getParent().getParent().getName());
102  1 assertEquals(EntityType.SPACE, reference.getParent().getParent().getType());
103  1 assertEquals(DEFAULT_WIKI, reference.getParent().getParent().getParent().getName());
104  1 assertEquals(EntityType.WIKI, reference.getParent().getParent().getParent().getType());
105    }
106   
 
107  1 toggle @Test
108    public void resolveDocumentReferenceWhenMissingParentBetweenReferences()
109    {
110  1 EntityReference partialReference =
111    new EntityReference("document", EntityType.DOCUMENT, new EntityReference("wiki", EntityType.WIKI));
112   
113  1 EntityReference reference = this.resolver.resolve(partialReference, EntityType.DOCUMENT);
114   
115  1 assertNotSame(partialReference, reference);
116  1 assertEquals(DEFAULT_SPACE, reference.getParent().getName());
117  1 assertEquals(EntityType.SPACE, reference.getParent().getType());
118  1 assertNotSame(partialReference.getParent().getParent(), reference.getParent().getParent());
119  1 assertEquals("wiki", reference.getParent().getParent().getName());
120  1 assertEquals(EntityType.WIKI, reference.getParent().getParent().getType());
121    }
122   
 
123  1 toggle @Test
124    public void resolveAttachmentReferenceWhenMissingParentBetweenReferences()
125    {
126  1 EntityReference reference = this.resolver.resolve(
127    new EntityReference("filename", EntityType.ATTACHMENT, new EntityReference("wiki", EntityType.WIKI)),
128    EntityType.ATTACHMENT);
129   
130  1 assertEquals(DEFAULT_DOCUMENT, reference.getParent().getName());
131  1 assertEquals(EntityType.DOCUMENT, reference.getParent().getType());
132  1 assertEquals(DEFAULT_SPACE, reference.getParent().getParent().getName());
133  1 assertEquals(EntityType.SPACE, reference.getParent().getParent().getType());
134  1 assertEquals("wiki", reference.getParent().getParent().getParent().getName());
135  1 assertEquals(EntityType.WIKI, reference.getParent().getParent().getParent().getType());
136    }
137   
 
138  1 toggle @Test
139    public void resolveDocumentReferenceWhenInvalidReference()
140    {
141  1 try {
142  1 this.resolver.resolve(new EntityReference("document", EntityType.DOCUMENT,
143    new EntityReference("filename", EntityType.ATTACHMENT)), EntityType.DOCUMENT);
144   
145  0 fail("Should have thrown an exception here");
146    } catch (InvalidEntityReferenceException expected) {
147  1 assertEquals("Invalid reference [Document filename???document]", expected.getMessage());
148    }
149    }
150   
 
151  1 toggle @Test
152    public void resolveDocumentReferenceWhenTypeIsSpace()
153    {
154  1 EntityReference reference =
155    this.resolver.resolve(new EntityReference("space", EntityType.SPACE), EntityType.DOCUMENT);
156   
157  1 assertEquals(EntityType.DOCUMENT, reference.getType());
158  1 assertEquals(DEFAULT_DOCUMENT, reference.getName());
159  1 assertEquals(EntityType.SPACE, reference.getParent().getType());
160  1 assertEquals("space", reference.getParent().getName());
161  1 assertEquals(EntityType.WIKI, reference.getParent().getParent().getType());
162  1 assertEquals(DEFAULT_WIKI, reference.getParent().getParent().getName());
163    }
164   
 
165  1 toggle @Test
166    public void resolveSpaceReferenceWhenTypeIsDocument()
167    {
168  1 EntityReference reference =
169    this.resolver.resolve(new EntityReference("document", EntityType.DOCUMENT), EntityType.SPACE);
170   
171  1 assertEquals(EntityType.SPACE, reference.getType());
172  1 assertEquals(DEFAULT_SPACE, reference.getName());
173  1 assertEquals(EntityType.WIKI, reference.getParent().getType());
174  1 assertEquals(DEFAULT_WIKI, reference.getParent().getName());
175    }
176   
177    /**
178    * Tests that a relative object reference is resolved correctly and completed with the default document parent.
179    */
 
180  1 toggle @Test
181    public void resolveObjectReferenceWhenMissingParents()
182    {
183  1 EntityReference reference =
184    resolver.resolve(new EntityReference("object", EntityType.OBJECT), EntityType.OBJECT);
185  1 assertEquals(EntityType.OBJECT, reference.getType());
186  1 assertEquals("object", reference.getName());
187  1 assertEquals(EntityType.DOCUMENT, reference.getParent().getType());
188  1 assertEquals(DEFAULT_DOCUMENT, reference.getParent().getName());
189  1 assertEquals(EntityType.SPACE, reference.getParent().getParent().getType());
190  1 assertEquals(DEFAULT_SPACE, reference.getParent().getParent().getName());
191  1 assertEquals(EntityType.WIKI, reference.getParent().getParent().getParent().getType());
192  1 assertEquals(DEFAULT_WIKI, reference.getParent().getParent().getParent().getName());
193    }
194   
195    /**
196    * Tests that a relative object property is resolved correctly and completed with the default object parent.
197    */
 
198  1 toggle @Test
199    public void resolveObjectPropertyReferenceWhenMissingParents()
200    {
201  1 EntityReference reference =
202    resolver.resolve(new EntityReference("property", EntityType.OBJECT_PROPERTY), EntityType.OBJECT_PROPERTY);
203  1 assertEquals(EntityType.OBJECT_PROPERTY, reference.getType());
204  1 assertEquals("property", reference.getName());
205  1 assertEquals(EntityType.OBJECT, reference.getParent().getType());
206  1 assertEquals(DEFAULT_OBJECT, reference.getParent().getName());
207  1 assertEquals(EntityType.DOCUMENT, reference.getParent().getParent().getType());
208  1 assertEquals(DEFAULT_DOCUMENT, reference.getParent().getParent().getName());
209  1 assertEquals(EntityType.SPACE, reference.getParent().getParent().getParent().getType());
210  1 assertEquals(DEFAULT_SPACE, reference.getParent().getParent().getParent().getName());
211  1 assertEquals(EntityType.WIKI, reference.getParent().getParent().getParent().getParent().getType());
212  1 assertEquals(DEFAULT_WIKI, reference.getParent().getParent().getParent().getParent().getName());
213    }
214   
215    /**
216    * Test that a document reference, when resolved as an object reference, is correctly completed with the default
217    * values for object name.
218    */
 
219  1 toggle @Test
220    public void resolveObjectReferenceWhenTypeIsDocument()
221    {
222  1 EntityReference reference = resolver.resolve(
223    new EntityReference("document", EntityType.DOCUMENT,
224    new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI))),
225    EntityType.OBJECT);
226  1 assertEquals(EntityType.OBJECT, reference.getType());
227  1 assertEquals(DEFAULT_OBJECT, reference.getName());
228  1 assertEquals(EntityType.DOCUMENT, reference.getParent().getType());
229  1 assertEquals("document", reference.getParent().getName());
230  1 assertEquals(EntityType.SPACE, reference.getParent().getParent().getType());
231  1 assertEquals("space", reference.getParent().getParent().getName());
232  1 assertEquals(EntityType.WIKI, reference.getParent().getParent().getParent().getType());
233  1 assertEquals("wiki", reference.getParent().getParent().getParent().getName());
234    }
235   
236    /**
237    * Test that a document reference, when resolved as a property reference, is correctly completed with the default
238    * values for object and property name.
239    */
 
240  1 toggle @Test
241    public void resolveObjectPropertyReferenceWhenTypeIsDocument()
242    {
243  1 EntityReference reference = resolver.resolve(
244    new EntityReference("document", EntityType.DOCUMENT,
245    new EntityReference("space", EntityType.SPACE, new EntityReference("wiki", EntityType.WIKI))),
246    EntityType.OBJECT_PROPERTY);
247  1 assertEquals(EntityType.OBJECT_PROPERTY, reference.getType());
248  1 assertEquals(DEFAULT_OBJECT_PROPERTY, reference.getName());
249  1 assertEquals(EntityType.OBJECT, reference.getParent().getType());
250  1 assertEquals(DEFAULT_OBJECT, reference.getParent().getName());
251  1 assertEquals(EntityType.DOCUMENT, reference.getParent().getParent().getType());
252  1 assertEquals("document", reference.getParent().getParent().getName());
253  1 assertEquals(EntityType.SPACE, reference.getParent().getParent().getParent().getType());
254  1 assertEquals("space", reference.getParent().getParent().getParent().getName());
255  1 assertEquals(EntityType.WIKI, reference.getParent().getParent().getParent().getParent().getType());
256  1 assertEquals("wiki", reference.getParent().getParent().getParent().getParent().getName());
257    }
258   
 
259  1 toggle @Test
260    public void resolveDocumentReferenceWhenNullReference()
261    {
262  1 EntityReference reference = this.resolver.resolve(null, EntityType.DOCUMENT);
263   
264  1 assertEquals(EntityType.DOCUMENT, reference.getType());
265  1 assertEquals(DEFAULT_DOCUMENT, reference.getName());
266  1 assertEquals(EntityType.SPACE, reference.getParent().getType());
267  1 assertEquals(DEFAULT_SPACE, reference.getParent().getName());
268  1 assertEquals(EntityType.WIKI, reference.getParent().getParent().getType());
269  1 assertEquals(DEFAULT_WIKI, reference.getParent().getParent().getName());
270    }
271   
 
272  1 toggle @Test
273    public void resolvePageReferenceWhenTypeIsDocument()
274    {
275  1 EntityReference reference =
276    this.resolver.resolve(new EntityReference("document", EntityType.DOCUMENT), EntityType.PAGE);
277   
278  1 assertEquals(new PageReference(DEFAULT_WIKI, DEFAULT_SPACE, "document"), reference);
279   
280  1 reference = this.resolver.resolve(new EntityReference(DEFAULT_DOCUMENT, EntityType.DOCUMENT), EntityType.PAGE);
281   
282  1 assertEquals(new PageReference(DEFAULT_WIKI, DEFAULT_SPACE), reference);
283    }
284   
 
285  1 toggle @Test
286    public void resolvePageReferenceWhenTypeIsSpace()
287    {
288  1 EntityReference reference =
289    this.resolver.resolve(new EntityReference("space", EntityType.SPACE), EntityType.PAGE);
290   
291  1 assertEquals(new PageReference(DEFAULT_WIKI, "space"), reference);
292    }
293   
 
294  1 toggle @Test
295    public void resolveDocumentReferenceWhenTypeIsPage()
296    {
297  1 EntityReference reference =
298    this.resolver.resolve(new EntityReference("page", EntityType.PAGE), EntityType.DOCUMENT);
299   
300  1 assertEquals(new DocumentReference(DEFAULT_WIKI, "page", DEFAULT_DOCUMENT), reference);
301   
302  1 reference = this.resolver.resolve(
303    new EntityReference("page1", EntityType.PAGE, new EntityReference("page2", EntityType.PAGE)),
304    EntityType.DOCUMENT);
305   
306  1 assertEquals(new DocumentReference(DEFAULT_WIKI, Arrays.asList("page2", "page1"), DEFAULT_DOCUMENT), reference);
307    }
308   
 
309  1 toggle @Test
310    public void resolveSpaceReferenceWhenTypeIsPage()
311    {
312  1 EntityReference reference =
313    this.resolver.resolve(new EntityReference("page", EntityType.PAGE), EntityType.SPACE);
314   
315  1 assertEquals(new SpaceReference(DEFAULT_WIKI, Arrays.asList("page")), reference);
316   
317  1 reference = this.resolver.resolve(
318    new EntityReference("page1", EntityType.PAGE, new EntityReference("page2", EntityType.PAGE)),
319    EntityType.SPACE);
320   
321  1 assertEquals(new SpaceReference(DEFAULT_WIKI, Arrays.asList("page2", "page1")), reference);
322    }
323    }