Search in sources :

Example 71 with ResourceResolver

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());
    }
}
Also used : Expectations(org.jmock.Expectations) HashMap(java.util.HashMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Property(javax.jcr.Property) Test(org.junit.Test)

Example 72 with ResourceResolver

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;
}
Also used : Path(org.apache.sling.api.resource.path.Path) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) LoginException(org.apache.sling.api.resource.LoginException) List(java.util.List) ArrayList(java.util.ArrayList)

Example 73 with ResourceResolver

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());
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Test(org.junit.Test)

Example 74 with ResourceResolver

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);
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Example 75 with ResourceResolver

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);
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) Test(org.junit.Test)

Aggregations

ResourceResolver (org.apache.sling.api.resource.ResourceResolver)339 Resource (org.apache.sling.api.resource.Resource)168 Test (org.junit.Test)131 HashMap (java.util.HashMap)65 LoginException (org.apache.sling.api.resource.LoginException)53 PersistenceException (org.apache.sling.api.resource.PersistenceException)52 Session (javax.jcr.Session)31 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)29 ValueMap (org.apache.sling.api.resource.ValueMap)27 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)26 ArrayList (java.util.ArrayList)23 DistributionRequest (org.apache.sling.distribution.DistributionRequest)23 DistributionPackage (org.apache.sling.distribution.packaging.DistributionPackage)21 Map (java.util.Map)19 Before (org.junit.Before)19 IOException (java.io.IOException)17 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)17 ChildResource (org.apache.sling.validation.model.ChildResource)17 HashSet (java.util.HashSet)16 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)15