public void AllRegisteredObjectsAreNotifiedOfDifferentSectionsChangesForAppConfig()
        {
            SystemConfigurationSourceImplementation implementation = new SystemConfigurationSourceImplementation(false);
            implementation.GetSection(localSection);
            implementation.GetSection(localSection2);
            implementation.AddSectionChangeHandler(localSection, new ConfigurationChangedEventHandler(OnConfigurationChanged));
            implementation.AddSectionChangeHandler(localSection2, new ConfigurationChangedEventHandler(OnConfigurationChanged));

            implementation.ConfigSourceChanged(localSectionSource);

            Assert.AreEqual(1, updatedSectionsTally[localSection]);
            Assert.AreEqual(1, updatedSectionsTally[localSection2]);
        }
        public void WatchedSectionInAppConfigValuesAreUpdatedIfAppConfigChangesAndNotificationIsFired()
        {
            SystemConfigurationSourceImplementation implementation = new SystemConfigurationSourceImplementation(false);

            object section1 = implementation.GetSection(localSection);
            Assert.IsNotNull(section1);
            DummySection dummySection1 = section1 as DummySection;
            Assert.AreEqual(localSection, dummySection1.Name);
            Assert.AreEqual(10, dummySection1.Value);

            System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            DummySection rwSection = rwConfiguration.GetSection(localSection) as DummySection;
            rwSection.Value = 15;
            rwConfiguration.Save();

            implementation.ConfigSourceChanged(localSectionSource);

            section1 = implementation.GetSection(localSection);
            Assert.IsNotNull(section1);
            dummySection1 = section1 as DummySection;
            Assert.AreEqual(localSection, dummySection1.Name);
            Assert.AreEqual(15, dummySection1.Value);
        }
        public void WatchedExistingSectionIsNoLongerWatchedIfRemovedFromConfiguration()
        {
            SystemConfigurationSourceImplementation implementation = new SystemConfigurationSourceImplementation(false);
            DummySection dummySection1 = implementation.GetSection(localSection) as DummySection;
            DummySection dummySection2 = implementation.GetSection(localSection2) as DummySection;
            Assert.IsNotNull(dummySection1);
            Assert.IsNotNull(dummySection2);
            Assert.AreEqual(1, implementation.WatchedConfigSources.Count);
            Assert.IsTrue(implementation.WatchedConfigSources.Contains(localSectionSource));
            Assert.AreEqual(2, implementation.WatchedSections.Count);
            Assert.IsTrue(implementation.WatchedSections.Contains(localSection));
            Assert.IsTrue(implementation.WatchedSections.Contains(localSection2));

            System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            rwConfiguration.Sections.Remove(localSection2);
            rwConfiguration.Save();

            implementation.ConfigSourceChanged(localSectionSource);

            Assert.AreEqual(2, implementation.WatchedConfigSources.Count);
            Assert.IsTrue(implementation.WatchedConfigSources.Contains(localSectionSource));
            Assert.IsTrue(implementation.WatchedConfigSources.Contains(SystemConfigurationSourceImplementation.NullConfigSource));
            Assert.AreEqual(2, implementation.WatchedSections.Count);
            Assert.IsTrue(implementation.WatchedSections.Contains(localSection));
            Assert.IsTrue(implementation.WatchedSections.Contains(localSection2));
            Assert.AreEqual(1, implementation.ConfigSourceWatcherMappings[string.Empty].WatchedSections.Count);
            Assert.IsTrue(implementation.ConfigSourceWatcherMappings[string.Empty].WatchedSections.Contains(localSection));
            Assert.AreEqual(1, implementation.ConfigSourceWatcherMappings[SystemConfigurationSourceImplementation.NullConfigSource].WatchedSections.Count);
            Assert.IsTrue(implementation.ConfigSourceWatcherMappings[SystemConfigurationSourceImplementation.NullConfigSource].WatchedSections.Contains(localSection2));
        }
        public void RestoredSectionGetsNotificationOnRestoreAndGetsFurtherNotifications()
        {
            SystemConfigurationSourceImplementation implementation = new SystemConfigurationSourceImplementation(false);

            object section1 = implementation.GetSection(localSection);
            Assert.IsNotNull(section1);
            object section2 = implementation.GetSection(localSection2);
            Assert.IsNotNull(section2);

            implementation.AddSectionChangeHandler(localSection, new ConfigurationChangedEventHandler(OnConfigurationChanged));
            implementation.AddSectionChangeHandler(localSection2, new ConfigurationChangedEventHandler(OnConfigurationChanged));

            // a change in system config notifies both sections
            System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            rwConfiguration.Save(ConfigurationSaveMode.Minimal, true);

            implementation.ConfigSourceChanged(localSectionSource);
            Assert.AreEqual(1, updatedSectionsTally[localSection]);
            Assert.AreEqual(1, updatedSectionsTally[localSection2]);

            // removal of the section notifies both sections
            rwConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            rwConfiguration.Sections.Remove(localSection2);
            rwConfiguration.Save(ConfigurationSaveMode.Minimal, true);

            implementation.ConfigSourceChanged(localSectionSource);
            Assert.AreEqual(2, updatedSectionsTally[localSection]);
            Assert.AreEqual(2, updatedSectionsTally[localSection2]);

            // further updates only notify the remaining section
            rwConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            rwConfiguration.Save(ConfigurationSaveMode.Minimal, true);

            implementation.ConfigSourceChanged(localSectionSource);
            Assert.AreEqual(3, updatedSectionsTally[localSection]);
            Assert.AreEqual(2, updatedSectionsTally[localSection2]);

            // restore of section gets notified
            DummySection rwSection = new DummySection();
            rwSection.Name = localSection2;
            rwSection.Value = 30;
            rwSection.SectionInformation.ConfigSource = localSectionSource;
            rwConfiguration.Sections.Add(localSection2, rwSection);
            rwConfiguration.Save(ConfigurationSaveMode.Minimal, true);

            implementation.ConfigSourceChanged(localSectionSource);
            Assert.AreEqual(4, updatedSectionsTally[localSection]);
            Assert.AreEqual(3, updatedSectionsTally[localSection2]);

            // further updates notify both sections
            rwConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            rwConfiguration.Save(ConfigurationSaveMode.Minimal, true);

            implementation.ConfigSourceChanged(localSectionSource);
            Assert.AreEqual(5, updatedSectionsTally[localSection]);
            Assert.AreEqual(4, updatedSectionsTally[localSection2]);
        }
        public void RemovedSectionGetsNotificationOnRemovalAndDoesNotGetFurtherNotifications()
        {
            ConfigurationChangeWatcher.SetDefaultPollDelayInMilliseconds(100);

            SystemConfigurationSourceImplementation implementation = new SystemConfigurationSourceImplementation(false);

            object section1 = implementation.GetSection(localSection);
            Assert.IsNotNull(section1);
            object section2 = implementation.GetSection(localSection2);
            Assert.IsNotNull(section2);

            implementation.AddSectionChangeHandler(localSection, new ConfigurationChangedEventHandler(OnConfigurationChanged));
            implementation.AddSectionChangeHandler(localSection2, new ConfigurationChangedEventHandler(OnConfigurationChanged));

            // a change in system config notifies both sections
            System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            rwConfiguration.Save(ConfigurationSaveMode.Minimal, true);

            implementation.ConfigSourceChanged(localSectionSource);
            Assert.AreEqual(1, updatedSectionsTally[localSection]);
            Assert.AreEqual(1, updatedSectionsTally[localSection2]);

            // removal of the section notifies both sections
            rwConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            rwConfiguration.Sections.Remove(localSection2);
            rwConfiguration.Save(ConfigurationSaveMode.Minimal, true);

            implementation.ConfigSourceChanged(localSectionSource);
            Assert.AreEqual(2, updatedSectionsTally[localSection]);
            Assert.AreEqual(2, updatedSectionsTally[localSection2]);

            // further updates only notify the remaining section
            rwConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            rwConfiguration.Save(ConfigurationSaveMode.Minimal, true);

            implementation.ConfigSourceChanged(localSectionSource);
            Assert.AreEqual(3, updatedSectionsTally[localSection]);
            Assert.AreEqual(2, updatedSectionsTally[localSection2]);
        }
        public void RegisteredObjectForNonRequestedSectionIsNotNotified()
        {
            SystemConfigurationSourceImplementation implementation = new SystemConfigurationSourceImplementation(false);
            implementation.AddSectionChangeHandler(localSection, new ConfigurationChangedEventHandler(OnConfigurationChanged));

            implementation.ConfigSourceChanged(localSectionSource);

            Assert.IsFalse(updatedSectionsTally.ContainsKey(localSection));
        }
        public void RegisteredObjectForExternalFileIsNotNotifiedOfSectionChangesForAppConfigIfConfigSourceForExternalSectionHasNotChanged()
        {
            SystemConfigurationSourceImplementation implementation = new SystemConfigurationSourceImplementation(false);
            implementation.GetSection(externalSection);
            implementation.AddSectionChangeHandler(externalSection, new ConfigurationChangedEventHandler(OnConfigurationChanged));

            implementation.ConfigSourceChanged(localSectionSource);

            Assert.IsFalse(updatedSectionsTally.ContainsKey(externalSection));
        }