|
31 | 31 | import org.xwiki.store.merge.MergeManagerResult; |
32 | 32 | import org.xwiki.test.junit5.mockito.MockComponent; |
33 | 33 |
|
| 34 | +import com.xpn.xwiki.XWikiContext; |
| 35 | +import com.xpn.xwiki.XWikiException; |
34 | 36 | import com.xpn.xwiki.doc.XWikiDocument; |
35 | 37 | import com.xpn.xwiki.doc.merge.MergeConfiguration; |
36 | 38 | import com.xpn.xwiki.doc.merge.MergeResult; |
| 39 | +import com.xpn.xwiki.objects.classes.BaseClass; |
| 40 | +import com.xpn.xwiki.objects.classes.PropertyClass; |
37 | 41 | import com.xpn.xwiki.test.MockitoOldcore; |
38 | 42 | import com.xpn.xwiki.test.junit5.mockito.InjectMockitoOldcore; |
39 | 43 | import com.xpn.xwiki.test.junit5.mockito.OldcoreTest; |
40 | 44 | import com.xpn.xwiki.test.reference.ReferenceComponentList; |
41 | 45 |
|
42 | 46 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 47 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
43 | 48 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
44 | 49 | import static org.junit.jupiter.api.Assertions.assertNotSame; |
| 50 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
45 | 51 | import static org.junit.jupiter.api.Assertions.fail; |
46 | 52 | import static org.mockito.ArgumentMatchers.any; |
47 | 53 | import static org.mockito.ArgumentMatchers.eq; |
| 54 | +import static org.mockito.Mockito.mock; |
| 55 | +import static org.mockito.Mockito.times; |
| 56 | +import static org.mockito.Mockito.verify; |
48 | 57 | import static org.mockito.Mockito.when; |
49 | 58 |
|
50 | 59 | /** |
@@ -224,4 +233,106 @@ void cloneWithoutDetach() |
224 | 233 | assertNotSame(object.getOwnerDocument(), clonedObject.getOwnerDocument()); |
225 | 234 | assertNotNull(clonedObject.getOwnerDocument()); |
226 | 235 | } |
| 236 | + |
| 237 | + @Test |
| 238 | + void setString() throws XWikiException |
| 239 | + { |
| 240 | + DocumentReference documentReference = new DocumentReference("wiki", "space", "document"); |
| 241 | + DocumentReference classReference = new DocumentReference("wiki", "space", "class"); |
| 242 | + XWikiDocument classDocument = new XWikiDocument(classReference); |
| 243 | + XWikiDocument ownerDocument = new XWikiDocument(new DocumentReference("wiki", "space", "page")); |
| 244 | + BaseObject object = new BaseObject(); |
| 245 | + object.setDocumentReference(documentReference); |
| 246 | + object.setXClassReference(classReference); |
| 247 | + object.setOwnerDocument(ownerDocument); |
| 248 | + |
| 249 | + XWikiContext context = this.oldcore.getXWikiContext();; |
| 250 | + String fieldName = "myField"; |
| 251 | + String value = "myValue"; |
| 252 | + |
| 253 | + BaseClass baseClass = mock(BaseClass.class); |
| 254 | + classDocument.setXClass(baseClass); |
| 255 | + when(this.oldcore.getSpyXWiki().getDocument(classReference, context)).thenReturn(classDocument); |
| 256 | + |
| 257 | + PropertyClass propertyClass = mock(PropertyClass.class); |
| 258 | + when(baseClass.get(fieldName)).thenReturn(propertyClass); |
| 259 | + |
| 260 | + BaseProperty newProp = mock(BaseProperty.class); |
| 261 | + when(propertyClass.fromString(value)).thenReturn(newProp); |
| 262 | + |
| 263 | + object.set(fieldName, value, context); |
| 264 | + assertTrue(object.isDirty()); |
| 265 | + assertTrue(ownerDocument.isMetaDataDirty()); |
| 266 | + verify(newProp, times(2)).setOwnerDocument(ownerDocument); |
| 267 | + verify(newProp).setName(fieldName); |
| 268 | + verify(newProp).setObject(object); |
| 269 | + assertTrue(object.getFieldList().contains(newProp)); |
| 270 | + |
| 271 | + ownerDocument.setMetaDataDirty(false); |
| 272 | + object.setDirty(false); |
| 273 | + |
| 274 | + // Now the property exists let's call it again |
| 275 | + BaseProperty newProp2 = mock(BaseProperty.class); |
| 276 | + when(propertyClass.fromString(value)).thenReturn(newProp2); |
| 277 | + when(newProp2.getValue()).thenReturn(value); |
| 278 | + object.set(fieldName, value, context); |
| 279 | + verify(newProp).setValue(value); |
| 280 | + assertFalse(object.isDirty()); |
| 281 | + assertFalse(ownerDocument.isMetaDataDirty()); |
| 282 | + |
| 283 | + // no new calls |
| 284 | + verify(newProp, times(2)).setOwnerDocument(ownerDocument); |
| 285 | + verify(newProp).setName(fieldName); |
| 286 | + verify(newProp).setObject(object); |
| 287 | + } |
| 288 | + |
| 289 | + @Test |
| 290 | + void setObject() throws XWikiException |
| 291 | + { |
| 292 | + DocumentReference documentReference = new DocumentReference("wiki", "space", "document"); |
| 293 | + DocumentReference classReference = new DocumentReference("wiki", "space", "class"); |
| 294 | + XWikiDocument classDocument = new XWikiDocument(classReference); |
| 295 | + XWikiDocument ownerDocument = new XWikiDocument(new DocumentReference("wiki", "space", "page")); |
| 296 | + BaseObject object = new BaseObject(); |
| 297 | + object.setDocumentReference(documentReference); |
| 298 | + object.setXClassReference(classReference); |
| 299 | + object.setOwnerDocument(ownerDocument); |
| 300 | + |
| 301 | + XWikiContext context = this.oldcore.getXWikiContext();; |
| 302 | + String fieldName = "myField"; |
| 303 | + Object value = 4545; |
| 304 | + |
| 305 | + BaseClass baseClass = mock(BaseClass.class); |
| 306 | + classDocument.setXClass(baseClass); |
| 307 | + when(this.oldcore.getSpyXWiki().getDocument(classReference, context)).thenReturn(classDocument); |
| 308 | + |
| 309 | + PropertyClass propertyClass = mock(PropertyClass.class); |
| 310 | + when(baseClass.get(fieldName)).thenReturn(propertyClass); |
| 311 | + |
| 312 | + BaseProperty newProp = mock(BaseProperty.class); |
| 313 | + when(propertyClass.newProperty()).thenReturn(newProp); |
| 314 | + |
| 315 | + object.set(fieldName, value, context); |
| 316 | + assertTrue(object.isDirty()); |
| 317 | + assertTrue(ownerDocument.isMetaDataDirty()); |
| 318 | + verify(newProp).setValue(value); |
| 319 | + verify(newProp, times(2)).setOwnerDocument(ownerDocument); |
| 320 | + verify(newProp).setName(fieldName); |
| 321 | + verify(newProp).setObject(object); |
| 322 | + assertTrue(object.getFieldList().contains(newProp)); |
| 323 | + |
| 324 | + ownerDocument.setMetaDataDirty(false); |
| 325 | + object.setDirty(false); |
| 326 | + |
| 327 | + // Now the property exists let's call it again |
| 328 | + object.set(fieldName, value, context); |
| 329 | + verify(newProp, times(2)).setValue(value); |
| 330 | + assertFalse(object.isDirty()); |
| 331 | + assertFalse(ownerDocument.isMetaDataDirty()); |
| 332 | + |
| 333 | + // no new calls |
| 334 | + verify(newProp, times(2)).setOwnerDocument(ownerDocument); |
| 335 | + verify(newProp).setName(fieldName); |
| 336 | + verify(newProp).setObject(object); |
| 337 | + } |
227 | 338 | } |
0 commit comments