use of org.apache.sling.resourceresolver.impl.Fixture 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);
}
use of org.apache.sling.resourceresolver.impl.Fixture in project sling by apache.
the class ResourceProviderTrackerTest method testActivationDeactivation.
@Test
public void testActivationDeactivation() throws Exception {
final BundleContext bundleContext = MockOsgi.newBundleContext();
final EventAdmin eventAdmin = mock(EventAdmin.class);
final Fixture fixture = new Fixture(bundleContext);
final ResourceProviderTracker tracker = new ResourceProviderTracker();
tracker.setObservationReporterGenerator(new SimpleObservationReporterGenerator(new NoDothingObservationReporter()));
// create boolean markers for the listener
final AtomicBoolean addedCalled = new AtomicBoolean(false);
final AtomicBoolean removedCalled = new AtomicBoolean(false);
final ChangeListener listener = new ChangeListener() {
@Override
public void providerAdded() {
addedCalled.set(true);
}
@Override
public void providerRemoved(String name, String pid, boolean stateful, boolean used) {
removedCalled.set(true);
}
};
// activate and check that no listener is called yet
tracker.activate(bundleContext, eventAdmin, listener);
assertFalse(addedCalled.get());
assertFalse(removedCalled.get());
// add a new resource provider
@SuppressWarnings("unchecked") ResourceProvider<Object> rp = mock(ResourceProvider.class);
final ResourceProviderInfo info = fixture.registerResourceProvider(rp, "/", AuthType.no);
// check added is called but not removed
assertTrue(addedCalled.get());
assertFalse(removedCalled.get());
// verify a single provider
assertThat(tracker.getResourceProviderStorage().getAllHandlers().size(), equalTo(1));
// reset boolean markers
addedCalled.set(false);
removedCalled.set(false);
// remove provider
fixture.unregisterResourceProvider(info);
// verify removed is called but not added
assertTrue(removedCalled.get());
assertFalse(addedCalled.get());
// no provider anymore
assertThat(tracker.getResourceProviderStorage().getAllHandlers().size(), equalTo(0));
}
use of org.apache.sling.resourceresolver.impl.Fixture in project sling by apache.
the class ResourceProviderTrackerTest method testReactivation.
@Test
public void testReactivation() throws Exception {
final BundleContext bundleContext = MockOsgi.newBundleContext();
final EventAdmin eventAdmin = mock(EventAdmin.class);
final Fixture fixture = new Fixture(bundleContext);
final ResourceProviderTracker tracker = new ResourceProviderTracker();
tracker.setObservationReporterGenerator(new SimpleObservationReporterGenerator(new NoDothingObservationReporter()));
// create boolean markers for the listener
final AtomicBoolean addedCalled = new AtomicBoolean(false);
final AtomicBoolean removedCalled = new AtomicBoolean(false);
final ChangeListener listener = new ChangeListener() {
@Override
public void providerAdded() {
addedCalled.set(true);
}
@Override
public void providerRemoved(String name, String pid, boolean stateful, boolean used) {
removedCalled.set(true);
}
};
// activate and check that no listener is called yet
tracker.activate(bundleContext, eventAdmin, listener);
assertFalse(addedCalled.get());
assertFalse(removedCalled.get());
// activate and check that no listener is called yet
@SuppressWarnings("unchecked") ResourceProvider<Object> rp = mock(ResourceProvider.class);
final ResourceProviderInfo info = fixture.registerResourceProvider(rp, "/", AuthType.no);
// check added is called but not removed
assertTrue(addedCalled.get());
assertFalse(removedCalled.get());
// verify a single provider
assertThat(tracker.getResourceProviderStorage().getAllHandlers().size(), equalTo(1));
// reset boolean markers
addedCalled.set(false);
removedCalled.set(false);
// add overlay provider
final ResourceProviderInfo infoOverlay = fixture.registerResourceProvider(rp, "/", AuthType.no);
// check added and removed is called
assertTrue(addedCalled.get());
assertTrue(removedCalled.get());
// verify a single provider
assertThat(tracker.getResourceProviderStorage().getAllHandlers().size(), equalTo(1));
// reset boolean markers
addedCalled.set(false);
removedCalled.set(false);
// unregister overlay provider
fixture.unregisterResourceProvider(infoOverlay);
// check added and removed is called
assertTrue(addedCalled.get());
assertTrue(removedCalled.get());
// verify a single provider
assertThat(tracker.getResourceProviderStorage().getAllHandlers().size(), equalTo(1));
// reset boolean markers
addedCalled.set(false);
removedCalled.set(false);
// unregister first provider
fixture.unregisterResourceProvider(info);
// check removed is called but not added
assertTrue(removedCalled.get());
assertFalse(addedCalled.get());
// verify no provider
assertThat(tracker.getResourceProviderStorage().getAllHandlers().size(), equalTo(0));
}
use of org.apache.sling.resourceresolver.impl.Fixture in project sling by apache.
the class ResourceProviderTrackerTest method prepare.
@Before
public void prepare() throws Exception {
BundleContext bundleContext = MockOsgi.newBundleContext();
fixture = new Fixture(bundleContext);
EventAdmin eventAdmin = mock(EventAdmin.class);
@SuppressWarnings("unchecked") ResourceProvider<Object> rp = mock(ResourceProvider.class);
@SuppressWarnings("unchecked") ResourceProvider<Object> rp2 = mock(ResourceProvider.class);
@SuppressWarnings("unchecked") ResourceProvider<Object> rp3 = mock(ResourceProvider.class);
fixture.registerResourceProvider(rp, "/", AuthType.no);
rp2Info = fixture.registerResourceProvider(rp2, "/path", AuthType.lazy);
fixture.registerResourceProvider(rp3, "invalid", AuthType.no);
tracker = new ResourceProviderTracker();
tracker.setObservationReporterGenerator(new SimpleObservationReporterGenerator(new NoDothingObservationReporter()));
tracker.activate(bundleContext, eventAdmin, new DoNothingChangeListener());
}
Aggregations