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

File CompactStringEntityReferenceSerializerTest.java

 

Code metrics

0
59
8
1
185
130
8
0.14
7.38
8
1

Classes

Class Line # Actions
CompactStringEntityReferenceSerializerTest 51 59 0% 8 0
1.0100%
 

Contributing tests

This file is covered by 8 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 com.xpn.xwiki.internal.model.reference;
21   
22    import java.util.Arrays;
23   
24    import org.junit.Assert;
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.xwiki.component.manager.ComponentLookupException;
28    import org.xwiki.model.EntityType;
29    import org.xwiki.model.internal.DefaultModelConfiguration;
30    import org.xwiki.model.internal.reference.DefaultSymbolScheme;
31    import org.xwiki.model.reference.AttachmentReference;
32    import org.xwiki.model.reference.DocumentReference;
33    import org.xwiki.model.reference.EntityReference;
34    import org.xwiki.model.reference.EntityReferenceSerializer;
35    import org.xwiki.test.annotation.ComponentList;
36    import org.xwiki.test.mockito.MockitoComponentMockingRule;
37   
38    import com.xpn.xwiki.doc.XWikiDocument;
39    import com.xpn.xwiki.test.MockitoOldcoreRule;
40   
41    /**
42    * Unit tests for {@link CompactStringEntityReferenceSerializer}.
43    *
44    * @version $Id: f55a5e4697b20b6e8a4fe43b78dffd5eb2ba2a38 $
45    */
46    @ComponentList({
47    DefaultSymbolScheme.class,
48    CurrentEntityReferenceProvider.class,
49    DefaultModelConfiguration.class
50    })
 
51    public class CompactStringEntityReferenceSerializerTest
52    {
53    public MockitoComponentMockingRule<EntityReferenceSerializer<String>> mocker =
54    new MockitoComponentMockingRule<EntityReferenceSerializer<String>>(CompactStringEntityReferenceSerializer.class);
55   
56    @Rule
57    public MockitoOldcoreRule oldcore = new MockitoOldcoreRule(this.mocker);
58   
 
59  1 toggle @Test
60    public void testSerializeWhenNoContext() throws Exception
61    {
62  1 DocumentReference reference = new DocumentReference("wiki", "space", "page");
63  1 Assert.assertEquals("wiki:space.page", this.mocker.getComponentUnderTest().serialize(reference));
64    }
65   
 
66  1 toggle @Test
67    public void testSerializeWhenNoContextDocument() throws Exception
68    {
69  1 DocumentReference reference = new DocumentReference("wiki", "space", "page");
70  1 Assert.assertEquals("wiki:space.page", this.mocker.getComponentUnderTest().serialize(reference));
71    }
72   
 
73  1 toggle @Test
74    public void testSerializeDocumentReferenceWhenContextDocument() throws Exception
75    {
76  1 DocumentReference reference = new DocumentReference("wiki", "space", "page");
77   
78  1 this.oldcore.getXWikiContext().setWikiReference(reference.getWikiReference());
79  1 this.oldcore.getXWikiContext().setDoc(new XWikiDocument(reference));
80  1 Assert.assertEquals("page", this.mocker.getComponentUnderTest().serialize(reference));
81   
82  1 this.oldcore.getXWikiContext().setWikiReference(reference.getWikiReference());
83  1 this.oldcore.getXWikiContext().setDoc(new XWikiDocument(new DocumentReference("wiki", "space", "otherpage")));
84  1 Assert.assertEquals("page", this.mocker.getComponentUnderTest().serialize(reference));
85   
86  1 this.oldcore.getXWikiContext().setWikiReference(reference.getWikiReference());
87  1 this.oldcore.getXWikiContext().setDoc(
88    new XWikiDocument(new DocumentReference("wiki", "otherspace", "otherpage")));
89  1 Assert.assertEquals("space.page", this.mocker.getComponentUnderTest().serialize(reference));
90   
91  1 this.oldcore.getXWikiContext().setWikiId("otherwiki");
92  1 this.oldcore.getXWikiContext().setDoc(
93    new XWikiDocument(new DocumentReference("otherwiki", "otherspace", "otherpage")));
94  1 Assert.assertEquals("wiki:space.page", this.mocker.getComponentUnderTest().serialize(reference));
95   
96  1 this.oldcore.getXWikiContext().setWikiReference(reference.getWikiReference());
97  1 this.oldcore.getXWikiContext().setDoc(new XWikiDocument(new DocumentReference("wiki", "otherspace", "page")));
98  1 Assert.assertEquals("space.page", this.mocker.getComponentUnderTest().serialize(reference));
99   
100  1 this.oldcore.getXWikiContext().setWikiId("otherwiki");
101  1 this.oldcore.getXWikiContext().setDoc(
102    new XWikiDocument(new DocumentReference("otherwiki", "otherspace", "page")));
103  1 Assert.assertEquals("wiki:space.page", this.mocker.getComponentUnderTest().serialize(reference));
104   
105  1 this.oldcore.getXWikiContext().setWikiId("otherwiki");
106  1 this.oldcore.getXWikiContext().setDoc(new XWikiDocument(new DocumentReference("otherwiki", "space", "page")));
107  1 Assert.assertEquals("wiki:space.page", this.mocker.getComponentUnderTest().serialize(reference));
108   
109  1 this.oldcore.getXWikiContext().setWikiId("otherwiki");
110  1 this.oldcore.getXWikiContext().setDoc(
111    new XWikiDocument(new DocumentReference("otherwiki", "space", "otherpage")));
112  1 Assert.assertEquals("wiki:space.page", this.mocker.getComponentUnderTest().serialize(reference));
113    }
114   
 
115  1 toggle @Test
116    public void testSerializeSpaceReferenceWhenHasChildren() throws Exception
117    {
118  1 AttachmentReference reference =
119    new AttachmentReference("filename", new DocumentReference("wiki", "space", "page"));
120   
121  1 this.oldcore.getXWikiContext().setWikiId("wiki");
122  1 this.oldcore.getXWikiContext().setDoc(new XWikiDocument(new DocumentReference("wiki", "space", "page")));
123  1 Assert.assertEquals("page", this.mocker.getComponentUnderTest().serialize(reference.getParent()));
124  1 Assert.assertEquals("space", this.mocker.getComponentUnderTest().serialize(reference.getParent().getParent()));
125   
126  1 this.oldcore.getXWikiContext().setWikiId("xwiki");
127  1 this.oldcore.getXWikiContext().setDoc(new XWikiDocument(new DocumentReference("xwiki", "xspace", "xpage")));
128  1 Assert.assertEquals("wiki:space.page", this.mocker.getComponentUnderTest().serialize(reference.getParent()));
129  1 Assert.assertEquals("wiki:space",
130    this.mocker.getComponentUnderTest().serialize(reference.getParent().getParent()));
131   
132    }
133   
 
134  1 toggle @Test
135    public void testSerializeAttachmentReferenceWhenContextDocument() throws Exception
136    {
137  1 AttachmentReference reference =
138    new AttachmentReference("filename", new DocumentReference("wiki", "space", "page"));
139   
140  1 this.oldcore.getXWikiContext().setWikiId("wiki");
141  1 this.oldcore.getXWikiContext().setDoc(new XWikiDocument(new DocumentReference("wiki", "space", "page")));
142  1 Assert.assertEquals("filename", this.mocker.getComponentUnderTest().serialize(reference));
143   
144  1 this.oldcore.getXWikiContext().setWikiId("wiki");
145  1 this.oldcore.getXWikiContext().setDoc(new XWikiDocument(new DocumentReference("wiki", "space", "otherpage")));
146  1 Assert.assertEquals("page@filename", this.mocker.getComponentUnderTest().serialize(reference));
147   
148  1 this.oldcore.getXWikiContext().setWikiId("otherwiki");
149  1 this.oldcore.getXWikiContext().setDoc(new XWikiDocument(new DocumentReference("otherwiki", "space", "page")));
150  1 Assert.assertEquals("wiki:space.page@filename", this.mocker.getComponentUnderTest().serialize(reference));
151    }
152   
 
153  1 toggle @Test
154    public void testSerializeEntityReferenceWithExplicit() throws ComponentLookupException
155    {
156  1 DocumentReference reference = new DocumentReference("wiki", "space", "page");
157   
158  1 this.oldcore.getXWikiContext().setWikiId("wiki");
159  1 this.oldcore.getXWikiContext().setDoc(new XWikiDocument(new DocumentReference("wiki", "space", "page")));
160  1 Assert.assertEquals(
161    "space.page",
162    this.mocker.getComponentUnderTest().serialize(reference,
163    new EntityReference("otherspace", EntityType.SPACE)));
164    }
165   
 
166  1 toggle @Test
167    public void testSerializeNestedSpaceFromBaseReference() throws ComponentLookupException
168    {
169  1 DocumentReference baseReference = new DocumentReference("wiki", "space", "page");
170  1 DocumentReference reference = new DocumentReference("wiki", Arrays.asList("space", "nested"), "page");
171   
172  1 Assert.assertEquals("space.nested.page", this.mocker.getComponentUnderTest().serialize(reference, baseReference));
173    }
174   
 
175  1 toggle @Test
176    public void testSerializeNestedSpaceFromContext() throws ComponentLookupException
177    {
178  1 DocumentReference reference = new DocumentReference("wiki", Arrays.asList("space", "nested"), "page");
179   
180  1 this.oldcore.getXWikiContext().setWikiId("wiki");
181  1 this.oldcore.getXWikiContext().setDoc(new XWikiDocument(new DocumentReference("wiki", "space", "page2")));
182   
183  1 Assert.assertEquals("space.nested.page", this.mocker.getComponentUnderTest().serialize(reference));
184    }
185    }