use of org.apache.sling.api.resource.Resource in project sling by apache.
the class MapEntriesTest method test_doRemoveAttributessWithDisableAliasOptimization.
//SLING-3727
@Test
public void test_doRemoveAttributessWithDisableAliasOptimization() throws Exception {
final Method removeAlias = MapEntries.class.getDeclaredMethod("removeAlias", String.class, String.class, AtomicBoolean.class);
removeAlias.setAccessible(true);
when(resourceResolverFactory.isOptimizeAliasResolutionEnabled()).thenReturn(false);
mapEntries = new MapEntries(resourceResolverFactory, bundleContext, eventAdmin);
Resource parent = mock(Resource.class);
when(parent.getPath()).thenReturn("/parent");
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"));
removeAlias.invoke(mapEntries, "/parent", "/parent/child", new AtomicBoolean());
Map<String, String> aliasMap = mapEntries.getAliasMap("/parent");
assertNull(aliasMap);
}
use of org.apache.sling.api.resource.Resource in project sling by apache.
the class MapEntriesTest method test_getVanityPaths_4.
@Test
public //SLING-4891
void test_getVanityPaths_4() throws Exception {
final Resource badVanityPath = mock(Resource.class, "badVanityPath");
when(badVanityPath.getPath()).thenReturn("/badVanityPath");
when(badVanityPath.getName()).thenReturn("badVanityPath");
when(badVanityPath.getValueMap()).thenReturn(buildValueMap("sling:vanityPath", "/content/mypage/en-us-{132"));
when(resourceResolver.findResources(anyString(), eq("sql"))).thenAnswer(new Answer<Iterator<Resource>>() {
@Override
public Iterator<Resource> answer(InvocationOnMock invocation) throws Throwable {
if (invocation.getArguments()[0].toString().contains("sling:vanityPath")) {
return Collections.singleton(badVanityPath).iterator();
} else {
return Collections.<Resource>emptySet().iterator();
}
}
});
when(this.resourceResolverFactory.getMaxCachedVanityPathEntries()).thenReturn(0L);
when(this.resourceResolverFactory.isMaxCachedVanityPathEntriesStartup()).thenReturn(true);
Method method = MapEntries.class.getDeclaredMethod("getVanityPaths", String.class);
method.setAccessible(true);
method.invoke(mapEntries, "/content/mypage/en-us-{132");
Field vanityCounter = MapEntries.class.getDeclaredField("vanityCounter");
vanityCounter.setAccessible(true);
AtomicLong counter = (AtomicLong) vanityCounter.get(mapEntries);
assertEquals(0, counter.longValue());
}
use of org.apache.sling.api.resource.Resource in project sling by apache.
the class MapEntriesTest method test_getMapEntryList.
@Test
public //SLING-4891
void test_getMapEntryList() throws Exception {
List<MapEntry> entries = mapEntries.getResolveMaps();
assertEquals(0, entries.size());
final 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"));
when(resourceResolver.findResources(anyString(), eq("sql"))).thenAnswer(new Answer<Iterator<Resource>>() {
@Override
public Iterator<Resource> answer(InvocationOnMock invocation) throws Throwable {
if (invocation.getArguments()[0].toString().contains("sling:vanityPath")) {
return Collections.singleton(justVanityPath).iterator();
} else {
return Collections.<Resource>emptySet().iterator();
}
}
});
Method method = MapEntries.class.getDeclaredMethod("getMapEntryList", String.class);
method.setAccessible(true);
method.invoke(mapEntries, "/target/justVanityPath");
entries = mapEntries.getResolveMaps();
assertEquals(2, entries.size());
Field vanityCounter = MapEntries.class.getDeclaredField("vanityCounter");
vanityCounter.setAccessible(true);
AtomicLong counter = (AtomicLong) vanityCounter.get(mapEntries);
assertEquals(2, counter.longValue());
method.invoke(mapEntries, "/target/justVanityPath");
entries = mapEntries.getResolveMaps();
assertEquals(2, entries.size());
counter = (AtomicLong) vanityCounter.get(mapEntries);
assertEquals(2, counter.longValue());
}
use of org.apache.sling.api.resource.Resource in project sling by apache.
the class MapEntriesTest method test_doRemoveAlias.
@Test
public void test_doRemoveAlias() throws Exception {
final Method addResource = MapEntries.class.getDeclaredMethod("addResource", String.class, AtomicBoolean.class);
addResource.setAccessible(true);
final Method removeAlias = MapEntries.class.getDeclaredMethod("removeAlias", String.class, String.class, AtomicBoolean.class);
removeAlias.setAccessible(true);
// check that alias map is empty
assertEquals(0, aliasMap.size());
final Resource parent = mock(Resource.class);
when(parent.getPath()).thenReturn("/parent");
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.invoke(mapEntries, "/parent/child", new AtomicBoolean());
Map<String, String> aliasMapEntry = mapEntries.getAliasMap("/parent");
assertNotNull(aliasMapEntry);
assertTrue(aliasMapEntry.containsKey("alias"));
assertEquals("child", aliasMapEntry.get("alias"));
assertEquals(1, aliasMap.size());
removeAlias.invoke(mapEntries, "/parent", "/parent/child", new AtomicBoolean());
aliasMapEntry = mapEntries.getAliasMap("/parent");
assertNull(aliasMapEntry);
assertEquals(0, aliasMap.size());
//re-add node and test nodeDeletion true
addResource.invoke(mapEntries, "/parent/child", new AtomicBoolean());
aliasMapEntry = mapEntries.getAliasMap("/parent");
assertNotNull(aliasMapEntry);
assertTrue(aliasMapEntry.containsKey("alias"));
assertEquals("child", aliasMapEntry.get("alias"));
assertEquals(1, aliasMap.size());
when(resourceResolver.getResource("/parent/child")).thenReturn(null);
removeAlias.invoke(mapEntries, "/parent", "/parent/child", new AtomicBoolean());
aliasMapEntry = mapEntries.getAliasMap("/parent");
assertNull(aliasMapEntry);
assertEquals(0, aliasMap.size());
}
use of org.apache.sling.api.resource.Resource in project sling by apache.
the class MapEntriesTest method test_doUpdateAttributesWithDisableAliasOptimization.
//SLING-3727
@Test
public void test_doUpdateAttributesWithDisableAliasOptimization() throws Exception {
final Method addResource = MapEntries.class.getDeclaredMethod("addResource", String.class, AtomicBoolean.class);
addResource.setAccessible(true);
when(resourceResolverFactory.isOptimizeAliasResolutionEnabled()).thenReturn(false);
mapEntries = new MapEntries(resourceResolverFactory, bundleContext, eventAdmin);
Resource parent = mock(Resource.class);
when(parent.getPath()).thenReturn("/parent");
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.invoke(mapEntries, "/parent/child", new AtomicBoolean());
Map<String, String> aliasMap = mapEntries.getAliasMap("/parent");
assertNull(aliasMap);
}
Aggregations