### Question:
UpdateObjectsOnClassRenameListener extends AbstractEventListener { @Override public void onEvent(Event event, Object source, Object data) { if (event instanceof DocumentRenamedEvent) { boolean updateLinks = true; if (data instanceof MoveRequest) { updateLinks = ((MoveRequest) data).isUpdateLinks(); } if (updateLinks) { DocumentRenamedEvent documentRenamedEvent = (DocumentRenamedEvent) event; updateObjects(documentRenamedEvent.getSourceReference(), documentRenamedEvent.getTargetReference()); } } } UpdateObjectsOnClassRenameListener(); @Override void onEvent(Event event, Object source, Object data); static final String NAME; }### Answer:
@Test public void onClassRenamed() throws Exception { Query query = mock(Query.class); when(this.queryManager.createQuery(any(), any())).thenReturn(query); when(query.execute()).thenReturn(Collections.singletonList("Some.Page")); DocumentReference documentReference = new DocumentReference("foo", "Some", "Page"); when(this.documentReferenceResolver.resolve("Some.Page", oldClassReference)).thenReturn(documentReference); XWikiDocument document = mock(XWikiDocument.class); when(this.wiki.getDocument(documentReference, this.xcontext)).thenReturn(document); BaseObject oldObject = mock(BaseObject.class, "old"); when(document.getXObjects(oldClassReference)).thenReturn(Collections.singletonList(oldObject)); BaseObject newObject = mock(BaseObject.class, "new"); when(document.newXObject(newClassReference, this.xcontext)).thenReturn(newObject); @SuppressWarnings("unchecked") BaseProperty<EntityReference> oldProperty = mock(BaseProperty.class); when(oldProperty.getName()).thenReturn("age"); when(oldObject.getProperties()).thenReturn(new Object[] {oldProperty}); @SuppressWarnings("unchecked") BaseProperty<EntityReference> newProperty = mock(BaseProperty.class); when(oldProperty.clone()).thenReturn(newProperty); this.listener.onEvent(new DocumentRenamedEvent(this.oldClassReference, this.newClassReference), null, null); verify(query).setWiki("foo"); verify(query).bindValue("className", "Code.OldClass"); verify(newObject).safeput("age", newProperty); verify(document).removeXObject(oldObject); verify(this.wiki).saveDocument(document, "Rename [foo:Code.OldClass] objects into [foo:Code.NewClass]", this.xcontext); assertEquals("Updating the xobjects of type [foo:Code.OldClass] after the xclass has been renamed to " + "[foo:Code.NewClass].", logCapture.getMessage(0)); }
@Test public void onClassMovedToDifferentWiki() throws Exception { this.listener.onEvent( new DocumentRenamedEvent(this.oldClassReference, new DocumentReference("bar", "Code", "NewClass")), null, null); verify(this.queryManager, never()).createQuery(any(), any()); verify(this.wiki, never()).saveDocument(any(), any()); }
@Test public void onClassRenamedWithoutUpdateLinks() throws Exception { MoveRequest moveRequest = new MoveRequest(); moveRequest.setUpdateLinks(false); this.listener.onEvent(new DocumentRenamedEvent(this.oldClassReference, this.newClassReference), null, moveRequest); verify(this.queryManager, never()).createQuery(any(), any()); verify(this.wiki, never()).saveDocument(any(), any()); }
@Test public void onOtherEvents() throws Exception { this.listener.onEvent(null, null, null); verify(this.queryManager, never()).createQuery(any(), any()); verify(this.wiki, never()).saveDocument(any(), any()); }