1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.refactoring.internal; |
21 |
|
|
22 |
|
import java.util.Arrays; |
23 |
|
import java.util.Collections; |
24 |
|
import java.util.HashMap; |
25 |
|
import java.util.Map; |
26 |
|
|
27 |
|
import javax.inject.Provider; |
28 |
|
|
29 |
|
import org.junit.Before; |
30 |
|
import org.junit.Rule; |
31 |
|
import org.junit.Test; |
32 |
|
import org.xwiki.component.manager.ComponentManager; |
33 |
|
import org.xwiki.component.util.DefaultParameterizedType; |
34 |
|
import org.xwiki.model.reference.DocumentReference; |
35 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
36 |
|
import org.xwiki.model.reference.EntityReference; |
37 |
|
import org.xwiki.model.reference.EntityReferenceResolver; |
38 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
39 |
|
import org.xwiki.model.reference.SpaceReference; |
40 |
|
import org.xwiki.rendering.block.Block; |
41 |
|
import org.xwiki.rendering.block.LinkBlock; |
42 |
|
import org.xwiki.rendering.block.MacroBlock; |
43 |
|
import org.xwiki.rendering.block.XDOM; |
44 |
|
import org.xwiki.rendering.internal.resolver.DefaultResourceReferenceEntityReferenceResolver; |
45 |
|
import org.xwiki.rendering.listener.reference.ResourceReference; |
46 |
|
import org.xwiki.rendering.listener.reference.ResourceType; |
47 |
|
import org.xwiki.rendering.renderer.BlockRenderer; |
48 |
|
import org.xwiki.rendering.syntax.Syntax; |
49 |
|
import org.xwiki.test.annotation.ComponentList; |
50 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
51 |
|
|
52 |
|
import com.xpn.xwiki.XWiki; |
53 |
|
import com.xpn.xwiki.XWikiContext; |
54 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
55 |
|
import com.xpn.xwiki.internal.render.DefaultLinkedResourceHelper; |
56 |
|
|
57 |
|
import static org.junit.Assert.assertEquals; |
58 |
|
import static org.mockito.ArgumentMatchers.any; |
59 |
|
import static org.mockito.ArgumentMatchers.eq; |
60 |
|
import static org.mockito.Mockito.mock; |
61 |
|
import static org.mockito.Mockito.verify; |
62 |
|
import static org.mockito.Mockito.when; |
63 |
|
|
64 |
|
|
65 |
|
@link |
66 |
|
|
67 |
|
@version |
68 |
|
|
69 |
|
@ComponentList(DefaultLinkedResourceHelper.class) |
|
|
| 100% |
Uncovered Elements: 0 (208) |
Complexity: 9 |
Complexity Density: 0.05 |
|
70 |
|
public class DefaultLinkRefactoringTest |
71 |
|
{ |
72 |
|
@Rule |
73 |
|
public MockitoComponentMockingRule<LinkRefactoring> mocker = new MockitoComponentMockingRule<LinkRefactoring>( |
74 |
|
DefaultLinkRefactoring.class); |
75 |
|
|
76 |
|
private XWikiContext xcontext = mock(XWikiContext.class); |
77 |
|
|
78 |
|
private EntityReferenceSerializer<String> compactEntityReferenceSerializer; |
79 |
|
|
80 |
|
private DocumentReferenceResolver<EntityReference> defaultReferenceDocumentReferenceResolver; |
81 |
|
|
82 |
|
private EntityReferenceResolver<ResourceReference> resourceReferenceResolver; |
83 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
84 |
7 |
@Before... |
85 |
|
public void configure() throws Exception |
86 |
|
{ |
87 |
7 |
XWiki xwiki = mock(XWiki.class); |
88 |
7 |
when(this.xcontext.getWiki()).thenReturn(xwiki); |
89 |
|
|
90 |
7 |
Provider<XWikiContext> xcontextProvider = this.mocker.getInstance(XWikiContext.TYPE_PROVIDER); |
91 |
7 |
when(xcontextProvider.get()).thenReturn(this.xcontext); |
92 |
|
|
93 |
7 |
this.resourceReferenceResolver = |
94 |
|
this.mocker.getInstance(DefaultResourceReferenceEntityReferenceResolver.TYPE_RESOURCEREFERENCE); |
95 |
7 |
this.defaultReferenceDocumentReferenceResolver = |
96 |
|
this.mocker.getInstance(DocumentReferenceResolver.TYPE_REFERENCE); |
97 |
7 |
this.compactEntityReferenceSerializer = |
98 |
|
this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "compact"); |
99 |
|
|
100 |
7 |
Provider<ComponentManager> contextComponentManagerProvider = |
101 |
|
this.mocker.registerMockComponent( |
102 |
|
new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context"); |
103 |
7 |
when(contextComponentManagerProvider.get()).thenReturn(this.mocker); |
104 |
|
} |
105 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (28) |
Complexity: 1 |
Complexity Density: 0.04 |
1PASS
|
|
106 |
1 |
@Test... |
107 |
|
public void updateRelativeLinks() throws Exception |
108 |
|
{ |
109 |
1 |
DocumentReference oldReference = new DocumentReference("wiki", "A", "B"); |
110 |
1 |
DocumentReference newReference = new DocumentReference("wiki", "X", "Y"); |
111 |
|
|
112 |
1 |
XWikiDocument newDocument = mock(XWikiDocument.class); |
113 |
1 |
when(this.xcontext.getWiki().getDocument(newReference, this.xcontext)).thenReturn(newDocument); |
114 |
1 |
when(newDocument.getDocumentReference()).thenReturn(newReference); |
115 |
1 |
when(newDocument.getSyntax()).thenReturn(Syntax.XWIKI_2_1); |
116 |
1 |
this.mocker.registerMockComponent(BlockRenderer.class, Syntax.XWIKI_2_1.toIdString()); |
117 |
|
|
118 |
1 |
XDOM xdom = mock(XDOM.class); |
119 |
1 |
when(newDocument.getXDOM()).thenReturn(xdom); |
120 |
|
|
121 |
1 |
ResourceReference docLinkReference = new ResourceReference("C", ResourceType.DOCUMENT); |
122 |
1 |
LinkBlock docLinkBlock = new LinkBlock(Collections.<Block>emptyList(), docLinkReference, false); |
123 |
|
|
124 |
1 |
ResourceReference spaceLinkReference = new ResourceReference("Z", ResourceType.SPACE); |
125 |
1 |
LinkBlock spaceLinkBlock = new LinkBlock(Collections.<Block>emptyList(), spaceLinkReference, false); |
126 |
|
|
127 |
1 |
when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT))) |
128 |
|
.thenReturn(Arrays.<Block>asList(docLinkBlock, spaceLinkBlock)); |
129 |
|
|
130 |
1 |
DocumentReference originalDocLinkReference = new DocumentReference("C", oldReference.getLastSpaceReference()); |
131 |
1 |
when(this.resourceReferenceResolver.resolve(docLinkReference, null, oldReference)).thenReturn( |
132 |
|
originalDocLinkReference); |
133 |
1 |
DocumentReference newDocLinkReference = new DocumentReference("C", newReference.getLastSpaceReference()); |
134 |
1 |
when(this.resourceReferenceResolver.resolve(docLinkReference, null, newReference)).thenReturn( |
135 |
|
newDocLinkReference); |
136 |
|
|
137 |
1 |
SpaceReference originalSpaceReference = new SpaceReference("wiki", "Z"); |
138 |
1 |
when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, oldReference)).thenReturn( |
139 |
|
originalSpaceReference); |
140 |
1 |
when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, newReference)).thenReturn( |
141 |
|
originalSpaceReference); |
142 |
|
|
143 |
1 |
when(this.compactEntityReferenceSerializer.serialize(originalDocLinkReference, newReference)).thenReturn("A.C"); |
144 |
|
|
145 |
1 |
this.mocker.getComponentUnderTest().updateRelativeLinks(oldReference, newReference); |
146 |
|
|
147 |
|
|
148 |
1 |
assertEquals("A.C", docLinkBlock.getReference().getReference()); |
149 |
1 |
assertEquals(ResourceType.DOCUMENT, docLinkBlock.getReference().getType()); |
150 |
|
|
151 |
1 |
assertEquals("Z", spaceLinkBlock.getReference().getReference()); |
152 |
1 |
assertEquals(ResourceType.SPACE, spaceLinkBlock.getReference().getType()); |
153 |
1 |
verifyDocumentSave(newDocument, "Updated the relative links.", true); |
154 |
|
} |
155 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (30) |
Complexity: 1 |
Complexity Density: 0.03 |
1PASS
|
|
156 |
1 |
@Test... |
157 |
|
public void updateRelativeLinksAcrossWikis() throws Exception |
158 |
|
{ |
159 |
1 |
DocumentReference oldReference = new DocumentReference("wiki1", "A", "B"); |
160 |
1 |
DocumentReference newReference = new DocumentReference("wiki2", "X", "Y"); |
161 |
|
|
162 |
1 |
XWikiDocument newDocument = mock(XWikiDocument.class); |
163 |
1 |
when(this.xcontext.getWiki().getDocument(newReference, this.xcontext)).thenReturn(newDocument); |
164 |
1 |
when(newDocument.getDocumentReference()).thenReturn(newReference); |
165 |
1 |
when(newDocument.getSyntax()).thenReturn(Syntax.XWIKI_2_1); |
166 |
1 |
this.mocker.registerMockComponent(BlockRenderer.class, Syntax.XWIKI_2_1.toIdString()); |
167 |
|
|
168 |
1 |
XDOM xdom = mock(XDOM.class); |
169 |
1 |
when(newDocument.getXDOM()).thenReturn(xdom); |
170 |
|
|
171 |
1 |
ResourceReference docLinkReference = new ResourceReference("C", ResourceType.DOCUMENT); |
172 |
1 |
LinkBlock docLinkBlock = new LinkBlock(Collections.<Block>emptyList(), docLinkReference, false); |
173 |
|
|
174 |
1 |
ResourceReference spaceLinkReference = new ResourceReference("Z", ResourceType.SPACE); |
175 |
1 |
LinkBlock spaceLinkBlock = new LinkBlock(Collections.<Block>emptyList(), spaceLinkReference, false); |
176 |
|
|
177 |
1 |
when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT))) |
178 |
|
.thenReturn(Arrays.<Block>asList(docLinkBlock, spaceLinkBlock)); |
179 |
|
|
180 |
1 |
DocumentReference originalDocLinkReference = new DocumentReference("C", oldReference.getLastSpaceReference()); |
181 |
1 |
when(this.resourceReferenceResolver.resolve(docLinkReference, null, oldReference)).thenReturn( |
182 |
|
originalDocLinkReference); |
183 |
1 |
DocumentReference newDocLinkReference = new DocumentReference("C", newReference.getLastSpaceReference()); |
184 |
1 |
when(this.resourceReferenceResolver.resolve(docLinkReference, null, newReference)).thenReturn( |
185 |
|
newDocLinkReference); |
186 |
|
|
187 |
1 |
SpaceReference originalSpaceReference = new SpaceReference("wiki1", "Z"); |
188 |
1 |
when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, oldReference)).thenReturn( |
189 |
|
originalSpaceReference); |
190 |
1 |
SpaceReference newSpaceReference = new SpaceReference("wiki2", "Z"); |
191 |
1 |
when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, newReference)).thenReturn( |
192 |
|
newSpaceReference); |
193 |
|
|
194 |
1 |
when(this.compactEntityReferenceSerializer.serialize(originalDocLinkReference, newReference)).thenReturn( |
195 |
|
"wiki1:A.C"); |
196 |
1 |
when(this.compactEntityReferenceSerializer.serialize(originalSpaceReference, newReference)).thenReturn( |
197 |
|
"wiki1:Z"); |
198 |
|
|
199 |
1 |
this.mocker.getComponentUnderTest().updateRelativeLinks(oldReference, newReference); |
200 |
|
|
201 |
|
|
202 |
1 |
assertEquals("wiki1:A.C", docLinkBlock.getReference().getReference()); |
203 |
1 |
assertEquals(ResourceType.DOCUMENT, docLinkBlock.getReference().getType()); |
204 |
|
|
205 |
1 |
assertEquals("wiki1:Z", spaceLinkBlock.getReference().getReference()); |
206 |
1 |
assertEquals(ResourceType.SPACE, spaceLinkBlock.getReference().getType()); |
207 |
1 |
verifyDocumentSave(newDocument, "Updated the relative links.", true); |
208 |
|
} |
209 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (20) |
Complexity: 1 |
Complexity Density: 0.05 |
1PASS
|
|
210 |
1 |
@Test... |
211 |
|
public void renameLinks() throws Exception |
212 |
|
{ |
213 |
1 |
DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page"); |
214 |
1 |
XWikiDocument document = mock(XWikiDocument.class); |
215 |
1 |
when(this.xcontext.getWiki().getDocument(documentReference, this.xcontext)).thenReturn(document); |
216 |
1 |
when(document.getDocumentReference()).thenReturn(documentReference); |
217 |
1 |
when(document.getSyntax()).thenReturn(Syntax.XWIKI_2_1); |
218 |
1 |
this.mocker.registerMockComponent(BlockRenderer.class, Syntax.XWIKI_2_1.toIdString()); |
219 |
|
|
220 |
|
|
221 |
1 |
DocumentReference oldLinkTarget = new DocumentReference("wiki", "A", "B"); |
222 |
1 |
DocumentReference newLinkTarget = new DocumentReference("wiki", "X", "Y"); |
223 |
|
|
224 |
1 |
XDOM xdom = mock(XDOM.class); |
225 |
1 |
when(document.getXDOM()).thenReturn(xdom); |
226 |
|
|
227 |
1 |
ResourceReference linkReference = new ResourceReference("A.B", ResourceType.DOCUMENT); |
228 |
1 |
LinkBlock linkBlock = new LinkBlock(Collections.<Block>emptyList(), linkReference, false); |
229 |
1 |
when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT))).thenReturn(Arrays.<Block>asList(linkBlock)); |
230 |
|
|
231 |
1 |
when(this.resourceReferenceResolver.resolve(linkReference, null, documentReference)).thenReturn(oldLinkTarget); |
232 |
1 |
when(this.defaultReferenceDocumentReferenceResolver.resolve(oldLinkTarget)).thenReturn(oldLinkTarget); |
233 |
|
|
234 |
1 |
when(this.compactEntityReferenceSerializer.serialize(newLinkTarget, documentReference)).thenReturn("X.Y"); |
235 |
|
|
236 |
1 |
this.mocker.getComponentUnderTest().renameLinks(documentReference, oldLinkTarget, newLinkTarget); |
237 |
|
|
238 |
1 |
assertEquals("X.Y", linkBlock.getReference().getReference()); |
239 |
1 |
assertEquals(ResourceType.DOCUMENT, linkBlock.getReference().getType()); |
240 |
1 |
verifyDocumentSave(document, "Renamed back-links.", false); |
241 |
|
} |
242 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (28) |
Complexity: 1 |
Complexity Density: 0.04 |
1PASS
|
|
243 |
1 |
@Test... |
244 |
|
public void renameNonTerminalDocumentLinks() throws Exception |
245 |
|
{ |
246 |
1 |
DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page"); |
247 |
1 |
XWikiDocument document = mock(XWikiDocument.class); |
248 |
1 |
when(this.xcontext.getWiki().getDocument(documentReference, this.xcontext)).thenReturn(document); |
249 |
1 |
when(document.getDocumentReference()).thenReturn(documentReference); |
250 |
1 |
when(document.getSyntax()).thenReturn(Syntax.XWIKI_2_1); |
251 |
1 |
this.mocker.registerMockComponent(BlockRenderer.class, Syntax.XWIKI_2_1.toIdString()); |
252 |
|
|
253 |
|
|
254 |
1 |
DocumentReference oldLinkTarget = new DocumentReference("wiki", "A", "WebHome"); |
255 |
1 |
DocumentReference newLinkTarget = new DocumentReference("wiki", "X", "WebHome"); |
256 |
|
|
257 |
1 |
XDOM xdom = mock(XDOM.class); |
258 |
1 |
when(document.getXDOM()).thenReturn(xdom); |
259 |
|
|
260 |
1 |
ResourceReference docLinkReference = new ResourceReference("A.WebHome", ResourceType.DOCUMENT); |
261 |
1 |
LinkBlock documentLinkBlock = new LinkBlock(Collections.<Block>emptyList(), docLinkReference, false); |
262 |
|
|
263 |
1 |
ResourceReference spaceLinkReference = new ResourceReference("A", ResourceType.SPACE); |
264 |
1 |
LinkBlock spaceLinkBlock = new LinkBlock(Collections.<Block>emptyList(), spaceLinkReference, false); |
265 |
|
|
266 |
1 |
when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT))) |
267 |
|
.thenReturn(Arrays.<Block>asList(documentLinkBlock, spaceLinkBlock)); |
268 |
|
|
269 |
|
|
270 |
1 |
when(this.resourceReferenceResolver.resolve(docLinkReference, null, documentReference)).thenReturn( |
271 |
|
oldLinkTarget); |
272 |
1 |
when(this.defaultReferenceDocumentReferenceResolver.resolve(oldLinkTarget)).thenReturn(oldLinkTarget); |
273 |
1 |
when(this.compactEntityReferenceSerializer.serialize(newLinkTarget, documentReference)).thenReturn("X.WebHome"); |
274 |
|
|
275 |
|
|
276 |
1 |
SpaceReference spaceReference = oldLinkTarget.getLastSpaceReference(); |
277 |
1 |
when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, documentReference)).thenReturn( |
278 |
|
spaceReference); |
279 |
1 |
when(this.defaultReferenceDocumentReferenceResolver.resolve(spaceReference)).thenReturn(oldLinkTarget); |
280 |
1 |
when(this.compactEntityReferenceSerializer.serialize(newLinkTarget.getLastSpaceReference(), documentReference)) |
281 |
|
.thenReturn("X"); |
282 |
|
|
283 |
1 |
this.mocker.getComponentUnderTest().renameLinks(documentReference, oldLinkTarget, newLinkTarget); |
284 |
|
|
285 |
1 |
assertEquals("X.WebHome", documentLinkBlock.getReference().getReference()); |
286 |
1 |
assertEquals(ResourceType.DOCUMENT, documentLinkBlock.getReference().getType()); |
287 |
1 |
assertEquals("X", spaceLinkBlock.getReference().getReference()); |
288 |
1 |
assertEquals(ResourceType.SPACE, spaceLinkBlock.getReference().getType()); |
289 |
1 |
verifyDocumentSave(document, "Renamed back-links.", false); |
290 |
|
} |
291 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (28) |
Complexity: 1 |
Complexity Density: 0.04 |
1PASS
|
|
292 |
1 |
@Test... |
293 |
|
public void renameNonTerminalToTerminalDocumentLinks() throws Exception |
294 |
|
{ |
295 |
1 |
DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page"); |
296 |
1 |
XWikiDocument document = mock(XWikiDocument.class); |
297 |
1 |
when(this.xcontext.getWiki().getDocument(documentReference, this.xcontext)).thenReturn(document); |
298 |
1 |
when(document.getDocumentReference()).thenReturn(documentReference); |
299 |
1 |
when(document.getSyntax()).thenReturn(Syntax.XWIKI_2_1); |
300 |
1 |
this.mocker.registerMockComponent(BlockRenderer.class, Syntax.XWIKI_2_1.toIdString()); |
301 |
|
|
302 |
|
|
303 |
1 |
DocumentReference oldLinkTarget = new DocumentReference("wiki", "A", "WebHome"); |
304 |
1 |
DocumentReference newLinkTarget = new DocumentReference("wiki", "X", "Y"); |
305 |
|
|
306 |
1 |
XDOM xdom = mock(XDOM.class); |
307 |
1 |
when(document.getXDOM()).thenReturn(xdom); |
308 |
|
|
309 |
1 |
ResourceReference docLinkReference = new ResourceReference("A.WebHome", ResourceType.DOCUMENT); |
310 |
1 |
LinkBlock documentLinkBlock = new LinkBlock(Collections.<Block>emptyList(), docLinkReference, false); |
311 |
|
|
312 |
1 |
ResourceReference spaceLinkReference = new ResourceReference("A", ResourceType.SPACE); |
313 |
1 |
LinkBlock spaceLinkBlock = new LinkBlock(Collections.<Block>emptyList(), spaceLinkReference, false); |
314 |
|
|
315 |
1 |
when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT))) |
316 |
|
.thenReturn(Arrays.<Block>asList(documentLinkBlock, spaceLinkBlock)); |
317 |
|
|
318 |
|
|
319 |
1 |
when(this.resourceReferenceResolver.resolve(docLinkReference, null, documentReference)).thenReturn( |
320 |
|
oldLinkTarget); |
321 |
1 |
when(this.defaultReferenceDocumentReferenceResolver.resolve(oldLinkTarget)).thenReturn(oldLinkTarget); |
322 |
1 |
when(this.compactEntityReferenceSerializer.serialize(newLinkTarget, documentReference)).thenReturn("X.Y"); |
323 |
|
|
324 |
|
|
325 |
1 |
SpaceReference spaceReference = oldLinkTarget.getLastSpaceReference(); |
326 |
1 |
when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, documentReference)).thenReturn( |
327 |
|
spaceReference); |
328 |
1 |
when(this.defaultReferenceDocumentReferenceResolver.resolve(spaceReference)).thenReturn(oldLinkTarget); |
329 |
1 |
when(this.compactEntityReferenceSerializer.serialize(newLinkTarget.getLastSpaceReference(), documentReference)) |
330 |
|
.thenReturn("X"); |
331 |
|
|
332 |
1 |
this.mocker.getComponentUnderTest().renameLinks(documentReference, oldLinkTarget, newLinkTarget); |
333 |
|
|
334 |
|
|
335 |
|
|
336 |
1 |
assertEquals("X.Y", documentLinkBlock.getReference().getReference()); |
337 |
1 |
assertEquals(ResourceType.DOCUMENT, documentLinkBlock.getReference().getType()); |
338 |
1 |
assertEquals("X.Y", spaceLinkBlock.getReference().getReference()); |
339 |
1 |
assertEquals(ResourceType.DOCUMENT, spaceLinkBlock.getReference().getType()); |
340 |
1 |
verifyDocumentSave(document, "Renamed back-links.", false); |
341 |
|
} |
342 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (29) |
Complexity: 1 |
Complexity Density: 0.03 |
1PASS
|
|
343 |
1 |
@Test... |
344 |
|
public void renameLinksFromMacros() throws Exception |
345 |
|
{ |
346 |
1 |
DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page"); |
347 |
1 |
XWikiDocument document = mock(XWikiDocument.class); |
348 |
1 |
when(this.xcontext.getWiki().getDocument(documentReference, this.xcontext)).thenReturn(document); |
349 |
1 |
when(document.getDocumentReference()).thenReturn(documentReference); |
350 |
1 |
when(document.getSyntax()).thenReturn(Syntax.XWIKI_2_1); |
351 |
1 |
this.mocker.registerMockComponent(BlockRenderer.class, Syntax.XWIKI_2_1.toIdString()); |
352 |
|
|
353 |
|
|
354 |
1 |
DocumentReference oldLinkTarget = new DocumentReference("wiki", "A", "B"); |
355 |
1 |
DocumentReference newLinkTarget = new DocumentReference("wiki", "X", "Y"); |
356 |
|
|
357 |
1 |
XDOM xdom = mock(XDOM.class); |
358 |
1 |
when(document.getXDOM()).thenReturn(xdom); |
359 |
|
|
360 |
1 |
Map<String, String> includeParameters = new HashMap<String, String>(); |
361 |
1 |
includeParameters.put("reference", "A.B"); |
362 |
1 |
MacroBlock includeMacroBlock1 = new MacroBlock("include", includeParameters, false); |
363 |
|
|
364 |
1 |
Map<String, String> includeOldParameters = new HashMap<String, String>(); |
365 |
1 |
includeOldParameters.put("document", "A.B"); |
366 |
1 |
MacroBlock includeMacroBlock2 = new MacroBlock("include", includeOldParameters, false); |
367 |
|
|
368 |
1 |
Map<String, String> displayParameters = new HashMap<String, String>(); |
369 |
1 |
displayParameters.put("reference", "A.B"); |
370 |
1 |
MacroBlock displayMacroBlock = new MacroBlock("display", displayParameters, false); |
371 |
|
|
372 |
1 |
when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT))) |
373 |
|
.thenReturn(Arrays.<Block>asList(includeMacroBlock1, includeMacroBlock2, displayMacroBlock)); |
374 |
|
|
375 |
1 |
ResourceReference macroResourceReference = new ResourceReference("A.B", ResourceType.DOCUMENT); |
376 |
|
|
377 |
1 |
when(this.resourceReferenceResolver.resolve(macroResourceReference, null, documentReference)).thenReturn( |
378 |
|
oldLinkTarget); |
379 |
1 |
when(this.defaultReferenceDocumentReferenceResolver.resolve(oldLinkTarget)).thenReturn(oldLinkTarget); |
380 |
1 |
when(this.compactEntityReferenceSerializer.serialize(newLinkTarget, documentReference)).thenReturn("X.Y"); |
381 |
|
|
382 |
1 |
this.mocker.getComponentUnderTest().renameLinks(documentReference, oldLinkTarget, newLinkTarget); |
383 |
|
|
384 |
1 |
assertEquals("X.Y", includeMacroBlock1.getParameter("reference")); |
385 |
1 |
assertEquals("X.Y", includeMacroBlock2.getParameter("document")); |
386 |
1 |
assertEquals("X.Y", displayMacroBlock.getParameter("reference")); |
387 |
1 |
verifyDocumentSave(document, "Renamed back-links.", false); |
388 |
|
} |
389 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (24) |
Complexity: 1 |
Complexity Density: 0.04 |
1PASS
|
|
390 |
1 |
@Test... |
391 |
|
public void renameLinksFromLinksAndMacros() throws Exception |
392 |
|
{ |
393 |
1 |
DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page"); |
394 |
1 |
XWikiDocument document = mock(XWikiDocument.class); |
395 |
1 |
when(this.xcontext.getWiki().getDocument(documentReference, this.xcontext)).thenReturn(document); |
396 |
1 |
when(document.getDocumentReference()).thenReturn(documentReference); |
397 |
1 |
when(document.getSyntax()).thenReturn(Syntax.XWIKI_2_1); |
398 |
1 |
this.mocker.registerMockComponent(BlockRenderer.class, Syntax.XWIKI_2_1.toIdString()); |
399 |
|
|
400 |
|
|
401 |
1 |
DocumentReference oldLinkTarget = new DocumentReference("wiki", "A", "B"); |
402 |
1 |
DocumentReference newLinkTarget = new DocumentReference("wiki", "X", "Y"); |
403 |
|
|
404 |
1 |
XDOM xdom = mock(XDOM.class); |
405 |
1 |
when(document.getXDOM()).thenReturn(xdom); |
406 |
|
|
407 |
1 |
Map<String, String> includeParameters = new HashMap<String, String>(); |
408 |
1 |
includeParameters.put("reference", "A.B"); |
409 |
1 |
MacroBlock includeMacroBlock = new MacroBlock("include", includeParameters, false); |
410 |
|
|
411 |
1 |
ResourceReference resourceReference = new ResourceReference("A.B", ResourceType.DOCUMENT); |
412 |
1 |
LinkBlock documentLinkBlock = new LinkBlock(Collections.<Block>emptyList(), resourceReference, false); |
413 |
|
|
414 |
1 |
when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT))) |
415 |
|
.thenReturn(Arrays.<Block>asList(includeMacroBlock, documentLinkBlock)); |
416 |
|
|
417 |
1 |
when(this.resourceReferenceResolver.resolve(resourceReference, null, documentReference)).thenReturn( |
418 |
|
oldLinkTarget); |
419 |
1 |
when(this.defaultReferenceDocumentReferenceResolver.resolve(oldLinkTarget)).thenReturn(oldLinkTarget); |
420 |
1 |
when(this.compactEntityReferenceSerializer.serialize(newLinkTarget, documentReference)).thenReturn("X.Y"); |
421 |
|
|
422 |
1 |
this.mocker.getComponentUnderTest().renameLinks(documentReference, oldLinkTarget, newLinkTarget); |
423 |
|
|
424 |
1 |
assertEquals("X.Y", includeMacroBlock.getParameter("reference")); |
425 |
1 |
assertEquals("X.Y", documentLinkBlock.getReference().getReference()); |
426 |
1 |
assertEquals(ResourceType.DOCUMENT, documentLinkBlock.getReference().getType()); |
427 |
1 |
verifyDocumentSave(document, "Renamed back-links.", false); |
428 |
|
} |
429 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
430 |
7 |
private void verifyDocumentSave(XWikiDocument document, String comment, boolean minorEdit) throws Exception... |
431 |
|
{ |
432 |
|
|
433 |
7 |
verify(document).setContentDirty(false); |
434 |
|
|
435 |
7 |
verify(document).setMetaDataDirty(true); |
436 |
7 |
verify(this.xcontext.getWiki()).saveDocument(document, comment, minorEdit, this.xcontext); |
437 |
|
} |
438 |
|
} |