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

reschke 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 809ba3b3 SLING-12884: ResourceResolver: refactor mocking in 
AliasMapEntriesTest (#189)
809ba3b3 is described below

commit 809ba3b3e37be14c323329152b51868c1fc8f3b0
Author: Julian Reschke <[email protected]>
AuthorDate: Thu Aug 14 14:43:19 2025 +0200

    SLING-12884: ResourceResolver: refactor mocking in AliasMapEntriesTest 
(#189)
---
 .../impl/mapping/AliasMapEntriesTest.java          | 401 ++++++++-------------
 1 file changed, 147 insertions(+), 254 deletions(-)

diff --git 
a/src/test/java/org/apache/sling/resourceresolver/impl/mapping/AliasMapEntriesTest.java
 
b/src/test/java/org/apache/sling/resourceresolver/impl/mapping/AliasMapEntriesTest.java
index 6923a1c0..3454c201 100644
--- 
a/src/test/java/org/apache/sling/resourceresolver/impl/mapping/AliasMapEntriesTest.java
+++ 
b/src/test/java/org/apache/sling/resourceresolver/impl/mapping/AliasMapEntriesTest.java
@@ -41,6 +41,7 @@ import org.apache.sling.api.resource.QuerySyntaxException;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceUtil;
+import org.apache.sling.api.resource.ValueMap;
 import org.apache.sling.api.resource.path.Path;
 import org.apache.sling.resourceresolver.impl.ResourceResolverImpl;
 import org.apache.sling.resourceresolver.impl.ResourceResolverMetrics;
@@ -284,24 +285,13 @@ public class AliasMapEntriesTest extends 
AbstractMappingMapEntriesTest {
             boolean queryAlwaysThrows,
             boolean pagedQueryThrows,
             String... aliases) {
-        Resource parent = mock(Resource.class);
-        Resource result = mock(Resource.class);
-        Resource content = mock(Resource.class);
 
-        when(parent.getChildren()).thenReturn(Set.of(result));
-        when(result.getParent()).thenReturn(null); // should be root
-        when(parent.getPath()).thenReturn("/parent");
-        when(parent.getName()).thenReturn("parent");
+        Resource parent = createMockedResource("/parent");
+        Resource result = createMockedResource(parent, "child");
+        Resource content = createMockedResource(result, "jcr:content");
 
-        when(result.getChildren()).thenReturn(Set.of(content));
         when(result.getParent()).thenReturn(withNullParent && !onJcrContent ? 
null : parent);
-        when(result.getPath()).thenReturn("/parent/child");
-        when(result.getName()).thenReturn("child");
-
-        when(content.getChildren()).thenReturn(Set.of());
         when(content.getParent()).thenReturn(withNullParent && onJcrContent ? 
null : result);
-        when(content.getPath()).thenReturn("/parent/child/jcr:content");
-        when(content.getName()).thenReturn("jcr:content");
 
         Resource aliasResource = onJcrContent ? content : result;
 
@@ -342,19 +332,12 @@ public class AliasMapEntriesTest extends 
AbstractMappingMapEntriesTest {
 
     @Test
     public void test_that_duplicate_alias_does_not_replace_first_alias() {
-        Resource parent = mock(Resource.class);
-        when(parent.getPath()).thenReturn("/parent");
+        Resource parent = createMockedResource("/parent");
+        Resource result = createMockedResource(parent, "child");
 
-        final Resource result = mock(Resource.class);
-        when(result.getParent()).thenReturn(parent);
-        when(result.getPath()).thenReturn("/parent/child");
-        when(result.getName()).thenReturn("child");
         
when(result.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS,
 "alias"));
 
-        final Resource secondResult = mock(Resource.class);
-        when(secondResult.getParent()).thenReturn(parent);
-        when(secondResult.getPath()).thenReturn("/parent/child2");
-        when(secondResult.getName()).thenReturn("child2");
+        Resource secondResult = createMockedResource(parent, "child2");
         
when(secondResult.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS,
 "alias"));
 
         when(resourceResolver.findResources(anyString(), eq("JCR-SQL2")))
@@ -378,32 +361,11 @@ public class AliasMapEntriesTest extends 
AbstractMappingMapEntriesTest {
 
     // checks that alias lists for "x" and "x/jcr:content" are merged
     private void internal_test_alias_on_parent_and_on_content_child(boolean 
cached) {
-        String parentPath = "/parent";
-        Resource parent = mock(Resource.class, "mock for " + parentPath);
-        when(resourceResolver.getResource(parentPath)).thenReturn(parent);
-
-        String nodePath = "/parent/node";
-        Resource node = mock(Resource.class, "mock for " + nodePath);
-        when(resourceResolver.getResource(nodePath)).thenReturn(node);
-
-        String contentPath = "/parent/node/jcr:content";
-        Resource content = mock(Resource.class, "mock for " + contentPath);
-        when(resourceResolver.getResource(contentPath)).thenReturn(content);
-
-        when(parent.getPath()).thenReturn(parentPath);
-        when(parent.getName()).thenReturn(ResourceUtil.getName(parentPath));
-        when(parent.getChildren()).thenReturn(List.of(node));
-
-        when(node.getParent()).thenReturn(parent);
-        when(node.getPath()).thenReturn(nodePath);
-        when(node.getName()).thenReturn(ResourceUtil.getName(nodePath));
-        when(node.getChildren()).thenReturn(List.of(content));
-        
when(node.getChild(ResourceUtil.getName(contentPath))).thenReturn(content);
-        
when(node.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS,
 "alias"));
+        Resource parent = createMockedResource("/parent");
+        Resource node = createMockedResource(parent, "node");
+        Resource content = createMockedResource(node, "jcr:content");
 
-        when(content.getParent()).thenReturn(node);
-        when(content.getPath()).thenReturn(contentPath);
-        when(content.getName()).thenReturn(ResourceUtil.getName(contentPath));
+        
when(node.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS,
 "alias"));
         
when(content.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS,
 "contentalias"));
 
         when(resourceResolver.findResources(anyString(), eq("JCR-SQL2")))
@@ -492,14 +454,9 @@ public class AliasMapEntriesTest extends 
AbstractMappingMapEntriesTest {
         mapEntries = new MapEntries(
                 resourceResolverFactory, bundleContext, eventAdmin, 
stringInterpolationProvider, metrics);
 
-        Resource parent = mock(Resource.class);
-        when(parent.getPath()).thenReturn("/parent");
+        Resource parent = createMockedResource("/parent");
+        Resource result = createMockedResource(parent, "child");
 
-        final Resource result = mock(Resource.class);
-        when(resourceResolver.getResource("/parent/child")).thenReturn(result);
-        when(result.getParent()).thenReturn(parent);
-        when(result.getPath()).thenReturn("/parent/child");
-        when(result.getName()).thenReturn("child");
         
when(result.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS,
 "alias"));
 
         addResource(mapEntries, "/parent/child", new AtomicBoolean());
@@ -515,20 +472,21 @@ public class AliasMapEntriesTest extends 
AbstractMappingMapEntriesTest {
         mapEntries = new MapEntries(
                 resourceResolverFactory, bundleContext, eventAdmin, 
stringInterpolationProvider, metrics);
 
-        Resource parent = mock(Resource.class);
-        when(parent.getPath()).thenReturn("/parent");
+        Resource parent = createMockedResource("/parent");
+        Resource result = createMockedResource(parent, "child");
 
-        final Resource result = mock(Resource.class);
-        when(resourceResolver.getResource("/parent/child")).thenReturn(result);
-        when(result.getParent()).thenReturn(parent);
-        when(result.getPath()).thenReturn("/parent/child");
-        when(result.getName()).thenReturn("child");
         
when(result.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS,
 "alias"));
 
+        Map<String, Collection<String>> aliasMapBefore = 
mapEntries.getAliasMap("/parent");
+        assertEquals(1, aliasMapBefore.size());
+
+        // this simulates an add event, but that is immaterial here as there 
is no cache anyway
         addResource(mapEntries, "/parent/child", new AtomicBoolean());
 
-        Map<String, Collection<String>> aliasMap = 
mapEntries.getAliasMap("/parent");
-        assertEquals(Collections.emptyMap(), aliasMap);
+        Map<String, Collection<String>> aliasMapAfter = 
mapEntries.getAliasMap("/parent");
+        assertEquals(1, aliasMapAfter.size());
+
+        assertEquals(aliasMapBefore, aliasMapAfter);
     }
 
     // SLING-3727
@@ -538,34 +496,30 @@ public class AliasMapEntriesTest extends 
AbstractMappingMapEntriesTest {
         mapEntries = new MapEntries(
                 resourceResolverFactory, bundleContext, eventAdmin, 
stringInterpolationProvider, metrics);
 
-        Resource parent = mock(Resource.class);
-        when(parent.getPath()).thenReturn("/parent");
+        Resource parent = createMockedResource("/parent");
+        Resource result = createMockedResource(parent, "child");
 
-        final Resource result = mock(Resource.class);
-        when(resourceResolver.getResource("/parent/child")).thenReturn(result);
-        when(result.getParent()).thenReturn(parent);
-        when(result.getPath()).thenReturn("/parent/child");
-        when(result.getName()).thenReturn("child");
         
when(result.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS,
 "alias"));
 
+        Map<String, Collection<String>> aliasMapBefore = 
mapEntries.getAliasMap("/parent");
+        assertEquals(1, aliasMapBefore.size());
+
+        // this simulates a remove event, but that is immaterial here as there 
is no cache anyway
         removeAlias(mapEntries, resourceResolver, "/parent", "/parent/child", 
NOOP);
 
-        Map<String, Collection<String>> aliasMap = 
mapEntries.getAliasMap("/parent");
-        assertEquals(Collections.emptyMap(), aliasMap);
+        Map<String, Collection<String>> aliasMapAfter = 
mapEntries.getAliasMap("/parent");
+        assertEquals(1, aliasMapAfter.size());
+
+        assertEquals(aliasMapBefore, aliasMapAfter);
     }
 
     @Test
     public void test_doAddAlias() throws Exception {
         assertEquals(0, aliasMap.size());
 
-        Resource parent = mock(Resource.class);
-        when(parent.getPath()).thenReturn("/parent");
+        Resource parent = createMockedResource("/parent");
+        Resource result = createMockedResource(parent, "child");
 
-        final Resource result = mock(Resource.class);
-        when(resourceResolver.getResource("/parent/child")).thenReturn(result);
-        when(result.getParent()).thenReturn(parent);
-        when(result.getPath()).thenReturn("/parent/child");
-        when(result.getName()).thenReturn("child");
         
when(result.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS,
 "alias"));
 
         addResource(mapEntries, "/parent/child", new AtomicBoolean());
@@ -578,11 +532,8 @@ public class AliasMapEntriesTest extends 
AbstractMappingMapEntriesTest {
         assertEquals(1, aliasMap.size());
 
         // test_that_duplicate_alias_does_not_replace_first_alias
-        final Resource secondResult = mock(Resource.class);
-        
when(resourceResolver.getResource("/parent/child2")).thenReturn(secondResult);
-        when(secondResult.getParent()).thenReturn(parent);
-        when(secondResult.getPath()).thenReturn("/parent/child2");
-        when(secondResult.getName()).thenReturn("child2");
+        Resource secondResult = createMockedResource(parent, "child2");
+
         
when(secondResult.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS,
 "alias"));
 
         addResource(mapEntries, "/parent/child2", new AtomicBoolean());
@@ -595,11 +546,8 @@ public class AliasMapEntriesTest extends 
AbstractMappingMapEntriesTest {
         assertEquals(1, aliasMap.size());
 
         // testing jcr:content node
-        final Resource jcrContentResult = mock(Resource.class);
-        
when(resourceResolver.getResource("/parent/child/jcr:content")).thenReturn(jcrContentResult);
-        when(jcrContentResult.getParent()).thenReturn(result);
-        
when(jcrContentResult.getPath()).thenReturn("/parent/child/jcr:content");
-        when(jcrContentResult.getName()).thenReturn("jcr:content");
+        Resource jcrContentResult = createMockedResource(result, 
"jcr:content");
+
         when(jcrContentResult.getValueMap())
                 .thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, 
"aliasJcrContent"));
 
@@ -618,14 +566,9 @@ public class AliasMapEntriesTest extends 
AbstractMappingMapEntriesTest {
     public void test_doAddAlias2() throws Exception {
         assertEquals(0, aliasMap.size());
 
-        Resource parent = mock(Resource.class);
-        when(parent.getPath()).thenReturn("/");
+        Resource parent = createMockedResource("/");
+        Resource result = createMockedResource(parent, "parent");
 
-        final Resource result = mock(Resource.class);
-        when(resourceResolver.getResource("/parent")).thenReturn(result);
-        when(result.getParent()).thenReturn(parent);
-        when(result.getPath()).thenReturn("/parent");
-        when(result.getName()).thenReturn("parent");
         
when(result.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS,
 "alias"));
 
         addResource(mapEntries, "/parent", new AtomicBoolean());
@@ -638,11 +581,8 @@ public class AliasMapEntriesTest extends 
AbstractMappingMapEntriesTest {
         assertEquals(1, aliasMap.size());
 
         // test_that_duplicate_alias_does_not_replace_first_alias
-        final Resource secondResult = mock(Resource.class);
-        
when(resourceResolver.getResource("/parent2")).thenReturn(secondResult);
-        when(secondResult.getParent()).thenReturn(parent);
-        when(secondResult.getPath()).thenReturn("/parent2");
-        when(secondResult.getName()).thenReturn("parent2");
+        Resource secondResult = createMockedResource(parent, "parent2");
+
         
when(secondResult.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS,
 "alias"));
 
         addResource(mapEntries, "/parent2", new AtomicBoolean());
@@ -655,11 +595,8 @@ public class AliasMapEntriesTest extends 
AbstractMappingMapEntriesTest {
         assertEquals(1, aliasMap.size());
 
         // testing jcr:content node
-        final Resource jcrContentResult = mock(Resource.class);
-        
when(resourceResolver.getResource("/parent/jcr:content")).thenReturn(jcrContentResult);
-        when(jcrContentResult.getParent()).thenReturn(result);
-        when(jcrContentResult.getPath()).thenReturn("/parent/jcr:content");
-        when(jcrContentResult.getName()).thenReturn("jcr:content");
+        Resource jcrContentResult = createMockedResource(result, 
"jcr:content");
+
         when(jcrContentResult.getValueMap())
                 .thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, 
"aliasJcrContent"));
 
@@ -674,11 +611,8 @@ public class AliasMapEntriesTest extends 
AbstractMappingMapEntriesTest {
         assertEquals(1, aliasMap.size());
 
         // trying to add invalid alias path
-        final Resource invalidResourcePath = mock(Resource.class);
-        
when(resourceResolver.getResource("/notallowedparent")).thenReturn(invalidResourcePath);
-        when(invalidResourcePath.getParent()).thenReturn(parent);
-        when(invalidResourcePath.getPath()).thenReturn("/notallowedparent");
-        when(invalidResourcePath.getName()).thenReturn("notallowedparent");
+        Resource invalidResourcePath = createMockedResource(parent, 
"notallowedparent");
+
         
when(invalidResourcePath.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS,
 "alias"));
 
         addResource(mapEntries, "/notallowedparent", new AtomicBoolean());
@@ -694,14 +628,9 @@ public class AliasMapEntriesTest extends 
AbstractMappingMapEntriesTest {
     public void test_doUpdateAlias() throws Exception {
         assertEquals(0, aliasMap.size());
 
-        Resource parent = mock(Resource.class);
-        when(parent.getPath()).thenReturn("/parent");
+        Resource parent = createMockedResource("/parent");
+        Resource result = createMockedResource(parent, "child");
 
-        final Resource result = mock(Resource.class);
-        when(resourceResolver.getResource("/parent/child")).thenReturn(result);
-        when(result.getParent()).thenReturn(parent);
-        when(result.getPath()).thenReturn("/parent/child");
-        when(result.getName()).thenReturn("child");
         
when(result.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS,
 "alias"));
 
         updateResource(mapEntries, "/parent/child", new AtomicBoolean());
@@ -725,11 +654,8 @@ public class AliasMapEntriesTest extends 
AbstractMappingMapEntriesTest {
         assertEquals(1, aliasMap.size());
 
         // testing jcr:content node update
-        final Resource jcrContentResult = mock(Resource.class);
-        
when(resourceResolver.getResource("/parent/child/jcr:content")).thenReturn(jcrContentResult);
-        when(jcrContentResult.getParent()).thenReturn(result);
-        
when(jcrContentResult.getPath()).thenReturn("/parent/child/jcr:content");
-        when(jcrContentResult.getName()).thenReturn("jcr:content");
+        Resource jcrContentResult = createMockedResource(result, 
"jcr:content");
+
         when(jcrContentResult.getValueMap())
                 .thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, 
"aliasJcrContent"));
         when(result.getChild("jcr:content")).thenReturn(jcrContentResult);
@@ -766,11 +692,8 @@ public class AliasMapEntriesTest extends 
AbstractMappingMapEntriesTest {
         assertEquals(List.of("aliasUpdated", "aliasJcrContentUpdated"), 
aliasMapEntry.get("child"));
 
         // add another node with different alias and check that the update 
doesn't break anything (see also SLING-3728)
-        final Resource secondResult = mock(Resource.class);
-        
when(resourceResolver.getResource("/parent/child2")).thenReturn(secondResult);
-        when(secondResult.getParent()).thenReturn(parent);
-        when(secondResult.getPath()).thenReturn("/parent/child2");
-        when(secondResult.getName()).thenReturn("child2");
+        Resource secondResult = createMockedResource(parent, "child2");
+
         
when(secondResult.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS,
 "alias2"));
 
         updateResource(mapEntries, "/parent/child2", new AtomicBoolean());
@@ -811,14 +734,9 @@ public class AliasMapEntriesTest extends 
AbstractMappingMapEntriesTest {
         // check that alias map is empty
         assertEquals(0, aliasMap.size());
 
-        final Resource parent = mock(Resource.class);
-        when(parent.getPath()).thenReturn("/parent");
+        Resource parent = createMockedResource("/parent");
+        Resource child = createMockedResource(parent, "child");
 
-        final Resource child = mock(Resource.class);
-        when(resourceResolver.getResource("/parent/child")).thenReturn(child);
-        when(child.getParent()).thenReturn(parent);
-        when(child.getPath()).thenReturn("/parent/child");
-        when(child.getName()).thenReturn("child");
         
when(child.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS,
 "alias"));
 
         addResource(mapEntries, "/parent/child", new AtomicBoolean());
@@ -862,25 +780,16 @@ public class AliasMapEntriesTest extends 
AbstractMappingMapEntriesTest {
     public void test_doRemoveAlias2() throws Exception {
         assertEquals(0, aliasMap.size());
 
-        Resource parent = mock(Resource.class);
-        when(parent.getPath()).thenReturn("/parent");
+        Resource parent = createMockedResource("/parent");
+        Resource result = createMockedResource(parent, "child");
 
-        final Resource result = mock(Resource.class);
-        when(resourceResolver.getResource("/parent/child")).thenReturn(result);
-        when(result.getParent()).thenReturn(parent);
-        when(result.getPath()).thenReturn("/parent/child");
-        when(result.getName()).thenReturn("child");
         when(result.getValueMap()).thenReturn(buildValueMap());
 
         // testing jcr:content node removal
-        final Resource jcrContentResult = mock(Resource.class);
-        
when(resourceResolver.getResource("/parent/child/jcr:content")).thenReturn(jcrContentResult);
-        when(jcrContentResult.getParent()).thenReturn(result);
-        
when(jcrContentResult.getPath()).thenReturn("/parent/child/jcr:content");
-        when(jcrContentResult.getName()).thenReturn("jcr:content");
+        Resource jcrContentResult = createMockedResource(result, 
"jcr:content");
+
         when(jcrContentResult.getValueMap())
                 .thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, 
"aliasJcrContent"));
-        when(result.getChild("jcr:content")).thenReturn(jcrContentResult);
 
         addResource(mapEntries, "/parent/child/jcr:content", new 
AtomicBoolean());
 
@@ -925,26 +834,17 @@ public class AliasMapEntriesTest extends 
AbstractMappingMapEntriesTest {
     public void test_doRemoveAlias3() throws Exception {
         assertEquals(0, aliasMap.size());
 
-        final Resource parentRsrc = mock(Resource.class);
-        when(parentRsrc.getPath()).thenReturn("/parent");
+        Resource parentRsrc = createMockedResource("/parent");
+        Resource childRsrc = createMockedResource(parentRsrc, "child");
 
-        final Resource childRsrc = mock(Resource.class);
-        
when(resourceResolver.getResource("/parent/child")).thenReturn(childRsrc);
-        when(childRsrc.getParent()).thenReturn(parentRsrc);
-        when(childRsrc.getPath()).thenReturn("/parent/child");
-        when(childRsrc.getName()).thenReturn("child");
         
when(childRsrc.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS,
 "alias"));
 
         addResource(mapEntries, "/parent/child", new AtomicBoolean());
 
-        final Resource jcrContentResult = mock(Resource.class);
-        
when(resourceResolver.getResource("/parent/child/jcr:content")).thenReturn(jcrContentResult);
-        when(jcrContentResult.getParent()).thenReturn(childRsrc);
-        
when(jcrContentResult.getPath()).thenReturn("/parent/child/jcr:content");
-        when(jcrContentResult.getName()).thenReturn("jcr:content");
+        Resource jcrContentResult = createMockedResource(childRsrc, 
"jcr:content");
+
         when(jcrContentResult.getValueMap())
                 .thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, 
"aliasJcrContent"));
-        when(childRsrc.getChild("jcr:content")).thenReturn(jcrContentResult);
 
         addResource(mapEntries, "/parent/child/jcr:content", new 
AtomicBoolean());
 
@@ -1030,14 +930,9 @@ public class AliasMapEntriesTest extends 
AbstractMappingMapEntriesTest {
     public void test_doRemoveAlias4() throws Exception {
         assertEquals(0, aliasMap.size());
 
-        Resource parent = mock(Resource.class);
-        when(parent.getPath()).thenReturn("/");
+        Resource parent = createMockedResource("/");
+        Resource result = createMockedResource(parent, "parent");
 
-        final Resource result = mock(Resource.class);
-        when(resourceResolver.getResource("/parent")).thenReturn(result);
-        when(result.getParent()).thenReturn(parent);
-        when(result.getPath()).thenReturn("/parent");
-        when(result.getName()).thenReturn("parent");
         
when(result.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS,
 "alias"));
 
         addResource(mapEntries, "/parent", new AtomicBoolean());
@@ -1081,25 +976,16 @@ public class AliasMapEntriesTest extends 
AbstractMappingMapEntriesTest {
     public void test_doRemoveAlias5() throws Exception {
         assertEquals(0, aliasMap.size());
 
-        Resource parent = mock(Resource.class);
-        when(parent.getPath()).thenReturn("/");
+        Resource parent = createMockedResource("/");
+        Resource result = createMockedResource(parent, "parent");
 
-        final Resource result = mock(Resource.class);
-        when(resourceResolver.getResource("/parent")).thenReturn(result);
-        when(result.getParent()).thenReturn(parent);
-        when(result.getPath()).thenReturn("/parent");
-        when(result.getName()).thenReturn("parent");
         when(result.getValueMap()).thenReturn(buildValueMap());
 
         // testing jcr:content node removal
-        final Resource jcrContentResult = mock(Resource.class);
-        
when(resourceResolver.getResource("/parent/jcr:content")).thenReturn(jcrContentResult);
-        when(jcrContentResult.getParent()).thenReturn(result);
-        when(jcrContentResult.getPath()).thenReturn("/parent/jcr:content");
-        when(jcrContentResult.getName()).thenReturn("jcr:content");
+        Resource jcrContentResult = createMockedResource(result, 
"jcr:content");
+
         when(jcrContentResult.getValueMap())
                 .thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, 
"aliasJcrContent"));
-        when(result.getChild("jcr:content")).thenReturn(jcrContentResult);
 
         addResource(mapEntries, "/parent/jcr:content", new AtomicBoolean());
 
@@ -1125,54 +1011,28 @@ public class AliasMapEntriesTest extends 
AbstractMappingMapEntriesTest {
     public void test_doNotRemoveAliasWhenJCRContentDeletedInParentPath() 
throws Exception {
         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");
+        Resource parent = createMockedResource("/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");
+        Resource container = createMockedResource(parent, "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");
+        Resource jcrContent = createMockedResource(container, "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");
+        Resource childContainer = createMockedResource(container, 
"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");
+
+        Resource grandChild = createMockedResource(childContainer, 
"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");
+
+        Resource grandChildJcrContent = createMockedResource(grandChild, 
"jcr:content");
+
         
when(grandChildJcrContent.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS,
 "gc"));
-        
when(grandChild.getChild("jcr:content")).thenReturn(grandChildJcrContent);
 
         addResource(mapEntries, grandChildJcrContent.getPath(), new 
AtomicBoolean());
 
@@ -1198,29 +1058,17 @@ public class AliasMapEntriesTest extends 
AbstractMappingMapEntriesTest {
     public void test_doRemoveAliasFromSibling() throws Exception {
         assertEquals(0, aliasMap.size());
 
-        Resource parent = mock(Resource.class);
-        when(parent.getPath()).thenReturn("/parent");
+        Resource parent = createMockedResource("/parent");
 
-        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 child1 = mock(Resource.class);
-        
when(resourceResolver.getResource("/parent/child1")).thenReturn(child1);
-        when(child1.getParent()).thenReturn(parent);
-        when(child1.getPath()).thenReturn("/parent/child1");
-        when(child1.getName()).thenReturn("child1");
+        Resource child1 = createMockedResource(parent, "child1");
+
         when(child1.getValueMap()).thenReturn(buildValueMap());
 
-        final Resource child1JcrContent = mock(Resource.class);
-        
when(resourceResolver.getResource("/parent/child1/jcr:content")).thenReturn(child1JcrContent);
-        when(child1JcrContent.getParent()).thenReturn(child1);
-        
when(child1JcrContent.getPath()).thenReturn("/parent/child1/jcr:content");
-        when(child1JcrContent.getName()).thenReturn("jcr:content");
+        Resource child1JcrContent = createMockedResource(child1, 
"jcr:content");
+
         
when(child1JcrContent.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS,
 "test1"));
-        when(child1.getChild("jcr:content")).thenReturn(child1JcrContent);
 
         when(parent.getChild("child1")).thenReturn(child1);
 
@@ -1233,18 +1081,12 @@ public class AliasMapEntriesTest extends 
AbstractMappingMapEntriesTest {
 
         assertEquals(1, aliasMap.size());
 
-        final Resource child2 = mock(Resource.class);
-        
when(resourceResolver.getResource("/parent/child2")).thenReturn(child2);
-        when(child2.getParent()).thenReturn(parent);
-        when(child2.getPath()).thenReturn("/parent/child2");
-        when(child2.getName()).thenReturn("child2");
+        Resource child2 = createMockedResource(parent, "child2");
+
         when(child2.getValueMap()).thenReturn(buildValueMap());
 
-        final Resource child2JcrContent = mock(Resource.class);
-        
when(resourceResolver.getResource("/parent/child2/jcr:content")).thenReturn(child2JcrContent);
-        when(child2JcrContent.getParent()).thenReturn(child2);
-        
when(child2JcrContent.getPath()).thenReturn("/parent/child2/jcr:content");
-        when(child2JcrContent.getName()).thenReturn("jcr:content");
+        Resource child2JcrContent = createMockedResource(child2, 
"jcr:content");
+
         
when(child2JcrContent.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS,
 "test2"));
         when(child2.getChild("jcr:content")).thenReturn(child2JcrContent);
 
@@ -1262,11 +1104,8 @@ public class AliasMapEntriesTest extends 
AbstractMappingMapEntriesTest {
         assertEquals(1, aliasMap.size());
         assertEquals(2, mapEntries.getAliasMap("/parent").size());
 
-        final Resource child2JcrContentChild = mock(Resource.class);
-        
when(resourceResolver.getResource("/parent/child2/jcr:content/test")).thenReturn(child2JcrContentChild);
-        when(child2JcrContentChild.getParent()).thenReturn(child2);
-        
when(child2JcrContentChild.getPath()).thenReturn("/parent/child2/jcr:content/test");
-        when(child2JcrContentChild.getName()).thenReturn("test");
+        Resource child2JcrContentChild = createMockedResource(child2, "test");
+
         
when(child2JcrContent.getChild("test")).thenReturn(child2JcrContentChild);
 
         removeResource(mapEntries, child2JcrContentChild.getPath(), new 
AtomicBoolean());
@@ -1365,4 +1204,58 @@ public class AliasMapEntriesTest extends 
AbstractMappingMapEntriesTest {
     private boolean matchesPagedQuery(String query) {
         return AQ_PAGED_PATTERN.matcher(query).matches();
     }
+
+    private Resource createMockedResource(Resource parent, String name) {
+
+        String path =
+                ResourceUtil.normalize(parent.getPath() + 
(parent.getPath().equals("/") ? "" : "/") + name);
+        Resource result = mock(Resource.class, "mock for " + path);
+
+        // the basics
+        when(result.getName()).thenReturn(ResourceUtil.getName(path));
+        when(result.getPath()).thenReturn(path);
+
+        // need to be specified later
+        when(result.getValueMap()).thenReturn(ValueMap.EMPTY);
+
+        // attach to resource resolver
+        when(resourceResolver.getResource(path)).thenReturn(result);
+
+        attachChildResource(parent, result);
+
+        return result;
+    }
+
+    private Resource createMockedResource(String path) {
+        Resource result = mock(Resource.class, "mock for " + path);
+
+        // the basics
+        when(result.getName()).thenReturn(ResourceUtil.getName(path));
+        when(result.getPath()).thenReturn(path);
+
+        // need to be attached later
+        when(result.getChildren()).thenReturn(Set.of());
+        when(result.getChild(anyString())).thenReturn(null);
+        when(result.getParent()).thenReturn(null);
+
+        // need to be specified later
+        when(result.getValueMap()).thenReturn(ValueMap.EMPTY);
+
+        // attach to resource resolver
+        when(resourceResolver.getResource(path)).thenReturn(result);
+
+        return result;
+    }
+
+    private void attachChildResource(Resource parent, Resource child) {
+
+        Set<Resource> newChildren = new HashSet<>();
+        parent.getChildren().forEach(newChildren::add);
+        newChildren.add(child);
+
+        when(parent.getChildren()).thenReturn(newChildren);
+        when(parent.getChild(child.getName())).thenReturn(child);
+
+        when(child.getParent()).thenReturn(parent);
+    }
 }


Reply via email to