### Question:
AutomaticRedirectCreatorListener extends AbstractEventListener { @Override public void onEvent(Event event, Object source, Object data) { if (event instanceof DocumentRenamedEvent) { boolean autoRedirect = true; if (data instanceof MoveRequest) { autoRedirect = ((MoveRequest) data).isAutoRedirect(); } if (autoRedirect) { DocumentRenamedEvent documentRenamedEvent = (DocumentRenamedEvent) event; this.logger.info("Creating automatic redirect from [{}] to [{}].", documentRenamedEvent.getSourceReference(), documentRenamedEvent.getTargetReference()); this.modelBridge.createRedirect(documentRenamedEvent.getSourceReference(), documentRenamedEvent.getTargetReference()); } } } AutomaticRedirectCreatorListener(); @Override void onEvent(Event event, Object source, Object data); static final String NAME; }### Answer:
@Test public void onDocumentRenamedWithAutomaticRedirect() { renameRequest.setAutoRedirect(true); this.listener.onEvent(documentRenamedEvent, null, renameRequest); verify(this.modelBridge).createRedirect(oldReference, newReference); assertEquals("Creating automatic redirect from [wiki:Users.Alice] to [wiki:Users.Bob].", logCapture.getMessage(0)); }
@Test public void onDocumentRenamedWithoutAutomaticRedirect() { renameRequest.setAutoRedirect(false); this.listener.onEvent(documentRenamedEvent, null, renameRequest); verify(this.modelBridge, never()).createRedirect(any(), any()); }
@Test public void onDocumentRenamedWithoutRenameRequest() { this.listener.onEvent(documentRenamedEvent, null, null); verify(this.modelBridge).createRedirect(oldReference, newReference); assertEquals("Creating automatic redirect from [wiki:Users.Alice] to [wiki:Users.Bob].", logCapture.getMessage(0)); }
@Test public void onOtherEvents() { this.listener.onEvent(null, null, null); verify(this.modelBridge, never()).createRedirect(any(), any()); }