This is an automated email from the ASF dual-hosted git repository.
reschke pushed a commit to branch SLING-12648
in repository
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-resourceresolver.git
The following commit(s) were added to refs/heads/SLING-12648 by this push:
new b7f8234 merge master
b7f8234 is described below
commit b7f82341f521adad3370369f07ddff5715d0fcfe
Author: Julian Reschke <[email protected]>
AuthorDate: Tue Feb 4 18:12:29 2025 +0100
merge master
---
.../impl/mapping/VanityPathMapEntriesTest.java | 114 +++++++++------------
1 file changed, 50 insertions(+), 64 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 c70eb4d..0e64904 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
@@ -42,6 +42,7 @@ import org.osgi.service.event.EventAdmin;
import java.io.IOException;
import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
@@ -184,12 +185,38 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
return (AtomicLong) vanityCounter.get(mapEntries.vph);
}
+ @SuppressWarnings("unchecked")
private static Map<String, List<String>> getVanityTargets(MapEntries
mapEntries) throws NoSuchFieldException, IllegalAccessException {
Field field = MapEntries.class.getDeclaredField("vanityTargets");
field.setAccessible(true);
return (Map<String, List<String>>) field.get(mapEntries);
}
+ @SuppressWarnings("unchecked")
+ private static Map<String, List<MapEntry>> getResolveMapsMap(MapEntries
mapEntries) throws NoSuchFieldException, IllegalAccessException {
+ Field field = MapEntries.class.getDeclaredField("resolveMapsMap");
+ field.setAccessible(true);
+ return (Map<String, List<MapEntry>>) field.get(mapEntries);
+ }
+
+ private static void getVanityPaths(MapEntries mapEntries, String path)
throws IllegalAccessException, NoSuchMethodException, InvocationTargetException
{
+ Method method =
MapEntries.VanityPathHandler.class.getDeclaredMethod("getVanityPaths",
String.class);
+ method.setAccessible(true);
+ method.invoke(mapEntries.vph, path);
+ }
+
+ private static void addResource(MapEntries mapEntries, String path,
AtomicBoolean bool) throws IllegalAccessException, NoSuchMethodException,
InvocationTargetException {
+ Method method = MapEntries.class.getDeclaredMethod("addResource",
String.class, AtomicBoolean.class);
+ method.setAccessible(true);
+ method.invoke(mapEntries, path, bool);
+ }
+
+ private static void loadVanityPaths(MapEntries mapEntries,
ResourceResolver resourceResolver) throws IllegalAccessException,
NoSuchMethodException, InvocationTargetException {
+ Method method =
MapEntries.VanityPathHandler.class.getDeclaredMethod("loadVanityPaths",
ResourceResolver.class);
+ method.setAccessible(true);
+ method.invoke(mapEntries.vph, resourceResolver);
+ }
+
@Override
@After
public void tearDown() {
@@ -553,23 +580,19 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
assertEquals("/content", actualContent);
}
- @SuppressWarnings("unchecked")
@Test
public void test_doAddVanity() throws Exception {
List<MapEntry> entries = mapEntries.getResolveMaps();
assertEquals(0, entries.size());
assertEquals(0, getVanityTargets(mapEntries).size());
- final Method addResource =
MapEntries.class.getDeclaredMethod("addResource", String.class,
AtomicBoolean.class);
- addResource.setAccessible(true);
-
Resource justVanityPath = mock(Resource.class, "justVanityPath");
when(resourceResolver.getResource("/justVanityPath")).thenReturn(justVanityPath);
when(justVanityPath.getPath()).thenReturn("/justVanityPath");
when(justVanityPath.getName()).thenReturn("justVanityPath");
when(justVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath",
"/target/justVanityPath"));
- addResource.invoke(mapEntries, "/justVanityPath", new AtomicBoolean());
+ addResource(mapEntries, "/justVanityPath", new AtomicBoolean());
entries = mapEntries.getResolveMaps();
assertEquals(2, entries.size());
@@ -583,7 +606,7 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
when(badVanityPath.getName()).thenReturn("badVanityPath");
when(badVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath",
"/content/mypage/en-us-{132"));
- addResource.invoke(mapEntries, "/badVanityPath", new AtomicBoolean());
+ addResource(mapEntries, "/badVanityPath", new AtomicBoolean());
assertEquals(2, entries.size());
@@ -601,7 +624,7 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
when(vanityPathOnJcrContent.getParent()).thenReturn(vanityPathOnJcrContentParent);
when(vanityPathOnJcrContent.getValueMap()).thenReturn(buildValueMap("sling:vanityPath",
"/target/vanityPathOnJcrContent"));
- addResource.invoke(mapEntries, "/vanityPathOnJcrContent/jcr:content",
new AtomicBoolean());
+ addResource(mapEntries, "/vanityPathOnJcrContent/jcr:content", new
AtomicBoolean());
entries = mapEntries.getResolveMaps();
assertEquals(4, entries.size());
@@ -615,7 +638,6 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
assertNotNull(vanityTargets.get("/vanityPathOnJcrContent"));
}
- @SuppressWarnings("unchecked")
@Test
public void test_doAddVanity_1() throws Exception {
when(this.resourceResolverFactory.getMaxCachedVanityPathEntries()).thenReturn(10L);
@@ -624,16 +646,13 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
assertEquals(0, entries.size());
assertEquals(0, getVanityTargets(mapEntries).size());
- final Method addResource =
MapEntries.class.getDeclaredMethod("addResource", String.class,
AtomicBoolean.class);
- addResource.setAccessible(true);
-
Resource justVanityPath = mock(Resource.class, "justVanityPath");
when(resourceResolver.getResource("/justVanityPath")).thenReturn(justVanityPath);
when(justVanityPath.getPath()).thenReturn("/justVanityPath");
when(justVanityPath.getName()).thenReturn("justVanityPath");
when(justVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath",
"/target/justVanityPath"));
- addResource.invoke(mapEntries, "/justVanityPath", new AtomicBoolean());
+ addResource(mapEntries, "/justVanityPath", new AtomicBoolean());
entries = mapEntries.getResolveMaps();
assertEquals(2, entries.size());
@@ -647,8 +666,7 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
when(badVanityPath.getName()).thenReturn("badVanityPath");
when(badVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath",
"/content/mypage/en-us-{132"));
- addResource.invoke(mapEntries, "/badVanityPath", new AtomicBoolean());
-
+ addResource(mapEntries, "/badVanityPath", new AtomicBoolean());
assertEquals(2, entries.size());
@@ -666,7 +684,7 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
when(vanityPathOnJcrContent.getParent()).thenReturn(vanityPathOnJcrContentParent);
when(vanityPathOnJcrContent.getValueMap()).thenReturn(buildValueMap("sling:vanityPath",
"/target/vanityPathOnJcrContent"));
- addResource.invoke(mapEntries, "/vanityPathOnJcrContent/jcr:content",
new AtomicBoolean());
+ addResource(mapEntries, "/vanityPathOnJcrContent/jcr:content", new
AtomicBoolean());
entries = mapEntries.getResolveMaps();
assertEquals(4, entries.size());
@@ -680,21 +698,14 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
assertNotNull(vanityTargets.get("/vanityPathOnJcrContent"));
}
-
- @SuppressWarnings("unchecked")
@Test
public void test_doUpdateVanity() throws Exception {
- Field field0 = MapEntries.class.getDeclaredField("resolveMapsMap");
- field0.setAccessible(true);
- Map<String, List<MapEntry>> resolveMapsMap = (Map<String,
List<MapEntry>>) field0.get(mapEntries);
+ Map<String, List<MapEntry>> resolveMapsMap =
getResolveMapsMap(mapEntries);
assertEquals(1, resolveMapsMap.size());
Map<String, List<String>> vanityTargets = getVanityTargets(mapEntries);
assertEquals(0, vanityTargets.size());
- final Method addResource =
MapEntries.class.getDeclaredMethod("addResource", String.class,
AtomicBoolean.class);
- addResource.setAccessible(true);
-
final Method updateResource =
MapEntries.class.getDeclaredMethod("updateResource", String.class,
AtomicBoolean.class);
updateResource.setAccessible(true);
@@ -704,7 +715,7 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
when(justVanityPath.getName()).thenReturn("justVanityPath");
when(justVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath",
"/target/justVanityPath"));
- addResource.invoke(mapEntries, "/justVanityPath", new AtomicBoolean());
+ addResource(mapEntries, "/justVanityPath", new AtomicBoolean());
assertEquals(2, resolveMapsMap.size());
assertEquals(1, vanityTargets.size());
@@ -737,7 +748,7 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
when(vanityPathOnJcrContent.getParent()).thenReturn(vanityPathOnJcrContentParent);
when(vanityPathOnJcrContent.getValueMap()).thenReturn(buildValueMap("sling:vanityPath",
"/target/vanityPathOnJcrContent"));
- addResource.invoke(mapEntries, "/vanityPathOnJcrContent/jcr:content",
new AtomicBoolean());
+ addResource(mapEntries, "/vanityPathOnJcrContent/jcr:content", new
AtomicBoolean());
assertEquals(3, resolveMapsMap.size());
assertEquals(2, vanityTargets.size());
@@ -758,20 +769,14 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
assertEquals("/target/vanityPathOnJcrContentUpdated",
vanityTargets.get("/vanityPathOnJcrContent").get(0));
}
- @SuppressWarnings("unchecked")
@Test
public void test_doRemoveVanity() throws Exception {
- Field field0 = MapEntries.class.getDeclaredField("resolveMapsMap");
- field0.setAccessible(true);
- Map<String, List<MapEntry>> resolveMapsMap = (Map<String,
List<MapEntry>>) field0.get(mapEntries);
+ Map<String, List<MapEntry>> resolveMapsMap =
getResolveMapsMap(mapEntries);
assertEquals(1, resolveMapsMap.size());
Map<String, List<String>> vanityTargets = getVanityTargets(mapEntries);
assertEquals(0, vanityTargets.size());
- final Method addResource =
MapEntries.class.getDeclaredMethod("addResource", String.class,
AtomicBoolean.class);
- addResource.setAccessible(true);
-
Method method1 =
MapEntries.VanityPathHandler.class.getDeclaredMethod("doRemoveVanity",
String.class);
method1.setAccessible(true);
@@ -781,7 +786,7 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
when(justVanityPath.getName()).thenReturn("justVanityPath");
when(justVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath",
"/target/justVanityPath"));
- addResource.invoke(mapEntries, "/justVanityPath", new AtomicBoolean());
+ addResource(mapEntries, "/justVanityPath", new AtomicBoolean());
assertEquals(2, getVanityCounter(mapEntries).longValue());
assertEquals(2, resolveMapsMap.size());
@@ -811,7 +816,7 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
when(vanityPathOnJcrContent.getParent()).thenReturn(vanityPathOnJcrContentParent);
when(vanityPathOnJcrContent.getValueMap()).thenReturn(buildValueMap("sling:vanityPath",
"/target/vanityPathOnJcrContent"));
- addResource.invoke(mapEntries, "/vanityPathOnJcrContent/jcr:content",
new AtomicBoolean());
+ addResource(mapEntries, "/vanityPathOnJcrContent/jcr:content", new
AtomicBoolean());
assertEquals(2, resolveMapsMap.size());
assertEquals(1, vanityTargets.size());
@@ -831,14 +836,10 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
@SuppressWarnings("unchecked")
@Test
public void test_doUpdateVanityOrder() throws Exception {
- Field field0 = MapEntries.class.getDeclaredField("resolveMapsMap");
- field0.setAccessible(true);
- Map<String, List<MapEntry>> resolveMapsMap = (Map<String,
List<MapEntry>>) field0.get(mapEntries);
+ Map<String, List<MapEntry>> resolveMapsMap =
getResolveMapsMap(mapEntries);
assertEquals(1, resolveMapsMap.size());
- Field field = MapEntries.class.getDeclaredField("vanityTargets");
- field.setAccessible(true);
- Map<String, List<String>> vanityTargets = (Map<String, List<String>>)
field.get(mapEntries);
+ Map<String, List<String>> vanityTargets = getVanityTargets(mapEntries);
assertEquals(0, vanityTargets.size());
Method method = MapEntries.class.getDeclaredMethod("doAddVanity",
String.class);
@@ -912,9 +913,7 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
when(this.resourceResolverFactory.getMaxCachedVanityPathEntries()).thenReturn(0L);
- Method method =
MapEntries.VanityPathHandler.class.getDeclaredMethod("getVanityPaths",
String.class);
- method.setAccessible(true);
- method.invoke(mapEntries.vph, "/notExisting");
+ getVanityPaths(mapEntries, "/notExisting");
assertEquals(0, getVanityCounter(mapEntries).longValue());
}
@@ -939,9 +938,7 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
when(this.resourceResolverFactory.getMaxCachedVanityPathEntries()).thenReturn(0L);
- Method method =
MapEntries.VanityPathHandler.class.getDeclaredMethod("getVanityPaths",
String.class);
- method.setAccessible(true);
- method.invoke(mapEntries.vph, "/target/justVanityPath");
+ getVanityPaths(mapEntries, "/target/justVanityPath");
assertEquals(this.isMaxCachedVanityPathEntriesStartup ? 2 : 0,
getVanityCounter(mapEntries).longValue());
@@ -959,7 +956,7 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
}
});
- method.invoke(mapEntries.vph, "/target/justVanityPath");
+ getVanityPaths(mapEntries, "/target/justVanityPath");
assertEquals(this.isMaxCachedVanityPathEntriesStartup ? 4 : 0,
getVanityCounter(mapEntries).longValue());
}
@@ -974,7 +971,6 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
when(justVanityPath.getName()).thenReturn("justVanityPath");
when(justVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath",
"/target/justVanityPath"));
-
when(resourceResolver.findResources(anyString(),
eq("JCR-SQL2"))).thenAnswer((Answer<Iterator<Resource>>) invocation -> {
if
(invocation.getArguments()[0].toString().contains("sling:vanityPath")) {
return Collections.singleton(justVanityPath).iterator();
@@ -986,9 +982,7 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
when(this.resourceResolverFactory.getMaxCachedVanityPathEntries()).thenReturn(0L);
when(this.resourceResolverFactory.isMaxCachedVanityPathEntriesStartup()).thenReturn(false);
- Method method =
MapEntries.VanityPathHandler.class.getDeclaredMethod("getVanityPaths",
String.class);
- method.setAccessible(true);
- method.invoke(mapEntries.vph, "/target/justVanityPath");
+ getVanityPaths(mapEntries, "/target/justVanityPath");
assertEquals(0, getVanityCounter(mapEntries).longValue());
}
@@ -1014,9 +1008,7 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
when(this.resourceResolverFactory.getMaxCachedVanityPathEntries()).thenReturn(0L);
when(this.resourceResolverFactory.isMaxCachedVanityPathEntriesStartup()).thenReturn(true);
- Method method =
MapEntries.VanityPathHandler.class.getDeclaredMethod("getVanityPaths",
String.class);
- method.setAccessible(true);
- method.invoke(mapEntries.vph, "/content/mypage/en-us-{132");
+ getVanityPaths(mapEntries, "/content/mypage/en-us-{132");
assertEquals(0, getVanityCounter(mapEntries).longValue());
}
@@ -1042,9 +1034,7 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
when(this.resourceResolverFactory.getMaxCachedVanityPathEntries()).thenReturn(2L);
when(this.resourceResolverFactory.isMaxCachedVanityPathEntriesStartup()).thenReturn(false);
- Method method =
MapEntries.VanityPathHandler.class.getDeclaredMethod("getVanityPaths",
String.class);
- method.setAccessible(true);
- method.invoke(mapEntries.vph, "/target/justVanityPath");
+ getVanityPaths(mapEntries, "/target/justVanityPath");
assertEquals(2, getVanityCounter(mapEntries).longValue());
@@ -1062,7 +1052,7 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
}
});
- method.invoke(mapEntries.vph, "/target/justVanityPath");
+ getVanityPaths(mapEntries, "/target/justVanityPath");
assertEquals(2, getVanityCounter(mapEntries).longValue());
}
@@ -1085,9 +1075,7 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
}
});
- Method method =
MapEntries.VanityPathHandler.class.getDeclaredMethod("loadVanityPaths",
ResourceResolver.class);
- method.setAccessible(true);
- method.invoke(mapEntries.vph, resourceResolver);
+ loadVanityPaths(mapEntries, resourceResolver);
assertEquals(2, getVanityCounter(mapEntries).longValue());
}
@@ -1110,9 +1098,7 @@ public class VanityPathMapEntriesTest extends
AbstractMappingMapEntriesTest {
}
});
- Method method =
MapEntries.VanityPathHandler.class.getDeclaredMethod("loadVanityPaths",
ResourceResolver.class);
- method.setAccessible(true);
- method.invoke(mapEntries.vph, resourceResolver);
+ loadVanityPaths(mapEntries, resourceResolver);
assertEquals(2, getVanityCounter(mapEntries).longValue());
}