示例#1
0
        public void ExceptionOnFailedBindingIncludesPath()
        {
            const string IncorrectValue = "Invalid data";
            const string ConfigKey      = "Nested:Value";

            var dic = new Dictionary <string, string>
            {
                { ConfigKey, IncorrectValue }
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            var options = new OptionsWithNesting();

            Assert.Multiple(() =>
            {
                var exception = Assert.Throws <InvalidOperationException>(
                    () => config.Bind(options));

                Assert.That(exception.Message, Does.Contain(ConfigKey));
            });
        }
示例#2
0
        public void CanReadAllSupportedTypes(string value, Type type)
        {
            var dic = new Dictionary <string, string>
            {
                { "Value", value }
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            var optionsType   = typeof(GenericOptions <>).MakeGenericType(type);
            var options       = Activator.CreateInstance(optionsType);
            var expectedValue = TypeDescriptor.GetConverter(type).ConvertFromInvariantString(value);

            config.Bind(options);
            var optionsValue  = options.GetType().GetProperty("Value").GetValue(options);
            var getValueValue = config.GetValue(type, "Value");
            var getValue      = config.GetSection("Value").Get(type);

            Assert.Multiple(() =>
            {
                Assert.That(expectedValue, Is.EqualTo(optionsValue));
                Assert.That(expectedValue, Is.EqualTo(getValue));
                Assert.That(expectedValue, Is.EqualTo(getValueValue));
            });
        }
示例#3
0
        public void GetNullValue()
        {
            var dic = new Dictionary <string, string>
            {
                { "Integer", null },
                { "Boolean", null },
                { "Nested:Integer", null },
                { "Object", null }
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            Assert.Multiple(() =>
            {
                Assert.That(config.GetValue <bool>("Boolean"), Is.False);
                Assert.That(0, Is.EqualTo(config.GetValue <int>("Integer")));
                Assert.That(0, Is.EqualTo(config.GetValue <int>("Nested:Integer")));
                Assert.That(config.GetValue <ComplexOptions>("Object"), Is.Null);
                Assert.That(config.GetSection("Boolean").Get <bool>(), Is.False);
                Assert.That(0, Is.EqualTo(config.GetSection("Integer").Get <int>()));
                Assert.That(0, Is.EqualTo(config.GetSection("Nested:Integer").Get <int>()));
                Assert.That(config.GetSection("Object").Get <ComplexOptions>(), Is.Null);
            });
        }
示例#4
0
        public void GetDefaultsWhenDataDoesNotExist()
        {
            var dic = new Dictionary <string, string>
            {
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            var foo = new ComplexOptions();

            Assert.Multiple(() =>
            {
                Assert.That(config.GetValue <bool>("Boolean"), Is.False);
                Assert.That(0, Is.EqualTo(config.GetValue <int>("Integer")));
                Assert.That(0, Is.EqualTo(config.GetValue <int>("Nested:Integer")));
                Assert.That(config.GetValue <ComplexOptions>("Object"), Is.Null);
                Assert.That(config.GetValue("Boolean", true), Is.True);
                Assert.That(3, Is.EqualTo(config.GetValue("Integer", 3)));
                Assert.That(1, Is.EqualTo(config.GetValue("Nested:Integer", 1)));
                Assert.That(config.GetValue("Object", foo), Is.SameAs(foo));
            });
        }
        public void GetChildren_WithSection_ReturnsAllDescendents()
        {
            var configManagerFixture = new ConfigurationManagerFixture();

            int numKeys = new Random().Next(1, 10);

            for (int keyCt = 1; keyCt <= numKeys; keyCt++)
            {
                configManagerFixture.WithAppSetting($"Key{keyCt}", $"Value{keyCt}");
                configManagerFixture.WithAppSetting($"Section:Key{keyCt}", $"Value{keyCt}");
                configManagerFixture.WithAppSetting($"Section:SubSection:Key{keyCt}", $"Value{keyCt}");
                configManagerFixture.WithAppSetting($"OtherSection:Key{keyCt}", $"Value{keyCt}");
            }

            IConfigurationManager configManager = configManagerFixture.Build();

            var appConfig = new AppConfiguration(configManager);

            IAppConfigurationSection section = appConfig.GetSection("Section");

            int actual = section.GetChildren().Count();

            const int numSections = 2;
            int       expected    = numKeys * numSections;

            Assert.That(actual, Is.EqualTo(expected));
        }
示例#6
0
        public void CanBindIConfigurationSection()
        {
            var dic = new Dictionary <string, string>
            {
                { "Section:Integer", "-2" },
                { "Section:Boolean", "TRUe" },
                { "Section:Nested:Integer", "11" },
                { "Section:Virtual", "Sup" }
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            var options = config.Get <ConfigurationInterfaceOptions>();

            var childOptions = options.Section.Get <DerivedOptions>();

            Assert.Multiple(() =>
            {
                Assert.That(childOptions.Boolean, Is.True);
                Assert.That(-2, Is.EqualTo(childOptions.Integer));
                Assert.That(11, Is.EqualTo(childOptions.Nested.Integer));
                Assert.That("Derived:Sup", Is.EqualTo(childOptions.Virtual));

                Assert.That("Section", Is.EqualTo(options.Section.Key));
                Assert.That("Section", Is.EqualTo(options.Section.Path));
                Assert.That(options.Section.Value, Is.Null);
            });
        }
示例#7
0
        public void GetCanReadInheritedProperties()
        {
            var dic = new Dictionary <string, string>
            {
                { "Integer", "-2" },
                { "Boolean", "TRUe" },
                { "Nested:Integer", "11" },
                { "Virtual", "Sup" }
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            var options = new DerivedOptions();

            config.Bind(options);

            Assert.Multiple(() =>
            {
                Assert.That(options.Boolean, Is.True);
                Assert.That(-2, Is.EqualTo(options.Integer));
                Assert.That(11, Is.EqualTo(options.Nested.Integer));
                Assert.That("Derived:Sup", Is.EqualTo(options.Virtual));
            });
        }
示例#8
0
        public void BindCanReadComplexProperties()
        {
            var dic = new Dictionary <string, string>
            {
                { "Integer", "-2" },
                { "Boolean", "TRUe" },
                { "Nested:Integer", "11" }
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            var instance = new ComplexOptions();

            config.Bind(instance);

            Assert.Multiple(() =>
            {
                Assert.That(instance.Boolean, Is.True);
                Assert.That(-2, Is.EqualTo(instance.Integer));
                Assert.That(11, Is.EqualTo(instance.Nested.Integer));
            });
        }
示例#9
0
        public void CanGetComplexOptionsWhichHasAlsoHasValue()
        {
            var dic = new Dictionary <string, string>
            {
                { "obj", "whut" },
                { "obj:Integer", "-2" },
                { "obj:Boolean", "TRUe" },
                { "obj:Nested:Integer", "11" }
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            var options = config.GetSection("obj").Get <ComplexOptions>();

            Assert.Multiple(() =>
            {
                Assert.That(options, Is.Not.Null);
                Assert.That(options.Boolean, Is.True);
                Assert.That(-2, Is.EqualTo(options.Integer));
                Assert.That(11, Is.EqualTo(options.Nested.Integer));
            });
        }
示例#10
0
        public void GetObjectList()
        {
            var input = new Dictionary <string, string>
            {
                { "ObjectList:0:Integer", "30" },
                { "ObjectList:1:Integer", "31" },
                { "ObjectList:2:Integer", "32" },
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(input)
                                .Build();

            var config = new AppConfiguration(configManager);

            var options = new List <NestedOptions>();

            config.GetSection("ObjectList").Bind(options);

            Assert.Multiple(() =>
            {
                Assert.That(3, Is.EqualTo(options.Count));

                Assert.That(30, Is.EqualTo(options[0].Integer));
                Assert.That(31, Is.EqualTo(options[1].Integer));
                Assert.That(32, Is.EqualTo(options[2].Integer));
            });
        }
示例#11
0
        public void GetUintEnumDictionary()
        {
            var input = new Dictionary <string, string>
            {
                { "EnumDictionary:abc", "val_1" },
                { "EnumDictionary:def", "val_2" },
                { "EnumDictionary:ghi", "val_3" }
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(input)
                                .Build();

            var config = new AppConfiguration(configManager);

            var options = new Dictionary <KeyUintEnum, string>();

            config.GetSection("EnumDictionary").Bind(options);

            Assert.Multiple(() =>
            {
                Assert.That(3, Is.EqualTo(options.Count));
                Assert.That("val_1", Is.EqualTo(options[KeyUintEnum.abc]));
                Assert.That("val_2", Is.EqualTo(options[KeyUintEnum.def]));
                Assert.That("val_3", Is.EqualTo(options[KeyUintEnum.ghi]));
            });
        }
示例#12
0
        public void ConsistentExceptionOnFailedBinding(Type type)
        {
            const string IncorrectValue = "Invalid data";
            const string ConfigKey      = "Value";
            var          dic            = new Dictionary <string, string>
            {
                { ConfigKey, IncorrectValue }
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            var optionsType = typeof(GenericOptions <>).MakeGenericType(type);
            var options     = Activator.CreateInstance(optionsType);

            var exception = Assert.Throws <InvalidOperationException>(
                () => config.Bind(options));

            var getValueException = Assert.Throws <InvalidOperationException>(
                () => config.GetValue(type, "Value"));

            var getException = Assert.Throws <InvalidOperationException>(
                () => config.GetSection("Value").Get(type));

            Assert.Multiple(() =>
            {
                Assert.That(exception.InnerException, Is.Not.Null);
                Assert.That(getException.InnerException, Is.Not.Null);
            });
        }
示例#13
0
        public void BindList()
        {
            var input = new Dictionary <string, string>
            {
                { "StringList:0", "val0" },
                { "StringList:1", "val1" },
                { "StringList:2", "val2" },
                { "StringList:x", "valx" }
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(input)
                                .Build();

            var config = new AppConfiguration(configManager);

            var list = new List <string>();

            config.GetSection("StringList").Bind(list);

            Assert.Multiple(() =>
            {
                Assert.That(4, Is.EqualTo(list.Count));

                Assert.That("val0", Is.EqualTo(list[0]));
                Assert.That("val1", Is.EqualTo(list[1]));
                Assert.That("val2", Is.EqualTo(list[2]));
                Assert.That("valx", Is.EqualTo(list[3]));
            });
        }
示例#14
0
        public void BinderIgnoresIndexerProperties()
        {
            var configManager = new ConfigurationManagerFixture()
                                .Build();

            var config = new AppConfiguration(configManager);

            config.Bind(new List <string>());
        }
        public void GetConnectionString_WithName_ReturnsConnectionString(string name, string connectionString)
        {
            var configManager = new ConfigurationManagerFixture()
                                .WithConnectionString(name, connectionString)
                                .Build();

            var appConfig = new AppConfiguration(configManager);

            string actual = appConfig.GetConnectionString(name);

            Assert.That(actual, Is.EqualTo(connectionString));
        }
        public void GetConnectionString_WithNotPresentName_ReturnsNull()
        {
            var configManager = new ConfigurationManagerFixture()
                                .WithConnectionString("Name", "ConnString")
                                .Build();

            var appConfig = new AppConfiguration(configManager);

            string actual = appConfig.GetConnectionString("NameNotPresent");

            Assert.That(actual, Is.Null);
        }
        public void KeyIndexer_WithNotPresentKey_ReturnsNull()
        {
            var configManager = new ConfigurationManagerFixture()
                                .WithAppSetting("Key", "Value1")
                                .Build();

            var appConfig = new AppConfiguration(configManager);

            string actual = appConfig["KeyNotPresent"];

            Assert.That(actual, Is.Null);
        }
        public void KeyIndexer_WithKey_ReturnsValue(string key, string value)
        {
            var configManager = new ConfigurationManagerFixture()
                                .WithAppSetting(key, value)
                                .Build();

            var appConfig = new AppConfiguration(configManager);

            string actual = appConfig[key];

            Assert.That(actual, Is.EqualTo(value));
        }
        public void GetSection_WithNotPresentSection_ReturnsEmptySection()
        {
            var configManager = new ConfigurationManagerFixture()
                                .WithAppSetting("Section:Key", "Value1")
                                .Build();

            var appConfig = new AppConfiguration(configManager);

            IAppConfigurationSection section = appConfig.GetSection("SectionNotPresent");

            bool actual = section.Exists();

            Assert.That(actual, Is.False);
        }
        public void GetSection_WithKeyWithNoSection_ReturnsKeyAsSection(string key, string value)
        {
            var configManager = new ConfigurationManagerFixture()
                                .WithAppSetting(key, value)
                                .Build();

            var appConfig = new AppConfiguration(configManager);

            IAppConfigurationSection section = appConfig.GetSection(key);

            string actual = section.Value;

            Assert.That(actual, Is.EqualTo(value));
        }
示例#21
0
        public void ExceptionIncludesKeyOfFailedBinding()
        {
            var dic = new Dictionary <string, string>
            {
                { "NestedOptionsProperty:NestedOptions2Property:ISomeInterfaceProperty:subkey", "x" }
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            Assert.That(() => config.Bind(new TestOptions()), Throws.InvalidOperationException);
        }
示例#22
0
        public void ExceptionWhenTryingToBindClassWithoutParameterlessConstructor()
        {
            var dic = new Dictionary <string, string>
            {
                { "ClassWithoutPublicConstructorProperty:Subkey", "x" }
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            Assert.That(() => config.Bind(new TestOptions()), Throws.InvalidOperationException);
        }
示例#23
0
        public void ExceptionWhenTryingToBindToInterface()
        {
            var dic = new Dictionary <string, string>
            {
                { "ISomeInterfaceProperty:Subkey", "x" }
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            Assert.That(() => config.Bind(new TestOptions()), Throws.InvalidOperationException);
        }
        public void AllKeys_WithAppSettingsAndConnectionStrings_ReturnsOnlyAppSettingsKeys()
        {
            var configManager = new ConfigurationManagerFixture()
                                .WithAppSetting("Key", "Value1")
                                .WithAppSetting("Section:Key", "Value2")
                                .WithConnectionString("Name", "ConnString")
                                .Build();

            var appConfig = new AppConfiguration(configManager);

            var actual = appConfig.AllKeys;

            var expected = configManager.AppSettings.AllKeys;

            CollectionAssert.AreEqual(expected, actual);
        }
示例#25
0
        public void NonPublicModeGetStillIgnoresReadonly(string property)
        {
            var dic = new Dictionary <string, string>
            {
                { property, "stuff" },
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            var options = config.Get <ComplexOptions>(o => o.BindNonPublicProperties = true);

            Assert.That(options.GetType().GetTypeInfo().GetDeclaredProperty(property).GetValue(options), Is.Null);
        }
示例#26
0
        public void GetCanSetNonPublicWhenSet(string property)
        {
            var dic = new Dictionary <string, string>
            {
                { property, "stuff" },
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            var options = config.Get <ComplexOptions>(o => o.BindNonPublicProperties = true);

            Assert.That("stuff", Is.EqualTo(options.GetType().GetTypeInfo().GetDeclaredProperty(property).GetValue(options)));
        }
示例#27
0
        public void GetIgnoresTests(string property)
        {
            var dic = new Dictionary <string, string>
            {
                { property, "stuff" },
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            var options = config.Get <ComplexOptions>();

            Assert.That(options.GetType().GetTypeInfo().GetDeclaredProperty(property).GetValue(options), Is.Null);
        }
示例#28
0
        public void GetUri()
        {
            var dic = new Dictionary <string, string>
            {
                { "AnUri", "http://www.bing.com" }
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            var uri = config.GetValue <Uri>("AnUri");

            Assert.That("http://www.bing.com", Is.EqualTo(uri.OriginalString));
        }
示例#29
0
        public void ExceptionWhenTryingToBindToTypeThrowsWhenActivated()
        {
            var dic = new Dictionary <string, string>
            {
                { "ThrowsWhenActivatedProperty:subkey", "x" }
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            Assert.Multiple(() =>
            {
                var exception = Assert.Throws <InvalidOperationException>(() => config.Bind(new TestOptions()));
                Assert.That(exception.InnerException, Is.Not.Null);
            });
        }
示例#30
0
        public void BindCanReadStaticProperty()
        {
            var dic = new Dictionary <string, string>
            {
                { "StaticProperty", "other stuff" },
            };

            var configManager = new ConfigurationManagerFixture()
                                .WithAppSettings(dic)
                                .Build();

            var config = new AppConfiguration(configManager);

            var instance = new ComplexOptions();

            config.Bind(instance);

            Assert.That("other stuff", Is.EqualTo(ComplexOptions.StaticProperty));
        }