Search in sources :

Example 1 with LockStore

use of org.alfresco.repo.lock.mem.LockStore in project alfresco-repository by Alfresco.

the class LockServiceImplTest method testEphemeralLock.

public void testEphemeralLock() {
    TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
    // Check that the node is not currently locked
    assertEquals(LockStatus.NO_LOCK, lockService.getLockStatus(noAspectNode));
    assertFalse(lockService.isLocked(noAspectNode));
    // Check that there really is no lockable aspect
    assertEquals(false, nodeService.hasAspect(noAspectNode, ContentModel.ASPECT_LOCKABLE));
    // Lock the node
    lockService.lock(noAspectNode, LockType.WRITE_LOCK, 86400, Lifetime.EPHEMERAL, "some extra data");
    // Check additionalInfo has been stored
    assertEquals("some extra data", lockService.getAdditionalInfo(noAspectNode));
    // Check that we can retrieve LockState
    LockState lockState = lockService.getLockState(noAspectNode);
    assertEquals(noAspectNode, lockState.getNodeRef());
    assertEquals(LockType.WRITE_LOCK, lockState.getLockType());
    assertEquals(GOOD_USER_NAME, lockState.getOwner());
    assertEquals(Lifetime.EPHEMERAL, lockState.getLifetime());
    assertNotNull(lockState.getExpires());
    assertEquals("some extra data", lockState.getAdditionalInfo());
    // The node should be locked
    assertEquals(LockStatus.LOCK_OWNER, lockService.getLockStatus(noAspectNode));
    assertTrue(lockService.isLocked(noAspectNode));
    // The node must still not have the lockable aspect applied
    assertEquals(false, nodeService.hasAspect(noAspectNode, ContentModel.ASPECT_LOCKABLE));
    // ...though the full node service should report that it is present
    NodeService fullNodeService = (NodeService) applicationContext.getBean("nodeService");
    assertEquals(true, fullNodeService.hasAspect(noAspectNode, ContentModel.ASPECT_LOCKABLE));
    TestWithUserUtils.authenticateUser(BAD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
    assertEquals(LockStatus.LOCKED, lockService.getLockStatus(noAspectNode));
    assertTrue(lockService.isLocked(noAspectNode));
    // Test lock when already locked
    try {
        lockService.lock(noAspectNode, LockType.WRITE_LOCK);
        fail("The user should not be able to lock the node since it is already locked by another user.");
    } catch (UnableToAquireLockException exception) {
        System.out.println(exception.getMessage());
    }
    TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
    assertEquals(LockStatus.LOCK_OWNER, lockService.getLockStatus(noAspectNode));
    assertTrue(lockService.isLocked(noAspectNode));
    // Test already locked by this user - relock
    try {
        lockService.lock(noAspectNode, LockType.WRITE_LOCK, 0, Lifetime.EPHEMERAL);
    } catch (Exception exception) {
        fail("No error should be thrown when a node is re-locked by the current lock owner.");
    }
    // The node should be locked
    assertEquals(LockStatus.LOCK_OWNER, lockService.getLockStatus(noAspectNode));
    assertTrue(lockService.isLocked(noAspectNode));
    // If we remove the lock info directly from the memory store then the node should no longer
    // be reported as locked (as it is an ephemeral lock)
    LockStore lockStore = (LockStore) applicationContext.getBean("lockStore");
    lockStore.clear();
    // The node must no longer be reported as locked
    assertEquals(LockStatus.NO_LOCK, lockService.getLockStatus(noAspectNode));
    assertFalse(lockService.isLocked(noAspectNode));
    // Lock again, ready to test unlocking an ephemeral lock.
    try {
        lockService.lock(noAspectNode, LockType.WRITE_LOCK, 0, Lifetime.EPHEMERAL);
    } catch (Exception exception) {
        fail("No error should be thrown when a node is re-locked by the current lock owner.");
    }
    assertEquals(LockStatus.LOCK_OWNER, lockService.getLockStatus(noAspectNode));
    assertTrue(lockService.isLocked(noAspectNode));
    lockService.unlock(noAspectNode);
    assertEquals(LockStatus.NO_LOCK, lockService.getLockStatus(noAspectNode));
    assertFalse(lockService.isLocked(noAspectNode));
}
Also used : NodeService(org.alfresco.service.cmr.repository.NodeService) LockState(org.alfresco.repo.lock.mem.LockState) UnableToAquireLockException(org.alfresco.service.cmr.lock.UnableToAquireLockException) LockStore(org.alfresco.repo.lock.mem.LockStore) NodeLockedException(org.alfresco.service.cmr.lock.NodeLockedException) UnableToReleaseLockException(org.alfresco.service.cmr.lock.UnableToReleaseLockException) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) NotSupportedException(javax.transaction.NotSupportedException) UnableToAquireLockException(org.alfresco.service.cmr.lock.UnableToAquireLockException) SystemException(javax.transaction.SystemException)

Example 2 with LockStore

use of org.alfresco.repo.lock.mem.LockStore in project alfresco-repository by Alfresco.

the class LockServiceImplTest method testGetLockStatus.

/**
 * Test getLockStatus
 */
public void testGetLockStatus() {
    TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
    // Check an unlocked node
    LockStatus lockStatus1 = this.lockService.getLockStatus(this.parentNode);
    assertEquals(LockStatus.NO_LOCK, lockStatus1);
    this.lockService.lock(this.parentNode, LockType.WRITE_LOCK);
    TestWithUserUtils.authenticateUser(BAD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
    // Check for locked status
    LockStatus lockStatus2 = this.lockService.getLockStatus(this.parentNode);
    assertEquals(LockStatus.LOCKED, lockStatus2);
    // Check lockstore is not used for persistent locks
    // Previously LockStore was doubling up as a cache - the change in requirements means a test
    // is necessary to ensure the work has been implemented correctly (despite being an odd test)
    LockStore lockStore = (LockStore) applicationContext.getBean("lockStore");
    lockStore.clear();
    LockState lockState = lockStore.get(parentNode);
    // Nothing stored against node ref
    assertNull(lockState);
    lockService.getLockStatus(parentNode);
    // In-memory store still empty - only used for ephemeral locks
    lockState = lockStore.get(parentNode);
    assertNull(lockState);
    TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
    // Check for lock owner status
    LockStatus lockStatus3 = this.lockService.getLockStatus(this.parentNode);
    assertEquals(LockStatus.LOCK_OWNER, lockStatus3);
    // Test with no apect node
    this.lockService.getLockStatus(this.noAspectNode);
    // Test method overload
    LockStatus lockStatus4 = this.lockService.getLockStatus(this.parentNode);
    assertEquals(LockStatus.LOCK_OWNER, lockStatus4);
}
Also used : LockStatus(org.alfresco.service.cmr.lock.LockStatus) LockStore(org.alfresco.repo.lock.mem.LockStore) LockState(org.alfresco.repo.lock.mem.LockState)

Aggregations

LockState (org.alfresco.repo.lock.mem.LockState)2 LockStore (org.alfresco.repo.lock.mem.LockStore)2 NotSupportedException (javax.transaction.NotSupportedException)1 SystemException (javax.transaction.SystemException)1 AccessDeniedException (org.alfresco.repo.security.permissions.AccessDeniedException)1 LockStatus (org.alfresco.service.cmr.lock.LockStatus)1 NodeLockedException (org.alfresco.service.cmr.lock.NodeLockedException)1 UnableToAquireLockException (org.alfresco.service.cmr.lock.UnableToAquireLockException)1 UnableToReleaseLockException (org.alfresco.service.cmr.lock.UnableToReleaseLockException)1 NodeService (org.alfresco.service.cmr.repository.NodeService)1