jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ang...@apache.org
Subject svn commit: r736658 - in /jackrabbit/trunk: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/ jackrabbit-core/src/test/java/org/apache/jackrabbit/api/jsr283/lock/ jackrabbit-jcr2spi...
Date Thu, 22 Jan 2009 14:25:32 GMT
Author: angela
Date: Thu Jan 22 06:25:32 2009
New Revision: 736658

URL: http://svn.apache.org/viewvc?rev=736658&view=rev
Log:
JCR-1590: JSR 283: Locking

- move lock token handling from Session to LockManager.java
- add more tests
- adjust tests in jcr2spi 

TODO:
- sessionscoped locks still expose the lock token
- fixing that needs some more adjustments in jcr2spi and the spi
- timeout handling

Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/SessionImpl.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/AbstractLockInfo.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockManager.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockManagerImpl.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/SessionLockManager.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/XAEnvironment.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/XALockManager.java
    jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/api/jsr283/lock/AbstractLockTest.java
    jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/api/jsr283/lock/LockManagerTest.java
    jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/api/jsr283/lock/OpenScopedLockTest.java
    jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/api/jsr283/lock/SessionScopedLockTest.java
    jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/lock/AbstractLockTest.java
    jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/lock/OpenScopedLockTest.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/SessionImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/SessionImpl.java?rev=736658&r1=736657&r2=736658&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/SessionImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/SessionImpl.java
Thu Jan 22 06:25:32 2009
@@ -208,11 +208,6 @@
     protected final Map listeners = new ReferenceMap(ReferenceMap.WEAK, ReferenceMap.WEAK);
 
     /**
-     * Lock tokens
-     */
-    protected final Set lockTokens = new HashSet();
-
-    /**
      * value factory
      */
     protected ValueFactory valueFactory;
@@ -1260,23 +1255,10 @@
      * {@inheritDoc}
      */
     public void addLockToken(String lt) {
-        addLockToken(lt, true);
-    }
-
-    /**
-     * Internal implementation of {@link #addLockToken(String)}. Additionally
-     * takes a parameter indicating whether the lock manager needs to be
-     * informed.
-     */
-    public void addLockToken(String lt, boolean notify) {
-        synchronized (lockTokens) {
-            if (lockTokens.add(lt) && notify) {
-                try {
-                    getLockManager().lockTokenAdded(this, lt);
-                } catch (RepositoryException e) {
-                    log.error("Lock manager not available.", e);
-                }
-            }
+        try {
+            wsp.get283LockManager().addLockToken(lt);
+        } catch (RepositoryException e) {
+            log.debug("Error while adding lock token.");
         }
     }
 
@@ -1284,10 +1266,11 @@
      * {@inheritDoc}
      */
     public String[] getLockTokens() {
-        synchronized (lockTokens) {
-            String[] result = new String[lockTokens.size()];
-            lockTokens.toArray(result);
-            return result;
+        try {
+            return wsp.get283LockManager().getLockTokens();
+        } catch (RepositoryException e) {
+            log.debug("Error while accessing lock tokens.");
+            return new String[0];
         }
     }
 
@@ -1295,23 +1278,10 @@
      * {@inheritDoc}
      */
     public void removeLockToken(String lt) {
-        removeLockToken(lt, true);
-    }
-
-    /**
-     * Internal implementation of {@link #removeLockToken(String)}. Additionally
-     * takes a parameter indicating whether the lock manager needs to be
-     * informed.
-     */
-    public void removeLockToken(String lt, boolean notify) {
-        synchronized (lockTokens) {
-            if (lockTokens.remove(lt) && notify) {
-                try {
-                    getLockManager().lockTokenRemoved(this, lt);
-                } catch (RepositoryException e) {
-                    log.error("Lock manager not available.", e);
-                }
-            }
+        try {
+            wsp.get283LockManager().removeLockToken(lt);
+        } catch (RepositoryException e) {
+            log.debug("Error while removing lock token.");
         }
     }
 

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/AbstractLockInfo.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/AbstractLockInfo.java?rev=736658&r1=736657&r2=736658&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/AbstractLockInfo.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/AbstractLockInfo.java
Thu Jan 22 06:25:32 2009
@@ -84,7 +84,7 @@
         this.sessionScoped = sessionScoped;
         this.deep = deep;
         this.lockOwner = lockOwner;
-        // TODO: TOBEFIXED for 2.0 respect and deal timeout hint.
+        // TODO: TOBEFIXED for 2.0 respect and deal with timeout hint.
     }
 
     /**

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockManager.java?rev=736658&r1=736657&r2=736658&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockManager.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockManager.java
Thu Jan 22 06:25:32 2009
@@ -157,13 +157,17 @@
      * Invoked by a session to inform that a lock token has been added.
      * @param session session that has a added lock token
      * @param lt added lock token
+     * @throws LockException
+     * @throws RepositoryException
      */
-    void lockTokenAdded(SessionImpl session, String lt);
+    void lockTokenAdded(SessionImpl session, String lt) throws LockException, RepositoryException;
 
     /**
      * Invoked by a session to inform that a lock token has been removed.
      * @param session session that has a removed lock token
      * @param lt removed lock token
+     * @throws LockException
+     * @throws RepositoryException
      */
-    void lockTokenRemoved(SessionImpl session, String lt);
+    void lockTokenRemoved(SessionImpl session, String lt) throws LockException, RepositoryException;
 }

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockManagerImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockManagerImpl.java?rev=736658&r1=736657&r2=736658&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockManagerImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/LockManagerImpl.java
Thu Jan 22 06:25:32 2009
@@ -274,6 +274,11 @@
         }
     }
 
+    static SessionLockManager getSessionLockManager(SessionImpl session) throws RepositoryException
{
+        WorkspaceImpl wsp = (WorkspaceImpl) session.getWorkspace();
+        return (SessionLockManager) wsp.get283LockManager();
+    }
+
     /**
      * Internal <code>lock</code> implementation that takes the same parameters
      * as the public method.
@@ -330,11 +335,9 @@
             info.setLockHolder(session);
             info.setLive(true);
             session.addListener(info);
-            // TODO: TOBEFIXED for 2.0
-            // TODO  only tokens of open-scoped locks must be added to the session.
-            // if (!info.isSessionScoped()) {
-                session.addLockToken(info.lockToken.toString(), false);
-            //}
+            if (!info.isSessionScoped()) {
+                getSessionLockManager(session).lockTokenAdded(info.lockToken.toString());
+            }
             lockMap.put(path, info);
 
             if (!info.sessionScoped) {
@@ -357,7 +360,7 @@
      * @throws LockException       if the node can not be unlocked
      * @throws RepositoryException if another error occurs
      */
-    void internalUnlock(NodeImpl node)
+    boolean internalUnlock(NodeImpl node)
             throws LockException, RepositoryException {
 
         ClusterOperation operation = null;
@@ -384,8 +387,8 @@
                 throw new LockException("Node not locked by session: " + node);
             }
 
-            session.removeLockToken(info.getLockToken(session), false);
-
+            getSessionLockManager(session).lockTokenRemoved(info.getLockToken(session));
+            
             element.set(null);
             info.setLive(false);
 
@@ -393,6 +396,7 @@
                 save();
                 successful = true;
             }
+            return true;
         } finally {
             release();
 
@@ -526,9 +530,7 @@
      * events, content modifications should not be made from within code
      * sections that hold monitors. (see #JCR-194)
      */
-    public void unlock(NodeImpl node)
-            throws LockException, RepositoryException {
-
+    public void unlock(NodeImpl node) throws LockException, RepositoryException {
         removeLockProperties(node);
         internalUnlock(node);
     }
@@ -629,7 +631,7 @@
     /**
      * {@inheritDoc}
      */
-    public void lockTokenAdded(SessionImpl session, String lt) {
+    public void lockTokenAdded(SessionImpl session, String lt) throws LockException, RepositoryException
{
         try {
             LockToken lockToken = LockToken.parse(lt);
 
@@ -645,23 +647,25 @@
                             session.addListener((LockInfo) info);
                         }
                     } else {
-                        log.warn("Adding lock token has no effect: "
-                                + "lock already held by other session.");
+                        String msg = "Cannot add lock token: lock already held by other session.";
+                        log.warn(msg);
+                        throw new LockException(msg);
                     }
                 }
             }
+            // inform SessionLockManager
+            getSessionLockManager(session).lockTokenAdded(lt);
         } catch (IllegalArgumentException e) {
-            log.warn("Bad lock token: " + e.getMessage());
-        } catch (RepositoryException e) {
-            log.warn("Unable to set lock holder: " + e.getMessage());
-            log.debug("Root cause: ", e);
+            String msg = "Bad lock token: " + e.getMessage();
+            log.warn(msg);
+            throw new LockException(msg);
         }
     }
 
     /**
      * {@inheritDoc}
      */
-    public void lockTokenRemoved(SessionImpl session, String lt) {
+    public void lockTokenRemoved(SessionImpl session, String lt) throws LockException, RepositoryException
{
         try {
             LockToken lockToken = LockToken.parse(lt);
 
@@ -674,16 +678,18 @@
                     if (session == info.getLockHolder()) {
                         info.setLockHolder(null);
                     } else {
-                        log.warn("Removing lock token has no effect: "
-                                + "lock held by other session.");
+                        String msg = "Cannot remove lock token: lock held by other session.";
+                        log.warn(msg);
+                        throw new LockException(msg);
                     }
                 }
             }
+            // inform SessionLockManager
+            getSessionLockManager(session).lockTokenRemoved(lt);
         } catch (IllegalArgumentException e) {
-            log.warn("Bad lock token: " + e.getMessage());
-        } catch (RepositoryException e) {
-            log.warn("Unable to reset lock holder: " + e.getMessage());
-            log.debug("Root cause: ", e);
+            String msg = "Bad lock token: " + e.getMessage();
+            log.warn(msg);
+            throw new LockException(msg);
         }
     }
 

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/SessionLockManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/SessionLockManager.java?rev=736658&r1=736657&r2=736658&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/SessionLockManager.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/SessionLockManager.java
Thu Jan 22 06:25:32 2009
@@ -40,7 +40,7 @@
  * is associated with a single <code>Session</code> and its
  * <code>Workspace</code>.
  *
- * @see org.apache.jackrabbit.api.jsr283.Workspace#getLockManager()
+ * @see javax.jcr.Workspace#getLockManager()
  */
 public class SessionLockManager implements org.apache.jackrabbit.api.jsr283.lock.LockManager
{
 
@@ -70,16 +70,22 @@
      * @see org.apache.jackrabbit.api.jsr283.lock.LockManager#addLockToken(String)
      */
     public void addLockToken(String lockToken) throws LockException, RepositoryException
{
-        // TODO
-        throw new UnsupportedRepositoryOperationException("Not yet implemented");
+        if (!lockTokens.contains(lockToken)) {
+            systemLockMgr.lockTokenAdded(session, lockToken);
+        } else {
+            log.debug("Lock token already present with session -> no effect.");
+        }
     }
 
     /**
      * @see org.apache.jackrabbit.api.jsr283.lock.LockManager#removeLockToken(String)
      */
     public void removeLockToken(String lockToken) throws LockException, RepositoryException
{
-        // TODO
-        throw new UnsupportedRepositoryOperationException("Not yet implemented");
+        if (lockTokens.contains(lockToken)) {
+            systemLockMgr.lockTokenRemoved(session, lockToken);
+        } else {
+            throw new LockException("Lock token " + lockToken + " not present with session.");
+        }
     }
 
     /**
@@ -189,6 +195,28 @@
 
     //--------------------------------------------------------------------------
     /**
+     *
+     * @param lockToken
+     * @return
+     */
+    boolean lockTokenAdded(String lockToken) {
+        synchronized (lockTokens) {
+            return lockTokens.add(lockToken);
+        }
+    }
+
+    /**
+     * 
+     * @param lockToken
+     * @return
+     */
+    boolean lockTokenRemoved(String lockToken) {
+        synchronized (lockTokens) {
+            return lockTokens.remove(lockToken);
+        }
+    }
+
+    /**
      * Checks if the given node is lockable, i.e. has 'mix:lockable'.
      *
      * @param node

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/XAEnvironment.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/XAEnvironment.java?rev=736658&r1=736657&r2=736658&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/XAEnvironment.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/XAEnvironment.java
Thu Jan 22 06:25:32 2009
@@ -150,7 +150,7 @@
         info.setLockHolder(session);
         info.setLive(true);
 
-        session.addLockToken(info.lockToken.toString(), false);
+        LockManagerImpl.getSessionLockManager(session).lockTokenAdded(info.lockToken.toString());
         lockedNodesMap.put(id, info);
         operations.add(info);
 

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/XALockManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/XALockManager.java?rev=736658&r1=736657&r2=736658&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/XALockManager.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/lock/XALockManager.java
Thu Jan 22 06:25:32 2009
@@ -203,7 +203,7 @@
     /**
      * {@inheritDoc}
      */
-    public void lockTokenAdded(SessionImpl session, String lt) {
+    public void lockTokenAdded(SessionImpl session, String lt) throws RepositoryException
{
         if (isInXA()) {
             xaEnv.addLockToken(lt);
         } else {
@@ -214,7 +214,7 @@
     /**
      * {@inheritDoc}
      */
-    public void lockTokenRemoved(SessionImpl session, String lt) {
+    public void lockTokenRemoved(SessionImpl session, String lt) throws RepositoryException
{
         if (isInXA()) {
             xaEnv.removeLockToken(lt);
         } else {

Modified: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/api/jsr283/lock/AbstractLockTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/api/jsr283/lock/AbstractLockTest.java?rev=736658&r1=736657&r2=736658&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/api/jsr283/lock/AbstractLockTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/api/jsr283/lock/AbstractLockTest.java
Thu Jan 22 06:25:32 2009
@@ -26,6 +26,7 @@
 import javax.jcr.Node;
 import javax.jcr.Session;
 import javax.jcr.RepositoryException;
+import javax.jcr.Repository;
 import javax.jcr.lock.LockException;
 import javax.jcr.nodetype.ConstraintViolationException;
 
@@ -42,6 +43,11 @@
     protected void setUp() throws Exception {
         super.setUp();
 
+        // check for lock support
+        if (Boolean.FALSE.toString().equals(superuser.getRepository().getDescriptor(Repository.OPTION_LOCKING_SUPPORTED)))
{
+            throw new NotExecutableException();
+        }
+
         lockedNode = testRootNode.addNode(nodeName1, testNodeType);
         lockedNode.addMixin(mixLockable);
         childNode = lockedNode.addNode(nodeName2, testNodeType);

Modified: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/api/jsr283/lock/LockManagerTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/api/jsr283/lock/LockManagerTest.java?rev=736658&r1=736657&r2=736658&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/api/jsr283/lock/LockManagerTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/api/jsr283/lock/LockManagerTest.java
Thu Jan 22 06:25:32 2009
@@ -25,7 +25,11 @@
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
 import javax.jcr.InvalidItemStateException;
+import javax.jcr.Session;
+import javax.jcr.Repository;
 import javax.jcr.lock.LockException;
+import java.util.Arrays;
+import java.util.List;
 
 /** <code>LockManagerTest</code>... */
 public class LockManagerTest extends AbstractJCRTest {
@@ -39,12 +43,17 @@
     protected void setUp() throws Exception {
         super.setUp();
 
+        // check for lock support
+        if (Boolean.FALSE.toString().equals(superuser.getRepository().getDescriptor(Repository.OPTION_LOCKING_SUPPORTED)))
{
+            throw new NotExecutableException();
+        }
+        
         testNode = testRootNode.addNode(nodeName1, testNodeType);
         testRootNode.save();
         testPath = testNode.getPath();
 
         // TODO: rm cast and adjust call as soon as 283 is released
-        lockMgr = ((WorkspaceImpl) superuser.getWorkspace()).get283LockManager();
+        lockMgr = getLockManager(superuser);
     }
 
     protected void tearDown() throws Exception  {
@@ -54,6 +63,31 @@
         super.tearDown();
     }
 
+    private void assertLockable(Node n) throws RepositoryException {
+        if (!n.isNodeType(mixLockable)) {
+            n.addMixin(mixLockable);
+            n.getSession().save();
+        }
+    }
+
+   private static LockManager getLockManager(Session session) throws RepositoryException
{
+        // TODO: rm cast and adjust call as soon as 283 is released
+        return ((WorkspaceImpl) session.getWorkspace()).get283LockManager();
+    }
+
+    private static boolean containsLockToken(LockManager lMgr, String token) throws RepositoryException
{
+        return containsLockToken(lMgr.getLockTokens(), token);
+    }
+
+    private static boolean containsLockToken(String[] tokens, String token) throws RepositoryException
{
+        for (int i = 0; i < tokens.length; i++) {
+            if (tokens[i].equals(token)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     public void testLockNonLockable() throws NotExecutableException, RepositoryException
{
         if (testNode.isNodeType(mixLockable)) {
             throw new NotExecutableException();
@@ -67,6 +101,8 @@
     }
 
     public void testLockWithPendingChanges() throws RepositoryException {
+        assertLockable(testNode);
+
         // transient modification
         testNode.addNode(nodeName2);
         try {
@@ -78,10 +114,272 @@
     }
 
     public void testNullOwnerHint() throws RepositoryException {
-        testNode.addMixin(mixLockable);
-        testRootNode.save();
+        assertLockable(testNode);
 
         Lock l = lockMgr.lock(testPath, true, true, Long.MAX_VALUE, null);
         assertNotNull(l.getLockOwner());
     }
+
+    public void testGetLockTokens() throws RepositoryException {
+        assertLockable(testNode);
+
+        boolean sessionScoped = false;
+        Lock l = lockMgr.lock(testPath, true, sessionScoped, Long.MAX_VALUE, null);
+        String ltoken = l.getLockToken();
+        assertTrue("Creating open scoped lock must add token to the lock manager.",
+                containsLockToken(lockMgr, ltoken));
+        assertTrue("Creating open scoped lock must add token to the lock manager.",
+                containsLockToken(superuser.getLockTokens(), ltoken));
+    }
+
+    public void testGetLockTokensAfterUnlock() throws RepositoryException {
+        assertLockable(testNode);
+
+        boolean sessionScoped = false;
+        Lock l = lockMgr.lock(testPath, true, sessionScoped, Long.MAX_VALUE, null);
+        String ltoken = l.getLockToken();
+
+        lockMgr.unlock(testPath);
+        assertFalse("Removing an open scoped lock must remove the token from the lock manager.",
+                containsLockToken(lockMgr, ltoken));
+        assertFalse("Removing an open scoped lock must remove the token from the lock manager.",
+                containsLockToken(superuser.getLockTokens(), ltoken));
+    }
+
+    public void testGetLockTokensSessionScoped() throws RepositoryException {
+        assertLockable(testNode);
+
+        List tokensBefore = Arrays.asList(lockMgr.getLockTokens());
+
+        boolean sessionScoped = true;
+        Lock l = lockMgr.lock(testPath, true, sessionScoped, Long.MAX_VALUE, null);
+
+        assertEquals("Creating a session scoped lock must not change the lock tokens.",
+                tokensBefore, Arrays.asList(lockMgr.getLockTokens()));
+        assertEquals("Creating a session scoped lock must not change the lock tokens.",
+                tokensBefore, Arrays.asList(superuser.getLockTokens()));
+    }
+
+    public void testAddLockToken() throws RepositoryException {
+        assertLockable(testNode);
+
+        boolean sessionScoped = false;
+        Lock l = lockMgr.lock(testPath, true, sessionScoped, Long.MAX_VALUE, null);
+        String ltoken = l.getLockToken();
+
+        // adding lock token should have no effect.
+        lockMgr.addLockToken(ltoken);
+    }
+
+    public void testAddInvalidLockToken() throws RepositoryException {
+        try {
+            lockMgr.addLockToken("any-token");
+            fail("Adding an invalid token must fail.");
+        } catch (LockException e) {
+            // success
+        }
+    }
+
+    public void testAddLockTokenToAnotherSession() throws RepositoryException {
+        // TODO: for 283 add config option for simutaneous tokens....
+        
+        assertLockable(testNode);
+
+        boolean sessionScoped = false;
+        Lock l = lockMgr.lock(testPath, true, sessionScoped, Long.MAX_VALUE, null);
+        String ltoken = l.getLockToken();
+
+        Session other = helper.getReadWriteSession();
+        try {
+            LockManager otherLockMgr = getLockManager(other);
+            assertFalse(containsLockToken(otherLockMgr, ltoken));
+
+            try {
+                otherLockMgr.addLockToken(ltoken);
+                fail("Adding token to another session must fail.");
+            } catch (LockException e) {
+                // success
+            }
+        } finally {
+            other.logout();
+        }
+    }
+
+    public void testRemoveLockToken() throws Exception {
+        assertLockable(testNode);
+
+        boolean sessionScoped = false;
+        Lock l = lockMgr.lock(testPath, true, sessionScoped, Long.MAX_VALUE, null);
+        String ltoken = l.getLockToken();
+
+        try {
+            // remove lock token
+            lockMgr.removeLockToken(ltoken);
+
+            assertFalse(containsLockToken(lockMgr, ltoken));
+            assertFalse(containsLockToken(superuser.getLockTokens(), ltoken));
+        } finally {
+            // make sure lock token is added even if test fail
+            lockMgr.addLockToken(ltoken);
+            assertTrue(containsLockToken(lockMgr, ltoken));
+            assertNotNull("Token must be exposed again", l.getLockToken());
+            assertEquals("The lock must get the same token again.", ltoken, l.getLockToken());
+        }
+    }
+
+    public void testRemoveLockToken2() throws Exception {
+        assertLockable(testNode);
+
+        boolean sessionScoped = false;
+        Lock l = lockMgr.lock(testPath, true, sessionScoped, Long.MAX_VALUE, null);
+        String ltoken = l.getLockToken();
+
+        try {
+            lockMgr.removeLockToken(ltoken);
+
+            assertNull("Lock token must not be exposed any more.", l.getLockToken());
+        } finally {
+            // make sure lock token is added even if test fail
+            lockMgr.addLockToken(ltoken);
+        }
+    }
+
+    public void testRemoveLockToken3() throws Exception {
+        assertLockable(testNode);
+
+        boolean sessionScoped = false;
+        Lock l = lockMgr.lock(testPath, true, sessionScoped, Long.MAX_VALUE, null);
+        String ltoken = l.getLockToken();
+
+        try {
+            lockMgr.removeLockToken(ltoken);
+
+            // without holding the token session must not be allowed to modify
+            // the locked node.
+            try {
+                testNode.addNode(nodeName2, testNodeType);
+                fail("Session must not be allowed to modify node");
+            } catch (LockException e) {
+                // expected
+            }
+        } finally {
+            // make sure lock token is added even if test fail
+            lockMgr.addLockToken(ltoken);
+        }
+    }
+
+    public void testRemoveLockTokenTwice() throws Exception {
+        assertLockable(testNode);
+
+        boolean sessionScoped = false;
+        Lock l = lockMgr.lock(testPath, true, sessionScoped, Long.MAX_VALUE, null);
+        String ltoken = l.getLockToken();
+
+        lockMgr.removeLockToken(ltoken);
+        try {
+            // remove token a second time
+            lockMgr.removeLockToken(ltoken);
+            fail("Removing a lock token twice must fail.");
+        } catch (LockException e) {
+            // success
+        } finally {
+            // make sure lock token is added even if test fail
+            lockMgr.addLockToken(ltoken);
+        }
+    }
+
+    public void testAddLockTokenAgain() throws Exception {
+        assertLockable(testNode);
+
+        boolean sessionScoped = false;
+        Lock l = lockMgr.lock(testPath, true, sessionScoped, Long.MAX_VALUE, null);
+        String ltoken = l.getLockToken();
+
+        try {
+            // remove lock token
+            lockMgr.removeLockToken(ltoken);
+        } finally {
+            // make sure lock token is added even if test fail
+            lockMgr.addLockToken(ltoken);
+            assertTrue(containsLockToken(lockMgr, ltoken));
+            assertNotNull("Token must be exposed again", l.getLockToken());
+            assertEquals("The lock must get the same token again.", ltoken, l.getLockToken());
+        }
+    }
+
+    public void testLockTransfer() throws Exception {        
+        assertLockable(testNode);
+
+        boolean sessionScoped = false;
+        Lock l = lockMgr.lock(testPath, true, sessionScoped, Long.MAX_VALUE, null);
+        String ltoken = l.getLockToken();
+
+        Session other = helper.getReadWriteSession();
+        LockManager otherLockMgr = getLockManager(other);
+        try {
+            lockMgr.removeLockToken(ltoken);
+            otherLockMgr.addLockToken(ltoken);
+
+            assertTrue("The new holding manager must contain the token.", containsLockToken(otherLockMgr,
ltoken));
+
+            Lock otherL = otherLockMgr.getLock(testPath);
+            assertNotNull("Token must be exposed to new lock holder.", otherL.getLockToken());
+            assertEquals("Token must be the same again.", ltoken, otherL.getLockToken());
+
+        } finally {
+            otherLockMgr.removeLockToken(ltoken);
+            lockMgr.addLockToken(ltoken);
+            other.logout();
+        }
+    }
+
+    public void testLockTransfer2() throws Exception {
+        // TODO: for 283 add config option for simutaneous tokens....
+
+        assertLockable(testNode);
+
+        boolean sessionScoped = false;
+        Lock l = lockMgr.lock(testPath, true, sessionScoped, Long.MAX_VALUE, null);
+        String ltoken = l.getLockToken();
+
+        Session other = helper.getReadWriteSession();
+        LockManager otherLockMgr = getLockManager(other);
+        try {
+            lockMgr.removeLockToken(ltoken);
+            otherLockMgr.addLockToken(ltoken);
+
+            lockMgr.addLockToken(ltoken);
+            fail("Adding the token to another session must fail.");
+        } catch (LockException e) {
+            // success
+        } finally {
+            otherLockMgr.removeLockToken(ltoken);
+            lockMgr.addLockToken(ltoken);
+            other.logout();
+        }
+    }
+
+    public void testLockTransfer3() throws Exception {
+        assertLockable(testNode);
+
+        boolean sessionScoped = false;
+        Lock l = lockMgr.lock(testPath, true, sessionScoped, Long.MAX_VALUE, null);
+        String ltoken = l.getLockToken();
+
+        Session other = helper.getReadWriteSession();
+        LockManager otherLockMgr = getLockManager(other);
+        try {
+            lockMgr.removeLockToken(ltoken);
+            otherLockMgr.addLockToken(ltoken);
+
+            lockMgr.removeLockToken(ltoken);
+            fail("Removing a token that has been transfered to another manager must fail.");
+        } catch (LockException e) {
+            // success
+        } finally {
+            otherLockMgr.removeLockToken(ltoken);
+            lockMgr.addLockToken(ltoken);
+            other.logout();
+        }
+    }
 }
\ No newline at end of file

Modified: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/api/jsr283/lock/OpenScopedLockTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/api/jsr283/lock/OpenScopedLockTest.java?rev=736658&r1=736657&r2=736658&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/api/jsr283/lock/OpenScopedLockTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/api/jsr283/lock/OpenScopedLockTest.java
Thu Jan 22 06:25:32 2009
@@ -37,5 +37,5 @@
      */
     public void testGetLockToken() {
         assertNotNull("A open scoped lock must expose the token to the lock holder.", lock.getLockToken());
-    }
+    } 
 }
\ No newline at end of file

Modified: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/api/jsr283/lock/SessionScopedLockTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/api/jsr283/lock/SessionScopedLockTest.java?rev=736658&r1=736657&r2=736658&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/api/jsr283/lock/SessionScopedLockTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/api/jsr283/lock/SessionScopedLockTest.java
Thu Jan 22 06:25:32 2009
@@ -37,7 +37,6 @@
      * always return <code>null</code> for session scoped locks.
      */
     /*
-    TODO: uncomment as soon as 283 lock token handing is in effect
     public void testGetLockToken() {
         assertNull("A session scoped lock may never expose the token.", lock.getLockToken());
     }

Modified: jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/lock/AbstractLockTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/lock/AbstractLockTest.java?rev=736658&r1=736657&r2=736658&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/lock/AbstractLockTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/lock/AbstractLockTest.java
Thu Jan 22 06:25:32 2009
@@ -316,27 +316,6 @@
         }
     }
 
-    /**
-     * Test if the lock token has been automatically added to the set of lock
-     * tokens present with the Session that created the new Lock.
-     *
-     * @throws RepositoryException
-     */
-    public void testLockTokenPresentWithSession() throws RepositoryException {
-        String token = lock.getLockToken();
-        String[] allTokens = lockedNode.getSession().getLockTokens();
-        for (int i = 0; i < allTokens.length; i++) {
-            if (allTokens[i].equals(token)) {
-                // lock token is present with the session that applied the lock
-                // OK
-                return;
-            }
-        }
-
-        // lock token not present within tokens returned by Session.getLockTokens.
-        fail("Upon successful call to Node.lock, the lock token must automatically be added
to the set of tokens held by the Session.");
-    }
-
     public void testRemoveLockedNode() throws RepositoryException {
         Node n = (Node) otherSession.getItem(lockedNode.getPath());
 
@@ -356,8 +335,10 @@
             String msg = "Lock should have been released.";
             assertFalse(msg, lock.isLive());
             assertFalse(msg, lockedNode.isLocked());
-            List tokens = Arrays.asList(superuser.getLockTokens());
-            assertFalse(msg, tokens.contains(lock.getLockToken()));
+            if (!isSessionScoped()) {
+                List tokens = Arrays.asList(superuser.getLockTokens());
+                assertFalse(msg, tokens.contains(lock.getLockToken()));
+            }
 
             assertFalse(msg, lockedNode.hasProperty(jcrLockOwner));
             assertFalse(msg, lockedNode.hasProperty(jcrlockIsDeep));
@@ -368,8 +349,10 @@
             String msg = "Lock must still be live.";
             assertTrue(msg, lock.isLive());
             assertTrue(msg, lockedNode.isLocked());
-            List tokens = Arrays.asList(superuser.getLockTokens());
-            assertTrue(tokens.contains(lock.getLockToken()));
+            if (!isSessionScoped()) {
+                List tokens = Arrays.asList(superuser.getLockTokens());
+                assertTrue(tokens.contains(lock.getLockToken()));
+            }
             assertTrue(msg, lockedNode.hasProperty(jcrLockOwner));
             assertTrue(msg, lockedNode.hasProperty(jcrlockIsDeep));
         } finally {

Modified: jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/lock/OpenScopedLockTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/lock/OpenScopedLockTest.java?rev=736658&r1=736657&r2=736658&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/lock/OpenScopedLockTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr2spi/src/test/java/org/apache/jackrabbit/jcr2spi/lock/OpenScopedLockTest.java
Thu Jan 22 06:25:32 2009
@@ -63,6 +63,27 @@
         }
     }
 
+    /**
+     * Test if the lock token has been automatically added to the set of lock
+     * tokens present with the Session that created the new Lock.
+     *
+     * @throws RepositoryException
+     */
+    public void testLockTokenPresentWithSession() throws RepositoryException {
+        String token = lock.getLockToken();
+        String[] allTokens = lockedNode.getSession().getLockTokens();
+        for (int i = 0; i < allTokens.length; i++) {
+            if (allTokens[i].equals(token)) {
+                // lock token is present with the session that applied the lock
+                // OK
+                return;
+            }
+        }
+
+        // lock token not present within tokens returned by Session.getLockTokens.
+        fail("Upon successful call to Node.lock, the lock token must automatically be added
to the set of tokens held by the Session.");
+    }
+    
     public void testTokenTransfer() throws Exception {
         String lockToken = lock.getLockToken();
         try {



Mime
View raw message