use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.
the class JcrPropertyResourceTest method testCorrectUTF8ByteLength.
@Test
public void testCorrectUTF8ByteLength() throws RepositoryException, UnsupportedEncodingException {
final HashMap<Object, Integer> testData = new HashMap<Object, Integer>() {
{
put("some random ascii string", PropertyType.STRING);
put("русский язык", PropertyType.STRING);
put("贛語", PropertyType.STRING);
put("string with ümlaut", PropertyType.STRING);
put(true, PropertyType.BOOLEAN);
put(1000L, PropertyType.LONG);
put(BigDecimal.TEN, PropertyType.DECIMAL);
}
};
final ResourceResolver resolver = this.context.mock(ResourceResolver.class);
for (final Entry<Object, Integer> data : testData.entrySet()) {
final String stringValue = data.getKey().toString();
final long stringByteLength = stringValue.getBytes("UTF-8").length;
final Property property = this.context.mock(Property.class, stringValue);
this.context.checking(new Expectations() {
{
ignoring(resolver);
allowing(property).getParent();
allowing(property).getName();
allowing(property).isMultiple();
will(returnValue(false));
allowing(property).getLength();
will(returnValue((long) stringValue.length()));
allowing(property).getType();
will(returnValue(data.getValue()));
allowing(property).getString();
will(returnValue(stringValue));
}
});
final JcrPropertyResource propResource = new JcrPropertyResource(resolver, "/path/to/string-property", null, property);
assertEquals("Byte length of " + stringValue, stringByteLength, propResource.getResourceMetadata().getContentLength());
}
}
use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.
the class MapEntries method getVanityPaths.
/**
* get the vanity paths Search for all nodes having a specific vanityPath
*/
private Map<String, List<MapEntry>> getVanityPaths(String vanityPath) {
Map<String, List<MapEntry>> entryMap = new HashMap<>();
// sling:vanityPath (lowercase) is the property name
final String queryString = "SELECT sling:vanityPath, sling:redirect, sling:redirectStatus FROM nt:base WHERE sling:vanityPath =" + "'" + escapeIllegalXpathSearchChars(vanityPath).replaceAll("'", "''") + "' OR sling:vanityPath =" + "'" + escapeIllegalXpathSearchChars(vanityPath.substring(1)).replaceAll("'", "''") + "' ORDER BY sling:vanityOrder DESC";
ResourceResolver queryResolver = null;
try {
queryResolver = factory.getServiceResourceResolver(factory.getServiceUserAuthenticationInfo("mapping"));
final Iterator<Resource> i = queryResolver.findResources(queryString, "sql");
while (i.hasNext()) {
final Resource resource = i.next();
boolean isValid = false;
for (final Path sPath : this.factory.getObservationPaths()) {
if (sPath.matches(resource.getPath())) {
isValid = true;
break;
}
}
if (isValid) {
if (this.factory.isMaxCachedVanityPathEntriesStartup() || vanityCounter.longValue() < this.factory.getMaxCachedVanityPathEntries()) {
loadVanityPath(resource, resolveMapsMap, vanityTargets, true, false);
entryMap = resolveMapsMap;
} else {
final Map<String, List<String>> targetPaths = new HashMap<>();
loadVanityPath(resource, entryMap, targetPaths, true, false);
}
}
}
} catch (LoginException e) {
log.error("Exception while obtaining queryResolver", e);
} finally {
if (queryResolver != null) {
queryResolver.close();
}
}
return entryMap;
}
use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.
the class MockedResourceResolverImplTest method testResolverMisc.
/**
* Misceleneous coverage.
* @throws LoginException
*/
@Test
public void testResolverMisc() throws LoginException {
ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
try {
resourceResolver.getAttribute(null);
Assert.fail("Should have thrown a NPE");
} catch (NullPointerException e) {
// this is expected.
}
Assert.assertArrayEquals(new String[] { "/apps/", "/libs/" }, resourceResolver.getSearchPath());
}
use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.
the class MockedResourceResolverImplTest method testGetResource.
/**
* Test getResource for a resource provided by a resource provider.
* @throws LoginException
*/
@Test
public void testGetResource() throws LoginException {
ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
Assert.assertNotNull(resourceResolver);
Resource singleResource = buildResource("/single/test", EMPTY_RESOURCE_LIST, resourceResolver, resourceProvider);
Resource resource = resourceResolver.getResource("/single/test");
Assert.assertEquals(singleResource, resource);
}
use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.
the class MockedResourceResolverImplTest method testResourceResolverGetChildren.
/**
* Tests listing children via the resource resolver getChildren call.
* @throws LoginException
*/
@Test
public void testResourceResolverGetChildren() throws LoginException {
ResourceResolver resourceResolver = resourceResolverFactory.getResourceResolver(null);
buildResource("/single/test/withchildren", buildChildResources("/single/test/withchildren"), resourceResolver, resourceProvider);
Resource resource = resourceResolver.getResource("/single/test/withchildren");
Assert.assertNotNull(resource);
// test via the resource list children itself, this really just tests this test case.
Iterable<Resource> resourceIterator = resourceResolver.getChildren(resource);
Assert.assertNotNull(resourceResolver);
int i = 0;
for (Resource r : resourceIterator) {
Assert.assertEquals("m" + i, r.getName());
i++;
}
Assert.assertEquals(5, i);
}
Aggregations