You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 10:18:08 UTC

[sling-org-apache-sling-testing-jcr-mock] 12/27: SLING-4548 workaround for setting jcr:created/jcr:createdBy and new state properly when creating nt:file nodes

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.testing.jcr-mock-1.1.10
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-jcr-mock.git

commit 6e986976903bcbc37d9beb596496195b4efc1db9
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Tue Apr 7 20:35:44 2015 +0000

    SLING-4548 workaround for setting jcr:created/jcr:createdBy and new state properly when creating nt:file nodes
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/jcr-mock@1671940 13f79535-47bb-0310-9956-ffa450edef68
---
 .../org/apache/sling/testing/mock/jcr/AbstractItem.java     |  4 ++--
 .../java/org/apache/sling/testing/mock/jcr/ItemData.java    | 11 ++++++++++-
 .../java/org/apache/sling/testing/mock/jcr/MockNode.java    |  8 ++++++++
 .../java/org/apache/sling/testing/mock/jcr/MockSession.java |  5 ++++-
 .../org/apache/sling/testing/mock/jcr/MockNodeTest.java     |  8 ++++++++
 .../org/apache/sling/testing/mock/jcr/MockSessionTest.java  | 13 +++++++++++++
 6 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/apache/sling/testing/mock/jcr/AbstractItem.java b/src/main/java/org/apache/sling/testing/mock/jcr/AbstractItem.java
index 9284fe5..38870cd 100644
--- a/src/main/java/org/apache/sling/testing/mock/jcr/AbstractItem.java
+++ b/src/main/java/org/apache/sling/testing/mock/jcr/AbstractItem.java
@@ -67,9 +67,9 @@ abstract class AbstractItem implements Item {
 
     @Override
     public boolean isNew() {
-        return false;
+        return itemData.isNew();
     }
-
+    
     @Override
     public Item getAncestor(final int depth) throws RepositoryException {
         if (depth < 0 || depth > getDepth()) {
diff --git a/src/main/java/org/apache/sling/testing/mock/jcr/ItemData.java b/src/main/java/org/apache/sling/testing/mock/jcr/ItemData.java
index 64ec7c1..538844f 100644
--- a/src/main/java/org/apache/sling/testing/mock/jcr/ItemData.java
+++ b/src/main/java/org/apache/sling/testing/mock/jcr/ItemData.java
@@ -37,14 +37,15 @@ class ItemData {
     private final NodeType nodeType;
     private Value[] values;
     private boolean isMultiple;
+    private boolean isNew;
     
     private ItemData(String path, boolean isNode, String uuid, NodeType nodeType) {
-        super();
         this.path = path;
         this.name = ResourceUtil.getName(path);
         this.uuid = uuid;
         this.isNode = isNode;
         this.nodeType = nodeType;
+        this.isNew = true;
     }
     
     public String getPath() {
@@ -114,6 +115,14 @@ class ItemData {
         }
     }
     
+    public boolean isNew() {
+        return isNew;
+    }
+
+    public void setIsNew(boolean isNew) {
+        this.isNew = isNew;
+    }
+
     @Override
     public int hashCode() {
         return path.hashCode();
diff --git a/src/main/java/org/apache/sling/testing/mock/jcr/MockNode.java b/src/main/java/org/apache/sling/testing/mock/jcr/MockNode.java
index 3913479..f06c3a9 100644
--- a/src/main/java/org/apache/sling/testing/mock/jcr/MockNode.java
+++ b/src/main/java/org/apache/sling/testing/mock/jcr/MockNode.java
@@ -40,6 +40,7 @@ import javax.jcr.nodetype.NodeType;
 import javax.jcr.version.Version;
 import javax.jcr.version.VersionHistory;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.jackrabbit.JcrConstants;
 import org.apache.jackrabbit.commons.iterator.NodeIteratorAdapter;
 import org.apache.jackrabbit.commons.iterator.PropertyIteratorAdapter;
@@ -64,6 +65,13 @@ class MockNode extends AbstractItem implements Node {
         ItemData itemData = ItemData.newNode(path, new MockNodeType(primaryNodeTypeName));
         Node node = new MockNode(itemData, getSession());
         getMockedSession().addItem(itemData);
+        
+        // special handling for some node types
+        if (StringUtils.equals(primaryNodeTypeName, JcrConstants.NT_FILE)) {
+            node.setProperty(JcrConstants.JCR_CREATED, Calendar.getInstance());
+            node.setProperty("jcr:createdBy", getMockedSession().getUserID());
+        }
+        
         return node;
     }
 
diff --git a/src/main/java/org/apache/sling/testing/mock/jcr/MockSession.java b/src/main/java/org/apache/sling/testing/mock/jcr/MockSession.java
index 3fd661d..41b7db2 100644
--- a/src/main/java/org/apache/sling/testing/mock/jcr/MockSession.java
+++ b/src/main/java/org/apache/sling/testing/mock/jcr/MockSession.java
@@ -239,7 +239,10 @@ class MockSession implements Session {
 
     @Override
     public void save() throws RepositoryException {
-        // do nothing
+        // reset new flags
+        for (ItemData itemData : this.items.values()) {
+            itemData.setIsNew(false);
+        }
     }
 
     @Override
diff --git a/src/test/java/org/apache/sling/testing/mock/jcr/MockNodeTest.java b/src/test/java/org/apache/sling/testing/mock/jcr/MockNodeTest.java
index b666f5f..fc8ad9a 100644
--- a/src/test/java/org/apache/sling/testing/mock/jcr/MockNodeTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/jcr/MockNodeTest.java
@@ -20,6 +20,7 @@ package org.apache.sling.testing.mock.jcr;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 import javax.jcr.ItemNotFoundException;
@@ -128,4 +129,11 @@ public class MockNodeTest {
         this.node1.getPrimaryItem();
     }
 
+    @Test
+    public void testNtFileNode() throws RepositoryException {
+        Node ntFile = this.session.getRootNode().addNode("testFile", JcrConstants.NT_FILE);
+        assertNotNull(ntFile.getProperty(JcrConstants.JCR_CREATED).getDate());
+        assertNotNull(ntFile.getProperty("jcr:createdBy").getString());
+    }
+    
 }
diff --git a/src/test/java/org/apache/sling/testing/mock/jcr/MockSessionTest.java b/src/test/java/org/apache/sling/testing/mock/jcr/MockSessionTest.java
index 22fd014..51068d7 100644
--- a/src/test/java/org/apache/sling/testing/mock/jcr/MockSessionTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/jcr/MockSessionTest.java
@@ -247,4 +247,17 @@ public class MockSessionTest {
         this.session.removeItem("/foo/");
         assertFalse("Removing /foo/ should succeed", this.session.nodeExists("/foo"));
     }
+    
+    @Test
+    public void testNewState() throws RepositoryException {
+        Node node = this.session.getRootNode().addNode("foo");
+        Property property = node.setProperty("testProp", "value123");
+        assertTrue(node.isNew());
+        assertTrue(property.isNew());
+        
+        this.session.save();
+        assertFalse(node.isNew());
+        assertFalse(property.isNew());
+    }
+    
 }

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.