use of org.apache.sling.api.resource.Resource in project sling by apache.
the class MapEntriesTest method test_that_duplicate_alias_doesnt_replace_first_alias.
@Test
public void test_that_duplicate_alias_doesnt_replace_first_alias() {
Resource parent = mock(Resource.class);
when(parent.getPath()).thenReturn("/parent");
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");
when(secondResult.getValueMap()).thenReturn(buildValueMap(ResourceResolverImpl.PROP_ALIAS, "alias"));
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(ResourceResolverImpl.PROP_ALIAS)) {
return Arrays.asList(result, secondResult).iterator();
} else {
return Collections.<Resource>emptySet().iterator();
}
}
});
mapEntries.doInit();
Map<String, String> aliasMap = mapEntries.getAliasMap("/parent");
assertNotNull(aliasMap);
assertTrue(aliasMap.containsKey("alias"));
assertEquals("child", aliasMap.get("alias"));
}
use of org.apache.sling.api.resource.Resource in project sling by apache.
the class MapEntriesTest method test_vanity_path_updates.
@Test
public void test_vanity_path_updates() throws Exception {
Resource parent = mock(Resource.class, "parent");
when(parent.getPath()).thenReturn("/foo/parent");
when(parent.getName()).thenReturn("parent");
when(parent.getValueMap()).thenReturn(new ValueMapDecorator(Collections.<String, Object>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");
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("sql"))).thenAnswer(new Answer<Iterator<Resource>>() {
@Override
public Iterator<Resource> answer(InvocationOnMock invocation) throws Throwable {
return Collections.<Resource>emptySet().iterator();
}
});
mapEntries.doInit();
mapEntries.initializeVanityPaths();
// map entries should have no alias atm
assertTrue(mapEntries.getResolveMaps().isEmpty());
// add parent
mapEntries.onChange(Arrays.asList(new ResourceChange(ChangeType.ADDED, parent.getPath(), false)));
assertTrue(mapEntries.getResolveMaps().isEmpty());
// add child
mapEntries.onChange(Arrays.asList(new ResourceChange(ChangeType.ADDED, child.getPath(), false)));
// two entries for the vanity path
List<MapEntry> entries = mapEntries.getResolveMaps();
assertEquals(2, entries.size());
for (MapEntry entry : entries) {
assertTrue(entry.getPattern().contains("/target/found"));
}
// update parent - no change
mapEntries.onChange(Arrays.asList(new ResourceChange(ChangeType.CHANGED, parent.getPath(), false)));
entries = mapEntries.getResolveMaps();
assertEquals(2, entries.size());
for (MapEntry entry : entries) {
assertTrue(entry.getPattern().contains("/target/found"));
}
// update child - no change
mapEntries.onChange(Arrays.asList(new ResourceChange(ChangeType.CHANGED, child.getPath(), false)));
entries = mapEntries.getResolveMaps();
assertEquals(2, entries.size());
for (MapEntry entry : entries) {
assertTrue(entry.getPattern().contains("/target/found"));
}
// remove child - empty again
when(resourceResolver.getResource(child.getPath())).thenReturn(null);
when(parent.getChild(child.getName())).thenReturn(null);
mapEntries.onChange(Arrays.asList(new ResourceChange(ChangeType.REMOVED, child.getPath(), false)));
assertTrue(mapEntries.getResolveMaps().isEmpty());
// remove parent - still empty
when(resourceResolver.getResource(parent.getPath())).thenReturn(null);
mapEntries.onChange(Arrays.asList(new ResourceChange(ChangeType.REMOVED, parent.getPath(), false)));
assertTrue(mapEntries.getResolveMaps().isEmpty());
}
use of org.apache.sling.api.resource.Resource in project sling by apache.
the class MapEntriesTest method sessionConcurrency.
// tests SLING-6542
@Test
public void sessionConcurrency() throws Exception {
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);
final Method handleConfigurationUpdate = MapEntries.class.getDeclaredMethod("handleConfigurationUpdate", String.class, AtomicBoolean.class, AtomicBoolean.class, boolean.class);
handleConfigurationUpdate.setAccessible(true);
final Semaphore sessionLock = new Semaphore(1);
// simulate somewhat slow (1ms) session operations that use locking
// to determine that they are using the session exclusively.
// if that lock mechanism detects concurrent access we fail
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
simulateSomewhatSlowSessionOperation(sessionLock);
return null;
}
}).when(resourceResolver).refresh();
Mockito.doAnswer(new Answer<Resource>() {
@Override
public Resource answer(InvocationOnMock invocation) throws Throwable {
simulateSomewhatSlowSessionOperation(sessionLock);
return null;
}
}).when(resourceResolver).getResource(any(String.class));
when(resourceResolverFactory.isMapConfiguration(any(String.class))).thenReturn(true);
final AtomicInteger failureCnt = new AtomicInteger(0);
final List<Exception> exceptions = new LinkedList<>();
final Semaphore done = new Semaphore(0);
final int NUM_THREADS = 30;
final Random random = new Random(12321);
for (int i = 0; i < NUM_THREADS; i++) {
final int randomWait = random.nextInt(10);
Runnable r = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(randomWait);
for (int i = 0; i < 3; i++) {
addResource.invoke(mapEntries, "/node", new AtomicBoolean());
updateResource.invoke(mapEntries, "/node", new AtomicBoolean());
handleConfigurationUpdate.invoke(mapEntries, "/node", new AtomicBoolean(), new AtomicBoolean(), false);
}
} catch (Exception e) {
e.printStackTrace();
synchronized (exceptions) {
exceptions.add(e);
}
failureCnt.incrementAndGet();
} finally {
done.release();
}
}
};
Thread th = new Thread(r);
th.setDaemon(true);
th.start();
}
assertTrue("threads did not finish in time", done.tryAcquire(NUM_THREADS, 30, TimeUnit.SECONDS));
if (failureCnt.get() != 0) {
synchronized (exceptions) {
throw new AssertionError("exceptions encountered (" + failureCnt.get() + "). First exception: ", exceptions.get(0));
}
}
}
use of org.apache.sling.api.resource.Resource in project sling by apache.
the class MapEntriesTest method test_doRemoveAlias4.
@Test
public void test_doRemoveAlias4() 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);
assertEquals(0, aliasMap.size());
Resource parent = mock(Resource.class);
when(parent.getPath()).thenReturn("/");
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.invoke(mapEntries, "/parent", new AtomicBoolean());
Map<String, String> aliasMapEntry = mapEntries.getAliasMap("/");
assertNotNull(aliasMapEntry);
assertTrue(aliasMapEntry.containsKey("alias"));
assertEquals("parent", aliasMapEntry.get("alias"));
assertEquals(1, aliasMap.size());
removeAlias.invoke(mapEntries, "/", "/parent", new AtomicBoolean());
aliasMapEntry = mapEntries.getAliasMap("/");
assertNull(aliasMapEntry);
assertEquals(0, aliasMap.size());
//re-add node and test nodeDeletion true
addResource.invoke(mapEntries, "/parent", new AtomicBoolean());
aliasMapEntry = mapEntries.getAliasMap("/");
assertNotNull(aliasMapEntry);
assertTrue(aliasMapEntry.containsKey("alias"));
assertEquals("parent", aliasMapEntry.get("alias"));
assertEquals(1, aliasMap.size());
when(resourceResolver.getResource("/parent")).thenReturn(null);
removeAlias.invoke(mapEntries, "/", "/parent", new AtomicBoolean());
aliasMapEntry = mapEntries.getAliasMap("/");
assertNull(aliasMapEntry);
assertEquals(0, aliasMap.size());
}
use of org.apache.sling.api.resource.Resource in project sling by apache.
the class ResourceResolverImplTest method testIsResourceType.
@Test
public void testIsResourceType() {
final PathBasedResourceResolverImpl resolver = getPathBasedResourceResolver();
final Resource r = resolver.add(new SyntheticResourceWithSupertype(resolver, "/a", "a:b", "d:e"));
resolver.add(new SyntheticResourceWithSupertype(resolver, "/d/e", "x:y", "t:c"));
assertTrue(resolver.isResourceType(r, "a:b"));
assertTrue(resolver.isResourceType(r, "d:e"));
assertFalse(resolver.isResourceType(r, "x:y"));
assertTrue(resolver.isResourceType(r, "t:c"));
assertFalse(resolver.isResourceType(r, "h:p"));
}
Aggregations