Search in sources :

Example 31 with BundleContext

use of org.osgi.framework.BundleContext in project sling by apache.

the class AdapterManagerTest method createComponentContext.

/**
     * Helper method to create a mock component context
     */
protected ComponentContext createComponentContext() throws Exception {
    final BundleContext bundleCtx = this.context.mock(BundleContext.class);
    final Filter filter = this.context.mock(Filter.class);
    final ComponentContext ctx = this.context.mock(ComponentContext.class);
    this.context.checking(new Expectations() {

        {
            allowing(ctx).locateService(with(any(String.class)), with(any(ServiceReference.class)));
            will(returnValue(new MockAdapterFactory()));
            allowing(ctx).getBundleContext();
            will(returnValue(bundleCtx));
            allowing(bundleCtx).createFilter(with(any(String.class)));
            will(returnValue(filter));
            allowing(bundleCtx).addServiceListener(with(any(ServiceListener.class)), with(any(String.class)));
            allowing(bundleCtx).getServiceReferences(with(any(String.class)), with(any(String.class)));
            will(returnValue(null));
            allowing(bundleCtx).removeServiceListener(with(any(ServiceListener.class)));
            allowing(bundleCtx).registerService(with(Adaption.class), with(AdaptionImpl.INSTANCE), with(any(Dictionary.class)));
            will(returnValue(null));
        }
    });
    return ctx;
}
Also used : Expectations(org.jmock.Expectations) Dictionary(java.util.Dictionary) ServiceListener(org.osgi.framework.ServiceListener) ComponentContext(org.osgi.service.component.ComponentContext) Filter(org.osgi.framework.Filter) MockAdapterFactory(org.apache.sling.adapter.mock.MockAdapterFactory) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Example 32 with BundleContext

use of org.osgi.framework.BundleContext in project sling by apache.

the class MockFactory method mockComponentContext.

public static ComponentContext mockComponentContext() {
    Mockery context = new JUnit4Mockery();
    final BundleContext bc = context.mock(BundleContext.class);
    context.checking(new Expectations() {

        {
            allowing(bc).registerService(with(any(String.class)), with(any(Object.class)), with(any(Dictionary.class)));
            will(VoidAction.INSTANCE);
            allowing(bc).getProperty(with(any(String.class)));
            will(new ReturnValueAction("foo"));
        }
    });
    final ComponentContext cc = context.mock(ComponentContext.class);
    context.checking(new Expectations() {

        {
            allowing(cc).getProperties();
            will(returnValue(new Properties()));
            allowing(cc).getBundleContext();
            will(returnValue(bc));
        }
    });
    return cc;
}
Also used : Expectations(org.jmock.Expectations) Dictionary(java.util.Dictionary) ReturnValueAction(org.jmock.lib.action.ReturnValueAction) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) ComponentContext(org.osgi.service.component.ComponentContext) Properties(java.util.Properties) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) Mockery(org.jmock.Mockery) BundleContext(org.osgi.framework.BundleContext)

Example 33 with BundleContext

use of org.osgi.framework.BundleContext in project sling by apache.

the class VotingHandler method registerEventHandler.

private void registerEventHandler() {
    BundleContext bundleContext = context == null ? null : context.getBundleContext();
    if (bundleContext == null) {
        logger.info("registerEventHandler: context or bundleContext is null - cannot register");
        return;
    }
    Dictionary<String, Object> properties = new Hashtable<String, Object>();
    properties.put(Constants.SERVICE_DESCRIPTION, "Voting Event Listener");
    String[] topics = new String[] { SlingConstants.TOPIC_RESOURCE_ADDED, SlingConstants.TOPIC_RESOURCE_CHANGED, SlingConstants.TOPIC_RESOURCE_REMOVED };
    properties.put(EventConstants.EVENT_TOPIC, topics);
    String path = config.getDiscoveryResourcePath();
    if (path.endsWith("/")) {
        path = path.substring(0, path.length() - 1);
    }
    path = path + "/*";
    properties.put(EventConstants.EVENT_FILTER, "(&(path=" + path + "))");
    eventHandlerRegistration = bundleContext.registerService(EventHandler.class.getName(), this, properties);
    logger.info("registerEventHandler: VotingHandler registered as EventHandler");
}
Also used : Hashtable(java.util.Hashtable) BundleContext(org.osgi.framework.BundleContext)

Example 34 with BundleContext

use of org.osgi.framework.BundleContext in project sling by apache.

the class JobConsumerManagerTest method testSimpleMappingConsumer.

@Test
public void testSimpleMappingConsumer() {
    final BundleContext bc = Mockito.mock(BundleContext.class);
    final JobConsumerManager jcs = new JobConsumerManager();
    jcs.activate(bc, getDefaultConfig());
    final JobConsumer jc1 = Mockito.mock(JobConsumer.class);
    final ServiceReference ref1 = Mockito.mock(ServiceReference.class);
    Mockito.when(ref1.getProperty(JobConsumer.PROPERTY_TOPICS)).thenReturn("a/b");
    Mockito.when(ref1.getProperty(Constants.SERVICE_RANKING)).thenReturn(1);
    Mockito.when(ref1.getProperty(Constants.SERVICE_ID)).thenReturn(1L);
    Mockito.when(bc.getService(ref1)).thenReturn(jc1);
    jcs.bindJobConsumer(ref1);
    assertNotNull(jcs.getExecutor("a/b"));
    assertNull(jcs.getExecutor("a"));
    assertNull(jcs.getExecutor("a/c"));
    assertNull(jcs.getExecutor("a/b/a"));
}
Also used : JobConsumer(org.apache.sling.event.jobs.consumer.JobConsumer) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 35 with BundleContext

use of org.osgi.framework.BundleContext in project sling by apache.

the class JobConsumerManagerTest method testSubCategoryMappingConsumer.

@Test
public void testSubCategoryMappingConsumer() {
    final BundleContext bc = Mockito.mock(BundleContext.class);
    final JobConsumerManager jcs = new JobConsumerManager();
    jcs.activate(bc, getDefaultConfig());
    final JobConsumer jc1 = Mockito.mock(JobConsumer.class);
    final ServiceReference ref1 = Mockito.mock(ServiceReference.class);
    Mockito.when(ref1.getProperty(JobConsumer.PROPERTY_TOPICS)).thenReturn("a/**");
    Mockito.when(ref1.getProperty(Constants.SERVICE_RANKING)).thenReturn(1);
    Mockito.when(ref1.getProperty(Constants.SERVICE_ID)).thenReturn(1L);
    Mockito.when(bc.getService(ref1)).thenReturn(jc1);
    jcs.bindJobConsumer(ref1);
    assertNotNull(jcs.getExecutor("a/b"));
    assertNull(jcs.getExecutor("a"));
    assertNotNull(jcs.getExecutor("a/c"));
    assertNotNull(jcs.getExecutor("a/b/a"));
}
Also used : JobConsumer(org.apache.sling.event.jobs.consumer.JobConsumer) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

BundleContext (org.osgi.framework.BundleContext)1052 Test (org.junit.Test)361 Bundle (org.osgi.framework.Bundle)298 ServiceReference (org.osgi.framework.ServiceReference)229 File (java.io.File)119 Hashtable (java.util.Hashtable)114 HashMap (java.util.HashMap)98 ArrayList (java.util.ArrayList)97 IOException (java.io.IOException)75 ServiceRegistration (org.osgi.framework.ServiceRegistration)68 BundleException (org.osgi.framework.BundleException)60 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)59 Equinox (org.eclipse.osgi.launch.Equinox)51 ComponentContext (org.osgi.service.component.ComponentContext)50 Dictionary (java.util.Dictionary)48 Before (org.junit.Before)48 Properties (java.util.Properties)47 LinkedHashMap (java.util.LinkedHashMap)46 URL (java.net.URL)43 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)40