From 05c76c48fb590f064432a85fa1aaaabc56e5bc97 Mon Sep 17 00:00:00 2001
From: Robert Munteanu <rombert@apache.org>
Date: Fri, 7 Aug 2015 13:24:22 +0300
Subject: [PATCH] OAK-2149 - Locks are not enforced

Strawman patch + throwaway test to make sure I'm heading in the right
direction .

I used dlg.isLocked() instead of isLocked() as that caused test failures
in
sessionIsolation[0](org.apache.jackrabbit.oak.jcr.CompatibilityIssuesTest):
java.lang.AssertionError: p1 + p2 < 0 . I have no idea where that is
coming from, but this change seems to work.
---
 .../jackrabbit/oak/jcr/session/NodeImpl.java       |  4 ++
 .../oak/jcr/lock/OpenScopedLocksTest.java          | 57 ++++++++++++++++++++++
 2 files changed, 61 insertions(+)
 create mode 100644 oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/lock/OpenScopedLocksTest.java

diff --git a/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/session/NodeImpl.java b/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/session/NodeImpl.java
index 71f12f9..fa7773c 100644
--- a/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/session/NodeImpl.java
+++ b/oak-jcr/src/main/java/org/apache/jackrabbit/oak/jcr/session/NodeImpl.java
@@ -55,6 +55,7 @@ import javax.jcr.RepositoryException;
 import javax.jcr.UnsupportedRepositoryOperationException;
 import javax.jcr.Value;
 import javax.jcr.lock.Lock;
+import javax.jcr.lock.LockException;
 import javax.jcr.lock.LockManager;
 import javax.jcr.nodetype.ConstraintViolationException;
 import javax.jcr.nodetype.NodeDefinition;
@@ -1351,6 +1352,9 @@ public class NodeImpl<T extends NodeDelegate> extends ItemImpl<T> implements Nod
                     throw new VersionException(
                             "Cannot set property. Node is checked in.");
                 }
+                if ( dlg.isLocked() ) {
+                    throw new LockException("Node is locked");
+                }
             }
             @Nonnull
             @Override
diff --git a/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/lock/OpenScopedLocksTest.java b/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/lock/OpenScopedLocksTest.java
new file mode 100644
index 0000000..a6ecb67
--- /dev/null
+++ b/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/lock/OpenScopedLocksTest.java
@@ -0,0 +1,57 @@
+package org.apache.jackrabbit.oak.jcr.lock;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import javax.jcr.Node;
+import javax.jcr.Session;
+import javax.jcr.lock.Lock;
+import javax.jcr.lock.LockException;
+
+import org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest;
+import org.apache.jackrabbit.oak.jcr.NodeStoreFixture;
+import org.junit.Test;
+
+public class OpenScopedLocksTest extends AbstractRepositoryTest {
+
+    public OpenScopedLocksTest(NodeStoreFixture fixture) {
+        super(fixture);
+    }
+
+    @Test
+    public void openScopedLockIsReflectedInAnotherSession() throws Exception {
+        Session session = createAdminSession();
+        try {
+            // make versionable
+            Node node = session.getRootNode().addNode("child1");
+            node.addMixin("mix:lockable");
+            node.getSession().save();
+
+            // lock
+            session.getWorkspace().getLockManager().lock(node.getPath(), true, false, Long.MAX_VALUE, null);
+        } finally {
+            session.logout();
+        }
+        
+        Node node = createAdminSession().getNode("/child1");
+        
+        assertTrue(node.isLocked());
+        
+        Lock lock = node.getSession().getWorkspace().getLockManager().getLock(node.getPath());
+        
+        assertFalse(lock.isLockOwningSession());
+        
+        try {
+            node.setProperty("some", "value");
+            node.getSession().save();
+            fail("Setting a property on a locked node should have thrown a LockException");
+        } catch ( LockException e ) {
+            // I'll allow it
+        } finally {
+            node.getSession().logout();
+        }
+        
+    }
+
+}
-- 
2.4.6

