use of org.osgi.service.cm.ConfigurationEvent in project feature-flags-for-osgi by amitjoy.
the class FeatureManagerProviderTest method testConfigurationEventDeleted.
@Test
public void testConfigurationEventDeleted() throws Exception {
final FeatureManagerProvider manager = new FeatureManagerProvider();
manager.setConfigurationAdmin(configurationAdmin);
manager.setMetaTypeService(metaTypeService);
manager.activate(bundleContext1);
final MetaTypeExtender extender = manager.getExtender();
final String[] pids = new String[] { "a" };
final BundleEvent bundleEvent = new BundleEvent(BundleEvent.STARTED, bundle);
when(metaTypeService.getMetaTypeInformation(bundle)).thenReturn(metaTypeInfo);
when(metaTypeInfo.getPids()).thenReturn(pids);
when(metaTypeInfo.getObjectClassDefinition("a", null)).thenReturn(ocd);
when(ocd.getAttributeDefinitions(ALL)).thenReturn(new AttributeDefinition[] { ad });
mockADWithoutDefaultValue();
when(bundleContext1.getBundle(0)).thenReturn(systemBundle);
when(bundle.getState()).thenReturn(ACTIVE);
when(bundle.getBundleContext()).thenReturn(bundleContext1);
extender.addingBundle(bundle, bundleEvent);
Thread.sleep(1000);
FeatureDTO feature = manager.getFeatures().collect(Collectors.toList()).get(0);
assertEquals(FEATURE_ID, feature.id);
assertEquals(FEATURE_DESC, feature.description);
assertFalse(feature.isEnabled);
feature = manager.getFeatures(FEATURE_ID).findFirst().get();
assertEquals(FEATURE_ID, feature.id);
assertEquals(FEATURE_DESC, feature.description);
assertFalse(feature.isEnabled);
feature = manager.getFeatures(FEATURE_ID).findAny().get();
assertEquals(FEATURE_DESC, feature.description);
assertFalse(feature.isEnabled);
final ConfigurationEvent configEvent = new ConfigurationEvent(reference, 2, null, "a");
manager.configurationEvent(configEvent);
final List<FeatureDTO> newFeatures = manager.getFeatures().collect(Collectors.toList());
assertTrue(newFeatures.isEmpty());
}
use of org.osgi.service.cm.ConfigurationEvent in project felix by apache.
the class LocationTest method testLocationChangeToRegionBinding.
@Test
public void testLocationChangeToRegionBinding() throws Exception {
final String pid = COMPONENT_NAME;
checkConfigurationCount(pid, 0, -1);
TestCase.assertNull(SimpleComponent.INSTANCE);
Configuration config = configure(pid);
delay();
findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
TestCase.assertNotNull(SimpleComponent.INSTANCE);
TestCase.assertEquals(PROP_NAME, SimpleComponent.INSTANCE.getProperty(PROP_NAME));
Bundle b2 = installBundle(descriptorFile, COMPONENT_PACKAGE, "simplecomponent2", "0.0.11", null);
b2.start();
checkConfigurationCount(b2, pid, 1, -1);
bundle.stop();
delay();
checkConfigurationCount(b2, pid, 1, -1);
ConfigurationListener listener = new ConfigurationListener() {
public void configurationEvent(ConfigurationEvent event) {
if (event.getType() == ConfigurationEvent.CM_LOCATION_CHANGED) {
eventReceived = true;
}
}
};
ServiceRegistration<ConfigurationListener> sr = bundleContext.registerService(ConfigurationListener.class, listener, null);
config.setBundleLocation(REGION);
delay();
if (eventReceived) {
checkConfigurationCount(b2, pid, 1, ComponentConfigurationDTO.ACTIVE);
}
sr.unregister();
}
use of org.osgi.service.cm.ConfigurationEvent in project felix by apache.
the class LocationTest method testLocationBinding.
/*
* tests that ds does not override a dynamic (null) location binding.
*/
@Test
public void testLocationBinding() throws Exception {
final String pid = COMPONENT_NAME;
deleteConfig(pid);
delay();
checkConfigurationCount(pid, 0, -1);
TestCase.assertNull(SimpleComponent.INSTANCE);
Configuration config = configure(pid);
delay();
findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
TestCase.assertNotNull(SimpleComponent.INSTANCE);
TestCase.assertEquals(PROP_NAME, SimpleComponent.INSTANCE.getProperty(PROP_NAME));
// dynamic (null) bundle location not overridden by ds, so all bundles can use the config.
Bundle b2 = installBundle(descriptorFile, COMPONENT_PACKAGE, "simplecomponent2", "0.0.11", null);
b2.start();
checkConfigurationCount(b2, pid, 1, -1);
bundle.stop();
delay();
checkConfigurationCount(b2, pid, 1, -1);
ConfigurationListener listener = new ConfigurationListener() {
public void configurationEvent(ConfigurationEvent event) {
if (event.getType() == ConfigurationEvent.CM_LOCATION_CHANGED) {
eventReceived = true;
}
}
};
ServiceRegistration<ConfigurationListener> sr = bundleContext.registerService(ConfigurationListener.class, listener, null);
config.setBundleLocation(null);
delay();
if (eventReceived) {
checkConfigurationCount(b2, pid, 1, ComponentConfigurationDTO.ACTIVE);
}
sr.unregister();
}
use of org.osgi.service.cm.ConfigurationEvent in project felix by apache.
the class ConfigInstallerTest method testUseExistingConfigWithFileinstallFilenameAndObserveCMDeleted.
public void testUseExistingConfigWithFileinstallFilenameAndObserveCMDeleted() throws Exception {
File file = File.createTempFile("test", ".config");
try (OutputStream os = new FileOutputStream(file)) {
os.write("key=F\"1137191584\"\n".getBytes("UTF-8"));
}
String pid = "test";
Capture<Dictionary<String, Object>> propsCapture = new Capture<>();
Dictionary<String, Object> props = new Hashtable<>();
props.put(DirectoryWatcher.FILENAME, file.toURI().toString());
EasyMock.expect(mockConfiguration.getProperties()).andReturn(props);
EasyMock.expect(mockBundleContext.getProperty((String) EasyMock.anyObject())).andReturn(null).anyTimes();
EasyMock.expect(mockConfigurationAdmin.listConfigurations((String) EasyMock.anyObject())).andReturn(null);
EasyMock.expect(mockConfigurationAdmin.getConfiguration(pid, "?")).andReturn(mockConfiguration);
EasyMock.expect(mockConfiguration.getPid()).andReturn(pid);
ServiceReference<ConfigurationAdmin> sr = EasyMock.createMock(ServiceReference.class);
mockConfiguration.update(EasyMock.capture(propsCapture));
EasyMock.expectLastCall();
EasyMock.replay(mockConfiguration, mockConfigurationAdmin, mockBundleContext, sr);
ConfigInstaller ci = new ConfigInstaller(mockBundleContext, mockConfigurationAdmin, new FileInstall());
ci.doConfigurationEvent(new ConfigurationEvent(sr, ConfigurationEvent.CM_UPDATED, null, pid));
ci.doConfigurationEvent(new ConfigurationEvent(sr, ConfigurationEvent.CM_DELETED, null, pid));
assertFalse("Configuration file should be deleted", file.isFile());
}
use of org.osgi.service.cm.ConfigurationEvent in project felix by apache.
the class ConfigInstallerTest method testUseExistingConfigAndObserveCMDeleted.
public void testUseExistingConfigAndObserveCMDeleted() throws Exception {
String pid = "test";
Capture<Dictionary<String, Object>> props = new Capture<>();
EasyMock.expect(mockConfiguration.getProperties()).andReturn(null);
EasyMock.expect(mockBundleContext.getProperty((String) EasyMock.anyObject())).andReturn(null).anyTimes();
EasyMock.expect(mockConfigurationAdmin.listConfigurations((String) EasyMock.anyObject())).andReturn(null);
EasyMock.expect(mockConfigurationAdmin.getConfiguration(pid, "?")).andReturn(mockConfiguration);
ServiceReference<ConfigurationAdmin> sr = EasyMock.createMock(ServiceReference.class);
mockConfiguration.update(EasyMock.capture(props));
EasyMock.expectLastCall();
EasyMock.replay(mockConfiguration, mockConfigurationAdmin, mockBundleContext, sr);
ConfigInstaller ci = new ConfigInstaller(mockBundleContext, mockConfigurationAdmin, new FileInstall());
ci.doConfigurationEvent(new ConfigurationEvent(sr, ConfigurationEvent.CM_UPDATED, null, pid));
ci.doConfigurationEvent(new ConfigurationEvent(sr, ConfigurationEvent.CM_DELETED, null, pid));
}
Aggregations