public virtual void TestGetPropertyWithDynamicSource() { TestDynamicConfigurationSource source = CreateDynamicSource(); IConfigurationManager manager = CreateManager(new Dictionary <int, IConfigurationSource>() { { 1, source } }); PropertyConfig <string, string> propertyConfig = ConfigurationProperties.NewConfigBuilder <string, string>() .SetKey("exist").Build(); IProperty <string, string> property = manager.GetProperty(propertyConfig); Console.WriteLine("property: " + property + "\n"); Assert.Equal("ok.2", property.Value); source.SetPropertyValue("exist", "okx"); Console.WriteLine("property: " + property + "\n"); Assert.Equal("okx", property.Value); source.SetPropertyValue("exist", "ok.2"); Console.WriteLine("property: " + property + "\n"); Assert.Equal("ok.2", property.Value); ObjectReference <bool> touched = new ObjectReference <bool>(); property.OnChange += (o, e) => touched.Value = true; property.OnChange += (o, e) => Console.WriteLine("property: {0}, changeTime: {1}, from: {2}, to: {3}\n", e.Property, e.ChangeTime, e.OldValue, e.NewValue); source.SetPropertyValue("exist", "okx"); Console.WriteLine("property: " + property + "\n"); Assert.Equal("okx", property.Value); Assert.True(touched.Value); }
public virtual void TestDuplicatePrioritySource() { TestConfigurationSource source1 = CreateSource(); TestDynamicConfigurationSource source2 = CreateDynamicSource(); Assert.Throws <ArgumentException>( () => ConfigurationManagers.NewConfigBuilder().AddSource(1, source1).AddSource(1, source2)); }
public virtual void TestGetPropertyWithComparator() { TestDynamicConfigurationSource source = CreateDynamicSource(); IConfigurationManager manager = CreateManager(new Dictionary <int, IConfigurationSource>() { { 1, source } }); HashSet <string> equalsSet = new HashSet <string>() { "e.1", "e.2" }; Func <string, string, int> customComparator = (o1, o2) => { if (equalsSet.Contains(o1) && equalsSet.Contains(o2)) { return(0); } return(o1 == o2 ? 0 : -1); }; PropertyConfig <string, string> propertyConfig = ConfigurationProperties.NewConfigBuilder <string, string>() .SetKey("exist").SetValueComparator(customComparator).Build(); IProperty <string, string> property = manager.GetProperty(propertyConfig); Console.WriteLine("property: " + property + "\n"); Assert.Equal("ok.2", property.Value); source.SetPropertyValue("exist", "okx"); Console.WriteLine("property: " + property + "\n"); Assert.Equal("okx", property.Value); source.SetPropertyValue("exist", "ok.2"); Console.WriteLine("property: " + property + "\n"); Assert.Equal("ok.2", property.Value); ObjectReference <bool> touched = new ObjectReference <bool>(); property.OnChange += (o, e) => touched.Value = true; property.OnChange += (o, e) => Console.WriteLine("property: {0}, changeTime: {1}, from: {2}, to: {3}\n", e.Property, e.ChangeTime, e.OldValue, e.NewValue); source.SetPropertyValue("exist", "okx"); Console.WriteLine("property: " + property + "\n"); Assert.Equal("okx", property.Value); Assert.True(touched.Value); source.SetPropertyValue("exist", "e.1"); Console.WriteLine("property: " + property + "\n"); Assert.Equal("e.1", property.Value); source.SetPropertyValue("exist", "e.2"); Console.WriteLine("property: " + property + "\n"); Assert.Equal("e.1", property.Value); source.SetPropertyValue("exist", "n.1"); Console.WriteLine("property: " + property + "\n"); Assert.Equal("n.1", property.Value); }
protected virtual TestDynamicConfigurationSource CreateDynamicSource() { ConfigurationSourceConfig sourceConfig = ConfigurationSources.NewConfig("test-source"); Dictionary <string, string> properties = new Dictionary <string, string>(); properties["exist"] = "ok.2"; properties["exist2"] = "ok2.2"; properties["exist3"] = "ok3.2"; properties["exist4"] = "ok4.2"; TestDynamicConfigurationSource source = new TestDynamicConfigurationSource(sourceConfig, properties); Console.WriteLine("source config: " + sourceConfig + "\n"); return(source); }
public void TestPropertySource() { IConfigurationSource source = CreateSource(); IConfigurationManager manager = CreateManager( new Dictionary <int, IConfigurationSource>() { { 1, source } }); string key = "not-exist"; PropertyConfig <string, string> propertyConfig = ConfigurationProperties.NewConfigBuilder <string, string>() .SetKey(key).Build(); IProperty <string, string> property = manager.GetProperty(propertyConfig); Assert.Null(property.Source); key = "exist"; propertyConfig = ConfigurationProperties.NewConfigBuilder <string, string>().SetKey(key).Build(); property = manager.GetProperty(propertyConfig); Assert.Equal(source, property.Source); TestDynamicConfigurationSource dynamicSource = CreateDynamicSource(); manager = CreateManager(new Dictionary <int, IConfigurationSource>() { { 1, dynamicSource } }); key = "not-exist"; propertyConfig = ConfigurationProperties.NewConfigBuilder <string, string>().SetKey(key) .Build(); property = manager.GetProperty(propertyConfig); Assert.Null(property.Source); dynamicSource.SetPropertyValue(key, "ok"); Assert.Equal(dynamicSource, property.Source); key = "exist"; propertyConfig = ConfigurationProperties.NewConfigBuilder <string, string>().SetKey(key).Build(); property = manager.GetProperty(propertyConfig); Assert.Equal(dynamicSource, property.Source); dynamicSource.SetPropertyValue(key, null); Assert.Null(property.Source); }
public virtual void TestGetPropertiesMultipleSource() { TestConfigurationSource source1 = CreateSource(); TestDynamicConfigurationSource source2 = CreateDynamicSource(); IConfigurationManager manager = CreateManager( new Dictionary <int, IConfigurationSource>() { { 1, source1 }, { 2, source2 }, }); PropertyConfig <string, string> propertyConfig = ConfigurationProperties.NewConfigBuilder <string, string>() .SetKey("not-exist").Build(); IProperty <string, string> property = manager.GetProperty(propertyConfig); Console.WriteLine("property: " + property + "\n"); Assert.Null(property.Value); propertyConfig = ConfigurationProperties.NewConfigBuilder <string, string>().SetKey("not-exist2") .SetDefaultValue("default").Build(); property = manager.GetProperty(propertyConfig); Console.WriteLine("property: " + property + "\n"); Assert.Equal("default", property.Value); propertyConfig = ConfigurationProperties.NewConfigBuilder <string, string>().SetKey("exist") .SetDefaultValue("default").Build(); property = manager.GetProperty(propertyConfig); Console.WriteLine("property: " + property + "\n"); Assert.Equal("ok.2", property.Value); propertyConfig = ConfigurationProperties.NewConfigBuilder <string, string>().SetKey("exist5") .SetDefaultValue("default").Build(); property = manager.GetProperty(propertyConfig); Console.WriteLine("property: " + property + "\n"); Assert.Equal("ok5", property.Value); source2.SetPropertyValue("exist5", "ok5.2"); Console.WriteLine("property: " + property + "\n"); Assert.Equal("ok5.2", property.Value); source2.SetPropertyValue("exist5", null); Console.WriteLine("property: " + property + "\n"); Assert.Equal("ok5", property.Value); }
public void TestPropertyConfigStatic() { TestDynamicConfigurationSource source = CreateDynamicSource(); IConfigurationManager manager = CreateManager(new Dictionary <int, IConfigurationSource>() { { 1, source } }); bool isStatic = false; string key = "exist"; PropertyConfig <string, string> propertyConfig = ConfigurationProperties.NewConfigBuilder <string, string>() .SetKey(key).SetStatic(isStatic).Build(); Assert.Equal(isStatic, propertyConfig.IsStatic); IProperty <string, string> property = manager.GetProperty(propertyConfig); Assert.Equal(isStatic, property.Config.IsStatic); Assert.Equal("ok.2", property.Value); string value = manager.GetPropertyValue(propertyConfig); Assert.Equal("ok.2", value); source.SetPropertyValue(key, null); Assert.Null(property.Value); value = manager.GetPropertyValue(propertyConfig); Assert.Null(value); isStatic = true; key = "exist2"; propertyConfig = ConfigurationProperties.NewConfigBuilder <string, string>().SetKey(key) .SetStatic(isStatic).Build(); Assert.Equal(isStatic, propertyConfig.IsStatic); property = manager.GetProperty(propertyConfig); Assert.Equal(isStatic, property.Config.IsStatic); Assert.Equal("ok2.2", property.Value); value = manager.GetPropertyValue(propertyConfig); Assert.Equal("ok2.2", value); source.SetPropertyValue(key, null); Assert.Equal("ok2.2", property.Value); value = manager.GetPropertyValue(propertyConfig); Assert.Null(value); }
public void testPropertyConfigRequiredDynamic() { TestDynamicConfigurationSource source = CreateDynamicSource(); IConfigurationManager manager = CreateManager( new Dictionary <int, IConfigurationSource>() { { 1, source } }); bool required = false; string key = "exist"; PropertyConfig <string, string> propertyConfig = ConfigurationProperties .NewConfigBuilder <string, string>().SetKey(key).SetRequired(required).Build(); Assert.Equal(required, propertyConfig.IsRequired); IProperty <string, string> property = manager.GetProperty(propertyConfig); Assert.Equal(required, property.Config.IsRequired); Assert.Equal("ok.2", property.Value); string value = manager.GetPropertyValue(propertyConfig); Assert.Equal("ok.2", value); source.SetPropertyValue(key, null); Assert.Null(property.Value); value = manager.GetPropertyValue(propertyConfig); Assert.Null(value); required = true; key = "exist2"; propertyConfig = ConfigurationProperties.NewConfigBuilder <string, string>().SetKey(key) .SetRequired(required).Build(); Assert.Equal(required, propertyConfig.IsRequired); property = manager.GetProperty(propertyConfig); Assert.Equal(required, property.Config.IsRequired); Assert.Equal("ok2.2", property.Value); value = manager.GetPropertyValue(propertyConfig); Assert.Equal("ok2.2", value); source.SetPropertyValue(key, null); Assert.Equal("ok2.2", property.Value); Assert.Throws <InvalidOperationException>(() => manager.GetPropertyValue(propertyConfig)); }
public virtual void TestChangeListener() { TestDynamicConfigurationSource source = CreateDynamicSource(); IConfigurationManager manager = CreateManager(new Dictionary <int, IConfigurationSource>() { { 1, source } }); ObjectReference <int> changeCount = new ObjectReference <int>(); manager.OnChange += (o, e) => { changeCount.Value = changeCount.Value + 1; Console.WriteLine("property changed: " + e); }; PropertyConfig <string, string> propertyConfig = ConfigurationProperties.NewConfigBuilder <string, string>() .SetKey("exist").Build(); IProperty <string, string> property = manager.GetProperty(propertyConfig); ObjectReference <int> changeCount2 = new ObjectReference <int>(); property.OnChange += (o, e) => changeCount2.Value = changeCount2.Value + 1; source.SetPropertyValue("exist", "okx"); Assert.Equal(1, changeCount.Value); Assert.Equal(1, changeCount2.Value); source.SetPropertyValue("exist", "ok.2"); Assert.Equal(2, changeCount.Value); Assert.Equal(2, changeCount2.Value); source.SetPropertyValue("exist", "okx"); Assert.Equal(3, changeCount.Value); Assert.Equal(3, changeCount2.Value); // value not change, no change event source.SetPropertyValue("exist", "okx"); Assert.Equal(3, changeCount.Value); Assert.Equal(3, changeCount2.Value); }