| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package com.xpn.xwiki; |
| 21 |
|
|
| 22 |
|
import java.sql.Timestamp; |
| 23 |
|
import java.util.Arrays; |
| 24 |
|
import java.util.Date; |
| 25 |
|
import java.util.HashMap; |
| 26 |
|
import java.util.Locale; |
| 27 |
|
import java.util.Map; |
| 28 |
|
|
| 29 |
|
import org.junit.Assert; |
| 30 |
|
import org.junit.Before; |
| 31 |
|
import org.junit.Rule; |
| 32 |
|
import org.junit.Test; |
| 33 |
|
import org.mockito.ArgumentMatcher; |
| 34 |
|
import org.mockito.invocation.InvocationOnMock; |
| 35 |
|
import org.mockito.stubbing.Answer; |
| 36 |
|
import org.xwiki.bridge.event.DocumentDeletedEvent; |
| 37 |
|
import org.xwiki.bridge.event.DocumentDeletingEvent; |
| 38 |
|
import org.xwiki.bridge.event.DocumentRolledBackEvent; |
| 39 |
|
import org.xwiki.bridge.event.DocumentRollingBackEvent; |
| 40 |
|
import org.xwiki.bridge.event.DocumentUpdatedEvent; |
| 41 |
|
import org.xwiki.bridge.event.DocumentUpdatingEvent; |
| 42 |
|
import org.xwiki.configuration.ConfigurationSource; |
| 43 |
|
import org.xwiki.environment.Environment; |
| 44 |
|
import org.xwiki.localization.ContextualLocalizationManager; |
| 45 |
|
import org.xwiki.model.reference.AttachmentReference; |
| 46 |
|
import org.xwiki.model.reference.AttachmentReferenceResolver; |
| 47 |
|
import org.xwiki.model.reference.DocumentReference; |
| 48 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
| 49 |
|
import org.xwiki.model.reference.EntityReference; |
| 50 |
|
import org.xwiki.model.reference.EntityReferenceProvider; |
| 51 |
|
import org.xwiki.model.reference.EntityReferenceResolver; |
| 52 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
| 53 |
|
import org.xwiki.model.reference.ObjectReferenceResolver; |
| 54 |
|
import org.xwiki.model.reference.SpaceReference; |
| 55 |
|
import org.xwiki.model.reference.WikiReference; |
| 56 |
|
import org.xwiki.observation.ObservationManager; |
| 57 |
|
import org.xwiki.rendering.syntax.SyntaxFactory; |
| 58 |
|
import org.xwiki.resource.ResourceReferenceManager; |
| 59 |
|
import org.xwiki.test.mockito.MockitoComponentManagerRule; |
| 60 |
|
|
| 61 |
|
import com.xpn.xwiki.doc.XWikiAttachment; |
| 62 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
| 63 |
|
import com.xpn.xwiki.internal.XWikiCfgConfigurationSource; |
| 64 |
|
import com.xpn.xwiki.objects.BaseObject; |
| 65 |
|
import com.xpn.xwiki.store.AttachmentRecycleBinStore; |
| 66 |
|
import com.xpn.xwiki.store.XWikiRecycleBinStoreInterface; |
| 67 |
|
import com.xpn.xwiki.store.XWikiStoreInterface; |
| 68 |
|
import com.xpn.xwiki.store.XWikiVersioningStoreInterface; |
| 69 |
|
import com.xpn.xwiki.web.Utils; |
| 70 |
|
import com.xpn.xwiki.web.XWikiURLFactory; |
| 71 |
|
|
| 72 |
|
import static org.junit.Assert.assertEquals; |
| 73 |
|
import static org.mockito.ArgumentMatchers.any; |
| 74 |
|
import static org.mockito.ArgumentMatchers.anyString; |
| 75 |
|
import static org.mockito.ArgumentMatchers.argThat; |
| 76 |
|
import static org.mockito.ArgumentMatchers.eq; |
| 77 |
|
import static org.mockito.ArgumentMatchers.same; |
| 78 |
|
import static org.mockito.Mockito.mock; |
| 79 |
|
import static org.mockito.Mockito.never; |
| 80 |
|
import static org.mockito.Mockito.verify; |
| 81 |
|
import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 82 |
|
import static org.mockito.Mockito.when; |
| 83 |
|
|
| 84 |
|
|
| 85 |
|
@link |
| 86 |
|
|
| 87 |
|
@version |
| 88 |
|
|
| |
|
| 98.7% |
Uncovered Elements: 3 (231) |
Complexity: 16 |
Complexity Density: 0.08 |
|
| 89 |
|
public class XWikiMockitoTest |
| 90 |
|
{ |
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
@Rule |
| 95 |
|
public MockitoComponentManagerRule mocker = new MockitoComponentManagerRule(); |
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
|
private XWiki xwiki; |
| 101 |
|
|
| 102 |
|
|
| 103 |
|
@link |
| 104 |
|
|
| 105 |
|
private XWikiContext context = new XWikiContext(); |
| 106 |
|
|
| 107 |
|
private ConfigurationSource xwikiCfgConfigurationSource; |
| 108 |
|
|
| 109 |
|
private XWikiStoreInterface storeMock; |
| 110 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (29) |
Complexity: 1 |
Complexity Density: 0.03 |
|
| 111 |
10 |
@Before... |
| 112 |
|
public void setUp() throws Exception |
| 113 |
|
{ |
| 114 |
10 |
this.mocker.registerMockComponent(EntityReferenceSerializer.TYPE_STRING); |
| 115 |
10 |
this.mocker.registerMockComponent(EntityReferenceSerializer.TYPE_STRING, "local"); |
| 116 |
10 |
this.mocker.registerMockComponent(EntityReferenceSerializer.TYPE_STRING, "compact"); |
| 117 |
10 |
this.mocker.registerMockComponent(EntityReferenceSerializer.TYPE_STRING, "compactwiki"); |
| 118 |
10 |
this.mocker.registerMockComponent(EntityReferenceSerializer.TYPE_STRING, "uid"); |
| 119 |
10 |
this.mocker.registerMockComponent(EntityReferenceSerializer.TYPE_STRING, "local/uid"); |
| 120 |
10 |
this.mocker.registerMockComponent(EntityReferenceResolver.TYPE_STRING, "relative"); |
| 121 |
10 |
this.mocker.registerMockComponent(EntityReferenceResolver.TYPE_STRING, "currentmixed"); |
| 122 |
10 |
this.mocker.registerMockComponent(EntityReferenceResolver.TYPE_STRING, "xclass"); |
| 123 |
10 |
this.mocker.registerMockComponent(DocumentReferenceResolver.TYPE_REFERENCE, "current"); |
| 124 |
10 |
this.mocker.registerMockComponent(DocumentReferenceResolver.TYPE_REFERENCE, "explicit"); |
| 125 |
10 |
this.mocker.registerMockComponent(DocumentReferenceResolver.TYPE_STRING, "current"); |
| 126 |
10 |
this.mocker.registerMockComponent(DocumentReferenceResolver.TYPE_STRING, "explicit"); |
| 127 |
10 |
this.mocker.registerMockComponent(DocumentReferenceResolver.TYPE_STRING, "currentmixed"); |
| 128 |
10 |
this.mocker.registerMockComponent(ObjectReferenceResolver.TYPE_REFERENCE, "current"); |
| 129 |
10 |
this.mocker.registerMockComponent(EntityReferenceProvider.class); |
| 130 |
10 |
this.mocker.registerMockComponent(SyntaxFactory.class); |
| 131 |
10 |
this.mocker.registerMockComponent(ResourceReferenceManager.class); |
| 132 |
10 |
this.mocker.registerMockComponent(Environment.class); |
| 133 |
10 |
this.mocker.registerMockComponent(ObservationManager.class); |
| 134 |
10 |
this.mocker.registerMockComponent(ConfigurationSource.class, XWikiCfgConfigurationSource.ROLEHINT); |
| 135 |
|
|
| 136 |
10 |
Utils.setComponentManager(mocker); |
| 137 |
10 |
xwiki = new XWiki(); |
| 138 |
10 |
this.context.setWiki(this.xwiki); |
| 139 |
|
|
| 140 |
10 |
this.storeMock = mock(XWikiStoreInterface.class); |
| 141 |
10 |
xwiki.setStore(storeMock); |
| 142 |
|
|
| 143 |
10 |
XWikiVersioningStoreInterface versioningStore = mock(XWikiVersioningStoreInterface.class); |
| 144 |
10 |
xwiki.setVersioningStore(versioningStore); |
| 145 |
|
|
| 146 |
10 |
this.xwikiCfgConfigurationSource = this.mocker.registerMockComponent(ConfigurationSource.class, "xwikicfg"); |
| 147 |
|
} |
| 148 |
|
|
| 149 |
|
|
| 150 |
|
|
| 151 |
|
|
| 152 |
|
@see |
| 153 |
|
|
| 154 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
| 155 |
1 |
@Test... |
| 156 |
|
public void copyDocumentPreservesAttachmentsVersion() throws Exception |
| 157 |
|
{ |
| 158 |
1 |
DocumentReference targetReference = new DocumentReference("bar", "Space", "Target"); |
| 159 |
1 |
XWikiDocument target = mock(XWikiDocument.class); |
| 160 |
1 |
when(target.isNew()).thenReturn(true); |
| 161 |
1 |
when(target.getDocumentReference()).thenReturn(targetReference); |
| 162 |
|
|
| 163 |
1 |
DocumentReference sourceReference = new DocumentReference("foo", "Space", "Source"); |
| 164 |
1 |
XWikiDocument source = mock(XWikiDocument.class); |
| 165 |
1 |
when(source.copyDocument(targetReference, context)).thenReturn(target); |
| 166 |
|
|
| 167 |
1 |
when(xwiki.getStore().loadXWikiDoc(any(XWikiDocument.class), same(context))).thenReturn(source, target); |
| 168 |
|
|
| 169 |
1 |
Assert.assertTrue(xwiki.copyDocument(sourceReference, targetReference, context)); |
| 170 |
|
|
| 171 |
|
|
| 172 |
1 |
verify(target).setNew(true); |
| 173 |
|
|
| 174 |
1 |
verify(xwiki.getStore()).saveXWikiDoc(target, context); |
| 175 |
|
} |
| 176 |
|
|
| 177 |
|
|
| 178 |
|
@link |
| 179 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
| 180 |
1 |
@Test... |
| 181 |
|
public void rollbackFiresEvents() throws Exception |
| 182 |
|
{ |
| 183 |
1 |
ObservationManager observationManager = mocker.getInstance(ObservationManager.class); |
| 184 |
|
|
| 185 |
1 |
DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page"); |
| 186 |
1 |
XWikiDocument document = mock(XWikiDocument.class); |
| 187 |
1 |
when(document.getDocumentReference()).thenReturn(documentReference); |
| 188 |
|
|
| 189 |
1 |
XWikiDocument originalDocument = mock(XWikiDocument.class); |
| 190 |
|
|
| 191 |
1 |
when(originalDocument.isNew()).thenReturn(false); |
| 192 |
|
|
| 193 |
1 |
XWikiDocument result = mock(XWikiDocument.class); |
| 194 |
1 |
when(result.clone()).thenReturn(result); |
| 195 |
1 |
when(result.getDocumentReference()).thenReturn(documentReference); |
| 196 |
1 |
when(result.getOriginalDocument()).thenReturn(originalDocument); |
| 197 |
|
|
| 198 |
1 |
String revision = "3.5"; |
| 199 |
1 |
when(xwiki.getVersioningStore().loadXWikiDoc(document, revision, context)).thenReturn(result); |
| 200 |
|
|
| 201 |
1 |
this.mocker.registerMockComponent(ContextualLocalizationManager.class); |
| 202 |
|
|
| 203 |
1 |
xwiki.rollback(document, revision, context); |
| 204 |
|
|
| 205 |
1 |
verify(observationManager).notify(new DocumentRollingBackEvent(documentReference, revision), result, context); |
| 206 |
1 |
verify(observationManager).notify(new DocumentUpdatingEvent(documentReference), result, context); |
| 207 |
1 |
verify(observationManager).notify(new DocumentUpdatedEvent(documentReference), result, context); |
| 208 |
1 |
verify(observationManager).notify(new DocumentRolledBackEvent(documentReference, revision), result, context); |
| 209 |
|
} |
| 210 |
|
|
| 211 |
|
|
| 212 |
|
@see |
| 213 |
|
|
| 214 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (32) |
Complexity: 1 |
Complexity Density: 0.03 |
1PASS
|
|
| 215 |
1 |
@Test... |
| 216 |
|
public void rollbackDoesNotSaveUnchangedAttachment() throws Exception |
| 217 |
|
{ |
| 218 |
1 |
String version = "1.1"; |
| 219 |
1 |
String fileName = "logo.png"; |
| 220 |
1 |
Date date = new Date(); |
| 221 |
1 |
XWikiAttachment currentAttachment = mock(XWikiAttachment.class); |
| 222 |
1 |
when(currentAttachment.getAttachmentRevision(version, context)).thenReturn(currentAttachment); |
| 223 |
1 |
when(currentAttachment.getDate()).thenReturn(new Timestamp(date.getTime())); |
| 224 |
1 |
when(currentAttachment.getVersion()).thenReturn(version); |
| 225 |
1 |
when(currentAttachment.getFilename()).thenReturn(fileName); |
| 226 |
|
|
| 227 |
1 |
XWikiAttachment oldAttachment = mock(XWikiAttachment.class); |
| 228 |
1 |
when(oldAttachment.getFilename()).thenReturn(fileName); |
| 229 |
1 |
when(oldAttachment.getVersion()).thenReturn(version); |
| 230 |
1 |
when(oldAttachment.getDate()).thenReturn(date); |
| 231 |
|
|
| 232 |
1 |
DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page"); |
| 233 |
1 |
XWikiDocument document = mock(XWikiDocument.class); |
| 234 |
1 |
when(document.getDocumentReference()).thenReturn(documentReference); |
| 235 |
1 |
when(document.getAttachmentList()).thenReturn(Arrays.asList(currentAttachment)); |
| 236 |
1 |
when(document.getAttachment(fileName)).thenReturn(currentAttachment); |
| 237 |
|
|
| 238 |
1 |
XWikiDocument result = mock(XWikiDocument.class); |
| 239 |
1 |
when(result.clone()).thenReturn(result); |
| 240 |
1 |
when(result.getDocumentReference()).thenReturn(documentReference); |
| 241 |
1 |
when(result.getAttachmentList()).thenReturn(Arrays.asList(oldAttachment)); |
| 242 |
1 |
when(result.getAttachment(fileName)).thenReturn(oldAttachment); |
| 243 |
|
|
| 244 |
1 |
String revision = "3.5"; |
| 245 |
1 |
when(xwiki.getVersioningStore().loadXWikiDoc(document, revision, context)).thenReturn(result); |
| 246 |
|
|
| 247 |
1 |
AttachmentRecycleBinStore attachmentRecycleBinStore = mock(AttachmentRecycleBinStore.class); |
| 248 |
1 |
xwiki.setAttachmentRecycleBinStore(attachmentRecycleBinStore); |
| 249 |
|
|
| 250 |
1 |
DocumentReference reference = document.getDocumentReference(); |
| 251 |
1 |
this.mocker.registerMockComponent(ContextualLocalizationManager.class); |
| 252 |
1 |
when(xwiki.getStore().loadXWikiDoc(any(XWikiDocument.class), same(context))) |
| 253 |
|
.thenReturn(new XWikiDocument(reference)); |
| 254 |
|
|
| 255 |
1 |
xwiki.rollback(document, revision, context); |
| 256 |
|
|
| 257 |
1 |
verify(attachmentRecycleBinStore, never()).saveToRecycleBin(same(currentAttachment), any(String.class), |
| 258 |
|
any(Date.class), same(context), eq(true)); |
| 259 |
1 |
verify(oldAttachment, never()).setMetaDataDirty(true); |
| 260 |
|
} |
| 261 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
| 262 |
1 |
@Test... |
| 263 |
|
public void deleteAllDocumentsAndWithoutSendingToTrash() throws Exception |
| 264 |
|
{ |
| 265 |
1 |
XWiki xwiki = new XWiki(); |
| 266 |
|
|
| 267 |
1 |
XWikiDocument document = mock(XWikiDocument.class); |
| 268 |
1 |
DocumentReference reference = new DocumentReference("wiki", "space", "page"); |
| 269 |
1 |
when(document.getDocumentReference()).thenReturn(reference); |
| 270 |
|
|
| 271 |
|
|
| 272 |
1 |
XWikiRecycleBinStoreInterface recycleBinStoreInterface = mock(XWikiRecycleBinStoreInterface.class); |
| 273 |
1 |
xwiki.setRecycleBinStore(recycleBinStoreInterface); |
| 274 |
1 |
when(xwikiCfgConfigurationSource.getProperty("xwiki.recyclebin", "1")).thenReturn("1"); |
| 275 |
|
|
| 276 |
|
|
| 277 |
1 |
XWikiStoreInterface storeInterface = mock(XWikiStoreInterface.class); |
| 278 |
1 |
xwiki.setStore(storeInterface); |
| 279 |
1 |
XWikiContext xwikiContext = mock(XWikiContext.class); |
| 280 |
|
|
| 281 |
1 |
xwiki.deleteAllDocuments(document, false, xwikiContext); |
| 282 |
|
|
| 283 |
|
|
| 284 |
|
|
| 285 |
1 |
verify(recycleBinStoreInterface, never()).saveToRecycleBin(any(XWikiDocument.class), any(String.class), |
| 286 |
|
any(Date.class), any(XWikiContext.class), any(Boolean.class)); |
| 287 |
|
|
| 288 |
|
|
| 289 |
1 |
verify(storeInterface).deleteXWikiDoc(document, xwikiContext); |
| 290 |
|
} |
| 291 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
| 292 |
1 |
@Test... |
| 293 |
|
public void deleteDocument() throws Exception |
| 294 |
|
{ |
| 295 |
1 |
final DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page"); |
| 296 |
1 |
XWikiDocument document = mock(XWikiDocument.class); |
| 297 |
1 |
when(document.getDocumentReference()).thenReturn(documentReference); |
| 298 |
|
|
| 299 |
1 |
final XWikiDocument originalDocument = mock(XWikiDocument.class); |
| 300 |
1 |
when(document.getOriginalDocument()).thenReturn(originalDocument); |
| 301 |
|
|
| 302 |
1 |
this.xwiki.deleteDocument(document, this.context); |
| 303 |
|
|
| 304 |
1 |
ObservationManager observation = this.mocker.getInstance(ObservationManager.class); |
| 305 |
|
|
| 306 |
1 |
ArgumentMatcher<XWikiDocument> matcher = new ArgumentMatcher<XWikiDocument>() |
| 307 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 308 |
4 |
@Override... |
| 309 |
|
public boolean matches(XWikiDocument argument) |
| 310 |
|
{ |
| 311 |
4 |
return argument.getDocumentReference().equals(documentReference) |
| 312 |
|
&& argument.getOriginalDocument() == originalDocument; |
| 313 |
|
} |
| 314 |
|
}; |
| 315 |
|
|
| 316 |
|
|
| 317 |
1 |
verify(observation).notify(eq(new DocumentDeletingEvent(documentReference)), argThat(matcher), |
| 318 |
|
same(this.context)); |
| 319 |
1 |
verify(observation).notify(eq(new DocumentDeletedEvent(documentReference)), argThat(matcher), |
| 320 |
|
same(this.context)); |
| 321 |
|
|
| 322 |
1 |
verifyNoMoreInteractions(observation); |
| 323 |
|
} |
| 324 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
| 325 |
1 |
@Test... |
| 326 |
|
public void getPlainUserName() throws XWikiException |
| 327 |
|
{ |
| 328 |
1 |
XWikiDocument document = mock(XWikiDocument.class); |
| 329 |
1 |
DocumentReference userReference = new DocumentReference("wiki", "XWiki", "user"); |
| 330 |
1 |
when(document.getDocumentReference()).thenReturn(userReference); |
| 331 |
1 |
when(this.storeMock.loadXWikiDoc(any(XWikiDocument.class), any(XWikiContext.class))).thenReturn(document); |
| 332 |
1 |
BaseObject userObject = mock(BaseObject.class); |
| 333 |
1 |
when(document.getObject("XWiki.XWikiUsers")).thenReturn(userObject); |
| 334 |
|
|
| 335 |
1 |
when(userObject.getStringValue("first_name")).thenReturn("first<name"); |
| 336 |
1 |
when(userObject.getStringValue("last_name")).thenReturn("last'name"); |
| 337 |
1 |
assertEquals("first<name last'name", xwiki.getPlainUserName(userReference, context)); |
| 338 |
|
|
| 339 |
1 |
when(userObject.getStringValue("first_name")).thenReturn("first<name"); |
| 340 |
1 |
when(userObject.getStringValue("last_name")).thenReturn(""); |
| 341 |
1 |
assertEquals("first<name", xwiki.getPlainUserName(userReference, context)); |
| 342 |
|
|
| 343 |
1 |
when(userObject.getStringValue("first_name")).thenReturn(""); |
| 344 |
1 |
when(userObject.getStringValue("last_name")).thenReturn("last'name"); |
| 345 |
1 |
assertEquals("last'name", xwiki.getPlainUserName(userReference, context)); |
| 346 |
|
} |
| 347 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
| 348 |
1 |
@Test... |
| 349 |
|
public void getURLWithDotsAndBackslashInSpaceName() throws Exception |
| 350 |
|
{ |
| 351 |
1 |
XWikiURLFactory urlFactory = mock(XWikiURLFactory.class); |
| 352 |
1 |
context.setURLFactory(urlFactory); |
| 353 |
|
|
| 354 |
1 |
DocumentReference reference = new DocumentReference("wiki", Arrays.asList("space.withdot.and\\and:"), "page"); |
| 355 |
|
|
| 356 |
1 |
EntityReferenceSerializer<String> serializer = |
| 357 |
|
this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "local"); |
| 358 |
1 |
when(serializer.serialize(reference.getLastSpaceReference())).thenReturn("somescapedspace"); |
| 359 |
|
|
| 360 |
1 |
this.xwiki.getURL(reference, "view", null, null, context); |
| 361 |
|
|
| 362 |
1 |
verify(urlFactory).createURL("somescapedspace", "page", "view", null, null, "wiki", context); |
| 363 |
|
} |
| 364 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
| 365 |
1 |
@Test... |
| 366 |
|
public void getURLWithLocale() throws Exception |
| 367 |
|
{ |
| 368 |
1 |
XWikiURLFactory urlFactory = mock(XWikiURLFactory.class); |
| 369 |
1 |
context.setURLFactory(urlFactory); |
| 370 |
|
|
| 371 |
1 |
DocumentReference reference = new DocumentReference("wiki", "Space", "Page", Locale.FRENCH); |
| 372 |
|
|
| 373 |
1 |
EntityReferenceSerializer<String> serializer = |
| 374 |
|
this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "local"); |
| 375 |
1 |
when(serializer.serialize(reference.getLastSpaceReference())).thenReturn("Space"); |
| 376 |
|
|
| 377 |
1 |
this.xwiki.getURL(reference, "view", null, null, context); |
| 378 |
1 |
verify(urlFactory).createURL("Space", "Page", "view", "language=fr", null, "wiki", context); |
| 379 |
|
|
| 380 |
1 |
this.xwiki.getURL(reference, "view", "language=ro", null, context); |
| 381 |
1 |
verify(urlFactory).createURL("Space", "Page", "view", "language=ro&language=fr", null, "wiki", context); |
| 382 |
|
} |
| 383 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
| 384 |
1 |
@Test... |
| 385 |
|
public void getEntityURLWithDefaultAction() throws Exception |
| 386 |
|
{ |
| 387 |
1 |
DocumentReference documentReference = new DocumentReference("tennis", Arrays.asList("Path", "To"), "Success"); |
| 388 |
1 |
AttachmentReference attachmentReference = new AttachmentReference("image.png", documentReference); |
| 389 |
|
|
| 390 |
1 |
XWikiURLFactory urlFactory = mock(XWikiURLFactory.class); |
| 391 |
1 |
context.setURLFactory(urlFactory); |
| 392 |
|
|
| 393 |
1 |
EntityReferenceSerializer<String> localSerializer = |
| 394 |
|
this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "local"); |
| 395 |
1 |
when(localSerializer.serialize(documentReference.getLastSpaceReference())).thenReturn("Path.To"); |
| 396 |
|
|
| 397 |
|
|
| 398 |
1 |
DocumentReferenceResolver<EntityReference> documentResolver = |
| 399 |
|
this.mocker.registerMockComponent(DocumentReferenceResolver.TYPE_REFERENCE, "currentgetdocument"); |
| 400 |
1 |
when(documentResolver.resolve(documentReference)).thenReturn(documentReference); |
| 401 |
|
|
| 402 |
1 |
this.xwiki.getURL(documentReference, this.context); |
| 403 |
1 |
verify(urlFactory).createURL("Path.To", "Success", "view", null, null, "tennis", this.context); |
| 404 |
|
|
| 405 |
|
|
| 406 |
1 |
AttachmentReferenceResolver<EntityReference> attachmentResolver = |
| 407 |
|
this.mocker.registerMockComponent(AttachmentReferenceResolver.TYPE_REFERENCE, "current"); |
| 408 |
1 |
when(attachmentResolver.resolve(attachmentReference)).thenReturn(attachmentReference); |
| 409 |
|
|
| 410 |
1 |
this.xwiki.getURL(attachmentReference, this.context); |
| 411 |
1 |
verify(urlFactory).createAttachmentURL("image.png", "Path.To", "Success", "download", null, "tennis", |
| 412 |
|
this.context); |
| 413 |
|
} |
| 414 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (47) |
Complexity: 1 |
Complexity Density: 0.02 |
1PASS
|
|
| 415 |
1 |
@Test... |
| 416 |
|
public void getSpacePreference() throws Exception |
| 417 |
|
{ |
| 418 |
1 |
this.mocker.registerMockComponent(ConfigurationSource.class, "wiki"); |
| 419 |
1 |
ConfigurationSource spaceConfiguration = this.mocker.registerMockComponent(ConfigurationSource.class, "space"); |
| 420 |
|
|
| 421 |
1 |
when(this.xwikiCfgConfigurationSource.getProperty(any(), anyString())).then(new Answer<String>() |
| 422 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 423 |
14 |
@Override... |
| 424 |
|
public String answer(InvocationOnMock invocation) throws Throwable |
| 425 |
|
{ |
| 426 |
14 |
return invocation.getArgument(1); |
| 427 |
|
} |
| 428 |
|
}); |
| 429 |
|
|
| 430 |
1 |
WikiReference wikiReference = new WikiReference("wiki"); |
| 431 |
1 |
SpaceReference space1Reference = new SpaceReference("space1", wikiReference); |
| 432 |
1 |
SpaceReference space2Reference = new SpaceReference("space2", space1Reference); |
| 433 |
|
|
| 434 |
|
|
| 435 |
|
|
| 436 |
1 |
assertEquals("", this.xwiki.getSpacePreference("pref", this.context)); |
| 437 |
1 |
assertEquals("defaultvalue", this.xwiki.getSpacePreference("pref", "defaultvalue", this.context)); |
| 438 |
1 |
assertEquals("", this.xwiki.getSpacePreference("pref", space2Reference, this.context)); |
| 439 |
1 |
assertEquals("defaultvalue", |
| 440 |
|
this.xwiki.getSpacePreference("pref", space2Reference, "defaultvalue", this.context)); |
| 441 |
|
|
| 442 |
|
|
| 443 |
|
|
| 444 |
1 |
this.context.setDoc(new XWikiDocument(new DocumentReference("document", space2Reference))); |
| 445 |
|
|
| 446 |
1 |
assertEquals("", this.xwiki.getSpacePreference("pref", this.context)); |
| 447 |
1 |
assertEquals("defaultvalue", this.xwiki.getSpacePreference("pref", "defaultvalue", this.context)); |
| 448 |
1 |
assertEquals("", this.xwiki.getSpacePreference("pref", space2Reference, this.context)); |
| 449 |
1 |
assertEquals("defaultvalue", |
| 450 |
|
this.xwiki.getSpacePreference("pref", space2Reference, "defaultvalue", this.context)); |
| 451 |
|
|
| 452 |
|
|
| 453 |
|
|
| 454 |
1 |
final Map<String, Map<String, String>> spacesPreferences = new HashMap<>(); |
| 455 |
1 |
Map<String, String> space1Preferences = new HashMap<>(); |
| 456 |
1 |
space1Preferences.put("pref", "prefvalue1"); |
| 457 |
1 |
space1Preferences.put("pref1", "pref1value1"); |
| 458 |
1 |
Map<String, String> space2Preferences = new HashMap<>(); |
| 459 |
1 |
space2Preferences.put("pref", "prefvalue2"); |
| 460 |
1 |
space2Preferences.put("pref2", "pref2value2"); |
| 461 |
1 |
spacesPreferences.put(space1Reference.getName(), space1Preferences); |
| 462 |
1 |
spacesPreferences.put(space2Reference.getName(), space2Preferences); |
| 463 |
|
|
| 464 |
1 |
when(spaceConfiguration.getProperty(any(), same(String.class))).then(new Answer<String>() |
| 465 |
|
{ |
| |
|
| 66.7% |
Uncovered Elements: 3 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
| 466 |
24 |
@Override... |
| 467 |
|
public String answer(InvocationOnMock invocation) throws Throwable |
| 468 |
|
{ |
| 469 |
24 |
if (context.getDoc() != null) { |
| 470 |
24 |
Map<String, String> spacePreferences = |
| 471 |
|
spacesPreferences.get(context.getDoc().getDocumentReference().getParent().getName()); |
| 472 |
24 |
if (spacePreferences != null) { |
| 473 |
24 |
return spacePreferences.get(invocation.getArgument(0)); |
| 474 |
|
} |
| 475 |
|
} |
| 476 |
|
|
| 477 |
0 |
return null; |
| 478 |
|
} |
| 479 |
|
}); |
| 480 |
|
|
| 481 |
1 |
this.context.setDoc(new XWikiDocument(new DocumentReference("document", space1Reference))); |
| 482 |
1 |
assertEquals("prefvalue1", this.xwiki.getSpacePreference("pref", this.context)); |
| 483 |
1 |
assertEquals("prefvalue1", this.xwiki.getSpacePreference("pref", "defaultvalue", this.context)); |
| 484 |
1 |
assertEquals("pref1value1", this.xwiki.getSpacePreference("pref1", this.context)); |
| 485 |
1 |
assertEquals("", this.xwiki.getSpacePreference("pref2", this.context)); |
| 486 |
|
|
| 487 |
1 |
this.context.setDoc(new XWikiDocument(new DocumentReference("document", space2Reference))); |
| 488 |
1 |
assertEquals("prefvalue2", this.xwiki.getSpacePreference("pref", this.context)); |
| 489 |
1 |
assertEquals("prefvalue2", this.xwiki.getSpacePreference("pref", "defaultvalue", this.context)); |
| 490 |
1 |
assertEquals("pref1value1", this.xwiki.getSpacePreference("pref1", this.context)); |
| 491 |
1 |
assertEquals("pref2value2", this.xwiki.getSpacePreference("pref2", this.context)); |
| 492 |
|
|
| 493 |
1 |
assertEquals("", this.xwiki.getSpacePreference("nopref", space1Reference, this.context)); |
| 494 |
1 |
assertEquals("defaultvalue", |
| 495 |
|
this.xwiki.getSpacePreference("nopref", space1Reference, "defaultvalue", this.context)); |
| 496 |
1 |
assertEquals("prefvalue1", this.xwiki.getSpacePreference("pref", space1Reference, this.context)); |
| 497 |
1 |
assertEquals("prefvalue1", |
| 498 |
|
this.xwiki.getSpacePreference("pref", space1Reference, "defaultvalue", this.context)); |
| 499 |
1 |
assertEquals("pref1value1", this.xwiki.getSpacePreference("pref1", space1Reference, this.context)); |
| 500 |
1 |
assertEquals("", this.xwiki.getSpacePreference("pref2", space1Reference, this.context)); |
| 501 |
|
|
| 502 |
1 |
assertEquals("", this.xwiki.getSpacePreference("nopref", space2Reference, this.context)); |
| 503 |
1 |
assertEquals("defaultvalue", |
| 504 |
|
this.xwiki.getSpacePreference("nopref", space2Reference, "defaultvalue", this.context)); |
| 505 |
1 |
assertEquals("prefvalue2", this.xwiki.getSpacePreference("pref", space2Reference, this.context)); |
| 506 |
1 |
assertEquals("prefvalue2", |
| 507 |
|
this.xwiki.getSpacePreference("pref", space2Reference, "defaultvalue", this.context)); |
| 508 |
1 |
assertEquals("pref1value1", this.xwiki.getSpacePreference("pref1", space2Reference, this.context)); |
| 509 |
1 |
assertEquals("pref2value2", this.xwiki.getSpacePreference("pref2", space2Reference, this.context)); |
| 510 |
|
} |
| 511 |
|
} |