Наследование: IConfiguration
Пример #1
0
        public static void SaveToProperties()
        {
            var dbConfig = new Configuration();
            dbConfig.SetValue("first-setting", "one");
            dbConfig.SetValue("second", 345.33);

            var stream = new MemoryStream();
            dbConfig.Save(stream, new PropertiesConfigurationFormatter());

            Assert.Greater(stream.Length, 0);

            stream.Seek(0, SeekOrigin.Begin);

            var reader = new StreamReader(stream);
            var line = reader.ReadLine();

            Assert.IsNotNullOrEmpty(line);
            Assert.IsTrue(line.StartsWith("#"));

            line = reader.ReadLine();

            Assert.IsNotNullOrEmpty(line);
            Assert.IsTrue(line.StartsWith("#"));

            line = reader.ReadLine();
            Assert.IsNotNullOrEmpty(line);
            Assert.AreEqual("first-setting=one", line);

            line = reader.ReadLine();
            Assert.IsNotNullOrEmpty(line);
            Assert.AreEqual("second=345.33", line);
        }
Пример #2
0
 public void DefaultConfig()
 {
     IConfiguration config = null;
     Assert.DoesNotThrow(() => config = new Configuration());
     Assert.IsNotNull(config);
     Assert.IsNull(config.Parent);
     Assert.IsNull(config.Source);
 }
Пример #3
0
        static Configuration()
        {
            Empty = new Configuration(true);

            SystemDefault = new Configuration(true);
            SystemConfigKeys.SetTo(SystemDefault);

            DatabaseDefault = new Configuration(true);
            DatabaseConfigKeys.SetTo(DatabaseDefault);
        }
        public static IConfigurationBuilder Load(this IConfigurationBuilder builder, IConfigurationSource source,
			IConfigurationFormatter formatter)
        {
            var config = new Configuration();
            formatter.LoadInto(config, source.InputStream);

            foreach (var pair in config) {
                builder = builder.WithSetting(pair.Key, pair.Value);
            }

            return builder;
        }
Пример #5
0
        public void GetBooleanValue(string key, string value, bool expected)
        {
            IConfiguration config = null;
            Assert.DoesNotThrow(() => config = new Configuration());
            Assert.IsNotNull(config);

            Assert.DoesNotThrow(() => config.SetValue(key, value));

            object configValue = null;
            Assert.DoesNotThrow(() => configValue = config.GetBoolean(key));
            Assert.IsNotNull(configValue);
            Assert.IsInstanceOf<bool>(configValue);
            Assert.AreEqual(expected, configValue);
        }
Пример #6
0
        public void Extensions_LoadFromProperties()
        {
            var properties = new StringBuilder();
            properties.AppendLine("system.readOnly = false");
            properties.AppendLine("caching.type = Memory");

            IConfiguration configuration = null;
            Assert.DoesNotThrow(() => configuration = new Configuration(new StringConfigSource(properties.ToString())));
            Assert.IsNotNull(configuration);
            Assert.DoesNotThrow(() => configuration.Load(new PropertiesConfigFormatter()));

            Assert.DoesNotThrow(() => Assert.IsNotNull(configuration.GetKey("system.readOnly")));

            bool readOnly = true;
            Assert.DoesNotThrow(() => readOnly = configuration.GetBoolean("system.readOnly"));
            Assert.IsFalse(readOnly);
        }
Пример #7
0
        public static void LoadProperties()
        {
            var stream = CreatePropertiesStreamToLoad();

            var dbConfig = new Configuration();
            dbConfig.Load(stream, new PropertiesConfigurationFormatter());

            Assert.AreEqual(2, dbConfig.GetKeys().Count());

            var firstSetting = dbConfig.GetValue("first-setting");
            Assert.IsNotNull(firstSetting);
            Assert.IsInstanceOf<string>(firstSetting);
            Assert.AreEqual("one", firstSetting);

            var secondSetting = dbConfig.GetValue("second");
            Assert.IsNotNull(secondSetting);
            Assert.IsInstanceOf<string>(secondSetting);
            Assert.AreEqual("345.33", secondSetting);
        }
Пример #8
0
        public void LoadProperties()
        {
            var dbConfig = new Configuration();

            var path = Path.Combine(Environment.CurrentDirectory, FileName);
            using (var source = new FileConfigurationSource(path)) {
                dbConfig.Load(source, new PropertiesConfigurationFormatter());
            }

            Assert.AreEqual(2, dbConfig.GetKeys().Count());

            var firstSetting = dbConfig.GetValue("first-setting");
            Assert.IsNotNull(firstSetting);
            Assert.IsInstanceOf<string>(firstSetting);
            Assert.AreEqual("one", firstSetting);

            var secondSetting = dbConfig.GetValue("second");
            Assert.IsNotNull(secondSetting);
            Assert.IsInstanceOf<string>(secondSetting);
            Assert.AreEqual("345.33", secondSetting);
        }
Пример #9
0
        public void GetKeysFromChild()
        {
            IConfiguration config = null;
            Assert.DoesNotThrow(() => config = Configuration.SystemDefault);
            Assert.IsNotNull(config);

            Assert.DoesNotThrow(() => config.SetKey(new ConfigKey("test.oneKey", "one", typeof(string))));

            IConfiguration child = null;
            Assert.DoesNotThrow(() => child = new Configuration(config));
            Assert.IsNotNull(child);
            Assert.IsNotNull(child.Parent);

            Assert.DoesNotThrow(() => config.SetKey(new ConfigKey("test.oneKey", 45, typeof(int))));

            ConfigKey key = null;
            Assert.DoesNotThrow(() => key = config.GetKey("test.oneKey"));
            Assert.IsNotNull(key);
            Assert.AreEqual(typeof(int), key.ValueType);
            Assert.AreEqual(45, key.DefaultValue);
        }
Пример #10
0
        public void LoadFromFile_Properties()
        {
            var filePath = Path.Combine(Environment.CurrentDirectory, "db.config");

            var config = new Configuration();
            using (var source = new FileConfigurationSource(filePath)) {
                config.Load(source, new PropertiesConfigurationFormatter());
            }

            Assert.AreEqual(2, config.Count());

            var keys = config.GetKeys().ToArray();
            Assert.IsNotEmpty(keys);
            Assert.AreEqual("system.configKey", keys.First());

            var configValue = config.GetValue("db.name");
            Assert.IsNotNull(configValue);
            Assert.IsInstanceOf<string>(configValue);
            Assert.AreEqual("testdb", (string)configValue);
        }
Пример #11
0
        public void SaveToFile_Properties()
        {
            var filePath = Path.Combine(Environment.CurrentDirectory, "db.config");

            var config = new Configuration();
            config.SetValue("system.configKey", 7679);
            config.SetValue("db.name", "testdb");

            config.Save(filePath, new PropertiesConfigurationFormatter());
        }
Пример #12
0
        public void GetEnumValue(object value, TestEnum expected)
        {
            IConfiguration config = null;
            Assert.DoesNotThrow(() => config = new Configuration());
            Assert.IsNotNull(config);

            Assert.DoesNotThrow(() => config.SetValue("test", value));

            object configValue = null;
            Assert.DoesNotThrow(() => configValue = config.GetValue<TestEnum>("test"));
            Assert.IsInstanceOf<TestEnum>(configValue);
            Assert.AreEqual(expected, configValue);
        }
Пример #13
0
        public void LoadFromClosedStream()
        {
            var bytes = new byte[256];
            var stream = new MemoryStream(bytes, false);
            stream.Close();

            var config = new Configuration();

            Assert.Throws<DatabaseConfigurationException>(() => config.Load(stream));
        }
Пример #14
0
        public void GetValuesFromRoot()
        {
            IConfiguration config = null;
            Assert.DoesNotThrow(() => config = new Configuration());
            Assert.IsNotNull(config);
            Assert.DoesNotThrow(() => config.SetValue("test.oneKey", 54));
            Assert.DoesNotThrow(() => config.SetValue("test.twoKeys", null));

            object value1 = null;
            object value2 = null;

            Assert.DoesNotThrow(() => value1 = config.GetValue("test.oneKey"));
            Assert.IsNotNull(value1);
            Assert.IsInstanceOf<int>(value1);
            Assert.AreEqual(54, value1);

            Assert.DoesNotThrow(() => value2 = config.GetValue("test.twoKeys"));
            Assert.IsNull(value2);
        }
Пример #15
0
        public void GetValuesFromChild()
        {
            IConfiguration config = null;
            Assert.DoesNotThrow(() => config = new Configuration());
            Assert.IsNotNull(config);

            Assert.DoesNotThrow(() => config.SetValue("test.oneKey", "one"));

            IConfiguration child = null;
            Assert.DoesNotThrow(() => child = new Configuration(config));
            Assert.IsNotNull(child);
            Assert.IsNotNull(child.Parent);

            Assert.DoesNotThrow(() => child.SetValue("test.oneKey", 45));

            object value = null;
            Assert.DoesNotThrow(() => value = child.GetValue("test.oneKey"));
            Assert.IsNotNull(value);
            Assert.IsInstanceOf<int>(value);
            Assert.AreEqual(45, value);

            Assert.DoesNotThrow(() => value = config.GetValue("test.oneKey"));
            Assert.IsNotNull(value);
            Assert.IsInstanceOf<string>(value);
            Assert.AreEqual("one", value);
        }
Пример #16
0
        public void GetValueAsInt32()
        {
            IConfiguration config = null;
            Assert.DoesNotThrow(() => config = new Configuration());
            Assert.IsNotNull(config);

            Assert.DoesNotThrow(() => config.SetValue("test.oneKey", "22"));

            object value = null;
            Assert.DoesNotThrow(() => value = config.GetInt32("test.oneKey"));
            Assert.IsNotNull(value);
            Assert.IsInstanceOf<int>(value);
            Assert.AreEqual(22, value);
        }
Пример #17
0
 public ConfigurationBuilder()
 {
     configuration = new Configuration();
 }