Search in sources :

Example 11 with ResourceProviderHandler

use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler in project sling by apache.

the class FactoryPreconditionsTest method getResourceProviderHandlers.

private List<ResourceProviderHandler> getResourceProviderHandlers(String[] pids) {
    final List<ResourceProviderHandler> result = new ArrayList<ResourceProviderHandler>();
    for (final String p : pids) {
        final ResourceProviderHandler handler = Mockito.mock(ResourceProviderHandler.class);
        final ResourceProviderInfo info = Mockito.mock(ResourceProviderInfo.class);
        final ServiceReference ref = Mockito.mock(ServiceReference.class);
        Mockito.when(handler.getInfo()).thenReturn(info);
        Mockito.when(info.getServiceReference()).thenReturn(ref);
        Mockito.when(ref.getProperty(Constants.SERVICE_PID)).thenReturn(p);
        result.add(handler);
    }
    return result;
}
Also used : ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) ArrayList(java.util.ArrayList) ResourceProviderInfo(org.apache.sling.resourceresolver.impl.providers.ResourceProviderInfo) ServiceReference(org.osgi.framework.ServiceReference)

Example 12 with ResourceProviderHandler

use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler in project sling by apache.

the class ResourceResolverImplTest method setup.

@Before
public void setup() throws LoginException {
    ResourceProvider<?> rp = new ResourceProvider<Object>() {

        @Override
        public Resource getResource(ResolveContext<Object> ctx, String path, ResourceContext rCtx, Resource parent) {
            return null;
        }

        @Override
        public Iterator<Resource> listChildren(ResolveContext<Object> ctx, Resource parent) {
            return null;
        }
    };
    List<ResourceProviderHandler> handlers = asList(createRPHandler(rp, "rp1", 0, "/"));
    resourceProviderTracker = mock(ResourceProviderTracker.class);
    ResourceProviderStorage storage = new ResourceProviderStorage(handlers);
    when(resourceProviderTracker.getResourceProviderStorage()).thenReturn(storage);
    ResourceResolverFactoryActivator activator = new ResourceResolverFactoryActivator();
    activator.resourceProviderTracker = resourceProviderTracker;
    activator.resourceAccessSecurityTracker = new ResourceAccessSecurityTracker();
    commonFactory = new CommonResourceResolverFactoryImpl(activator);
    final Bundle usingBundle = mock(Bundle.class);
    resFac = new ResourceResolverFactoryImpl(commonFactory, usingBundle, null);
    resResolver = resFac.getAdministrativeResourceResolver(null);
}
Also used : ResolveContext(org.apache.sling.spi.resource.provider.ResolveContext) ResourceContext(org.apache.sling.spi.resource.provider.ResourceContext) ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) Bundle(org.osgi.framework.Bundle) ResourceProviderTracker(org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) ResourceProviderStorage(org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage) ResourceProvider(org.apache.sling.spi.resource.provider.ResourceProvider) Before(org.junit.Before)

Example 13 with ResourceProviderHandler

use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler in project sling by apache.

the class ResourceDecoratorTestBase method setup.

@Before
public void setup() throws LoginException {
    final ResourceDecorator d = new ResourceDecorator() {

        @Override
        public Resource decorate(Resource resource) {
            return ResourceDecoratorTestBase.this.wrapResourceForTest(resource);
        }

        @Override
        public Resource decorate(Resource resource, HttpServletRequest request) {
            throw new UnsupportedOperationException("Not supposed to be used in these tests");
        }
    };
    final ResourceDecoratorTracker t = new ResourceDecoratorTracker();
    t.bindResourceDecorator(d, null);
    final ResourceProvider<?> provider = new ResourceProvider<Object>() {

        @Override
        public QueryLanguageProvider<Object> getQueryLanguageProvider() {
            return new QueryLanguageProvider<Object>() {

                @Override
                public String[] getSupportedLanguages(ResolveContext<Object> ctx) {
                    return new String[] { QUERY_LANGUAGE };
                }

                @Override
                public Iterator<Resource> findResources(ResolveContext<Object> ctx, String query, String language) {
                    final List<Resource> found = new ArrayList<Resource>();
                    found.add(mockResource("/tmp/C"));
                    found.add(mockResource("/tmp/D"));
                    found.add(mockResource("/var/one"));
                    found.add(mockResource("/var/two"));
                    return found.iterator();
                }

                @Override
                public Iterator<ValueMap> queryResources(ResolveContext<Object> ctx, String query, String language) {
                    return null;
                }
            };
        }

        @Override
        public Resource getResource(ResolveContext<Object> ctx, String path, final ResourceContext rCtx, Resource parent) {
            if (path.equals("/") || path.startsWith("/tmp") || path.startsWith("/var")) {
                return mockResource(path);
            }
            return null;
        }

        @Override
        public Iterator<Resource> listChildren(ResolveContext<Object> ctx, Resource parent) {
            final List<Resource> children = new ArrayList<Resource>();
            if ("/".equals(parent.getPath())) {
                children.add(mockResource("/tmp"));
                children.add(mockResource("/var"));
            } else if ("/var".equals(parent.getPath())) {
                children.add(mockResource("/var/one"));
                children.add(mockResource("/var/two"));
                children.add(mockResource("/var/three"));
            } else if ("/tmp".equals(parent.getPath())) {
                children.add(mockResource("/tmp/A"));
                children.add(mockResource("/tmp/B"));
                children.add(mockResource("/tmp/C"));
                children.add(mockResource("/tmp/D"));
            }
            return children.iterator();
        }
    };
    ResourceResolverFactoryActivator activator = new ResourceResolverFactoryActivator();
    final CommonResourceResolverFactoryImpl crf = new CommonResourceResolverFactoryImpl(activator) {

        @Override
        public ResourceDecoratorTracker getResourceDecoratorTracker() {
            return t;
        }

        @Override
        public ResourceAccessSecurityTracker getResourceAccessSecurityTracker() {
            return new ResourceAccessSecurityTracker();
        }
    };
    final List<ResourceProviderHandler> list = Arrays.asList(MockedResourceResolverImplTest.createRPHandler(provider, "A-provider", 0L, "/"));
    resolver = new ResourceResolverImpl(crf, false, null, new ResourceProviderStorageProvider() {

        @Override
        public ResourceProviderStorage getResourceProviderStorage() {
            return new ResourceProviderStorage(list);
        }
    });
}
Also used : ResolveContext(org.apache.sling.spi.resource.provider.ResolveContext) ResourceContext(org.apache.sling.spi.resource.provider.ResourceContext) ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) ValueMap(org.apache.sling.api.resource.ValueMap) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) ResourceProviderStorageProvider(org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorageProvider) ResourceDecorator(org.apache.sling.api.resource.ResourceDecorator) HttpServletRequest(javax.servlet.http.HttpServletRequest) ResourceProviderStorage(org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage) ResourceProvider(org.apache.sling.spi.resource.provider.ResourceProvider) ResourceDecoratorTracker(org.apache.sling.resourceresolver.impl.helper.ResourceDecoratorTracker) QueryLanguageProvider(org.apache.sling.spi.resource.provider.QueryLanguageProvider) Before(org.junit.Before)

Example 14 with ResourceProviderHandler

use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler in project sling by apache.

the class MockedResourceResolverImplTest method createRPHandler.

public static ResourceProviderHandler createRPHandler(ResourceProvider<?> rp, String pid, long ranking, String path) {
    ServiceReference ref = Mockito.mock(ServiceReference.class);
    BundleContext bc = Mockito.mock(BundleContext.class);
    Mockito.when(bc.getService(Mockito.eq(ref))).thenReturn(rp);
    Mockito.when(ref.getProperty(Mockito.eq(Constants.SERVICE_ID))).thenReturn(new Random().nextLong());
    Mockito.when(ref.getProperty(Mockito.eq(Constants.SERVICE_PID))).thenReturn(pid);
    Mockito.when(ref.getProperty(Mockito.eq(Constants.SERVICE_RANKING))).thenReturn(ranking);
    Mockito.when(ref.getProperty(Mockito.eq(ResourceProvider.PROPERTY_ROOT))).thenReturn(path);
    Mockito.when(ref.getProperty(Mockito.eq(ResourceProvider.PROPERTY_MODIFIABLE))).thenReturn(true);
    Mockito.when(ref.getProperty(Mockito.eq(ResourceProvider.PROPERTY_ATTRIBUTABLE))).thenReturn(true);
    Mockito.when(ref.getProperty(Mockito.eq(ResourceProvider.PROPERTY_ADAPTABLE))).thenReturn(true);
    ResourceProviderInfo info = new ResourceProviderInfo(ref);
    final ResourceProviderHandler handler = new ResourceProviderHandler(bc, info);
    handler.activate();
    return handler;
}
Also used : ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) Random(java.util.Random) ResourceProviderInfo(org.apache.sling.resourceresolver.impl.providers.ResourceProviderInfo) ServiceReference(org.osgi.framework.ServiceReference) BundleContext(org.osgi.framework.BundleContext)

Example 15 with ResourceProviderHandler

use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler in project sling by apache.

the class ProviderHandlerTest method testServletRegistrationAndSyntheticResources.

@SuppressWarnings("unchecked")
@Test
public void testServletRegistrationAndSyntheticResources() throws LoginException {
    final String servletpath = "/libs/a/b/GET.servlet";
    final Resource servletResource = Mockito.mock(Resource.class);
    Mockito.when(servletResource.getResourceMetadata()).then(new Answer<ResourceMetadata>() {

        @Override
        public ResourceMetadata answer(InvocationOnMock invocation) throws Throwable {
            return new ResourceMetadata();
        }
    });
    final ResourceProvider<?> leaveProvider = Mockito.mock(ResourceProvider.class);
    Mockito.when(leaveProvider.getResource(Mockito.any(ResolveContext.class), Mockito.eq(servletpath), Mockito.any(ResourceContext.class), Mockito.any(Resource.class))).thenReturn(servletResource);
    final ResourceProviderHandler h = createRPHandler(leaveProvider, "my-pid", 0, servletpath);
    ResourceResolverFactoryActivator activator = new ResourceResolverFactoryActivator();
    activator.resourceAccessSecurityTracker = new ResourceAccessSecurityTracker();
    ResourceResolver resolver = new ResourceResolverImpl(new CommonResourceResolverFactoryImpl(activator), false, null, new ResourceProviderStorageProvider() {

        @Override
        public ResourceProviderStorage getResourceProviderStorage() {
            return new ResourceProviderStorage(Arrays.asList(h));
        }
    });
    final Resource parent = resolver.getResource(ResourceUtil.getParent(servletpath));
    assertNotNull("Parent must be available", parent);
    assertTrue("Resource should be synthetic", ResourceUtil.isSyntheticResource(parent));
    final Resource servlet = resolver.getResource(servletpath);
    assertNotNull("Servlet resource must not be null", servlet);
    assertEquals(servletResource, servlet);
    assertNotNull(resolver.getResource("/libs"));
    // now check when doing a resolve()
    assertTrue(resolver.resolve("/libs") instanceof NonExistingResource);
    assertTrue(resolver.resolve(ResourceUtil.getParent(servletpath)) instanceof NonExistingResource);
    assertNotNull(resolver.resolve(servletpath));
    resolver.close();
}
Also used : ResolveContext(org.apache.sling.spi.resource.provider.ResolveContext) ResourceContext(org.apache.sling.spi.resource.provider.ResourceContext) ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) Resource(org.apache.sling.api.resource.Resource) ResourceProviderStorageProvider(org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorageProvider) ResourceMetadata(org.apache.sling.api.resource.ResourceMetadata) ResourceProviderStorage(org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage) InvocationOnMock(org.mockito.invocation.InvocationOnMock) NonExistingResource(org.apache.sling.api.resource.NonExistingResource) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Test(org.junit.Test)

Aggregations

ResourceProviderHandler (org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler)18 Resource (org.apache.sling.api.resource.Resource)9 ResourceProviderStorage (org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage)7 ResolveContext (org.apache.sling.spi.resource.provider.ResolveContext)7 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)6 Test (org.junit.Test)6 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)5 ResourceProviderTracker (org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker)5 ResourceContext (org.apache.sling.spi.resource.provider.ResourceContext)5 ResourceProvider (org.apache.sling.spi.resource.provider.ResourceProvider)5 Before (org.junit.Before)5 ArrayList (java.util.ArrayList)4 ResourceProviderInfo (org.apache.sling.resourceresolver.impl.providers.ResourceProviderInfo)4 ServiceReference (org.osgi.framework.ServiceReference)4 LoginException (org.apache.sling.api.resource.LoginException)3 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)3 ResourceAccessSecurityTracker (org.apache.sling.resourceresolver.impl.ResourceAccessSecurityTracker)3 ResourceProviderStorageProvider (org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorageProvider)3 QueryLanguageProvider (org.apache.sling.spi.resource.provider.QueryLanguageProvider)3 AbstractResource (org.apache.sling.api.resource.AbstractResource)2