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

rombert pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resourceresolver.git


The following commit(s) were added to refs/heads/master by this push:
     new ec02dd3  SLING-10476 Aliases in MapEntries are removed in delete event 
of jcr:content present in the parent directory
ec02dd3 is described below

commit ec02dd3afbc8391ba316f4ff2644244db71cb8d6
Author: Sagar Miglani <[email protected]>
AuthorDate: Thu Jun 17 15:41:51 2021 +0530

    SLING-10476 Aliases in MapEntries are removed in delete event of 
jcr:content present in the parent directory
---
 .../resourceresolver/impl/mapping/MapEntries.java  |  3 +-
 .../impl/mapping/MapEntriesTest.java               | 80 ++++++++++++++++++++++
 2 files changed, 82 insertions(+), 1 deletion(-)

diff --git 
a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java 
b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
index 000e62b..3047d30 100644
--- 
a/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
+++ 
b/src/main/java/org/apache/sling/resourceresolver/impl/mapping/MapEntries.java
@@ -317,9 +317,10 @@ public class MapEntries implements
             }
         }
         if (this.useOptimizeAliasResolution) {
+            final String pathPrefix = path + "/";
             for (final String contentPath : this.aliasMap.keySet()) {
                 if (path.startsWith(contentPath + "/") || 
path.equals(contentPath)
-                        || contentPath.startsWith(actualContentPathPrefix)) {
+                        || contentPath.startsWith(pathPrefix)) {
                     changed |= removeAlias(contentPath, path, 
resolverRefreshed);
                 }
             }
diff --git 
a/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntriesTest.java
 
b/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntriesTest.java
index 92310e4..cc2c670 100644
--- 
a/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntriesTest.java
+++ 
b/src/test/java/org/apache/sling/resourceresolver/impl/mapping/MapEntriesTest.java
@@ -1494,6 +1494,86 @@ public class MapEntriesTest extends 
AbstractMappingMapEntriesTest {
         assertEquals(0, aliasMap.size());
     }
 
+    // SLING-10476
+    @Test
+    public void test_doNotRemoveAliasWhenJCRContentDeletedInParentPath() 
throws Exception {
+        final Method addResource = 
MapEntries.class.getDeclaredMethod("addResource", String.class, 
AtomicBoolean.class);
+        addResource.setAccessible(true);
+
+        final Method removeResource = 
MapEntries.class.getDeclaredMethod("removeResource", String.class, 
AtomicBoolean.class);
+        removeResource.setAccessible(true);
+
+        assertEquals(0, aliasMap.size());
+
+        Resource parent = mock(Resource.class);
+        when(resourceResolver.getResource("/parent")).thenReturn(parent);
+        when(parent.getParent()).thenReturn(parent);
+        when(parent.getPath()).thenReturn("/parent");
+        when(parent.getName()).thenReturn("parent");
+        when(parent.getValueMap()).thenReturn(buildValueMap());
+
+        final Resource container = mock(Resource.class);
+        
when(resourceResolver.getResource("/parent/container")).thenReturn(container);
+        when(container.getParent()).thenReturn(parent);
+        when(container.getPath()).thenReturn("/parent/container");
+        when(container.getName()).thenReturn("container");
+        when(container.getValueMap()).thenReturn(buildValueMap());
+        when(parent.getChild("container")).thenReturn(container);
+
+        final Resource jcrContent = mock(Resource.class);
+        
when(resourceResolver.getResource("/parent/container/jcr:content")).thenReturn(jcrContent);
+        when(jcrContent.getParent()).thenReturn(container);
+        when(jcrContent.getPath()).thenReturn("/parent/container/jcr:content");
+        when(jcrContent.getName()).thenReturn("jcr:content");
+        when(jcrContent.getValueMap()).thenReturn(buildValueMap());
+        when(container.getChild("jcr:content")).thenReturn(jcrContent);
+
+        final Resource childContainer = mock(Resource.class);
+        
when(resourceResolver.getResource("/parent/container/childContainer")).thenReturn(childContainer);
+        when(childContainer.getParent()).thenReturn(container);
+        
when(childContainer.getPath()).thenReturn("/parent/container/childContainer");
+        when(childContainer.getName()).thenReturn("childContainer");
+        when(childContainer.getValueMap()).thenReturn(buildValueMap());
+        when(container.getChild("childContainer")).thenReturn(childContainer);
+
+        final Resource grandChild = mock(Resource.class);
+        
when(resourceResolver.getResource("/parent/container/childContainer/grandChild")).thenReturn(grandChild);
+        when(grandChild.getParent()).thenReturn(childContainer);
+        
when(grandChild.getPath()).thenReturn("/parent/container/childContainer/grandChild");
+        when(grandChild.getName()).thenReturn("grandChild");
+        when(grandChild.getValueMap()).thenReturn(buildValueMap());
+        when(childContainer.getChild("grandChild")).thenReturn(grandChild);
+
+        final Resource grandChildJcrContent = mock(Resource.class);
+        
when(resourceResolver.getResource("/parent/container/childContainer/grandChild/jcr:content")).thenReturn(grandChildJcrContent);
+        when(grandChildJcrContent.getParent()).thenReturn(grandChild);
+        
when(grandChildJcrContent.getPath()).thenReturn("/parent/container/childContainer/grandChild/jcr:content");
+        when(grandChildJcrContent.getName()).thenReturn("jcr:content");
+        
when(grandChildJcrContent.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS,
 "gc"));
+        
when(grandChild.getChild("jcr:content")).thenReturn(grandChildJcrContent);
+
+        addResource.invoke(mapEntries, grandChildJcrContent.getPath(), new 
AtomicBoolean());
+
+        Map<String, String> aliasMapEntry = 
mapEntries.getAliasMap("/parent/container/childContainer");
+        assertNotNull(aliasMapEntry);
+        assertEquals(1, aliasMapEntry.size());
+        assertTrue(aliasMapEntry.containsKey("gc"));
+        assertEquals("grandChild", aliasMapEntry.get("gc"));
+
+
+        // delete the jcr:content present in a parent path
+        when(container.getChild("jcr:content")).thenReturn(null);
+        removeResource.invoke(mapEntries, jcrContent.getPath(), new 
AtomicBoolean());
+
+        // Alias of the other resources under the same parent of deleted 
jcr:content, should not be deleted
+        aliasMapEntry = 
mapEntries.getAliasMap("/parent/container/childContainer");
+        assertNotNull(aliasMapEntry);
+        assertEquals(1, aliasMapEntry.size());
+        assertTrue(aliasMapEntry.containsKey("gc"));
+        assertEquals("grandChild", aliasMapEntry.get("gc"));
+
+    }
+
     @Test
     public void test_doRemoveAliasFromSibling() throws Exception {
         final Method addResource = 
MapEntries.class.getDeclaredMethod("addResource", String.class, 
AtomicBoolean.class);

Reply via email to