This is an automated email from the ASF dual-hosted git repository. reschke pushed a commit to branch SLING-12894 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resourceresolver.git
The following commit(s) were added to refs/heads/SLING-12894 by this push: new 35742fb2 SLING-12896: alias refactoring - clean up vanity path tests (#192) 35742fb2 is described below commit 35742fb2166aa475c4f92db3714b837791b50d42 Author: Julian Reschke <julian.resc...@gmx.de> AuthorDate: Wed Aug 20 18:40:31 2025 +0200 SLING-12896: alias refactoring - clean up vanity path tests (#192) --- .../impl/mapping/VanityPathMapEntriesTest.java | 286 ++++++++------------- 1 file changed, 103 insertions(+), 183 deletions(-) diff --git a/src/test/java/org/apache/sling/resourceresolver/impl/mapping/VanityPathMapEntriesTest.java b/src/test/java/org/apache/sling/resourceresolver/impl/mapping/VanityPathMapEntriesTest.java index 102a41ee..08b52eec 100644 --- a/src/test/java/org/apache/sling/resourceresolver/impl/mapping/VanityPathMapEntriesTest.java +++ b/src/test/java/org/apache/sling/resourceresolver/impl/mapping/VanityPathMapEntriesTest.java @@ -43,6 +43,8 @@ import java.util.regex.Pattern; 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.observation.ResourceChange; import org.apache.sling.api.resource.observation.ResourceChange.ChangeType; import org.apache.sling.api.resource.path.Path; @@ -287,34 +289,19 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { String additionalChildName, String vanityPath) { - final Resource parent = mock(Resource.class); - - when(parent.getParent()).thenReturn(null); - when(parent.getPath()).thenReturn("/" + containerName); - when(parent.getName()).thenReturn(containerName); - - final Resource vanity = mock(Resource.class); + Resource parent = createMockedResource("/" + containerName); + Resource vanity = createMockedResource("/" + containerName + "/" + childName); when(vanity.getParent()).thenReturn(withNullParent && !onJcrContent ? null : parent); - when(vanity.getPath()).thenReturn("/" + containerName + "/" + childName); - when(vanity.getName()).thenReturn(childName); - - final Resource content = mock(Resource.class); + Resource content = createMockedResource("/" + containerName + "/" + childName + "/jcr:content"); when(content.getParent()).thenReturn(withNullParent && onJcrContent ? null : vanity); - when(content.getPath()).thenReturn("/" + containerName + "/" + childName + "/jcr:content"); - when(content.getName()).thenReturn("jcr:content"); - - final Resource oneMore = mock(Resource.class); - - when(oneMore.getParent()).thenReturn(parent); - when(oneMore.getPath()).thenReturn("/" + containerName + "/" + additionalChildName); - when(oneMore.getName()).thenReturn(additionalChildName); + Resource oneMore = createMockedResource(parent, additionalChildName); when(oneMore.getValueMap()) .thenReturn(buildValueMap(VanityPathHandler.PROP_VANITY_PATH, vanityPath + "/onemore")); - final Resource vanityPropHolder = onJcrContent ? content : vanity; + Resource vanityPropHolder = onJcrContent ? content : vanity; when(vanityPropHolder.getValueMap()).thenReturn(buildValueMap(VanityPathHandler.PROP_VANITY_PATH, vanityPath)); @@ -337,30 +324,22 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { when(resourceResolverFactory.getDefaultVanityPathRedirectStatus()).thenReturn(DEFAULT_VANITY_STATUS); - final List<Resource> resources = new ArrayList<>(); + List<Resource> resources = new ArrayList<>(); - Resource justVanityPath = mock(Resource.class, "justVanityPath"); - when(justVanityPath.getPath()).thenReturn("/justVanityPath"); - when(justVanityPath.getName()).thenReturn("justVanityPath"); + Resource justVanityPath = createMockedResource("/justVanityPath"); when(justVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/target/justVanityPath")); resources.add(justVanityPath); - Resource badVanityPath = mock(Resource.class, "badVanityPath"); - when(badVanityPath.getPath()).thenReturn("/badVanityPath"); - when(badVanityPath.getName()).thenReturn("badVanityPath"); + Resource badVanityPath = createMockedResource("/badVanityPath"); when(badVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/content/mypage/en-us-{132")); resources.add(badVanityPath); - Resource redirectingVanityPath = mock(Resource.class, "redirectingVanityPath"); - when(redirectingVanityPath.getPath()).thenReturn("/redirectingVanityPath"); - when(redirectingVanityPath.getName()).thenReturn("redirectingVanityPath"); + Resource redirectingVanityPath = createMockedResource("/redirectingVanityPath"); when(redirectingVanityPath.getValueMap()) .thenReturn(buildValueMap("sling:vanityPath", "/target/redirectingVanityPath", "sling:redirect", true)); resources.add(redirectingVanityPath); - Resource redirectingVanityPath301 = mock(Resource.class, "redirectingVanityPath301"); - when(redirectingVanityPath301.getPath()).thenReturn("/redirectingVanityPath301"); - when(redirectingVanityPath301.getName()).thenReturn("redirectingVanityPath301"); + Resource redirectingVanityPath301 = createMockedResource("/redirectingVanityPath301"); when(redirectingVanityPath301.getValueMap()) .thenReturn(buildValueMap( "sling:vanityPath", @@ -371,14 +350,8 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { 301)); resources.add(redirectingVanityPath301); - Resource vanityPathOnJcrContentParent = mock(Resource.class, "vanityPathOnJcrContentParent"); - when(vanityPathOnJcrContentParent.getPath()).thenReturn("/vanityPathOnJcrContent"); - when(vanityPathOnJcrContentParent.getName()).thenReturn("vanityPathOnJcrContent"); - - Resource vanityPathOnJcrContent = mock(Resource.class, "vanityPathOnJcrContent"); - when(vanityPathOnJcrContent.getPath()).thenReturn("/vanityPathOnJcrContent/jcr:content"); - when(vanityPathOnJcrContent.getName()).thenReturn("jcr:content"); - when(vanityPathOnJcrContent.getParent()).thenReturn(vanityPathOnJcrContentParent); + Resource vanityPathOnJcrContentParent = createMockedResource("/vanityPathOnJcrContent"); + Resource vanityPathOnJcrContent = createMockedResource(vanityPathOnJcrContentParent, "jcr:content"); when(vanityPathOnJcrContent.getValueMap()) .thenReturn(buildValueMap("sling:vanityPath", "/target/vanityPathOnJcrContent")); resources.add(vanityPathOnJcrContent); @@ -425,19 +398,11 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { @Test public void test_vanity_path_updates() { - Resource parent = mock(Resource.class, "parent"); - when(parent.getPath()).thenReturn("/foo/parent"); - when(parent.getName()).thenReturn("parent"); + Resource parent = createMockedResource("/foo/parent"); when(parent.getValueMap()).thenReturn(new ValueMapDecorator(Collections.emptyMap())); - when(resourceResolver.getResource(parent.getPath())).thenReturn(parent); - Resource child = mock(Resource.class, "jcrcontent"); - when(child.getPath()).thenReturn("/foo/parent/jcr:content"); - when(child.getName()).thenReturn("jcr:content"); + Resource child = createMockedResource(parent, "jcr:content"); when(child.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/target/found")); - when(child.getParent()).thenReturn(parent); - when(parent.getChild(child.getName())).thenReturn(child); - when(resourceResolver.getResource(child.getPath())).thenReturn(child); when(resourceResolver.findResources(anyString(), eq("JCR-SQL2"))) .thenAnswer((Answer<Iterator<Resource>>) invocation -> Collections.emptyIterator()); @@ -491,27 +456,14 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { @Test public void test_vanity_path_updates_do_not_reload_multiple_times() { - Resource parent = mock(Resource.class, "parent"); - when(parent.getPath()).thenReturn("/foo/parent"); - when(parent.getName()).thenReturn("parent"); + Resource parent = createMockedResource("/foo/parent"); when(parent.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/target/found1")); - when(resourceResolver.getResource(parent.getPath())).thenReturn(parent); - Resource child = mock(Resource.class, "jcr:content"); - when(child.getPath()).thenReturn("/foo/parent/jcr:content"); - when(child.getName()).thenReturn("jcr:content"); + Resource child = createMockedResource(parent, "/foo/parent/jcr:content"); when(child.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/target/found2")); - when(child.getParent()).thenReturn(parent); - when(parent.getChild(child.getName())).thenReturn(child); - when(resourceResolver.getResource(child.getPath())).thenReturn(child); - Resource child2 = mock(Resource.class, "child2"); - when(child2.getPath()).thenReturn("/foo/parent/child2"); - when(child2.getName()).thenReturn("child2"); + Resource child2 = createMockedResource(parent, "child2"); when(child2.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/target/found3")); - when(child2.getParent()).thenReturn(parent); - when(parent.getChild(child2.getName())).thenReturn(child2); - when(resourceResolver.getResource(child2.getPath())).thenReturn(child2); when(resourceResolver.findResources(anyString(), eq("JCR-SQL2"))) .thenAnswer((Answer<Iterator<Resource>>) invocation -> Collections.emptyIterator()); @@ -620,10 +572,7 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { assertEquals(0, entries.size()); assertEquals(0, getVanityTargets(mapEntries).size()); - Resource justVanityPath = mock(Resource.class, "justVanityPath"); - when(resourceResolver.getResource("/justVanityPath")).thenReturn(justVanityPath); - when(justVanityPath.getPath()).thenReturn("/justVanityPath"); - when(justVanityPath.getName()).thenReturn("justVanityPath"); + Resource justVanityPath = createMockedResource("/justVanityPath"); when(justVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/target/justVanityPath")); addResource(mapEntries, "/justVanityPath", new AtomicBoolean()); @@ -634,10 +583,7 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { assertEquals(2, getVanityCounter(mapEntries).longValue()); // bad vanity - Resource badVanityPath = mock(Resource.class, "badVanityPath"); - when(resourceResolver.getResource("/badVanityPath")).thenReturn(badVanityPath); - when(badVanityPath.getPath()).thenReturn("/badVanityPath"); - when(badVanityPath.getName()).thenReturn("badVanityPath"); + Resource badVanityPath = createMockedResource("/badVanityPath"); when(badVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/content/mypage/en-us-{132")); addResource(mapEntries, "/badVanityPath", new AtomicBoolean()); @@ -646,16 +592,9 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { assertEquals(1, getVanityTargets(mapEntries).size()); // vanity under jcr:content - Resource vanityPathOnJcrContentParent = mock(Resource.class, "vanityPathOnJcrContentParent"); - when(vanityPathOnJcrContentParent.getPath()).thenReturn("/vanityPathOnJcrContent"); - when(vanityPathOnJcrContentParent.getName()).thenReturn("vanityPathOnJcrContent"); - - Resource vanityPathOnJcrContent = mock(Resource.class, "vanityPathOnJcrContent"); - when(resourceResolver.getResource("/vanityPathOnJcrContent/jcr:content")) - .thenReturn(vanityPathOnJcrContent); - when(vanityPathOnJcrContent.getPath()).thenReturn("/vanityPathOnJcrContent/jcr:content"); - when(vanityPathOnJcrContent.getName()).thenReturn("jcr:content"); - when(vanityPathOnJcrContent.getParent()).thenReturn(vanityPathOnJcrContentParent); + Resource vanityPathOnJcrContentParent = createMockedResource("/vanityPathOnJcrContent"); + + Resource vanityPathOnJcrContent = createMockedResource(vanityPathOnJcrContentParent, "jcr:content"); when(vanityPathOnJcrContent.getValueMap()) .thenReturn(buildValueMap("sling:vanityPath", "/target/vanityPathOnJcrContent")); @@ -681,10 +620,7 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { assertEquals(0, entries.size()); assertEquals(0, getVanityTargets(mapEntries).size()); - Resource justVanityPath = mock(Resource.class, "justVanityPath"); - when(resourceResolver.getResource("/justVanityPath")).thenReturn(justVanityPath); - when(justVanityPath.getPath()).thenReturn("/justVanityPath"); - when(justVanityPath.getName()).thenReturn("justVanityPath"); + Resource justVanityPath = createMockedResource("/justVanityPath"); when(justVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/target/justVanityPath")); addResource(mapEntries, "/justVanityPath", new AtomicBoolean()); @@ -695,10 +631,7 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { assertEquals(2, getVanityCounter(mapEntries).longValue()); // bad vanity - Resource badVanityPath = mock(Resource.class, "badVanityPath"); - when(resourceResolver.getResource("/badVanityPath")).thenReturn(badVanityPath); - when(badVanityPath.getPath()).thenReturn("/badVanityPath"); - when(badVanityPath.getName()).thenReturn("badVanityPath"); + Resource badVanityPath = createMockedResource("/badVanityPath"); when(badVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/content/mypage/en-us-{132")); addResource(mapEntries, "/badVanityPath", new AtomicBoolean()); @@ -708,16 +641,9 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { assertEquals(1, getVanityTargets(mapEntries).size()); // vanity under jcr:content - Resource vanityPathOnJcrContentParent = mock(Resource.class, "vanityPathOnJcrContentParent"); - when(vanityPathOnJcrContentParent.getPath()).thenReturn("/vanityPathOnJcrContent"); - when(vanityPathOnJcrContentParent.getName()).thenReturn("vanityPathOnJcrContent"); - - Resource vanityPathOnJcrContent = mock(Resource.class, "vanityPathOnJcrContent"); - when(resourceResolver.getResource("/vanityPathOnJcrContent/jcr:content")) - .thenReturn(vanityPathOnJcrContent); - when(vanityPathOnJcrContent.getPath()).thenReturn("/vanityPathOnJcrContent/jcr:content"); - when(vanityPathOnJcrContent.getName()).thenReturn("jcr:content"); - when(vanityPathOnJcrContent.getParent()).thenReturn(vanityPathOnJcrContentParent); + Resource vanityPathOnJcrContentParent = createMockedResource("/vanityPathOnJcrContent"); + + Resource vanityPathOnJcrContent = createMockedResource(vanityPathOnJcrContentParent, "jcr:content"); when(vanityPathOnJcrContent.getValueMap()) .thenReturn(buildValueMap("sling:vanityPath", "/target/vanityPathOnJcrContent")); @@ -747,10 +673,7 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { "updateResource", String.class, boolean.class, boolean.class, AtomicBoolean.class); updateResource.setAccessible(true); - Resource justVanityPath = mock(Resource.class, "justVanityPath"); - when(resourceResolver.getResource("/justVanityPath")).thenReturn(justVanityPath); - when(justVanityPath.getPath()).thenReturn("/justVanityPath"); - when(justVanityPath.getName()).thenReturn("justVanityPath"); + Resource justVanityPath = createMockedResource("/justVanityPath"); when(justVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/target/justVanityPath")); addResource(mapEntries, "/justVanityPath", new AtomicBoolean()); @@ -778,17 +701,10 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { vanityTargets.get("/justVanityPath").get(0)); // vanity under jcr:content - Resource vanityPathOnJcrContentParent = mock(Resource.class, "vanityPathOnJcrContentParent"); - when(vanityPathOnJcrContentParent.getPath()).thenReturn("/vanityPathOnJcrContent"); - when(vanityPathOnJcrContentParent.getName()).thenReturn("vanityPathOnJcrContent"); + Resource vanityPathOnJcrContentParent = createMockedResource("/vanityPathOnJcrContent"); when(vanityPathOnJcrContentParent.getValueMap()).thenReturn(buildValueMap()); - Resource vanityPathOnJcrContent = mock(Resource.class, "vanityPathOnJcrContent"); - when(resourceResolver.getResource("/vanityPathOnJcrContent/jcr:content")) - .thenReturn(vanityPathOnJcrContent); - when(vanityPathOnJcrContent.getPath()).thenReturn("/vanityPathOnJcrContent/jcr:content"); - when(vanityPathOnJcrContent.getName()).thenReturn("jcr:content"); - when(vanityPathOnJcrContent.getParent()).thenReturn(vanityPathOnJcrContentParent); + Resource vanityPathOnJcrContent = createMockedResource(vanityPathOnJcrContentParent, "jcr:content"); when(vanityPathOnJcrContent.getValueMap()) .thenReturn(buildValueMap("sling:vanityPath", "/target/vanityPathOnJcrContent")); @@ -829,10 +745,7 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { Method method1 = VanityPathHandler.class.getDeclaredMethod("doRemoveVanity", String.class); method1.setAccessible(true); - Resource justVanityPath = mock(Resource.class, "justVanityPath"); - when(resourceResolver.getResource("/justVanityPath")).thenReturn(justVanityPath); - when(justVanityPath.getPath()).thenReturn("/justVanityPath"); - when(justVanityPath.getName()).thenReturn("justVanityPath"); + Resource justVanityPath = createMockedResource("/justVanityPath"); when(justVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/target/justVanityPath")); addResource(mapEntries, "/justVanityPath", new AtomicBoolean()); @@ -855,16 +768,9 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { assertNull(resolveMapsMap.get("/target/justVanityPath")); // vanity under jcr:content - Resource vanityPathOnJcrContentParent = mock(Resource.class, "vanityPathOnJcrContentParent"); - when(vanityPathOnJcrContentParent.getPath()).thenReturn("/vanityPathOnJcrContent"); - when(vanityPathOnJcrContentParent.getName()).thenReturn("vanityPathOnJcrContent"); - - Resource vanityPathOnJcrContent = mock(Resource.class, "vanityPathOnJcrContent"); - when(resourceResolver.getResource("/vanityPathOnJcrContent/jcr:content")) - .thenReturn(vanityPathOnJcrContent); - when(vanityPathOnJcrContent.getPath()).thenReturn("/vanityPathOnJcrContent/jcr:content"); - when(vanityPathOnJcrContent.getName()).thenReturn("jcr:content"); - when(vanityPathOnJcrContent.getParent()).thenReturn(vanityPathOnJcrContentParent); + Resource vanityPathOnJcrContentParent = createMockedResource("/vanityPathOnJcrContent"); + + Resource vanityPathOnJcrContent = createMockedResource(vanityPathOnJcrContentParent, "jcr:content"); when(vanityPathOnJcrContent.getValueMap()) .thenReturn(buildValueMap("sling:vanityPath", "/target/vanityPathOnJcrContent")); @@ -975,10 +881,7 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { // SLING-4891 public void test_getVanityPaths_2() throws Exception { - final Resource justVanityPath = mock(Resource.class, "justVanityPath"); - when(resourceResolver.getResource("/justVanityPath")).thenReturn(justVanityPath); - when(justVanityPath.getPath()).thenReturn("/justVanityPath"); - when(justVanityPath.getName()).thenReturn("justVanityPath"); + Resource justVanityPath = createMockedResource("/justVanityPath"); when(justVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/target/justVanityPath")); when(resourceResolver.findResources(anyString(), eq("JCR-SQL2"))) @@ -999,10 +902,7 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { this.isMaxCachedVanityPathEntriesStartup ? 2 : 0, getVanityCounter(mapEntries).longValue()); - final Resource justVanityPath2 = mock(Resource.class, "justVanityPath2"); - when(resourceResolver.getResource("/justVanityPath2")).thenReturn(justVanityPath2); - when(justVanityPath2.getPath()).thenReturn("/justVanityPath2"); - when(justVanityPath2.getName()).thenReturn("justVanityPath2"); + Resource justVanityPath2 = createMockedResource("/justVanityPath2"); when(justVanityPath2.getValueMap()) .thenReturn(buildValueMap("sling:vanityPath", "/target/justVanityPath", "sling:vanityOrder", 100)); @@ -1027,10 +927,7 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { // SLING-4891 public void test_getVanityPaths_3() throws Exception { - final Resource justVanityPath = mock(Resource.class, "justVanityPath"); - when(resourceResolver.getResource("/justVanityPath")).thenReturn(justVanityPath); - when(justVanityPath.getPath()).thenReturn("/justVanityPath"); - when(justVanityPath.getName()).thenReturn("justVanityPath"); + Resource justVanityPath = createMockedResource("/justVanityPath"); when(justVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/target/justVanityPath")); when(resourceResolver.findResources(anyString(), eq("JCR-SQL2"))) @@ -1055,9 +952,7 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { // SLING-4891 public void test_getVanityPaths_4() throws Exception { - final Resource badVanityPath = mock(Resource.class, "badVanityPath"); - when(badVanityPath.getPath()).thenReturn("/badVanityPath"); - when(badVanityPath.getName()).thenReturn("badVanityPath"); + Resource badVanityPath = createMockedResource("/badVanityPath"); when(badVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/content/mypage/en-us-{132")); when(resourceResolver.findResources(anyString(), eq("JCR-SQL2"))) @@ -1082,10 +977,7 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { // SLING-4891 public void test_getVanityPaths_5() throws Exception { - final Resource justVanityPath = mock(Resource.class, "justVanityPath"); - when(resourceResolver.getResource("/justVanityPath")).thenReturn(justVanityPath); - when(justVanityPath.getPath()).thenReturn("/justVanityPath"); - when(justVanityPath.getName()).thenReturn("justVanityPath"); + Resource justVanityPath = createMockedResource("/justVanityPath"); when(justVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/target/justVanityPath")); when(resourceResolver.findResources(anyString(), eq("JCR-SQL2"))) @@ -1105,10 +997,7 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { assertEquals(2, getVanityCounter(mapEntries).longValue()); - final Resource justVanityPath2 = mock(Resource.class, "justVanityPath2"); - when(resourceResolver.getResource("/justVanityPath2")).thenReturn(justVanityPath2); - when(justVanityPath2.getPath()).thenReturn("/justVanityPath2"); - when(justVanityPath2.getName()).thenReturn("justVanityPath2"); + Resource justVanityPath2 = createMockedResource("/justVanityPath2"); when(justVanityPath2.getValueMap()) .thenReturn(buildValueMap("sling:vanityPath", "/target/justVanityPath", "sling:vanityOrder", 100)); @@ -1131,10 +1020,7 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { public void test_loadVanityPaths() throws Exception { when(this.resourceResolverFactory.getMaxCachedVanityPathEntries()).thenReturn(2L); - final Resource justVanityPath = mock(Resource.class, "justVanityPath"); - when(resourceResolver.getResource("/justVanityPath")).thenReturn(justVanityPath); - when(justVanityPath.getPath()).thenReturn("/justVanityPath"); - when(justVanityPath.getName()).thenReturn("justVanityPath"); + Resource justVanityPath = createMockedResource("/justVanityPath"); when(justVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/target/justVanityPath")); when(resourceResolver.findResources(anyString(), eq("JCR-SQL2"))) @@ -1156,10 +1042,7 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { // SLING-4891 public void test_loadVanityPaths_1() throws Exception { - final Resource justVanityPath = mock(Resource.class, "justVanityPath"); - when(resourceResolver.getResource("/justVanityPath")).thenReturn(justVanityPath); - when(justVanityPath.getPath()).thenReturn("/justVanityPath"); - when(justVanityPath.getName()).thenReturn("justVanityPath"); + Resource justVanityPath = createMockedResource("/justVanityPath"); when(justVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/target/justVanityPath")); when(resourceResolver.findResources(anyString(), eq("JCR-SQL2"))) @@ -1193,11 +1076,7 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { assertEquals(0, entries.size()); String vpName = "justVanityPath" + (withExtension ? ".txt" : ""); - final Resource justVanityPath = mock(Resource.class, vpName); - when(resourceResolver.getResource("/" + vpName)).thenReturn(justVanityPath); - when(justVanityPath.getPath()).thenReturn("/" + vpName); - when(justVanityPath.getName()).thenReturn(vpName); - + Resource justVanityPath = createMockedResource("/" + vpName); when(justVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/target/justVanityPath")); when(resourceResolver.findResources(anyString(), eq("JCR-SQL2"))) @@ -1244,10 +1123,7 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { public void test_concurrent_getResolveMapsIterator() throws Exception { ExecutorService pool = Executors.newFixedThreadPool(10); - final Resource justVanityPath = mock(Resource.class, "justVanityPath"); - when(resourceResolver.getResource("/justVanityPath")).thenReturn(justVanityPath); - when(justVanityPath.getPath()).thenReturn("/justVanityPath"); - when(justVanityPath.getName()).thenReturn("justVanityPath"); + Resource justVanityPath = createMockedResource("/justVanityPath"); when(justVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/target/justVanityPath")); when(resourceResolver.findResources(anyString(), eq("JCR-SQL2"))) @@ -1295,10 +1171,7 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { String targetPath = "/foo"; - final Resource simpleVanityResource = mock(Resource.class, "simpleVanityPath"); - when(resourceResolver.getResource("/simpleVanityPath")).thenReturn(simpleVanityResource); - when(simpleVanityResource.getPath()).thenReturn("/simpleVanityPath"); - when(simpleVanityResource.getName()).thenReturn("simpleVanityPath"); + Resource simpleVanityResource = createMockedResource("/simpleVanityPath"); when(simpleVanityResource.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", targetPath)); // exactly one resource found, both on regular (init) or specific query @@ -1379,18 +1252,11 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { // send a change event for a resource with vanity path; // this will be queued while init is running and then processed later on - Resource eventTest = mock(Resource.class, "eventTest"); - when(eventTest.getName()).thenReturn("eventTest"); - when(eventTest.getPath()).thenReturn("/eventTest"); + Resource eventTest = createMockedResource("/eventTest"); when(eventTest.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/baa")); // target for the resource got which we sent the event - Resource eventTestTarget = mock(Resource.class, "eventTestTarget"); - when(eventTestTarget.getName()).thenReturn("baa"); - when(eventTestTarget.getPath()).thenReturn("/baa"); - - when(resourceResolver.getResource(eventTest.getPath())).thenReturn(eventTest); - when(resourceResolver.getResource(eventTestTarget.getPath())).thenReturn(eventTestTarget); + createMockedResource("/baa"); mapEntries.onChange(List.of(new ResourceChange(ChangeType.ADDED, eventTest.getPath(), false))); @@ -1520,4 +1386,58 @@ public class VanityPathMapEntriesTest extends AbstractMappingMapEntriesTest { String s2 = getFirstVanityPath(o2); return s1.compareTo(s2); }; + + 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) { + + List<Resource> newChildren = new ArrayList<>(); + 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); + } }