1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
47 |
|
|
48 |
|
@version |
49 |
|
@since |
50 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (75) |
Complexity: 7 |
Complexity Density: 0.1 |
|
51 |
|
public class XWikiLinkLabelGeneratorTest |
52 |
|
{ |
53 |
|
@Rule |
54 |
|
public MockitoComponentMockingRule<XWikiLinkLabelGenerator> mocker = |
55 |
|
new MockitoComponentMockingRule<>(XWikiLinkLabelGenerator.class); |
56 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
57 |
6 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
69 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
95 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
122 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
145 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
170 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
193 |
1 |
@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 |
|
} |