/// <summary> /// Apply the polled result to the configuration. /// If the polled result is full result from source, each property in the result is either added to set /// to the configuration, and any property that is in the configuration but not in the result is deleted if IgnoreDeletesFromSource /// is false. If the polled result is incremental, properties added and changed in the partial result /// are set with the configuration, and deleted properties are deleted form configuration if ignoreDeletesFromSource /// is false. /// </summary> /// <param name="result"></param> /// <param name="config"></param> protected void PopulateProperties(PollResult result, IConfiguration config) { if (result == null || !result.HasChanges) { return; } if (!result.Incremental) { var props = result.Complete; if (props == null) { return; } foreach (var prop in props) { m_PropertyUpdater.AddOrChangeProperty(prop.Key, prop.Value, config); } var existingKeys = new HashSet <string>(config.Keys); if (!IgnoreDeletesFromSource) { foreach (string key in existingKeys.Where(k => !props.ContainsKey(k))) { m_PropertyUpdater.DeleteProperty(key, config); } } } else { var props = result.Added; if (props != null) { foreach (var prop in props) { m_PropertyUpdater.AddOrChangeProperty(prop.Key, prop.Value, config); } } props = result.Changed; if (props != null) { foreach (var prop in props) { m_PropertyUpdater.AddOrChangeProperty(prop.Key, prop.Value, config); } } if (!IgnoreDeletesFromSource) { props = result.Deleted; if (props != null) { foreach (var key in props.Keys) { m_PropertyUpdater.DeleteProperty(key, config); } } } } }
public void TestAddOrChangeProperty() { AbstractConfiguration.DefaultListDelimiter = ','; AbstractConfiguration config = new ConcurrentCompositeConfiguration(); config.ConfigurationChanged += OnConfigurationChanged; ResetEventCount(); config.SetProperty("test.host", "test,test1,test2"); Assert.AreEqual(1, m_EventCount); m_DynamicPropertyUpdater.AddOrChangeProperty("test.host", "test,test1,test2", config); Assert.AreEqual(3, ((IList)(config.GetProperty("test.host"))).Count); Assert.IsTrue(((IList)(config.GetProperty("test.host"))).Contains("test")); Assert.IsTrue(((IList)(config.GetProperty("test.host"))).Contains("test1")); Assert.IsTrue(((IList)(config.GetProperty("test.host"))).Contains("test2")); Assert.AreEqual(1, m_EventCount); m_DynamicPropertyUpdater.AddOrChangeProperty("test.host", "test,test1,test2", config); Assert.AreEqual(3, ((IList)(config.GetProperty("test.host"))).Count); Assert.IsTrue(((IList)(config.GetProperty("test.host"))).Contains("test")); Assert.IsTrue(((IList)(config.GetProperty("test.host"))).Contains("test1")); Assert.IsTrue(((IList)(config.GetProperty("test.host"))).Contains("test2")); Assert.AreEqual(1, m_EventCount); m_DynamicPropertyUpdater.AddOrChangeProperty("test.host", "test,test1", config); Assert.AreEqual(2, ((IList)(config.GetProperty("test.host"))).Count); Assert.IsTrue(((IList)(config.GetProperty("test.host"))).Contains("test")); Assert.IsTrue(((IList)(config.GetProperty("test.host"))).Contains("test1")); Assert.AreEqual(2, m_EventCount); m_DynamicPropertyUpdater.AddOrChangeProperty("test.host1", "test1,test12", config); Assert.AreEqual(2, ((IList)(config.GetProperty("test.host1"))).Count); Assert.IsTrue(((IList)(config.GetProperty("test.host1"))).Contains("test1")); Assert.IsTrue(((IList)(config.GetProperty("test.host1"))).Contains("test12")); Assert.AreEqual(3, m_EventCount); config.SetProperty("test.host1", "test1.test12"); m_DynamicPropertyUpdater.AddOrChangeProperty("test.host1", "test1.test12", config); Assert.AreEqual("test1.test12", config.GetProperty("test.host1")); Assert.AreEqual(4, m_EventCount); }