public void JsonConfiguration_Does_Not_Throw_On_Optional_Configuration()
        {
            var configSource = new JsonConfigurationSource("NotExistingConfig.json", optional: true);

            configSource.Load();
            Assert.Throws <InvalidOperationException>(() => configSource.Get("key"));
        }
Exemple #2
0
        public void PropertiesAreSortedByNumberOnlyFirst()
        {
            var json = @"{
                'setting': {
                    'hello': 'a',
                    'bob': 'b',
                    '42': 'c',
                    '4':'d',
                    '10': 'e',
                    '1text': 'f',
                }
            }";

            var jsonConfigSource = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));

            var builder = new ConfigurationBuilder();

            builder.Add(jsonConfigSource, load: false);
            var config = builder.Build();

            var configurationSection       = config.GetConfigurationSection("setting");
            var indexConfigurationSections = configurationSection.GetConfigurationSections().ToArray();

            Assert.Equal(6, indexConfigurationSections.Count());
            Assert.Equal("4", indexConfigurationSections[0].Key);
            Assert.Equal("10", indexConfigurationSections[1].Key);
            Assert.Equal("42", indexConfigurationSections[2].Key);
            Assert.Equal("1text", indexConfigurationSections[3].Key);
            Assert.Equal("bob", indexConfigurationSections[4].Key);
            Assert.Equal("hello", indexConfigurationSections[5].Key);
        }
Exemple #3
0
        public void ImplicitArrayItemReplacement()
        {
            var json1 = @"{
                'ip': [
                    '1.2.3.4',
                    '7.8.9.10',
                    '11.12.13.14'
                ]
            }";

            var json2 = @"{
                'ip': [
                    '15.16.17.18'
                ]
            }";

            var jsonConfigSource1 = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            jsonConfigSource1.Load(TestStreamHelpers.StringToStream(json1));

            var jsonConfigSource2 = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            jsonConfigSource2.Load(TestStreamHelpers.StringToStream(json2));

            var builder = new ConfigurationBuilder();

            builder.Add(jsonConfigSource1, load: false);
            builder.Add(jsonConfigSource2, load: false);
            var config = builder.Build();

            Assert.Equal(3, config.GetConfigurationSections("ip").Count());
            Assert.Equal("15.16.17.18", config.Get("ip:0"));
            Assert.Equal("7.8.9.10", config.Get("ip:1"));
            Assert.Equal("11.12.13.14", config.Get("ip:2"));
        }
Exemple #4
0
        public void ArraysAreKeptInFileOrder()
        {
            var json = @"{
                'setting': [
                    'b',
                    'a',
                    '2'
                ]
            }";

            var jsonConfigSource = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));

            var builder = new ConfigurationBuilder();

            builder.Add(jsonConfigSource, load: false);
            var config = builder.Build();

            var configurationSection       = config.GetConfigurationSection("setting");
            var indexConfigurationSections = configurationSection.GetConfigurationSections().ToArray();

            Assert.Equal(3, indexConfigurationSections.Count());
            Assert.Equal("b", indexConfigurationSections[0].Value.Get(null));
            Assert.Equal("a", indexConfigurationSections[1].Value.Get(null));
            Assert.Equal("2", indexConfigurationSections[2].Value.Get(null));
        }
        public void JsonConfiguration_Throws_On_Missing_Configuration_File()
        {
            var configSource = new JsonConfigurationSource("NotExistingConfig.json", optional: false);
            var exception    = Assert.Throws <FileNotFoundException>(() => configSource.Load());

            // Assert
            Assert.Equal(Resources.FormatError_FileNotFound("NotExistingConfig.json"), exception.Message);
        }
        public void NonObjectRootIsInvalid()
        {
            var json             = @"'test'";
            var jsonConfigSource = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            var exception = Assert.Throws <FormatException>(
                () => jsonConfigSource.Load(TestStreamHelpers.StringToStream(json)));

            Assert.NotNull(exception.Message);
        }
        public void LoadMethodCanHandleEmptyValue()
        {
            var json          = @"
{
    'name': ''
}";
            var jsonConfigSrc = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            jsonConfigSrc.Load(TestStreamHelpers.StringToStream(json));

            Assert.Equal(string.Empty, jsonConfigSrc.Get("name"));
        }
        public void ThrowExceptionWhenUnexpectedEndFoundBeforeFinishParsing()
        {
            var json             = @"{
                'name': 'test',
                'address': {
                    'street': 'Something street',
                    'zipcode': '12345'
                }
            /* Missing a right brace here*/";
            var jsonConfigSource = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            var exception = Assert.Throws <FormatException>(
                () => jsonConfigSource.Load(TestStreamHelpers.StringToStream(json)));

            Assert.NotNull(exception.Message);
        }
        public void SupportAndIgnoreComments()
        {
            var json          = @"/* Comments */
                {/* Comments */
                ""name"": /* Comments */ ""test"",
                ""address"": {
                    ""street"": ""Something street"", /* Comments */
                    ""zipcode"": ""12345""
                }
            }";
            var jsonConfigSrc = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            jsonConfigSrc.Load(TestStreamHelpers.StringToStream(json));

            Assert.Equal("test", jsonConfigSrc.Get("name"));
            Assert.Equal("Something street", jsonConfigSrc.Get("address:street"));
            Assert.Equal("12345", jsonConfigSrc.Get("address:zipcode"));
        }
Exemple #10
0
        public void ArraysAreConvertedToKeyValuePairs()
        {
            var json = @"{
                'ip': [
                    '1.2.3.4',
                    '7.8.9.10',
                    '11.12.13.14'
                ]
            }";

            var jsonConfigSource = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));

            Assert.Equal("1.2.3.4", jsonConfigSource.Get("ip:0"));
            Assert.Equal("7.8.9.10", jsonConfigSource.Get("ip:1"));
            Assert.Equal("11.12.13.14", jsonConfigSource.Get("ip:2"));
        }
        public void LoadKeyValuePairsFromValidJson()
        {
            var json          = @"
{
    'firstname': 'test',
    'test.last.name': 'last.name',
        'residential.address': {
            'street.name': 'Something street',
            'zipcode': '12345'
        }
}";
            var jsonConfigSrc = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            jsonConfigSrc.Load(TestStreamHelpers.StringToStream(json));

            Assert.Equal("test", jsonConfigSrc.Get("firstname"));
            Assert.Equal("last.name", jsonConfigSrc.Get("test.last.name"));
            Assert.Equal("Something street", jsonConfigSrc.Get("residential.address:STREET.name"));
            Assert.Equal("12345", jsonConfigSrc.Get("residential.address:zipcode"));
        }
        public void ArrayOfObjects()
        {
            var json = @"{
                'ip': [
                    {
                        'address': '1.2.3.4',
                        'hidden': false
                    },
                    {
                        'address': '5.6.7.8',
                        'hidden': true
                    }
                ]
            }";

            var jsonConfigSource = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);
            jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));

            Assert.Equal("1.2.3.4", jsonConfigSource.Get("ip:0:address"));
            Assert.Equal("False", jsonConfigSource.Get("ip:0:hidden"));
            Assert.Equal("5.6.7.8", jsonConfigSource.Get("ip:1:address"));
            Assert.Equal("True", jsonConfigSource.Get("ip:1:hidden"));
        }
Exemple #13
0
        public void NestedArrays()
        {
            var json = @"{
                'ip': [
                    [ 
                        '1.2.3.4',
                        '5.6.7.8'
                    ],
                    [ 
                        '9.10.11.12',
                        '13.14.15.16'
                    ],
                ]
            }";

            var jsonConfigSource = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));

            Assert.Equal("1.2.3.4", jsonConfigSource.Get("ip:0:0"));
            Assert.Equal("5.6.7.8", jsonConfigSource.Get("ip:0:1"));
            Assert.Equal("9.10.11.12", jsonConfigSource.Get("ip:1:0"));
            Assert.Equal("13.14.15.16", jsonConfigSource.Get("ip:1:1"));
        }
Exemple #14
0
        public void ArrayOfObjects()
        {
            var json = @"{
                'ip': [
                    {
                        'address': '1.2.3.4',
                        'hidden': false
                    },
                    {
                        'address': '5.6.7.8',
                        'hidden': true
                    }
                ]
            }";

            var jsonConfigSource = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));

            Assert.Equal("1.2.3.4", jsonConfigSource.Get("ip:0:address"));
            Assert.Equal("False", jsonConfigSource.Get("ip:0:hidden"));
            Assert.Equal("5.6.7.8", jsonConfigSource.Get("ip:1:address"));
            Assert.Equal("True", jsonConfigSource.Get("ip:1:hidden"));
        }
        public void ArrayMerge()
        {
            var json1 = @"{
                'ip': [
                    '1.2.3.4',
                    '7.8.9.10',
                    '11.12.13.14'
                ]
            }";

            var json2 = @"{
                'ip': {
                    '3': '15.16.17.18'
                }
            }";

            var jsonConfigSource1 = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            jsonConfigSource1.Load(TestStreamHelpers.StringToStream(json1));

            var jsonConfigSource2 = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);

            jsonConfigSource2.Load(TestStreamHelpers.StringToStream(json2));

            var builder = new ConfigurationBuilder();

            builder.Add(jsonConfigSource1, load: false);
            builder.Add(jsonConfigSource2, load: false);
            var config = builder.Build();

            Assert.Equal(4, config.GetSection("ip").GetChildren().Count());
            Assert.Equal("1.2.3.4", config["ip:0"]);
            Assert.Equal("7.8.9.10", config["ip:1"]);
            Assert.Equal("11.12.13.14", config["ip:2"]);
            Assert.Equal("15.16.17.18", config["ip:3"]);
        }
        public void ArraysAreKeptInFileOrder()
        {
            var json = @"{
                'setting': [
                    'b',
                    'a',
                    '2'
                ]
            }";

            var jsonConfigSource = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);
            jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));

            var builder = new ConfigurationBuilder();
            builder.Add(jsonConfigSource, load: false);
            var config = builder.Build();

            var configurationSection = config.GetSection("setting");
            var indexConfigurationSections = configurationSection.GetChildren().ToArray();

            Assert.Equal(3, indexConfigurationSections.Count());
            Assert.Equal("b", indexConfigurationSections[0].Value);
            Assert.Equal("a", indexConfigurationSections[1].Value);
            Assert.Equal("2", indexConfigurationSections[2].Value);
        }
        public void ArraysAreConvertedToKeyValuePairs()
        {
            var json = @"{
                'ip': [
                    '1.2.3.4',
                    '7.8.9.10',
                    '11.12.13.14'
                ]
            }";

            var jsonConfigSource = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);
            jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));

            Assert.Equal("1.2.3.4", jsonConfigSource.Get("ip:0"));
            Assert.Equal("7.8.9.10", jsonConfigSource.Get("ip:1"));
            Assert.Equal("11.12.13.14", jsonConfigSource.Get("ip:2"));
        }
Exemple #18
0
        public void ImplicitArrayItemReplacement()
        {
            var json1 = @"{
                'ip': [
                    '1.2.3.4',
                    '7.8.9.10',
                    '11.12.13.14'
                ]
            }";

            var json2 = @"{
                'ip': [
                    '15.16.17.18'
                ]
            }";

            var jsonConfigSource1 = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);
            jsonConfigSource1.Load(TestStreamHelpers.StringToStream(json1));

            var jsonConfigSource2 = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);
            jsonConfigSource2.Load(TestStreamHelpers.StringToStream(json2));

            var builder = new ConfigurationBuilder();
            builder.Add(jsonConfigSource1, load: false);
            builder.Add(jsonConfigSource2, load: false);
            var config = builder.Build();

            Assert.Equal(3, config.GetConfigurationSections("ip").Count());
            Assert.Equal("15.16.17.18", config.Get("ip:0"));
            Assert.Equal("7.8.9.10", config.Get("ip:1"));
            Assert.Equal("11.12.13.14", config.Get("ip:2"));
        }
        public void PropertiesAreSortedByNumberOnlyFirst()
        {
            var json = @"{
                'setting': {
                    'hello': 'a',
                    'bob': 'b',
                    '42': 'c',
                    '4':'d',
                    '10': 'e',
                    '1text': 'f',
                }
            }";

            var jsonConfigSource = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);
            jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));

            var builder = new ConfigurationBuilder();
            builder.Add(jsonConfigSource, load: false);
            var config = builder.Build();

            var configurationSection = config.GetSection("setting");
            var indexConfigurationSections = configurationSection.GetChildren().ToArray();

            Assert.Equal(6, indexConfigurationSections.Count());
            Assert.Equal("setting:4", indexConfigurationSections[0].Key);
            Assert.Equal("setting:10", indexConfigurationSections[1].Key);
            Assert.Equal("setting:42", indexConfigurationSections[2].Key);
            Assert.Equal("setting:1text", indexConfigurationSections[3].Key);
            Assert.Equal("setting:bob", indexConfigurationSections[4].Key);
            Assert.Equal("setting:hello", indexConfigurationSections[5].Key);
        }
        public void NestedArrays()
        {
            var json = @"{
                'ip': [
                    [
                        '1.2.3.4',
                        '5.6.7.8'
                    ],
                    [
                        '9.10.11.12',
                        '13.14.15.16'
                    ],
                ]
            }";

            var jsonConfigSource = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);
            jsonConfigSource.Load(TestStreamHelpers.StringToStream(json));

            Assert.Equal("1.2.3.4", jsonConfigSource.Get("ip:0:0"));
            Assert.Equal("5.6.7.8", jsonConfigSource.Get("ip:0:1"));
            Assert.Equal("9.10.11.12", jsonConfigSource.Get("ip:1:0"));
            Assert.Equal("13.14.15.16", jsonConfigSource.Get("ip:1:1"));
        }
Exemple #21
-1
        public void ArrayMerge()
        {
            var json1 = @"{
                'ip': [
                    '1.2.3.4',
                    '7.8.9.10',
                    '11.12.13.14'
                ]
            }";

            var json2 = @"{
                'ip': {
                    '3': '15.16.17.18'
                }
            }";

            var jsonConfigSource1 = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);
            jsonConfigSource1.Load(TestStreamHelpers.StringToStream(json1));

            var jsonConfigSource2 = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath);
            jsonConfigSource2.Load(TestStreamHelpers.StringToStream(json2));

            var builder = new ConfigurationBuilder();
            builder.Add(jsonConfigSource1, load: false);
            builder.Add(jsonConfigSource2, load: false);
            var config = builder.Build();

            Assert.Equal(4, config.GetSection("ip").GetChildren().Count());
            Assert.Equal("1.2.3.4", config["ip:0"]);
            Assert.Equal("7.8.9.10", config["ip:1"]);
            Assert.Equal("11.12.13.14", config["ip:2"]);
            Assert.Equal("15.16.17.18", config["ip:3"]);
        }