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

File LocalUidStringEntityReferenceSerializerTest.java

 

Code metrics

0
40
10
1
172
107
10
0.25
4
10
1

Classes

Class Line # Actions
LocalUidStringEntityReferenceSerializerTest 44 40 0% 10 0
1.0100%
 

Contributing tests

This file is covered by 9 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   
21    package org.xwiki.model.internal.reference;
22   
23    import java.util.Arrays;
24    import java.util.Locale;
25   
26    import org.junit.Assert;
27    import org.junit.Before;
28    import org.junit.Rule;
29    import org.junit.Test;
30    import org.xwiki.model.EntityType;
31    import org.xwiki.model.reference.DocumentReference;
32    import org.xwiki.model.reference.EntityReference;
33    import org.xwiki.model.reference.EntityReferenceResolver;
34    import org.xwiki.model.reference.EntityReferenceSerializer;
35    import org.xwiki.test.annotation.ComponentList;
36    import org.xwiki.test.mockito.MockitoComponentMockingRule;
37   
38    /**
39    * Unit tests for {@link LocalUidStringEntityReferenceSerializer}.
40    *
41    * @version $Id: 566ce5b6dc15548ac865e275a68b65fe6a1445ea $
42    */
43    @ComponentList({ DefaultSymbolScheme.class })
 
44    public class LocalUidStringEntityReferenceSerializerTest
45    {
46    @Rule
47    public MockitoComponentMockingRule<EntityReferenceSerializer<String>> mocker =
48    new MockitoComponentMockingRule<>(LocalUidStringEntityReferenceSerializer.class);
49   
50    @Rule
51    public MockitoComponentMockingRule<DefaultStringEntityReferenceResolver> resolverMocker =
52    new MockitoComponentMockingRule<>(DefaultStringEntityReferenceResolver.class);
53   
54    private EntityReferenceSerializer<String> serializer;
55   
56    private EntityReferenceResolver<String> resolver;
57   
 
58  9 toggle @Before
59    public void setUp() throws Exception
60    {
61  9 this.serializer = this.mocker.getComponentUnderTest();
62  9 this.resolver = this.resolverMocker.getComponentUnderTest();
63    }
64   
 
65  1 toggle @Test
66    public void serializeDocumentReference() throws Exception
67    {
68  1 EntityReference reference = resolver.resolve("wiki:space.page", EntityType.DOCUMENT);
69  1 Assert.assertEquals("5:space4:page", serializer.serialize(reference));
70   
71  1 reference = resolver.resolve("wiki1.wiki2:wiki3:some.space.page", EntityType.DOCUMENT);
72  1 Assert.assertEquals("4:some5:space4:page", serializer.serialize(reference));
73   
74    // Verify that passing null doesn't throw a NPE
75  1 Assert.assertNull(serializer.serialize(null));
76    }
77   
 
78  1 toggle @Test
79    public void serializeDocumentReferenceWithLocale() throws Exception
80    {
81  1 EntityReference reference = new DocumentReference("wiki", "space", "page", Locale.US);
82  1 Assert.assertEquals("5:space4:page5:en_US", serializer.serialize(reference));
83   
84  1 reference = new DocumentReference("wiki1.wiki2:wiki3", Arrays.asList("some", "space"), "page", Locale.US);
85  1 Assert.assertEquals("4:some5:space4:page5:en_US", serializer.serialize(reference));
86    }
87   
 
88  1 toggle @Test
89    public void serializeSpaceReference() throws Exception
90    {
91  1 EntityReference reference = resolver.resolve("wiki:space1.space2", EntityType.SPACE);
92  1 Assert.assertEquals("6:space16:space2", serializer.serialize(reference));
93    }
94   
 
95  1 toggle @Test
96    public void serializeAttachmentReference() throws Exception
97    {
98  1 EntityReference reference = resolver.resolve("wiki:space.page@filename", EntityType.ATTACHMENT);
99  1 Assert.assertEquals("5:space4:page8:filename", serializer.serialize(reference));
100    }
101   
 
102  1 toggle @Test
103    public void serializeReferenceWithChild()
104    {
105  1 EntityReference reference = resolver.resolve("wiki:Space.Page", EntityType.DOCUMENT);
106  1 Assert.assertEquals("5:Space", serializer.serialize(reference.getParent()));
107   
108  1 Assert.assertEquals("", serializer.serialize(reference.getParent().getParent()));
109    }
110   
111    /**
112    * Tests resolving and re-serializing an object reference.
113    */
 
114  1 toggle @Test
115    public void serializeObjectReference()
116    {
117  1 EntityReference reference = resolver.resolve("wiki:space.page^wiki:space.class[0]", EntityType.OBJECT);
118  1 Assert.assertEquals("5:space4:page14:space.class[0]", serializer.serialize(reference));
119   
120  1 reference = resolver.resolve("wiki:space.page^xwiki:space.class[0]", EntityType.OBJECT);
121  1 Assert.assertEquals("5:space4:page20:xwiki:space.class[0]", serializer.serialize(reference));
122   
123    // test escaping character
124  1 reference = resolver.resolve("wiki:space.page^wiki:space.cla\\^ss[0]", EntityType.OBJECT);
125  1 Assert.assertEquals("5:space4:page15:space.cla^ss[0]", serializer.serialize(reference));
126   
127  1 reference = resolver.resolve("wiki:spa^ce.page^wiki:space.cla\\^ss[0]", EntityType.OBJECT);
128  1 Assert.assertEquals("6:spa^ce4:page15:space.cla^ss[0]", serializer.serialize(reference));
129    }
130   
131    /**
132    * Tests resolving and re-serializing an property reference.
133    */
 
134  1 toggle @Test
135    public void serializeObjectPropertyReference()
136    {
137  1 EntityReference reference =
138    resolver.resolve("wiki:space.page^wiki:space.class[0].prop", EntityType.OBJECT_PROPERTY);
139  1 Assert.assertEquals("5:space4:page14:space.class[0]4:prop", serializer.serialize(reference));
140   
141  1 reference = resolver.resolve("wiki:space.page^xwiki:space.class[0].prop", EntityType.OBJECT_PROPERTY);
142  1 Assert.assertEquals("5:space4:page20:xwiki:space.class[0]4:prop", serializer.serialize(reference));
143   
144    // test escaping character
145  1 reference = resolver.resolve("wiki:space.page^wiki:space.class[0].prop\\.erty", EntityType.OBJECT_PROPERTY);
146  1 Assert.assertEquals("5:space4:page14:space.class[0]9:prop.erty", serializer.serialize(reference));
147    }
148   
149    /**
150    * Tests resolving and re-serializing an class property reference.
151    */
 
152  1 toggle @Test
153    public void serializeClassPropertyReference()
154    {
155  1 EntityReference reference = resolver.resolve("wiki:space.page^ClassProperty", EntityType.CLASS_PROPERTY);
156  1 Assert.assertEquals("5:space4:page13:ClassProperty", serializer.serialize(reference));
157   
158    // test escaping character
159  1 reference = resolver.resolve("wiki:space.page^ClassPro\\^perty", EntityType.CLASS_PROPERTY);
160  1 Assert.assertEquals("5:space4:page14:ClassPro^perty", serializer.serialize(reference));
161    }
162   
 
163  1 toggle @Test
164    public void serializeRelativeReference()
165    {
166  1 EntityReference reference = new EntityReference("page", EntityType.DOCUMENT);
167  1 Assert.assertEquals("4:page", serializer.serialize(reference));
168   
169  1 reference = new EntityReference("page", EntityType.DOCUMENT, new EntityReference("space", EntityType.SPACE));
170  1 Assert.assertEquals("5:space4:page", serializer.serialize(reference));
171    }
172    }