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

File DefaultStringEntityReferenceSerializerTest.java

 

Code metrics

0
124
12
1
320
202
12
0.1
10.33
12
1

Classes

Class Line # Actions
DefaultStringEntityReferenceSerializerTest 45 124 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.model.internal.reference;
21   
22    import org.junit.jupiter.api.BeforeEach;
23    import org.junit.jupiter.api.Test;
24    import org.xwiki.model.EntityType;
25    import org.xwiki.model.reference.EntityReference;
26    import org.xwiki.model.reference.EntityReferenceProvider;
27    import org.xwiki.model.reference.test.TestConstants;
28    import org.xwiki.test.annotation.ComponentList;
29    import org.xwiki.test.junit5.mockito.ComponentTest;
30    import org.xwiki.test.junit5.mockito.InjectMockComponents;
31    import org.xwiki.test.junit5.mockito.MockComponent;
32   
33    import static org.junit.Assert.assertEquals;
34    import static org.junit.Assert.assertNull;
35    import static org.mockito.Mockito.when;
36   
37    /**
38    * Unit tests for {@link DefaultStringEntityReferenceSerializer}.
39    *
40    * @version $Id: a58daf4fd9ec1e7d1dc9ed0be97f4204d23c8ae5 $
41    * @since 2.2M1
42    */
43    @ComponentTest
44    @ComponentList({ DefaultSymbolScheme.class })
 
45    public class DefaultStringEntityReferenceSerializerTest implements TestConstants
46    {
47    @MockComponent
48    private EntityReferenceProvider provider;
49   
50    @InjectMockComponents
51    private DefaultStringEntityReferenceSerializer serializer;
52   
53    @InjectMockComponents
54    private DefaultStringEntityReferenceResolver resolver;
55   
 
56  11 toggle @BeforeEach
57    public void beforeEach() throws Exception
58    {
59  11 when(this.provider.getDefaultReference(EntityType.WIKI)).thenReturn(DEFAULT_WIKI_REFERENCE);
60  11 when(this.provider.getDefaultReference(EntityType.SPACE)).thenReturn(DEFAULT_SPACE_REFERENCE);
61  11 when(this.provider.getDefaultReference(EntityType.DOCUMENT)).thenReturn(DEFAULT_DOCUMENT_REFERENCE);
62  11 when(this.provider.getDefaultReference(EntityType.ATTACHMENT)).thenReturn(DEFAULT_ATTACHMENT_REFERENCE);
63  11 when(this.provider.getDefaultReference(EntityType.OBJECT)).thenReturn(DEFAULT_OBJECT_REFERENCE);
64  11 when(this.provider.getDefaultReference(EntityType.OBJECT_PROPERTY))
65    .thenReturn(DEFAULT_OBJECT_PROPERTY_REFERENCE);
66  11 when(this.provider.getDefaultReference(EntityType.CLASS_PROPERTY)).thenReturn(DEFAULT_CLASS_PROPERTY_REFERENCE);
67  11 when(this.provider.getDefaultReference(EntityType.PAGE)).thenReturn(DEFAULT_PAGE_REFERENCE);
68  11 when(this.provider.getDefaultReference(EntityType.PAGE_ATTACHMENT))
69    .thenReturn(DEFAULT_PAGE_ATTACHMENT_REFERENCE);
70  11 when(this.provider.getDefaultReference(EntityType.PAGE_OBJECT)).thenReturn(DEFAULT_PAGE_OBJECT_REFERENCE);
71  11 when(this.provider.getDefaultReference(EntityType.PAGE_OBJECT_PROPERTY))
72    .thenReturn(DEFAULT_PAGE_OBJECT_PROPERTY_REFERENCE);
73  11 when(this.provider.getDefaultReference(EntityType.PAGE_CLASS_PROPERTY))
74    .thenReturn(DEFAULT_PAGE_CLASS_PROPERTY_REFERENCE);
75    }
76   
 
77  1 toggle @Test
78    public void serializeWikiReferences() throws Exception
79    {
80  1 EntityReference reference = resolver.resolve("wiki", EntityType.WIKI);
81  1 assertEquals("wiki", serializer.serialize(reference));
82    }
83   
 
84  1 toggle @Test
85    public void serializeDocumentReferences() throws Exception
86    {
87  1 EntityReference reference = resolver.resolve("wiki:space.page", EntityType.DOCUMENT);
88  1 assertEquals("wiki:space.page", serializer.serialize(reference));
89   
90  1 reference = resolver.resolve("wiki:space.", EntityType.DOCUMENT);
91  1 assertEquals("wiki:space.defdocument", serializer.serialize(reference));
92   
93  1 reference = resolver.resolve("space.", EntityType.DOCUMENT);
94  1 assertEquals("defwiki:space.defdocument", serializer.serialize(reference));
95   
96  1 reference = resolver.resolve("page", EntityType.DOCUMENT);
97  1 assertEquals("defwiki:defspace.page", serializer.serialize(reference));
98   
99  1 reference = resolver.resolve(".", EntityType.DOCUMENT);
100  1 assertEquals("defwiki:defspace.defdocument", serializer.serialize(reference));
101   
102  1 reference = resolver.resolve(null, EntityType.DOCUMENT);
103  1 assertEquals("defwiki:defspace.defdocument", serializer.serialize(reference));
104   
105  1 reference = resolver.resolve("", EntityType.DOCUMENT);
106  1 assertEquals("defwiki:defspace.defdocument", serializer.serialize(reference));
107   
108  1 reference = resolver.resolve("wiki1.wiki2:wiki3:some.space.page", EntityType.DOCUMENT);
109  1 assertEquals("wiki1.wiki2:wiki3:some.space.page", serializer.serialize(reference));
110   
111  1 reference = resolver.resolve("some.space.page", EntityType.DOCUMENT);
112  1 assertEquals("defwiki:some.space.page", serializer.serialize(reference));
113   
114  1 reference = resolver.resolve("wiki:page", EntityType.DOCUMENT);
115  1 assertEquals("defwiki:defspace.wiki:page", serializer.serialize(reference));
116   
117    // Verify that passing null doesn't throw a NPE
118  1 assertNull(serializer.serialize(null));
119   
120    // Test escapes
121   
122  1 reference = resolver.resolve("\\.:@\\.", EntityType.DOCUMENT);
123  1 assertEquals("defwiki:defspace.\\.:@\\.", serializer.serialize(reference));
124   
125  1 reference = resolver.resolve("\\\\:\\\\.\\\\", EntityType.DOCUMENT);
126  1 assertEquals("\\\\:\\\\.\\\\", serializer.serialize(reference));
127   
128    // The escaping here is not necessary but we want to test that it works
129  1 reference = resolver.resolve("\\wiki:\\space.\\page", EntityType.DOCUMENT);
130  1 assertEquals("wiki:space.page", serializer.serialize(reference));
131    }
132   
 
133  1 toggle @Test
134    public void serializeSpaceReferences() throws Exception
135    {
136  1 EntityReference reference = resolver.resolve("wiki:space1.space2", EntityType.SPACE);
137  1 assertEquals("wiki:space1.space2", serializer.serialize(reference));
138    }
139   
 
140  1 toggle @Test
141    public void serializeAttachmentReferences() throws Exception
142    {
143  1 EntityReference reference = resolver.resolve("wiki:space.page@filename", EntityType.ATTACHMENT);
144  1 assertEquals("wiki:space.page@filename", serializer.serialize(reference));
145   
146  1 reference = resolver.resolve("", EntityType.ATTACHMENT);
147  1 assertEquals("defwiki:defspace.defdocument@deffilename", serializer.serialize(reference));
148   
149  1 reference = resolver.resolve("wiki:space.page@my.png", EntityType.ATTACHMENT);
150  1 assertEquals("wiki:space.page@my.png", serializer.serialize(reference));
151   
152  1 reference = resolver.resolve("some:file.name", EntityType.ATTACHMENT);
153  1 assertEquals("defwiki:defspace.defdocument@some:file.name", serializer.serialize(reference));
154   
155    // Test escapes
156   
157  1 reference = resolver.resolve(":.\\@", EntityType.ATTACHMENT);
158  1 assertEquals("defwiki:defspace.defdocument@:.\\@", serializer.serialize(reference));
159    }
160   
 
161  1 toggle @Test
162    public void serializeReferencesWithChild()
163    {
164  1 EntityReference reference = resolver.resolve("wiki:Space.Page", EntityType.DOCUMENT);
165  1 assertEquals("wiki:Space", serializer.serialize(reference.getParent()));
166   
167  1 assertEquals("wiki", serializer.serialize(reference.getParent().getParent()));
168    }
169   
170    /**
171    * Tests resolving and re-serializing an object reference.
172    */
 
173  1 toggle @Test
174    public void serializeObjectReferences()
175    {
176  1 EntityReference reference = resolver.resolve("wiki:space.page^Object", EntityType.OBJECT);
177  1 assertEquals("wiki:space.page^Object", serializer.serialize(reference));
178   
179    // default values
180  1 reference = resolver.resolve("", EntityType.OBJECT);
181  1 assertEquals("defwiki:defspace.defdocument^defobject", serializer.serialize(reference));
182   
183    // property reference with no object
184  1 reference = resolver.resolve("wiki:space.page.property", EntityType.OBJECT);
185  1 assertEquals("defwiki:defspace.defdocument^wiki:space.page.property", serializer.serialize(reference));
186   
187    // test escaping character
188  1 reference = resolver.resolve("wiki:space.page^Obje\\^ct", EntityType.OBJECT);
189  1 assertEquals("wiki:space.page^Obje\\^ct", serializer.serialize(reference));
190   
191  1 reference = resolver.resolve("wiki:spa^ce.page^Obje\\^ct", EntityType.OBJECT);
192  1 assertEquals("wiki:spa^ce.page^Obje\\^ct", serializer.serialize(reference));
193   
194  1 reference = resolver.resolve(":.\\^@", EntityType.OBJECT);
195  1 assertEquals("defwiki:defspace.defdocument^:.\\^@", serializer.serialize(reference));
196    }
197   
198    /**
199    * Tests resolving and re-serializing an object reference.
200    */
 
201  1 toggle @Test
202    public void serializeObjectPropertyReferences()
203    {
204  1 EntityReference reference = resolver.resolve("wiki:space.page^xwiki.class[0].prop", EntityType.OBJECT_PROPERTY);
205  1 assertEquals("wiki:space.page^xwiki.class[0].prop", serializer.serialize(reference));
206   
207    // default values
208  1 reference = resolver.resolve("", EntityType.OBJECT_PROPERTY);
209  1 assertEquals("defwiki:defspace.defdocument^defobject.defobjproperty", serializer.serialize(reference));
210   
211    // using separators
212  1 reference = resolver.resolve("space^page@attachment", EntityType.OBJECT_PROPERTY);
213  1 assertEquals("defwiki:defspace.defdocument^defobject.space^page@attachment", serializer.serialize(reference));
214   
215  1 reference = resolver.resolve("wiki:space^object", EntityType.OBJECT_PROPERTY);
216  1 assertEquals("defwiki:defspace.defdocument^defobject.wiki:space^object", serializer.serialize(reference));
217   
218    // test escaping character
219  1 reference = resolver.resolve("wiki:space.page^xwiki.class[0].prop\\.erty", EntityType.OBJECT_PROPERTY);
220  1 assertEquals("wiki:space.page^xwiki.class[0].prop\\.erty", serializer.serialize(reference));
221   
222  1 reference = resolver.resolve(":\\.^@", EntityType.OBJECT_PROPERTY);
223  1 assertEquals("defwiki:defspace.defdocument^defobject.:\\.^@", serializer.serialize(reference));
224    }
225   
226    /**
227    * Tests resolving and re-serializing an object reference.
228    */
 
229  1 toggle @Test
230    public void serializeClassPropertyReferences()
231    {
232  1 EntityReference reference = resolver.resolve("wiki:space.page^ClassProperty", EntityType.CLASS_PROPERTY);
233  1 assertEquals("wiki:space.page^ClassProperty", serializer.serialize(reference));
234   
235    // default values
236  1 reference = resolver.resolve("", EntityType.CLASS_PROPERTY);
237  1 assertEquals("defwiki:defspace.defdocument^defclassproperty", serializer.serialize(reference));
238   
239    // property reference with no object
240  1 reference = resolver.resolve("wiki:space.page.property", EntityType.CLASS_PROPERTY);
241  1 assertEquals("defwiki:defspace.defdocument^wiki:space.page.property", serializer.serialize(reference));
242   
243    // test escaping character
244  1 reference = resolver.resolve("wiki:space.page^Obje\\^ct", EntityType.CLASS_PROPERTY);
245  1 assertEquals("wiki:space.page^Obje\\^ct", serializer.serialize(reference));
246   
247  1 reference = resolver.resolve("wiki:spa^ce.page^Obje\\^ct", EntityType.CLASS_PROPERTY);
248  1 assertEquals("wiki:spa^ce.page^Obje\\^ct", serializer.serialize(reference));
249   
250  1 reference = resolver.resolve(":.\\^@", EntityType.CLASS_PROPERTY);
251  1 assertEquals("defwiki:defspace.defdocument^:.\\^@", serializer.serialize(reference));
252    }
253   
 
254  1 toggle @Test
255    public void serializeRelativeReferences()
256    {
257  1 EntityReference reference = new EntityReference("page", EntityType.DOCUMENT);
258  1 assertEquals("page", serializer.serialize(reference));
259   
260  1 reference = new EntityReference("page", EntityType.DOCUMENT, new EntityReference("space", EntityType.SPACE));
261  1 assertEquals("space.page", serializer.serialize(reference));
262   
263    // Ensure that the page part is not displayed in the serialized result
264  1 assertEquals("space", serializer.serialize(reference.extractReference(EntityType.SPACE)));
265    }
266   
 
267  1 toggle @Test
268    public void serializePageReferences() throws Exception
269    {
270  1 EntityReference reference = resolver.resolve("wiki:space/page", EntityType.PAGE);
271  1 assertEquals("wiki:space/page", serializer.serialize(reference));
272   
273  1 reference = resolver.resolve("wiki:space/", EntityType.PAGE);
274  1 assertEquals("wiki:space/defpage", serializer.serialize(reference));
275   
276  1 reference = resolver.resolve("space/", EntityType.PAGE);
277  1 assertEquals("defwiki:space/defpage", serializer.serialize(reference));
278   
279  1 reference = resolver.resolve("page", EntityType.PAGE);
280  1 assertEquals("defwiki:page", serializer.serialize(reference));
281   
282  1 reference = resolver.resolve("/", EntityType.PAGE);
283  1 assertEquals("defwiki:defpage/defpage", serializer.serialize(reference));
284   
285  1 reference = resolver.resolve(null, EntityType.PAGE);
286  1 assertEquals("defwiki:defpage", serializer.serialize(reference));
287   
288  1 reference = resolver.resolve("", EntityType.PAGE);
289  1 assertEquals("defwiki:defpage", serializer.serialize(reference));
290   
291  1 reference = resolver.resolve("wiki1/wiki2:wiki3:some/space/page", EntityType.PAGE);
292  1 assertEquals("wiki1/wiki2:wiki3:some/space/page", serializer.serialize(reference));
293   
294  1 reference = resolver.resolve("some/space/page", EntityType.PAGE);
295  1 assertEquals("defwiki:some/space/page", serializer.serialize(reference));
296   
297    // Verify that passing null doesn't throw a NPE
298  1 assertNull(serializer.serialize(null));
299   
300    // Test escapes
301   
302  1 reference = resolver.resolve("\\\\:\\\\/\\\\", EntityType.PAGE);
303  1 assertEquals("\\\\:\\\\/\\\\", serializer.serialize(reference));
304   
305    // The escaping here is not necessary but we want to test that it works
306  1 reference = resolver.resolve("\\wiki:\\space/\\page", EntityType.PAGE);
307  1 assertEquals("wiki:space/page", serializer.serialize(reference));
308    }
309   
 
310  1 toggle @Test
311    public void serializePageReferencesParameters() throws Exception
312    {
313  1 EntityReference reference =
314    resolver.resolve("wiki:page1;param1=value1;param2=value2/page2;en_US", EntityType.PAGE);
315  1 assertEquals("wiki:page1;param1=value1;param2=value2/page2;en_US", serializer.serialize(reference));
316   
317  1 reference = resolver.resolve("wiki:pag\\;e1;pa\\;ram1=val\\;ue1;param2=value2/pag\\;e2;en_\\;US", EntityType.PAGE);
318  1 assertEquals("wiki:pag\\;e1;pa\\;ram1=val\\;ue1;param2=value2/pag\\;e2;en_\\;US", serializer.serialize(reference));
319    }
320    }