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

File UidStringEntityReferenceSerializerTest.java

 

Code metrics

0
40
10
1
169
104
10
0.25
4
10
1

Classes

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