use of org.osgi.service.cm.Configuration in project aries by apache.
the class ConfigurationAdminTest method testDelete.
@Test
public void testDelete() throws Exception {
org.osgi.service.cm.ConfigurationAdmin admin = mock(org.osgi.service.cm.ConfigurationAdmin.class);
String pid = "org.apache.aries.jmx.mock";
Configuration config = mock(Configuration.class);
when(admin.getConfiguration(pid, null)).thenReturn(config);
ConfigurationAdmin mbean = new ConfigurationAdmin(admin);
mbean.delete(pid);
verify(config).delete();
reset(config);
when(admin.getConfiguration(pid, "location")).thenReturn(config);
mbean.deleteForLocation(pid, "location");
verify(config).delete();
}
use of org.osgi.service.cm.Configuration in project aries by apache.
the class ConfigurationAdminTest method testGetProperties.
@Test
public void testGetProperties() throws Exception {
org.osgi.service.cm.ConfigurationAdmin admin = mock(org.osgi.service.cm.ConfigurationAdmin.class);
String pid = "org.apache.aries.jmx.mock";
Configuration config = mock(Configuration.class);
Dictionary<String, Object> props = new Hashtable<String, Object>();
props.put("one", "value");
props.put("two", 2);
when(admin.getConfiguration(eq(pid), anyString())).thenReturn(config);
when(config.getProperties()).thenReturn(props);
ConfigurationAdmin mbean = new ConfigurationAdmin(admin);
TabularData properties = mbean.getPropertiesForLocation(pid, null);
assertNotNull(properties);
assertEquals(PROPERTIES_TYPE, properties.getTabularType());
assertEquals(2, properties.values().size());
PropertyData<? extends Object> oneData = PropertyData.from(properties.get(new Object[] { "one" }));
assertEquals("value", oneData.getValue());
PropertyData<? extends Object> twoData = PropertyData.from(properties.get(new Object[] { "two" }));
assertEquals(2, twoData.getValue());
assertEquals("2", twoData.getEncodedValue());
}
use of org.osgi.service.cm.Configuration in project aries by apache.
the class ConfigurationAdminTest method testSetBundleLocation.
@Test
public void testSetBundleLocation() throws Exception {
org.osgi.service.cm.ConfigurationAdmin admin = mock(org.osgi.service.cm.ConfigurationAdmin.class);
String pid = "org.apache.aries.jmx.mock";
Configuration config = mock(Configuration.class);
when(admin.getConfiguration(pid, null)).thenReturn(config);
ConfigurationAdmin mbean = new ConfigurationAdmin(admin);
mbean.setBundleLocation(pid, "file:/newlocation");
ArgumentCaptor<String> locationArgument = ArgumentCaptor.forClass(String.class);
verify(config).setBundleLocation(locationArgument.capture());
assertEquals("file:/newlocation", locationArgument.getValue());
}
use of org.osgi.service.cm.Configuration in project aries by apache.
the class ConfigurationAdminTest method testGetConfigurations.
@Test
public void testGetConfigurations() throws Exception {
org.osgi.service.cm.ConfigurationAdmin admin = mock(org.osgi.service.cm.ConfigurationAdmin.class);
String factoryPid = "org.apache.aries.jmx.factory.mock";
String filter = "(" + org.osgi.service.cm.ConfigurationAdmin.SERVICE_FACTORYPID + "=org.apache.aries.jmx.factory.mock)";
String location = "../location";
Configuration a = mock(Configuration.class);
when(a.getPid()).thenReturn(factoryPid + "-2160133952674-0");
when(a.getBundleLocation()).thenReturn(location);
Configuration b = mock(Configuration.class);
when(b.getPid()).thenReturn(factoryPid + "-1260133982371-1");
when(b.getBundleLocation()).thenReturn(location);
when(admin.listConfigurations(filter)).thenReturn(new Configuration[] { a, b });
ConfigurationAdmin mbean = new ConfigurationAdmin(admin);
String[][] result = mbean.getConfigurations(filter);
assertEquals(2, result.length);
assertArrayEquals(new String[] { factoryPid + "-2160133952674-0", location }, result[0]);
assertArrayEquals(new String[] { factoryPid + "-1260133982371-1", location }, result[1]);
}
use of org.osgi.service.cm.Configuration in project aries by apache.
the class ComponentTest method testComponent.
@Test
public void testComponent() throws IOException {
OSGi<?> program = configurations("org.components.MyComponent").flatMap(props -> services(Service.class).flatMap(ms -> just(new Component(props, ms)).flatMap(component -> register(Component.class, component, new HashMap<>()).distribute(ign -> dynamic(highestService(ServiceOptional.class), component::setOptional, c -> component.setOptional(null)), ign -> dynamic(services(ServiceForList.class), component::addService, component::removeService)))));
ServiceTracker<Component, Component> serviceTracker = new ServiceTracker<>(_bundleContext, Component.class, null);
serviceTracker.open();
CountDownLatch countDownLatch = new CountDownLatch(1);
_bundleContext.registerService(ManagedService.class, dictionary -> countDownLatch.countDown(), new Hashtable<String, Object>() {
{
put("service.pid", "org.components.MyComponent");
}
});
Configuration factoryConfiguration = null;
try (OSGiResult<?> run = program.run(_bundleContext)) {
factoryConfiguration = _configurationAdmin.createFactoryConfiguration("org.components.MyComponent");
factoryConfiguration.update(new Hashtable<>());
countDownLatch.await(10, TimeUnit.SECONDS);
assertNull(serviceTracker.getService());
ServiceRegistration<Service> serviceRegistration = _bundleContext.registerService(Service.class, new Service(), new Hashtable<>());
Component component = serviceTracker.waitForService(10 * 1000);
assertNotNull(component);
assertNull(component.getOptional());
ServiceRegistration<ServiceOptional> serviceRegistration2 = _bundleContext.registerService(ServiceOptional.class, new ServiceOptional(), new Hashtable<>());
Thread.sleep(1000L);
assertNotNull(component.getOptional());
ServiceOptional serviceOptional = new ServiceOptional();
ServiceRegistration<ServiceOptional> serviceRegistration3 = _bundleContext.registerService(ServiceOptional.class, serviceOptional, new Hashtable<String, Object>() {
{
put("service.ranking", 1);
}
});
assertEquals(serviceOptional, component.getOptional());
serviceRegistration3.unregister();
assertNotNull(component.getOptional());
serviceRegistration2.unregister();
assertNull(component.getOptional());
ServiceRegistration<ServiceForList> serviceRegistration4 = _bundleContext.registerService(ServiceForList.class, new ServiceForList(), new Hashtable<>());
ServiceRegistration<ServiceForList> serviceRegistration5 = _bundleContext.registerService(ServiceForList.class, new ServiceForList(), new Hashtable<>());
assertEquals(2, component.getServiceForLists().size());
serviceRegistration4.unregister();
assertEquals(1, component.getServiceForLists().size());
serviceRegistration5.unregister();
assertEquals(0, component.getServiceForLists().size());
serviceRegistration.unregister();
assertNull(serviceTracker.getService());
} catch (IOException ioe) {
} catch (InterruptedException e) {
Assert.fail("Timeout waiting for configuration");
} finally {
serviceTracker.close();
if (factoryConfiguration != null) {
factoryConfiguration.delete();
}
}
}
Aggregations