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:00 UTC

[sling-org-apache-sling-testing-jcr-mock] 04/27: SLING-4238 - Jcr Mock does not normalize paths

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 422e276b70b8f096d355b5e0046a3416ebb738f2
Author: Julian Sedding <js...@apache.org>
AuthorDate: Thu Dec 11 15:14:48 2014 +0000

    SLING-4238 - Jcr Mock does not normalize paths
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/jcr-mock@1644669 13f79535-47bb-0310-9956-ffa450edef68
---
 .../apache/sling/testing/mock/jcr/MockSession.java | 44 +++++++++++-----------
 .../sling/testing/mock/jcr/MockNodeTest.java       |  5 ++-
 .../sling/testing/mock/jcr/MockSessionTest.java    | 17 +++++++++
 3 files changed, 42 insertions(+), 24 deletions(-)

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 1d491e7..2e7e062 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
@@ -71,7 +71,7 @@ class MockSession implements Session {
 
     @Override
     public Item getItem(final String absPath) throws RepositoryException {
-        ItemData itemData = this.items.get(absPath);
+        final ItemData itemData = getItemData(absPath);
         if (itemData != null) {
             if (itemData.isNode()) {
                 return new MockNode(itemData, this);
@@ -116,22 +116,12 @@ class MockSession implements Session {
 
     @Override
     public boolean nodeExists(final String absPath) throws RepositoryException {
-        try {
-            getNode(absPath);
-            return true;
-        } catch (PathNotFoundException ex) {
-            return false;
-        }
+        return itemExists(absPath) && getItemData(absPath).isNode();
     }
 
     @Override
     public boolean propertyExists(final String absPath) throws RepositoryException {
-        try {
-            getProperty(absPath);
-            return true;
-        } catch (PathNotFoundException ex) {
-            return false;
-        }
+        return itemExists(absPath) && getItemData(absPath).isProperty();
     }
 
     @Override
@@ -141,7 +131,7 @@ class MockSession implements Session {
 
     @Override
     public Node getRootNode() throws RepositoryException {
-        return (Node)this.items.get("/").getItem(this);
+        return getNode("/");
     }
 
     @Override
@@ -152,24 +142,32 @@ class MockSession implements Session {
     /**
      * Add item
      * @param itemData item data
-     * @throws RepositoryException
      */
-    void addItem(final ItemData itemData) throws RepositoryException {
+    void addItem(final ItemData itemData) {
         this.items.put(itemData.getPath(), itemData);
     }
 
+    private ItemData getItemData(final String absPath) {
+        final String normalizedPath = ResourceUtil.normalize(absPath);
+        return this.items.get(normalizedPath);
+    }
+
     /**
      * Remove item incl. children
-     * @param path Item path
+     * @param absPath Item path
      */
-    void removeItemWithChildren(final String path) {
-        List<String> pathsToRemove = new ArrayList<String>();
+    private void removeItemWithChildren(final String absPath) throws RepositoryException {
+        if (!itemExists(absPath)) {
+            return;
+        }
 
-        // build regex pattern for node and all its children
-        Pattern pattern = Pattern.compile("^" + Pattern.quote(path) + "(/.+)?$");
+        final ItemData parent = getItemData(absPath);
+        final String descendantPrefix = parent.getPath() + "/";
 
+        final List<String> pathsToRemove = new ArrayList<String>();
+        pathsToRemove.add(parent.getPath());
         for (String itemPath : this.items.keySet()) {
-            if (pattern.matcher(itemPath).matches()) {
+            if (itemPath.startsWith(descendantPrefix)) {
                 pathsToRemove.add(itemPath);
             }
         }
@@ -201,7 +199,7 @@ class MockSession implements Session {
 
     @Override
     public boolean itemExists(final String absPath) throws RepositoryException {
-        return this.items.get(absPath) != null;
+        return getItemData(absPath) != null;
     }
 
     @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 ef1bed3..b666f5f 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
@@ -53,12 +53,15 @@ public class MockNodeTest {
 
     @Test
     public void testGetNodes() throws RepositoryException {
+        final Node node111 = this.node11.addNode("node111");
+
         NodeIterator nodes = this.node1.getNodes();
         assertEquals(1, nodes.getSize());
         assertEquals(this.node11, nodes.nextNode());
 
         assertTrue(this.node1.hasNodes());
-        assertFalse(this.node11.hasNodes());
+        assertTrue(this.node11.hasNodes());
+        assertFalse(node111.hasNodes());
 
         nodes = this.node1.getNodes("^node.*$");
         assertEquals(1, nodes.getSize());
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 c137894..99767dc 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
@@ -38,6 +38,7 @@ import org.junit.Before;
 import org.junit.Test;
 
 import com.google.common.collect.ImmutableSet;
+import org.mockito.Mockito;
 
 public class MockSessionTest {
 
@@ -231,4 +232,20 @@ public class MockSessionTest {
         this.session.checkPermission("/any/path", "anyActions");
     }
 
+    @Test
+    public void testPathsAreNormalized() throws RepositoryException {
+        // 3.4.6 Passing Paths
+        // When a JCR path is passed as an argument to a JCR method it may be normalized
+        // or non-normalized and in standard or non-standard form.
+
+        this.session.getRootNode().addNode("foo");
+        assertTrue("Requesting node /foo/ should succeed", this.session.nodeExists("/foo/"));
+        assertTrue("Requesting item /foo/ should succeed", this.session.itemExists("/foo/"));
+
+        this.session.getRootNode().addNode("bar/");
+        assertTrue("Creating /bar/ should succeed", this.session.nodeExists("/bar"));
+
+        this.session.removeItem("/foo/");
+        assertFalse("Removing /foo/ should succeed", this.session.nodeExists("/foo"));
+    }
 }

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