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;
}
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);
}
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);
}
});
}
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;
}
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();
}
Aggregations