Search in sources :

Example 71 with Resource

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);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Resource(org.apache.sling.api.resource.Resource) Method(java.lang.reflect.Method) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 72 with Resource

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());
}
Also used : Field(java.lang.reflect.Field) AtomicLong(java.util.concurrent.atomic.AtomicLong) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Resource(org.apache.sling.api.resource.Resource) Iterator(java.util.Iterator) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 73 with Resource

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());
}
Also used : Field(java.lang.reflect.Field) AtomicLong(java.util.concurrent.atomic.AtomicLong) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Resource(org.apache.sling.api.resource.Resource) Iterator(java.util.Iterator) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 74 with Resource

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());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Resource(org.apache.sling.api.resource.Resource) Method(java.lang.reflect.Method) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 75 with Resource

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);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Resource(org.apache.sling.api.resource.Resource) Method(java.lang.reflect.Method) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Aggregations

Resource (org.apache.sling.api.resource.Resource)1151 Test (org.junit.Test)633 ValueMap (org.apache.sling.api.resource.ValueMap)263 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)164 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)159 Node (javax.jcr.Node)116 HashMap (java.util.HashMap)104 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)99 PersistenceException (org.apache.sling.api.resource.PersistenceException)94 ArrayList (java.util.ArrayList)70 HttpServletRequest (javax.servlet.http.HttpServletRequest)62 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)60 FakeSlingHttpServletRequest (org.apache.sling.launchpad.testservices.exported.FakeSlingHttpServletRequest)59 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)55 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)43 Map (java.util.Map)41 InputStream (java.io.InputStream)38 LoginException (org.apache.sling.api.resource.LoginException)37 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)36 Iterator (java.util.Iterator)33