Search in sources :

Example 1 with Fixture

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

Example 2 with Fixture

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));
}
Also used : EventAdmin(org.osgi.service.event.EventAdmin) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ChangeListener(org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker.ChangeListener) Fixture(org.apache.sling.resourceresolver.impl.Fixture) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 3 with Fixture

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));
}
Also used : EventAdmin(org.osgi.service.event.EventAdmin) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ChangeListener(org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker.ChangeListener) Fixture(org.apache.sling.resourceresolver.impl.Fixture) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 4 with Fixture

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());
}
Also used : EventAdmin(org.osgi.service.event.EventAdmin) Fixture(org.apache.sling.resourceresolver.impl.Fixture) BundleContext(org.osgi.framework.BundleContext) Before(org.junit.Before)

Aggregations

Fixture (org.apache.sling.resourceresolver.impl.Fixture)4 BundleContext (org.osgi.framework.BundleContext)4 EventAdmin (org.osgi.service.event.EventAdmin)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ChangeListener (org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker.ChangeListener)2 Before (org.junit.Before)2 Test (org.junit.Test)2 Iterator (java.util.Iterator)1 Resource (org.apache.sling.api.resource.Resource)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 ValueMap (org.apache.sling.api.resource.ValueMap)1 ResourceAccessSecurityTracker (org.apache.sling.resourceresolver.impl.ResourceAccessSecurityTracker)1 SimpleValueMapImpl (org.apache.sling.resourceresolver.impl.SimpleValueMapImpl)1 ResourceProviderHandler (org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler)1 ResourceProviderInfo (org.apache.sling.resourceresolver.impl.providers.ResourceProviderInfo)1 ResourceProviderStorage (org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage)1 ResourceProviderStorageProvider (org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorageProvider)1 ResourceProvider (org.apache.sling.spi.resource.provider.ResourceProvider)1