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