Search in sources :

Example 6 with ResourceProviderStorage

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

the class FactoryPreconditionsTest method testDeactivated.

@Test
public void testDeactivated() {
    final ResourceProviderTracker tracker = Mockito.mock(ResourceProviderTracker.class);
    final ResourceProviderStorage storage = Mockito.mock(ResourceProviderStorage.class);
    Mockito.when(tracker.getResourceProviderStorage()).thenReturn(storage);
    FactoryPreconditions conditions = new FactoryPreconditions();
    conditions.activate(null, null, null, tracker);
    assertTrue(conditions.checkPreconditions(null, null));
    conditions.deactivate();
    assertFalse(conditions.checkPreconditions(null, null));
}
Also used : ResourceProviderStorage(org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage) ResourceProviderTracker(org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker) Test(org.junit.Test)

Example 7 with ResourceProviderStorage

use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage 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 8 with ResourceProviderStorage

use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage 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 9 with ResourceProviderStorage

use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage 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)

Example 10 with ResourceProviderStorage

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

the class ResourceResolverControlTest method prepare.

@Before
public void prepare() throws Exception {
    BundleContext bc = MockOsgi.newBundleContext();
    Fixture fixture = new Fixture(bc);
    // sub-provider
    subProvider = Mockito.mock(ResourceProvider.class);
    ResourceProviderInfo info = fixture.registerResourceProvider(subProvider, "/some/path", AuthType.required);
    ResourceProviderHandler handler = new ResourceProviderHandler(bc, info);
    when(subProvider.getQueryLanguageProvider()).thenReturn(new SimpleQueryLanguageProvider(QL_MOCK, QL_ANOTHER_MOCK) {

        @Override
        public Iterator<ValueMap> queryResources(ResolveContext<Object> ctx, String query, String language) {
            if (query.equals(QUERY_MOCK_FIND_ALL) && language.equals(QL_MOCK)) {
                SimpleValueMapImpl valueMap = new SimpleValueMapImpl();
                valueMap.put("key", "value");
                return Collections.<ValueMap>singletonList(valueMap).iterator();
            }
            throw new UnsupportedOperationException();
        }

        @Override
        public Iterator<Resource> findResources(ResolveContext<Object> ctx, String query, String language) {
            if (query.equals(QUERY_MOCK_FIND_ALL) && language.equals(QL_MOCK)) {
                return Collections.<Resource>singletonList(newMockResource("/some/path/object")).iterator();
            }
            throw new UnsupportedOperationException();
        }
    });
    handler.activate();
    rootProvider = mock(ResourceProvider.class);
    ResourceProviderInfo rootInfo = fixture.registerResourceProvider(rootProvider, "/", AuthType.required);
    ResourceProviderHandler rootHandler = new ResourceProviderHandler(bc, rootInfo);
    when(rootProvider.getQueryLanguageProvider()).thenReturn(new SimpleQueryLanguageProvider(QL_NOOP));
    rootHandler.activate();
    // configure mock resources
    Resource root = configureResourceAt(rootProvider, "/");
    somethingResource = configureResourceAt(rootProvider, "/something");
    subProviderResource = configureResourceAt(subProvider, "/some/path/object");
    // configure query at '/'
    when(rootProvider.listChildren((ResolveContext<Object>) Mockito.anyObject(), Mockito.eq(root))).thenReturn(Collections.singleton(somethingResource).iterator());
    ResourceResolver rr = mock(ResourceResolver.class);
    ResourceAccessSecurityTracker securityTracker = Mockito.mock(ResourceAccessSecurityTracker.class);
    authInfo = getAuthInfo();
    handlers = Arrays.asList(rootHandler, handler);
    final ResourceProviderStorage storage = new ResourceProviderStorage(handlers);
    crp = new ResourceResolverControl(false, authInfo, new ResourceProviderStorageProvider() {

        @Override
        public ResourceProviderStorage getResourceProviderStorage() {
            return storage;
        }
    });
    context = new ResourceResolverContext(rr, securityTracker);
}
Also used : ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) ResourceProviderStorageProvider(org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorageProvider) SimpleValueMapImpl(org.apache.sling.resourceresolver.impl.SimpleValueMapImpl) ResourceProviderInfo(org.apache.sling.resourceresolver.impl.providers.ResourceProviderInfo) ResourceProviderStorage(org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage) ResourceProvider(org.apache.sling.spi.resource.provider.ResourceProvider) Iterator(java.util.Iterator) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ResourceAccessSecurityTracker(org.apache.sling.resourceresolver.impl.ResourceAccessSecurityTracker) Fixture(org.apache.sling.resourceresolver.impl.Fixture) BundleContext(org.osgi.framework.BundleContext) Before(org.junit.Before)

Aggregations

ResourceProviderStorage (org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage)10 ResourceProviderHandler (org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler)7 ResourceProviderTracker (org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker)6 Test (org.junit.Test)6 Resource (org.apache.sling.api.resource.Resource)4 Before (org.junit.Before)4 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)3 ResourceProviderStorageProvider (org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorageProvider)3 ResolveContext (org.apache.sling.spi.resource.provider.ResolveContext)3 ResourceContext (org.apache.sling.spi.resource.provider.ResourceContext)3 ResourceProvider (org.apache.sling.spi.resource.provider.ResourceProvider)3 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)2 ValueMap (org.apache.sling.api.resource.ValueMap)2 Bundle (org.osgi.framework.Bundle)2 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 Dictionary (java.util.Dictionary)1 Iterator (java.util.Iterator)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 ResourceDecorator (org.apache.sling.api.resource.ResourceDecorator)1