1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.internal.renderer

File XWikiLinkLabelGeneratorTest.java

 

Code metrics

0
68
7
1
217
156
7
0.1
9.71
7
1

Classes

Class Line # Actions
XWikiLinkLabelGeneratorTest 51 68 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 6 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.rendering.internal.renderer;
21   
22    import java.util.Arrays;
23   
24    import org.junit.Before;
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.xwiki.bridge.DocumentAccessBridge;
28    import org.xwiki.bridge.DocumentModelBridge;
29    import org.xwiki.component.util.DefaultParameterizedType;
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.EntityReferenceProvider;
34    import org.xwiki.model.reference.EntityReferenceResolver;
35    import org.xwiki.model.reference.EntityReferenceSerializer;
36    import org.xwiki.model.reference.SpaceReference;
37    import org.xwiki.rendering.configuration.RenderingConfiguration;
38    import org.xwiki.rendering.listener.reference.DocumentResourceReference;
39    import org.xwiki.rendering.listener.reference.ResourceReference;
40    import org.xwiki.test.mockito.MockitoComponentMockingRule;
41   
42    import static org.junit.Assert.*;
43    import static org.mockito.Mockito.*;
44   
45    /**
46    * Unit tests for {@link XWikiLinkLabelGenerator}.
47    *
48    * @version $Id: 76260025a1427ff2d163940736616bd0de7ded3d $
49    * @since 2.0M1
50    */
 
51    public class XWikiLinkLabelGeneratorTest
52    {
53    @Rule
54    public MockitoComponentMockingRule<XWikiLinkLabelGenerator> mocker =
55    new MockitoComponentMockingRule<>(XWikiLinkLabelGenerator.class);
56   
 
57  6 toggle @Before
58    public void setUp() throws Exception
59    {
60  6 RenderingConfiguration configuration = this.mocker.getInstance(RenderingConfiguration.class);
61  6 when(configuration.getLinkLabelFormat()).thenReturn(
62    "%l%la%n%na%N%NA [%w:%s.%p] %ls %np %P %NP (%t) [%w:%s.%p] %ls %np %P %NP (%t)");
63   
64  6 EntityReferenceProvider entityReferenceProvider = this.mocker.getInstance(EntityReferenceProvider.class);
65  6 when(entityReferenceProvider.getDefaultReference(EntityType.DOCUMENT)).thenReturn(
66    new EntityReference("WebHome", EntityType.DOCUMENT));
67    }
68   
 
69  1 toggle @Test
70    public void generateWhenTerminalPage() throws Exception
71    {
72  1 ResourceReference resourceReference = new DocumentResourceReference("HelloWorld");
73  1 DocumentReference documentReference =
74    new DocumentReference("wiki", Arrays.asList("space1", "space2"), "HelloWorld");
75   
76  1 EntityReferenceResolver<ResourceReference> resourceReferenceResolver = this.mocker.getInstance(
77    new DefaultParameterizedType(null, EntityReferenceResolver.class, ResourceReference.class));
78  1 when(resourceReferenceResolver.resolve(resourceReference, EntityType.DOCUMENT)).thenReturn(documentReference);
79   
80  1 DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
81  1 DocumentModelBridge dmb = mock(DocumentModelBridge.class);
82  1 when(dab.getDocument(documentReference)).thenReturn(dmb);
83  1 when(dmb.getTitle()).thenReturn("My title");
84   
85  1 EntityReferenceSerializer<String> localSerializer =
86    this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "local");
87  1 when(localSerializer.serialize(new SpaceReference("wiki", "space1", "space2"))).thenReturn("space1.space2");
88   
89  1 assertEquals("%l%la%n%na%N%NA "
90    + "[wiki:space1.space2.HelloWorld] space2 HelloWorld Hello World Hello World (My title) "
91    + "[wiki:space1.space2.HelloWorld] space2 HelloWorld Hello World Hello World (My title)",
92    this.mocker.getComponentUnderTest().generate(resourceReference));
93    }
94   
 
95  1 toggle @Test
96    public void generateWhenNestedPage() throws Exception
97    {
98  1 ResourceReference resourceReference = new DocumentResourceReference("WebHome");
99  1 DocumentReference documentReference =
100    new DocumentReference("wiki", Arrays.asList("space1", "NestedPage"), "WebHome");
101   
102  1 EntityReferenceResolver<ResourceReference> resourceReferenceResolver = this.mocker.getInstance(
103    new DefaultParameterizedType(null, EntityReferenceResolver.class, ResourceReference.class));
104  1 when(resourceReferenceResolver.resolve(resourceReference, EntityType.DOCUMENT)).thenReturn(documentReference);
105   
106  1 DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
107  1 DocumentModelBridge dmb = mock(DocumentModelBridge.class);
108  1 when(dab.getDocument(documentReference)).thenReturn(dmb);
109  1 when(dmb.getTitle()).thenReturn("My title");
110   
111  1 EntityReferenceSerializer<String> localSerializer =
112    this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "local");
113  1 when(localSerializer.serialize(new SpaceReference("wiki", "space1", "NestedPage"))).thenReturn(
114    "space1.NestedPage");
115   
116  1 assertEquals("%l%la%n%na%N%NA "
117    + "[wiki:space1.NestedPage.WebHome] NestedPage NestedPage Web Home Nested Page (My title) "
118    + "[wiki:space1.NestedPage.WebHome] NestedPage NestedPage Web Home Nested Page (My title)",
119    this.mocker.getComponentUnderTest().generate(resourceReference));
120    }
121   
 
122  1 toggle @Test
123    public void generateWhenDocumentFailsToLoad() throws Exception
124    {
125  1 ResourceReference resourceReference = new DocumentResourceReference("HelloWorld");
126  1 DocumentReference documentReference = new DocumentReference("xwiki", "Main", "HelloWorld");
127   
128  1 EntityReferenceResolver<ResourceReference> resourceReferenceResolver = this.mocker.getInstance(
129    new DefaultParameterizedType(null, EntityReferenceResolver.class, ResourceReference.class));
130  1 when(resourceReferenceResolver.resolve(resourceReference, EntityType.DOCUMENT)).thenReturn(documentReference);
131   
132  1 DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
133  1 when(dab.getDocument(documentReference)).thenThrow(new Exception("error"));
134   
135  1 EntityReferenceSerializer<String> localSerializer =
136    this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "local");
137  1 when(localSerializer.serialize(new SpaceReference("xwiki", "Main"))).thenReturn("Main");
138   
139  1 assertEquals("%l%la%n%na%N%NA "
140    + "[xwiki:Main.HelloWorld] Main HelloWorld Hello World Hello World (HelloWorld) "
141    + "[xwiki:Main.HelloWorld] Main HelloWorld Hello World Hello World (HelloWorld)",
142    this.mocker.getComponentUnderTest().generate(resourceReference));
143    }
144   
 
145  1 toggle @Test
146    public void generateWhenDocumentTitleIsNull() throws Exception
147    {
148  1 ResourceReference resourceReference = new DocumentResourceReference("HelloWorld");
149  1 DocumentReference documentReference = new DocumentReference("xwiki", "Main", "HelloWorld");
150   
151  1 EntityReferenceResolver<ResourceReference> resourceReferenceResolver = this.mocker.getInstance(
152    new DefaultParameterizedType(null, EntityReferenceResolver.class, ResourceReference.class));
153  1 when(resourceReferenceResolver.resolve(resourceReference, EntityType.DOCUMENT)).thenReturn(documentReference);
154   
155  1 DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
156  1 DocumentModelBridge dmb = mock(DocumentModelBridge.class);
157  1 when(dab.getDocument(documentReference)).thenReturn(dmb);
158  1 when(dmb.getTitle()).thenReturn(null);
159   
160  1 EntityReferenceSerializer<String> localSerializer =
161    this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "local");
162  1 when(localSerializer.serialize(new SpaceReference("xwiki", "Main"))).thenReturn("Main");
163   
164  1 assertEquals("%l%la%n%na%N%NA "
165    + "[xwiki:Main.HelloWorld] Main HelloWorld Hello World Hello World (HelloWorld) "
166    + "[xwiki:Main.HelloWorld] Main HelloWorld Hello World Hello World (HelloWorld)",
167    this.mocker.getComponentUnderTest().generate(resourceReference));
168    }
169   
 
170  1 toggle @Test
171    public void generateWhithRegexpSyntax() throws Exception
172    {
173  1 ResourceReference resourceReference = new DocumentResourceReference("HelloWorld");
174  1 DocumentReference documentReference = new DocumentReference("$0", "\\", "$0");
175   
176  1 EntityReferenceResolver<ResourceReference> resourceReferenceResolver = this.mocker.getInstance(
177    new DefaultParameterizedType(null, EntityReferenceResolver.class, ResourceReference.class));
178  1 when(resourceReferenceResolver.resolve(resourceReference, EntityType.DOCUMENT)).thenReturn(documentReference);
179   
180  1 DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
181  1 DocumentModelBridge dmb = mock(DocumentModelBridge.class);
182  1 when(dab.getDocument(documentReference)).thenReturn(dmb);
183  1 when(dmb.getTitle()).thenReturn("$0");
184   
185  1 EntityReferenceSerializer<String> localSerializer =
186    this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "local");
187  1 when(localSerializer.serialize(new SpaceReference("$0", "\\"))).thenReturn("\\");
188   
189  1 assertEquals("%l%la%n%na%N%NA [$0:\\.$0] \\ $0 $0 $0 ($0) [$0:\\.$0] \\ $0 $0 $0 ($0)",
190    this.mocker.getComponentUnderTest().generate(resourceReference));
191    }
192   
 
193  1 toggle @Test
194    public void generateWhithPageNameWithPercent() throws Exception
195    {
196  1 ResourceReference resourceReference = new DocumentResourceReference("HelloWorld");
197  1 DocumentReference documentReference = new DocumentReference("wiki", "space", "page%t");
198   
199  1 EntityReferenceResolver<ResourceReference> resourceReferenceResolver = this.mocker.getInstance(
200    new DefaultParameterizedType(null, EntityReferenceResolver.class, ResourceReference.class));
201  1 when(resourceReferenceResolver.resolve(resourceReference, EntityType.DOCUMENT)).thenReturn(documentReference);
202   
203  1 DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
204  1 DocumentModelBridge dmb = mock(DocumentModelBridge.class);
205  1 when(dab.getDocument(documentReference)).thenReturn(dmb);
206  1 when(dmb.getTitle()).thenReturn("my title");
207   
208  1 EntityReferenceSerializer<String> localSerializer =
209    this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "local");
210  1 when(localSerializer.serialize(new SpaceReference("wiki", "space"))).thenReturn("space");
211   
212  1 assertEquals("%l%la%n%na%N%NA "
213    + "[wiki:space.page%t] space page%t page%t page%t (my title) "
214    + "[wiki:space.page%t] space page%t page%t page%t (my title)",
215    this.mocker.getComponentUnderTest().generate(resourceReference));
216    }
217    }