use of javax.jcr.lock.LockManager in project pentaho-platform by pentaho.
the class DefaultDeleteHelperTest method testPermanentlyDeleteFile.
@Test
public void testPermanentlyDeleteFile() throws Exception {
String fileID = "testFileID";
final Node nodeDeletedFile = mock(Node.class);
final Node nodeTrash = mock(Node.class);
when(nodeTrash.hasNode(anyString())).thenReturn(true);
when(nodeTrash.getNode(anyString())).thenReturn(nodeDeletedFile);
final Node nodeUserFolder = mock(Node.class);
when(nodeUserFolder.hasNode(anyString())).thenReturn(true);
when(nodeUserFolder.getNode(anyString())).thenReturn(nodeTrash);
final Node nodeDeletedParent = mock(Node.class);
when(nodeDeletedParent.getPath()).thenReturn("parentPath");
final PropertyIterator referencesPropertyIterator = mock(PropertyIterator.class);
when(referencesPropertyIterator.hasNext()).thenReturn(false);
final Node nodeToRemove = mock(Node.class);
when(nodeToRemove.getPath()).thenReturn("nodePath");
when(nodeToRemove.getParent()).thenReturn(nodeDeletedParent);
when(nodeToRemove.getReferences()).thenReturn(referencesPropertyIterator);
when(nodeToRemove.isLocked()).thenReturn(false);
when(session.getNodeByIdentifier(fileID)).thenReturn(nodeToRemove);
when(session.getItem(anyString())).thenReturn(nodeUserFolder);
Workspace workspace = mock(Workspace.class);
VersionManager versionManager = mock(VersionManager.class);
when(workspace.getVersionManager()).thenReturn(versionManager);
when(session.getWorkspace()).thenReturn(workspace);
try {
defaultDeleteHelper.permanentlyDeleteFile(session, pentahoJcrConstants, fileID);
verify(nodeToRemove).remove();
} catch (Exception e) {
e.printStackTrace();
fail();
}
// test locked file
when(nodeToRemove.isLocked()).thenReturn(true);
final Lock lock = mock(Lock.class);
final LockManager lockManager = mock(LockManager.class);
when(lockManager.getLock(eq(nodeToRemove.getPath()))).thenReturn(lock);
when(workspace.getLockManager()).thenReturn(lockManager);
when(session.getWorkspace()).thenReturn(workspace);
try {
defaultDeleteHelper.permanentlyDeleteFile(session, pentahoJcrConstants, fileID);
verify(lockHelper).removeLockToken(eq(session), eq(pentahoJcrConstants), eq(lock));
verify(nodeToRemove, times(2)).remove();
} catch (Exception e) {
e.printStackTrace();
fail();
}
// if remove from trash folder, folder containing the file need to be removed
final String trashPath = ServerRepositoryPaths.getUserHomeFolderPath(new Tenant(null, true), PentahoSessionHolder.getSession().getName()) + RepositoryFile.SEPARATOR + ".trash";
when(nodeToRemove.getPath()).thenReturn(trashPath);
try {
defaultDeleteHelper.permanentlyDeleteFile(session, pentahoJcrConstants, fileID);
verify(nodeToRemove, times(3)).remove();
verify(nodeDeletedParent).remove();
} catch (Exception e) {
e.printStackTrace();
fail();
}
// test if there are references to this file
final Node referenceNode = mock(Node.class);
when(referenceNode.isNodeType(eq(pentahoJcrConstants.getPHO_NT_PENTAHOHIERARCHYNODE()))).thenReturn(true);
when(referenceNode.isNodeType(eq(pentahoJcrConstants.getPHO_NT_PENTAHOFOLDER()))).thenReturn(false);
final Property property = mock(Property.class);
when(property.getParent()).thenReturn(referenceNode);
when(referencesPropertyIterator.hasNext()).thenReturn(true, true, false);
when(referencesPropertyIterator.nextProperty()).thenReturn(property);
try {
defaultDeleteHelper.permanentlyDeleteFile(session, pentahoJcrConstants, fileID);
} catch (RepositoryFileDaoReferentialIntegrityException e1) {
// it's ok
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
use of javax.jcr.lock.LockManager in project pentaho-platform by pentaho.
the class DefaultLockHelper method unlockFile.
/**
* {@inheritDoc}
*/
public void unlockFile(final Session session, final PentahoJcrConstants pentahoJcrConstants, final Serializable fileId) throws RepositoryException {
Node fileNode = session.getNodeByIdentifier(fileId.toString());
LockManager lockManager = session.getWorkspace().getLockManager();
Lock lock = lockManager.getLock(fileNode.getPath());
String lockToken = getLockToken(session, pentahoJcrConstants, lock);
lockManager.addLockToken(lockToken);
// get the lock again so that it has a non-null lockToken
lock = lockManager.getLock(fileNode.getPath());
// don't need lock token anymore
removeLockToken(session, pentahoJcrConstants, lock);
lockManager.unlock(fileNode.getPath());
}
use of javax.jcr.lock.LockManager in project pentaho-platform by pentaho.
the class DefaultLockHelper method addLockTokenToSessionIfNecessary.
/**
* {@inheritDoc}
*/
public void addLockTokenToSessionIfNecessary(final Session session, final PentahoJcrConstants pentahoJcrConstants, final Serializable fileId) throws RepositoryException {
Node fileNode = session.getNodeByIdentifier(fileId.toString());
if (fileNode.isLocked()) {
LockManager lockManager = session.getWorkspace().getLockManager();
Lock lock = lockManager.getLock(fileNode.getPath());
String lockToken = getLockToken(session, pentahoJcrConstants, lock);
lockManager.addLockToken(lockToken);
}
}
use of javax.jcr.lock.LockManager in project jackrabbit by apache.
the class NodeImpl method holdsLock.
/**
* {@inheritDoc}
*/
public boolean holdsLock() throws RepositoryException {
// check state of this instance
sanityCheck();
LockManager lockMgr = getSession().getWorkspace().getLockManager();
return lockMgr.holdsLock(getPath());
}
use of javax.jcr.lock.LockManager in project jackrabbit by apache.
the class NodeImpl method unlock.
/**
* {@inheritDoc}
*/
public void unlock() throws UnsupportedRepositoryOperationException, LockException, AccessDeniedException, InvalidItemStateException, RepositoryException {
// check state of this instance
sanityCheck();
LockManager lockMgr = getSession().getWorkspace().getLockManager();
lockMgr.unlock(getPath());
}
Aggregations