void updateLockInfo(final @Observes LockInfo lockInfo) { /* Comparing URIs since lockInfo.getFile() can be an ObservablePath or a PathImpl. */ if (getLockTarget() != null && lockInfo.getFile().toURI().equals(lockTarget.getPath().toURI())) { this.lockInfo = lockInfo; this.lockSyncComplete = true; fireChangeTitleEvent(); fireUpdatedLockStatusEvent(); for (Runnable runnable : getSyncCompleteRunnables()) { runnable.run(); } getSyncCompleteRunnables().clear(); } }
@Override public void onBrowserEvent(Event event) { if (isLockedByCurrentUser()) { return; } if (lockDemandDetector.isLockRequired(event)) { acquireLock(); } } };
@Override public void onAttachOrDetach(AttachEvent event) { // Handle widget reattachment/reparenting if (event.isAttached()) { acquireLockOnDemand(element); } } });
@Override public void onFocus() { publishJsApi(); fireChangeTitleEvent(); fireUpdatedLockStatusEvent(); }
void onLockRequired(@Observes LockRequiredEvent event) { if (lockTarget != null && isVisible() && !isLockedByCurrentUser()) { acquireLock(); } }
@Test public void testUpdateLockInfoWhenLockTargetIsNull() { final LockInfo lockInfo = mock(LockInfo.class); final Runnable runnable1 = mock(Runnable.class); final Runnable runnable2 = mock(Runnable.class); final List<Runnable> runnables = spy(new ArrayList<>(Arrays.asList(runnable1, runnable2))); doReturn(runnables).when(lockManager).getSyncCompleteRunnables(); doReturn(null).when(lockManager).getLockTarget(); lockManager.updateLockInfo(lockInfo); assertNotEquals(lockInfo, lockManager.getLockInfo()); assertFalse(lockManager.isLockSyncComplete()); verify(lockManager, never()).fireChangeTitleEvent(); verify(lockManager, never()).fireUpdatedLockStatusEvent(); verify(runnable1, never()).run(); verify(runnable2, never()).run(); verify(runnables, never()).clear(); }
@Test public void testOnRenameInProgressWhenLockTargetIsNull() { final RenameInProgressEvent renameInProgressEvent = mock(RenameInProgressEvent.class); doReturn(null).when(lockManager).getLockTarget(); lockManager.onRenameInProgress(renameInProgressEvent); verify(lockManager, never()).releaseLock(); }
@Test public void testOnRenameInProgressWhenLockInfoPathIsEqualToLockTargetPath() { final RenameInProgressEvent renameInProgressEvent = mock(RenameInProgressEvent.class); when(renameInProgressEvent.getPath()).thenReturn(path); lockManager.onRenameInProgress(renameInProgressEvent); verify(lockManager).releaseLock(); }
@Test public void releaseOwnedLockOnly() { lockManager.acquireLockOnDemand(); simulateLockFailure(); simulateLockDemand(); lockManager.onResourceUpdated(new ResourceUpdatedEvent(path, "", new SessionInfoImpl(user))); verify(lockService, never()).releaseLock(any(Path.class), any(ParameterizedCommand.class)); }
@Override public void acquireLock() { if (lockTarget == null) { return; } if (isLockedByCurrentUser()) { fireChangeTitleEvent(); return; } if (lockInfo.isLocked()) { handleLockFailure(lockInfo); } else if (!lockRequestPending) { lockRequestPending = true; final ParameterizedCommand<LockResult> command = new ParameterizedCommand<LockResult>() { @Override public void execute(final LockResult result) { if (result.isSuccess()) { updateLockInfo(result.getLockInfo()); releaseLockOnClose(); } else { handleLockFailure(result.getLockInfo()); } lockRequestPending = false; } }; lockService.acquireLock(lockTarget.getPath(), command); } }
void onRenameInProgress(@Observes RenameInProgressEvent event) { if (getLockTarget() != null && event.getPath().equals(lockTarget.getPath())) { releaseLock(); } }
@Test public void releaseLockOnSave() { lockManager.acquireLockOnDemand(); simulateLockSuccess(); simulateLockDemand(); lockManager.onSaveInProgress(new SaveInProgressEvent(path)); verify(lockService, times(1)).releaseLock(any(Path.class), any(ParameterizedCommand.class)); }
@Override public void fireChangeTitleEvent() { final Path path = getLockInfo().getFile(); if (path == null) { return; } presenter.getActiveDecisionTable().ifPresent(dtPresenter -> { final ObservablePath dtPath = dtPresenter.getCurrentPath(); if (dtPath == null) { return; } if (path.equals(dtPath)) { super.fireChangeTitleEvent(); } }); } }
@Override public void execute(final LockInfo lockInfo) { if (!lockRequestPending && !unlockRequestPending) { updateLockInfo(lockInfo); } } };
@Override public void acquireLockOnDemand() { if (lockTarget == null) { return; } final Widget widget = getLockTargetWidget(); final Element element = widget.getElement(); acquireLockOnDemand(element); widget.addAttachHandler(new AttachEvent.Handler() { @Override public void onAttachOrDetach(AttachEvent event) { // Handle widget reattachment/reparenting if (event.isAttached()) { acquireLockOnDemand(element); } } }); }
void onResourceUpdated(@Observes ResourceUpdatedEvent res) { if (lockTarget != null && res.getPath().equals(lockTarget.getPath())) { if (!res.getSessionInfo().getIdentity().equals(user)) { reload(); } releaseLock(); } }
@Override public void onWindowClosing(ClosingEvent event) { releaseLock(); } });
@Test public void testUpdateLockInfoWhenLockTargetIsNull() { final LockInfo lockInfo = mock(LockInfo.class); final Runnable runnable1 = mock(Runnable.class); final Runnable runnable2 = mock(Runnable.class); final List<Runnable> runnables = spy(new ArrayList<>(Arrays.asList(runnable1, runnable2))); doReturn(runnables).when(lockManager).getSyncCompleteRunnables(); doReturn(null).when(lockManager).getLockTarget(); lockManager.updateLockInfo(lockInfo); assertNotEquals(lockInfo, lockManager.getLockInfo()); assertFalse(lockManager.isLockSyncComplete()); verify(lockManager, never()).fireChangeTitleEvent(); verify(lockManager, never()).fireUpdatedLockStatusEvent(); verify(runnable1, never()).run(); verify(runnable2, never()).run(); verify(runnables, never()).clear(); }
@Test public void testOnRenameInProgressWhenLockTargetIsNull() { final RenameInProgressEvent renameInProgressEvent = mock(RenameInProgressEvent.class); doReturn(null).when(lockManager).getLockTarget(); lockManager.onRenameInProgress(renameInProgressEvent); verify(lockManager, never()).releaseLock(); }
@Test public void testOnRenameInProgressWhenLockInfoPathIsEqualToLockTargetPath() { final RenameInProgressEvent renameInProgressEvent = mock(RenameInProgressEvent.class); when(renameInProgressEvent.getPath()).thenReturn(path); lockManager.onRenameInProgress(renameInProgressEvent); verify(lockManager).releaseLock(); }