public virtual void TestGetProperties(string kv) { string[] commandLineArgs = kv == null ? null : new string[] { kv }; IConfigurationManager manager = CreateManager(commandLineArgs); 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); if (commandLineArgs == null) { return; } propertyConfig = ConfigurationProperties.NewConfigBuilder <String, String>().SetKey("exist") .SetDefaultValue("default").Build(); property = manager.GetProperty(propertyConfig); Console.WriteLine("property: " + property + "\n"); Assert.Equal("ok", property.Value); }
public virtual void TestDynamicProperty() { MemoryDictionaryConfigurationSource source = CreateSource(); source.SetPropertyValue("exist", "ok"); IConfigurationManager manager = CreateManager(source); PropertyConfig <String, String> propertyConfig = ConfigurationProperties.NewConfigBuilder <String, String>() .SetKey("exist").SetDefaultValue("default").Build(); IProperty <String, String> property = manager.GetProperty(propertyConfig); Console.WriteLine("property: " + property + "\n"); Assert.Equal("ok", property.Value); source.SetPropertyValue("exist", "ok2"); Thread.Sleep(10); Console.WriteLine("property: " + property + "\n"); Assert.Equal("ok2", property.Value); source.SetPropertyValue("exist", "ok3"); Thread.Sleep(10); Console.WriteLine("property: " + property + "\n"); Assert.Equal("ok3", property.Value); source.SetPropertyValue("exist", "ok4"); Thread.Sleep(10); Console.WriteLine("property: " + property + "\n"); Assert.Equal("ok4", property.Value); }
public void TestPropertyConfigRequiredDefault() { bool required = false; string key = "not-exist"; string defaultValue = "default"; IConfigurationManager manager = CreateManager( new Dictionary <int, IConfigurationSource>() { { 1, CreateSource() } }); PropertyConfig <string, string> propertyConfig = ConfigurationProperties.NewConfigBuilder <string, string>() .SetKey(key).SetDefaultValue(defaultValue).Build(); Assert.Equal(required, propertyConfig.IsRequired); IProperty <string, string> property = manager.GetProperty(propertyConfig); Assert.Equal(required, property.Config.IsRequired); string value = manager.GetPropertyValue(propertyConfig); Assert.Equal(defaultValue, value); required = true; key = "not-exist-2"; propertyConfig = ConfigurationProperties.NewConfigBuilder <string, string>().SetKey(key) .SetRequired(required).SetDefaultValue(defaultValue).Build(); Assert.Equal(required, propertyConfig.IsRequired); property = manager.GetProperty(propertyConfig); value = manager.GetPropertyValue(propertyConfig); Assert.Equal(defaultValue, value); }
public virtual void TestGetProperties() { IConfigurationManager manager = CreateManager(new Dictionary <int, IConfigurationSource>() { { 1, CreateSource() } }); 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", property.Value); PropertyConfig <string, int> propertyConfig2 = ConfigurationProperties.NewConfigBuilder <string, int>() .SetKey("exist2").Build(); IProperty <string, int> property2 = manager.GetProperty(propertyConfig2); Console.WriteLine("property: " + property2 + "\n"); Assert.Equal(0, property2.Value); }
public void TestPropertyConfigDoc() { string doc = null; string key = "not-exist"; IConfigurationManager manager = CreateManager( new Dictionary <int, IConfigurationSource>() { { 1, CreateSource() } }); PropertyConfig <string, string> propertyConfig = ConfigurationProperties .NewConfigBuilder <string, string>().SetKey(key).Build(); Assert.Equal(doc, propertyConfig.Doc); IProperty <string, string> property = manager.GetProperty(propertyConfig); Assert.Equal(doc, property.Config.Doc); doc = "test-doc"; key = "not-exist-2"; propertyConfig = ConfigurationProperties.NewConfigBuilder <string, string>() .SetKey(key).SetDoc(doc).Build(); Assert.Equal(doc, propertyConfig.Doc); property = manager.GetProperty(propertyConfig); Assert.Equal(doc, property.Config.Doc); }
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 TestGetProperties(AppSettingsType type) { String propertyValue = ConfigurationManager.AppSettings["not-exist"]; Assert.Null(propertyValue); IConfigurationManager manager = CreateManager(type); 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); if (type == AppSettingsType.AppConfig) { return; } propertyConfig = ConfigurationProperties.NewConfigBuilder <String, String>().SetKey("exist") .SetDefaultValue("default").Build(); property = manager.GetProperty(propertyConfig); Console.WriteLine("property: " + property + "\n"); Assert.Equal("ok", property.Value); }
public virtual void TestGetPropertyWithFilter() { IConfigurationManager manager = CreateManager(new Dictionary <int, IConfigurationSource>() { { 1, CreateSource() } }); PropertyConfig <string, string> propertyConfig = ConfigurationProperties.NewConfigBuilder <string, string>() .SetKey("exist").SetValueFilter(v => { if (Object.Equals("ok", v)) { return("ok_new"); } return(null); }).Build(); IProperty <string, string> property = manager.GetProperty(propertyConfig); Console.WriteLine("property: " + property + "\n"); Assert.Equal("ok_new", property.Value); propertyConfig = ConfigurationProperties.NewConfigBuilder <string, string>().SetKey("exist2") .SetValueFilter(v => { return(v.Length >= 8 && v.Length <= 32 ? v : null); }).Build(); property = manager.GetProperty(propertyConfig); Console.WriteLine("property: " + property + "\n"); Assert.Null(property.Value); }
public virtual void TestGetPropertyWithDiffFilterInSimilarConfig() { IConfigurationManager manager = CreateManager(new Dictionary <int, IConfigurationSource>() { { 1, CreateSource() } }); PropertyConfig <string, string> propertyConfig = ConfigurationProperties.NewConfigBuilder <string, string>() .SetKey("exist").SetValueFilter(v => { if (Object.Equals("ok", v)) { return("ok_new"); } return(null); }).Build(); Console.WriteLine("propertyConfig: " + propertyConfig + "\n"); IProperty <string, string> property = manager.GetProperty(propertyConfig); Assert.Equal("ok_new", property.Value); propertyConfig = ConfigurationProperties.NewConfigBuilder <string, string>().SetKey("exist") .SetValueFilter(v => { if (Object.Equals("ok", v)) { return("ok_new"); } return(null); }).Build(); Console.WriteLine("propertyConfig: " + propertyConfig + "\n"); Assert.Throws <ArgumentException>(() => manager.GetProperty(propertyConfig)); }
public void TestGetPropertyWithConverter() { IConfigurationManager manager = CreateManager( new Dictionary <int, IConfigurationSource>() { { 1, CreateSource() } }); PropertyConfig <string, int?> propertyConfig = ConfigurationProperties.NewConfigBuilder <string, int?>() .SetKey("exist_int").AddValueConverter(NewTypeConverter()).Build(); IProperty <string, int?> property = manager.GetProperty(propertyConfig); Console.WriteLine("property: " + property + "\n"); Assert.Equal(1, property.Value); propertyConfig = ConfigurationProperties.NewConfigBuilder <string, int?>() .SetKey("exist").AddValueConverter(NewTypeConverter()).Build(); property = manager.GetProperty(propertyConfig); Console.WriteLine("property: " + property + "\n"); Assert.Null(property.Value); propertyConfig = ConfigurationProperties.NewConfigBuilder <string, int?>() .SetKey("not_exist").AddValueConverter(NewTypeConverter()).Build(); property = manager.GetProperty(propertyConfig); Console.WriteLine("property: " + property + "\n"); Assert.Null(property.Value); }
public virtual void TestSameConfigSameProperty() { IConfigurationManager manager = CreateManager(new Dictionary <int, IConfigurationSource>() { { 1, CreateSource() } }); 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); IProperty <string, string> property2 = manager.GetProperty(propertyConfig); Console.WriteLine("property2: " + property + "\n"); Assert.True(Object.ReferenceEquals(property, property2)); PropertyConfig <string, string> propertyConfig2 = ConfigurationProperties.NewConfigBuilder <string, string>() .SetKey("not-exist").Build(); IProperty <string, string> property3 = manager.GetProperty(propertyConfig2); Console.WriteLine("property3: " + property2 + "\n"); Assert.True(Object.ReferenceEquals(property, property3)); }
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); }
public virtual void TestDemo() { // create a scf yaml configuration source YamlFileConfigurationSourceConfig sourceConfig = new YamlFileConfigurationSourceConfig.Builder() .SetName("yaml-file").SetFileName("test.yaml").Build(); YamlFileConfigurationSource source = new YamlFileConfigurationSource(sourceConfig); // create scf manager & properties facade tool ConfigurationManagerConfig managerConfig = ConfigurationManagers.NewConfigBuilder().SetName("my-app") .AddSource(1, source).Build(); IConfigurationManager manager = ConfigurationManagers.NewManager(managerConfig); PropertyConfig <string, bool?> propertyConfig1 = ConfigurationProperties.NewConfigBuilder <string, bool?>() .SetKey("booleanProperty").SetDefaultValue(false).Build(); bool?boolValue = manager.GetPropertyValue(propertyConfig1); Console.WriteLine(boolValue); PropertyConfig <string, int?> propertyConfig2 = ConfigurationProperties.NewConfigBuilder <string, int?>() .SetKey("intProperty").SetDefaultValue(0).Build(); int?intValue = manager.GetPropertyValue(propertyConfig2); Console.WriteLine(intValue); PropertyConfig <string, long?> propertyConfig3 = ConfigurationProperties.NewConfigBuilder <string, long?>() .SetKey("longProperty").SetDefaultValue(0L).Build(); long?longValue = manager.GetPropertyValue(propertyConfig3); Console.WriteLine(longValue); PropertyConfig <string, string> propertyConfig4 = ConfigurationProperties.NewConfigBuilder <string, string>() .SetKey("stringProperty").Build(); string stringValue = manager.GetPropertyValue(propertyConfig4); Console.WriteLine(stringValue); PropertyConfig <string, List <string> > propertyConfig5 = ConfigurationProperties .NewConfigBuilder <string, List <string> >().SetKey("listProperty").Build(); List <string> listValue = manager.GetPropertyValue(propertyConfig5); Console.WriteLine(listValue == null ? null : string.Join(", ", listValue)); PropertyConfig <string, Dictionary <string, string> > propertyConfig6 = ConfigurationProperties .NewConfigBuilder <string, Dictionary <string, string> >().SetKey("mapProperty").Build(); Dictionary <string, string> mapValue = manager.GetPropertyValue(propertyConfig6); Console.WriteLine(mapValue == null ? null : (string.Join(", ", mapValue.Select(p => string.Format("{0}: {1}", p.Key, p.Value)).ToList()))); PropertyConfig <string, TestPojo> propertyConfig7 = ConfigurationProperties.NewConfigBuilder <string, TestPojo>() .SetKey("objProperty").Build(); TestPojo objValue = manager.GetPropertyValue(propertyConfig7); Console.WriteLine(objValue); }
private static void InitConfig() { // create a non-dynamic K/V (string/string) configuration source PropertiesFileConfigurationSourceConfig sourceConfig = StringPropertySources .NewPropertiesFileSourceConfigBuilder().SetName("app-properties-file").SetFileName("app.properties") .Build(); IConfigurationSource source = StringPropertySources.NewPropertiesFileSource(sourceConfig); // create a configuration manager with single source ConfigurationManagerConfig managerConfig = ConfigurationManagers.NewConfigBuilder().SetName("little-app") .AddSource(1, source).Build(); _manager = ConfigurationManagers.NewManager(managerConfig); // default to null PropertyConfig <string, string> propertyConfig = ConfigurationProperties.NewConfigBuilder <string, string>() .SetKey("app.id").Build(); _appId = _manager.GetProperty(propertyConfig); // default to "unknown" PropertyConfig <string, string> propertyConfig2 = ConfigurationProperties.NewConfigBuilder <string, string>() .SetKey("app.name").SetDefaultValue("unknown").Build(); _appName = _manager.GetProperty(propertyConfig2); // default to empty list PropertyConfig <string, List <string> > propertyConfig3 = ConfigurationProperties .NewConfigBuilder <string, List <string> >().SetKey("user.list") .SetDefaultValue(new List <string>()).AddValueConverter(StringToListConverter.Default).Build(); _userList = _manager.GetProperty(propertyConfig3); // default to empty map PropertyConfig <string, Dictionary <string, string> > propertyConfig4 = ConfigurationProperties .NewConfigBuilder <string, Dictionary <string, string> >().SetKey("user.data") .SetDefaultValue(new Dictionary <string, string>()).AddValueConverter(StringToDictionaryConverter.Default).Build(); _userData = _manager.GetProperty(propertyConfig4); // default to 1000, if value in app._properties is invalid, ignore the invalid value PropertyConfig <string, int?> propertyConfig5 = ConfigurationProperties.NewConfigBuilder <string, int?>() .SetKey("sleep.time").SetDefaultValue(1000) .AddValueConverter(StringToIntConverter.Default).SetValueFilter(v => v < 0 ? null : v).Build(); _sleepTime = _manager.GetProperty(propertyConfig5); // custom type property PropertyConfig <string, MyCustomType> propertyConfig6 = ConfigurationProperties .NewConfigBuilder <string, MyCustomType>().SetKey("my-custom-type-property") .AddValueConverter(MyCustomType.Converter).Build(); _myCustomData = _manager.GetProperty(propertyConfig6); }
public void TestPropertyConfigRequired() { bool required = false; string key = "not-exist"; IConfigurationManager manager = CreateManager( new Dictionary <int, IConfigurationSource>() { { 1, CreateSource() } }); PropertyConfig <string, string> propertyConfig = ConfigurationProperties .NewConfigBuilder <string, string>().SetKey(key).Build(); Assert.Equal(required, propertyConfig.IsRequired); IProperty <string, string> property = manager.GetProperty(propertyConfig); Assert.Equal(required, property.Config.IsRequired); string value = manager.GetPropertyValue(propertyConfig); Assert.Null(value); required = true; key = "not-exist-2"; propertyConfig = ConfigurationProperties.NewConfigBuilder <string, string>().SetKey(key) .SetRequired(required).Build(); Assert.Equal(required, propertyConfig.IsRequired); Assert.Throws <InvalidOperationException>(() => manager.GetProperty(propertyConfig)); Assert.Throws <InvalidOperationException>(() => manager.GetPropertyValue(propertyConfig)); required = false; key = "exist"; 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); value = manager.GetPropertyValue(propertyConfig); Assert.Equal("ok", 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); value = manager.GetPropertyValue(propertyConfig); Assert.Equal("ok2", value); }
public virtual void TestSameKeyDifferentConfig() { IConfigurationManager manager = CreateManager(new Dictionary <int, IConfigurationSource>() { { 1, CreateSource() } }); 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-exist") .SetDefaultValue("default").Build(); Assert.Throws <ArgumentException>(() => manager.GetProperty(propertyConfig)); }
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 virtual void TestGetLabeledPropertyValuePerf() { ConfigurationSourceConfig config = ConfigurationSources.NewConfig("labeled-source"); TestLabeledConfigurationSource labeledSource = CreateLabeledSource(config); config = ConfigurationSources.NewConfig("dynamic-labeled-source"); TestDynamicLabeledConfigurationSource dynamicLabeledSource = CreateDynamicLabeledSource(config); ILabeledConfigurationManager manager = CreateLabeledManager( new Dictionary <int, IConfigurationSource>() { { 1, labeledSource }, { 2, dynamicLabeledSource } }); List <IPropertyLabel> labels = new List <IPropertyLabel>(); labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.DC_KEY, "sh-1")); labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.APP_KEY, "app-1")); PropertyLabels propertyLabels = LabeledConfigurationProperties.NewLabels(labels); LabeledKey <string> key = LabeledConfigurationProperties.NewKeyBuilder <string>().SetKey("labeled-key-1") .SetPropertyLabels(propertyLabels).Build(); PropertyConfig <LabeledKey <string>, string> propertyConfig = ConfigurationProperties .NewConfigBuilder <LabeledKey <string>, string>().SetKey(key).SetDefaultValue("default-value-1").Build(); String propertyValue = manager.GetPropertyValue(propertyConfig); Assert.Equal("v-1-2", propertyValue); int times = 100 * 1000; DateTime start = DateTime.Now; for (int i = 0; i < times; i++) { propertyValue = manager.GetPropertyValue(propertyConfig); } DateTime end = DateTime.Now; TimeSpan elipsed = end - start; Console.WriteLine("{0} times GetPropertyValue, total time: {1}, avg time: {2}", times, elipsed.TotalMilliseconds, (double)elipsed.TotalMilliseconds / times); }
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); }
public virtual void TestGetProperties() { IConfigurationManager manager = CreateManager(); 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("PATH") .SetDefaultValue("default").Build(); property = manager.GetProperty(propertyConfig); Console.WriteLine("property: " + property + "\n"); Assert.NotNull(property.Value); Assert.NotEqual("default", property.Value); }
public static void DoMain(string[] args) { InitConfig(); // show properties Console.WriteLine("app.id: " + _appId.Value); Console.WriteLine("app.name: " + _appName.Value); Console.WriteLine("user.list: " + _userList.Value); Console.WriteLine("user.data: " + _userData.Value); Console.WriteLine("sleep.time: " + _sleepTime.Value); // get some property value for non-stable property (the key is not stable, not sure it exists or not) PropertyConfig <string, string> propertyConfig = ConfigurationProperties.NewConfigBuilder <string, string>() .SetKey("some.data").SetDefaultValue("not-sure").Build(); string someIPropertyValue = _manager.GetPropertyValue(propertyConfig); Console.WriteLine(); Console.WriteLine("some.data: " + someIPropertyValue); Console.WriteLine(); Console.WriteLine("Now sleep {0} ms ...", _sleepTime.Value); Thread.Sleep(_sleepTime.Value.Value); Console.WriteLine(); Console.WriteLine("My custom property data: " + _myCustomData.Value); Console.WriteLine(); for (int i = 0; i < _myCustomData.Value.Times; i++) { Console.WriteLine("{0} {1}!", _myCustomData.Value.Say, _myCustomData.Value.Name); } Console.WriteLine(); Console.WriteLine("Bye!"); }
public virtual void TestGetLabeledProperty() { ConfigurationSourceConfig config = ConfigurationSources.NewConfig("labeled-source"); TestLabeledConfigurationSource labeledSource = CreateLabeledSource(config); config = ConfigurationSources.NewConfig("dynamic-labeled-source"); TestDynamicLabeledConfigurationSource dynamicLabeledSource = CreateDynamicLabeledSource(config); ILabeledConfigurationManager manager = CreateLabeledManager( new Dictionary <int, IConfigurationSource>() { { 1, labeledSource }, { 2, dynamicLabeledSource } }); List <IPropertyLabel> labels = new List <IPropertyLabel>(); labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.DC_KEY, "sh-1")); labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.APP_KEY, "app-1")); PropertyLabels propertyLabels = LabeledConfigurationProperties.NewLabels(labels); LabeledKey <string> key = LabeledConfigurationProperties.NewKeyBuilder <string>().SetKey("labeled-key-1") .SetPropertyLabels(propertyLabels).Build(); PropertyConfig <LabeledKey <string>, string> propertyConfig = ConfigurationProperties .NewConfigBuilder <LabeledKey <string>, string>().SetKey(key).SetDefaultValue("default-value-1").Build(); IProperty <LabeledKey <string>, string> property = manager.GetProperty(propertyConfig); Console.WriteLine(property); Assert.Equal("v-1-2", property.Value); labels = new List <IPropertyLabel>(); labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.DC_KEY, "sh-1-not-exist")); labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.APP_KEY, "app-1")); propertyLabels = LabeledConfigurationProperties.NewLabels(labels); key = LabeledConfigurationProperties.NewKeyBuilder <String>().SetKey("labeled-key-1") .SetPropertyLabels(propertyLabels).Build(); propertyConfig = ConfigurationProperties.NewConfigBuilder <LabeledKey <string>, String>().SetKey(key) .SetDefaultValue("default-value-1").Build(); property = manager.GetProperty(propertyConfig); Console.WriteLine(property); Assert.Equal("default-value-1", property.Value); propertyLabels = LabeledConfigurationProperties.NewLabels( LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.DC_KEY, "sh-1"), LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.APP_KEY, "app-1")); labels = new List <IPropertyLabel>(); labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.DC_KEY, "sh-1-not-exist")); labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.APP_KEY, "app-1")); propertyLabels = LabeledConfigurationProperties.NewLabels(labels, propertyLabels); key = LabeledConfigurationProperties.NewKeyBuilder <string>().SetKey("labeled-key-1") .SetPropertyLabels(propertyLabels).Build(); propertyConfig = ConfigurationProperties.NewConfigBuilder <LabeledKey <string>, string>().SetKey(key) .SetDefaultValue("default-value-1").Build(); property = manager.GetProperty(propertyConfig); Console.WriteLine(property); Assert.Equal("v-1-2", property.Value); TestDataCenterSetting Setting = new TestDataCenterSetting("labeled-key-1", "v-4-2", "sh-1-not-exist", "app-1"); dynamicLabeledSource.updateSetting(Setting); Thread.Sleep(10); Console.WriteLine(property); Assert.Equal("v-4-2", property.Value); dynamicLabeledSource.removeSetting(Setting); Thread.Sleep(10); Console.WriteLine(property); Assert.Equal("v-1-2", property.Value); labels = new List <IPropertyLabel>(); labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.DC_KEY, "sh-1-not-exist")); labels.Add(LabeledConfigurationProperties.NewLabel(TestDataCenterSetting.APP_KEY, "app-1")); propertyLabels = LabeledConfigurationProperties.NewLabels(labels, PropertyLabels.Empty); key = LabeledConfigurationProperties.NewKeyBuilder <string>().SetKey("labeled-key-1") .SetPropertyLabels(propertyLabels).Build(); propertyConfig = ConfigurationProperties.NewConfigBuilder <LabeledKey <string>, string>().SetKey(key) .SetDefaultValue("default-value-1").Build(); property = manager.GetProperty(propertyConfig); Console.WriteLine(property); Assert.Equal("v-0-2", property.Value); }