public void DictionaryDataIsMergedWithLatestSourceOverridingSameKeys()
        {
            var baseDict = new Dictionary <string, string> {
                { "one", "direction" }, { "troll", "unittests" }
            };
            var overrideDict = new Dictionary <string, string> {
                { "one", "singular sensation" }, { "two", "if by sea" }
            };
            var baseData = JsonConvert.SerializeObject(new SampleData {
                Dictionary = baseDict
            });
            var overrideData = JsonConvert.SerializeObject(new SampleData {
                Dictionary = overrideDict
            });

            using (var firstSource = new TextConfigurationSource(baseData))
            {
                using (var secondSource = new TextConfigurationSource(overrideData))
                {
                    using (var data = new ConfigurationData <SampleData>(new[] { firstSource, secondSource }))
                    {
                        Assert.AreEqual(3, data.Value.Dictionary.Count);
                        Assert.AreEqual(baseDict["troll"], data.Value.Dictionary["troll"]);
                        Assert.AreEqual(overrideDict["one"], data.Value.Dictionary["one"]);
                        Assert.AreEqual(overrideDict["two"], data.Value.Dictionary["two"]);
                    }
                }
            }
        }
        public void UnhandledExceptionsDuringConversionCallExceptionHandler()
        {
            var sampleData = new SampleData
            {
                Dictionary = new Dictionary <string, string> {
                    { "hello", "goodbye" }
                },
                Number      = 42,
                MoreNumbers = new List <int> {
                    8, 6, 7, 5, 3, 0, 9
                },
                String = string.Empty,
            };

            using (var source = new TextConfigurationSource(JsonConvert.SerializeObject(sampleData)))
            {
                using (var data = new ConfigurationData <SampleData>(new[] { source }))
                {
                    Assert.IsNotNull(data.Value);
                    Assert.AreEqual(sampleData, data.Value);
                    Assert.IsTrue(data.IsValid);

                    var exceptionEventCalled = false;
                    data.UnhandledException += (src, ex) => exceptionEventCalled = true;

                    sampleData.Number = SampleData.MagicExceptionThrowingNumber;
                    Assert.Throws <OperationCanceledException>(
                        () =>
                        source.Contents = JsonConvert.SerializeObject(sampleData));
                    Assert.IsTrue(exceptionEventCalled);
                }
            }
        }
        public void ArrayDataIsReplacedByLatestSource()
        {
            var baseArray = new List <int> {
                8, 6, 7
            };
            var overrideArray = new List <int> {
                5, 3, 0, 9
            };
            var baseData = JsonConvert.SerializeObject(new SampleData {
                MoreNumbers = baseArray
            });
            var overrideData = JsonConvert.SerializeObject(new SampleData {
                MoreNumbers = overrideArray
            });

            using (var firstSource = new TextConfigurationSource(baseData))
            {
                using (var secondSource = new TextConfigurationSource(overrideData))
                {
                    using (var data = new ConfigurationData <SampleData>(new[] { firstSource, secondSource }))
                    {
                        Assert.That(data.Value.MoreNumbers, Is.EquivalentTo(overrideArray));
                    }
                }
            }
        }
        public void TextConfigurationSourceContentCannotBeNull()
        {
            Assert.Throws <ArgumentNullException>(() => new TextConfigurationSource(null));

            Assert.Throws <ArgumentNullException>(() =>
            {
                var source      = new TextConfigurationSource();
                source.Contents = null;
            });
        }
        public void TextConfigurationSourceContentCannotBeNull()
        {
            Assert.Throws<ArgumentNullException>(() => new TextConfigurationSource(null));

            Assert.Throws<ArgumentNullException>(() =>
                                                 {
                                                     var source = new TextConfigurationSource();
                                                     source.Contents = null;
                                                 });
        }
        public void ContentUpdateHandlerIsCalledForInvalidData()
        {
            var textSource = new TextConfigurationSource();
            var updated = false;
            textSource.SourceUpdated += () => updated = true;

            Assert.AreEqual(false, updated);
            textSource.Contents = "not valid JSON";
            Assert.AreEqual(true, updated);
        }
        public void ContentUpdateHandlerIsCalled()
        {
            var textSource = new TextConfigurationSource();
            var updated = false;
            textSource.SourceUpdated += () => updated = true;

            Assert.AreEqual(false, updated);
            textSource.Contents = string.Empty;
            Assert.AreEqual(true, updated);
        }
        public void ContentUpdateHandlerIsCalledForInvalidData()
        {
            var textSource = new TextConfigurationSource();
            var updated    = false;

            textSource.SourceUpdated += () => updated = true;

            Assert.AreEqual(false, updated);
            textSource.Contents = "not valid JSON";
            Assert.AreEqual(true, updated);
        }
        public void ContentUpdateHandlerIsCalled()
        {
            var textSource = new TextConfigurationSource();
            var updated    = false;

            textSource.SourceUpdated += () => updated = true;

            Assert.AreEqual(false, updated);
            textSource.Contents = string.Empty;
            Assert.AreEqual(true, updated);
        }
        public void IfConfigurationIsDeclaredInvalidItIsNotUsed()
        {
            var sampleData = new SampleData {
                Number = 42, Valid = false
            };

            using (var source = new TextConfigurationSource(JsonConvert.SerializeObject(sampleData)))
            {
                using (var data = new ConfigurationData <SampleData>(new[] { source }))
                {
                    Assert.IsNull(data.Value);
                    Assert.IsFalse(data.IsValid);
                }
            }
        }
        public void InvalidDataDoesNotOverrideValidData()
        {
            var sampleData = new SampleData {
                Number = 42,
            };

            using (var source = new TextConfigurationSource(JsonConvert.SerializeObject(sampleData)))
            {
                using (var data = new ConfigurationData <SampleData>(new[] { source }))
                {
                    Assert.IsNotNull(data.Value);
                    Assert.AreEqual(sampleData, data.Value);

                    var dataValue = data.Value;
                    source.Contents = "not valid";
                    Assert.IsFalse(data.IsValid);
                    Assert.AreSame(dataValue, data.Value);
                }
            }
        }
        public void SimplePropertiesAreOverwritten()
        {
            var baseData = JsonConvert.SerializeObject(new SampleData {
                String = "hello", Number = 867
            });
            var overrideData = JsonConvert.SerializeObject(new SampleData {
                String = "goodbye", Number = 5309
            });

            using (var firstSource = new TextConfigurationSource(baseData))
            {
                using (var secondSource = new TextConfigurationSource(overrideData))
                {
                    using (var data = new ConfigurationData <SampleData>(new[] { firstSource, secondSource }))
                    {
                        Assert.AreEqual("goodbye", data.Value.String);
                        Assert.AreEqual(5309, data.Value.Number);
                    }
                }
            }
        }
        public void CanGetValueFromSingleSource()
        {
            var sampleData = new SampleData
            {
                Dictionary = new Dictionary <string, string> {
                    { "hello", "goodbye" }
                },
                Number      = 42,
                MoreNumbers = new List <int> {
                    8, 6, 7, 5, 3, 0, 9
                },
                String = string.Empty,
            };

            using (var source = new TextConfigurationSource(JsonConvert.SerializeObject(sampleData)))
            {
                using (var data = new ConfigurationData <SampleData>(new[] { source }))
                {
                    Assert.IsNotNull(data.Value);
                    Assert.AreEqual(sampleData, data.Value);
                    Assert.IsTrue(data.IsValid);
                }
            }
        }
        public void ContentUpdateWithoutHandlerIsSafe()
        {
            var textSource = new TextConfigurationSource();

            textSource.Contents = "{}";
        }
 public void ContentUpdateWithoutHandlerIsSafe()
 {
     var textSource = new TextConfigurationSource();
     textSource.Contents = "{}";
 }