public void WatchedExistingSectionInExternalFileIsNoLongerWatchedIfRemovedFromConfigurationAndExternalFileWatcherIsRemoved()
        {
            IConfigurationSourceTest source        = new SystemConfigurationSource(false);
            DummySection             dummySection1 = source.GetSection(localSection) as DummySection;
            DummySection             dummySection2 = source.GetSection(externalSection) as DummySection;

            Assert.IsNotNull(dummySection1);
            Assert.IsNotNull(dummySection2);
            Assert.AreEqual(2, source.WatchedConfigSources.Count);
            Assert.IsTrue(source.WatchedConfigSources.Contains(localSectionSource));
            Assert.IsTrue(source.WatchedConfigSources.Contains(externalSectionSource));
            Assert.AreEqual(3, source.WatchedSections.Count); //watches  ConfigurationSourceSection + externalSection + localSection
            Assert.IsTrue(source.WatchedSections.Contains(localSection));
            Assert.IsTrue(source.WatchedSections.Contains(externalSection));

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

            source.ConfigSourceChanged(localSectionSource);

            Assert.AreEqual(2, source.WatchedConfigSources.Count);
            Assert.IsTrue(source.WatchedConfigSources.Contains(localSectionSource));
            Assert.IsTrue(source.WatchedConfigSources.Contains(SystemConfigurationSource.NullConfigSource));
            Assert.AreEqual(3, source.WatchedSections.Count);
            Assert.IsTrue(source.WatchedSections.Contains(localSection));
            Assert.IsTrue(source.WatchedSections.Contains(externalSection));
            Assert.AreEqual(2, source.ConfigSourceWatcherMappings[string.Empty].WatchedSections.Count);
            Assert.IsTrue(source.ConfigSourceWatcherMappings[string.Empty].WatchedSections.Contains(localSection));
            Assert.AreEqual(1, source.ConfigSourceWatcherMappings[SystemConfigurationSource.NullConfigSource].WatchedSections.Count);
            Assert.IsTrue(source.ConfigSourceWatcherMappings[SystemConfigurationSource.NullConfigSource].WatchedSections.Contains(externalSection));
        }
        public void WatchedSectionInExternalFileValuesAreUpdatedIfExternalFileChangesAndNotificationIsFired()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);

            object section1 = source.GetSection(externalSection);

            Assert.IsNotNull(section1);
            DummySection dummySection1 = section1 as DummySection;

            Assert.AreEqual(externalSection, dummySection1.Name);
            Assert.AreEqual(20, dummySection1.Value);

            System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            DummySection rwSection = rwConfiguration.GetSection(externalSection) as DummySection;

            rwSection.Value = 25;
            rwConfiguration.Save();

            source.ExternalConfigSourceChanged(externalSectionSource);

            section1 = source.GetSection(externalSection);
            Assert.IsNotNull(section1);
            dummySection1 = section1 as DummySection;
            Assert.AreEqual(externalSection, dummySection1.Name);
            Assert.AreEqual(25, dummySection1.Value);
        }
        public void RestoredSectionGetsNotificationOnRestoreAndGetsFurtherNotifications()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);

            object section1 = source.GetSection(localSection);

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

            Assert.IsNotNull(section2);

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

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

            source.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();

            source.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();

            source.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();

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

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

            source.ConfigSourceChanged(localSectionSource);
            Assert.AreEqual(5, updatedSectionsTally[localSection]);
            Assert.AreEqual(4, updatedSectionsTally[localSection2]);
        }
        public void RemovingSectionIsReflectedInMemoryAndOnDisk_Bug2931()
        {
            SystemConfigurationSource sysSource = new SystemConfigurationSource(false);
            var originalSection = (DummySection)(sysSource.GetSection(removeSectionName));
            Assert.IsNotNull(originalSection);

            sysSource.Remove(removeSectionName);

            var returnedSection = (DummySection)(sysSource.GetSection(removeSectionName));
            Assert.IsNull(returnedSection);
        }
        public void GetsNotifiedOfMultipleRetrievedSection()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);

            source.GetSection(localSection);
            source.GetSection(localSection2);
            source.SourceChanged += this.OnConfigurationSourceChanged;

            source.ConfigSourceChanged(localSectionSource);

            CollectionAssert.AreEquivalent(new[] { ConfigurationSourceSection.SectionName, localSection, localSection2 }, new List <string>(updatedSections));
        }
        public void RemovingSectionIsReflectedInMemoryAndOnDisk_Bug2931()
        {
            SystemConfigurationSource sysSource = new SystemConfigurationSource(false);
            var originalSection = (DummySection)(sysSource.GetSection(removeSectionName));

            Assert.IsNotNull(originalSection);

            sysSource.Remove(removeSectionName);

            var returnedSection = (DummySection)(sysSource.GetSection(removeSectionName));

            Assert.IsNull(returnedSection);
        }
        public void SecondRequestForSameSectionInAppConfigDoesNotCreateSecondWatcherForAppConfig()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);

            object section1 = source.GetSection(localSection);
            object section2 = source.GetSection(localSection);

            Assert.IsNotNull(section1);
            Assert.IsNotNull(section2);
            Assert.AreEqual(1, source.WatchedConfigSources.Count);
            Assert.IsTrue(source.WatchedConfigSources.Contains(localSectionSource));
            Assert.AreEqual(2, source.WatchedSections.Count); //watches  ConfigurationSourceSection + localSection
            Assert.IsTrue(source.WatchedSections.Contains(localSection));
        }
        public void AllRegisteredObjectsAreNotifiedOfDifferentSectionsChangesForAppConfig()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);

            source.GetSection(localSection);
            source.GetSection(localSection2);
            source.AddSectionChangeHandler(localSection, new ConfigurationChangedEventHandler(OnConfigurationChanged));
            source.AddSectionChangeHandler(localSection2, new ConfigurationChangedEventHandler(OnConfigurationChanged));

            source.ConfigSourceChanged(localSectionSource);

            Assert.AreEqual(1, updatedSectionsTally[localSection]);
            Assert.AreEqual(1, updatedSectionsTally[localSection2]);
        }
        public void SystemConfigurationSourceReturnsReadOnlySections()
        {
            SystemConfigurationSource source       = new SystemConfigurationSource(false);
            ConfigurationSection      dummySection = source.GetSection(localSection);

            Assert.IsTrue(dummySection.IsReadOnly());
        }
        public void RemovingAndAddingSection()
        {
            SystemConfigurationSource.ResetImplementation(true);
            SystemConfigurationSource sysSource = new SystemConfigurationSource();
            DummySection dummySection           = sysSource.GetSection(localSection) as DummySection;

            Assert.IsTrue(dummySection != null);
            Thread.Sleep(300);
            System.Configuration.Configuration rwConfiguration =
                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            string fileName    = rwConfiguration.FilePath;
            int    numSections = rwConfiguration.Sections.Count;
            FileConfigurationParameter parameter = new FileConfigurationParameter(fileName);

            sysSource.Remove(parameter, localSection);
            Thread.Sleep(300);
            rwConfiguration =
                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            Assert.AreEqual(rwConfiguration.Sections.Count, numSections - 1);
            sysSource.Add(parameter, localSection, new DummySection());
            Thread.Sleep(300);
            rwConfiguration =
                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            Assert.AreEqual(rwConfiguration.Sections.Count, numSections);
        }
Exemple #11
0
        /// <summary>
        /// Returns a new OnPremiseConfigurationSource configured with the specified settings.
        /// </summary>
        /// <returns>A new configuration source.</returns>
        public override IConfigurationSource CreateSource()
        {
            // The initial configuration containing the Service Bus endpoint information is expected to be defined in app.config.
            string configFileName = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

            SystemConfigurationSource       systemConfig     = new SystemConfigurationSource();
            ServiceBusConfigurationSettings serviceBusConfig = systemConfig.GetSection(ServiceBusConfigurationSettings.SectionName) as ServiceBusConfigurationSettings;

            if (serviceBusConfig != null)
            {
                ServiceBusEndpointInfo defaultEndpointInfo = UseDefaultServiceBusEndpoint ? serviceBusConfig.Endpoints[serviceBusConfig.DefaultEndpoint] : serviceBusConfig.Endpoints[ServiceBusEndpoint];
                if (defaultEndpointInfo != null)
                {
                    return(new OnPremiseConfigurationSource(defaultEndpointInfo));
                }
                else
                {
                    if (UseDefaultServiceBusEndpoint)
                    {
                        throw new ConfigurationErrorsException(String.Format(CultureInfo.InvariantCulture, ExceptionMessages.DefaultServiceBusEndpointNotFound, configFileName, ServiceBusConfigurationSettings.SectionName));
                    }
                    else
                    {
                        throw new ConfigurationErrorsException(String.Format(CultureInfo.InvariantCulture, ExceptionMessages.SpecifiedServiceBusEndpointNotFoundInConfigFile, configFileName, ServiceBusConfigurationSettings.SectionName, ServiceBusEndpoint));
                    }
                }
            }
            else
            {
                throw new ConfigurationErrorsException(String.Format(CultureInfo.InvariantCulture, ExceptionMessages.ConfigSectionNotFoundInConfigFile, configFileName, ServiceBusConfigurationSettings.SectionName));
            }
        }
        public void RequestsForAppConfigAndExternalFileCreatesWatchersForBoth()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);

            object section1 = source.GetSection(localSection);
            object section2 = source.GetSection(externalSection);

            Assert.IsNotNull(section1);
            Assert.IsNotNull(section2);
            Assert.AreEqual(2, source.WatchedConfigSources.Count);
            Assert.IsTrue(source.WatchedConfigSources.Contains(localSectionSource));
            Assert.IsTrue(source.WatchedConfigSources.Contains(externalSectionSource));
            Assert.AreEqual(3, source.WatchedSections.Count); //watches  ConfigurationSourceSection + externalSection + localSection
            Assert.IsTrue(source.WatchedSections.Contains(localSection));
            Assert.IsTrue(source.WatchedSections.Contains(externalSection));
        }
        public void CanGetExistingSectionInExternalFile()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);
            object section = source.GetSection(externalSection);

            Assert.IsNotNull(section);
        }
        public void SystemConfigurationSourceReturnsReadOnlySections()
        {
            SystemConfigurationSource source = new SystemConfigurationSource(false);
            ConfigurationSection dummySection = source.GetSection(localSection);

            Assert.IsTrue(dummySection.IsReadOnly());
        }
        public void CanGetExistingSectionInAppConfig()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);
            object section = source.GetSection(localSection);

            Assert.IsNotNull(section);
        }
        public void GetsNullIfSectionDoesNotExist()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);
            object section = source.GetSection(nonExistingSection);

            Assert.IsNull(section);
        }
Exemple #17
0
        public void DifferentFileConfigurationSourcesDoNotShareEvents()
        {
            ConfigurationChangeWatcher.SetDefaultPollDelayInMilliseconds(50);
            string otherConfigurationFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Other.config");

            FileConfigurationSource.ResetImplementation(otherConfigurationFile, true);
            SystemConfigurationSource.ResetImplementation(true);

            File.Copy(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, otherConfigurationFile);

            try
            {
                bool sysSourceChanged   = false;
                bool otherSourceChanged = false;

                SystemConfigurationSource systemSource = new SystemConfigurationSource();
                FileConfigurationSource   otherSource  = new FileConfigurationSource(otherConfigurationFile);

                DummySection sysDummySection   = systemSource.GetSection(localSection) as DummySection;
                DummySection otherDummySection = otherSource.GetSection(localSection) as DummySection;
                Assert.IsTrue(sysDummySection != null);
                Assert.IsTrue(otherDummySection != null);

                systemSource.AddSectionChangeHandler(localSection, delegate(object o,
                                                                            ConfigurationChangedEventArgs args)
                {
                    sysSourceChanged = true;
                });

                otherSource.AddSectionChangeHandler(localSection, delegate(object o,
                                                                           ConfigurationChangedEventArgs args)
                {
                    Assert.AreEqual(12, ((DummySection)otherSource.GetSection(localSection)).Value);
                    otherSourceChanged = true;
                });

                DummySection rwSection = new DummySection();
                System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenExeConfiguration(otherConfigurationFile);
                rwConfiguration.Sections.Remove(localSection);
                rwConfiguration.Sections.Add(localSection, rwSection = new DummySection());
                rwSection.Name  = localSection;
                rwSection.Value = 12;
                rwSection.SectionInformation.ConfigSource = localSectionSource;

                rwConfiguration.SaveAs(otherConfigurationFile);

                Thread.Sleep(200);

                Assert.AreEqual(false, sysSourceChanged);
                Assert.AreEqual(true, otherSourceChanged);
            }
            finally
            {
                if (File.Exists(otherConfigurationFile))
                {
                    File.Delete(otherConfigurationFile);
                }
            }
        }
Exemple #18
0
        private static void AddExceptionHandlingConfiguration(IUnityContainer factory)
        {
            var configSource = new SystemConfigurationSource(false);

            new UnityContainerConfigurator(factory).RegisterAll(
                configSource,
                (ITypeRegistrationsProvider)configSource.GetSection(ExceptionHandlingSettings.SectionName));
        }
        public void CanReceiveNotificationsFromImplementation()
        {
            SystemConfigurationSource.ResetImplementation(false);
            SystemConfigurationSource source = new SystemConfigurationSource();

            source.GetSection(localSection);
            source.AddSectionChangeHandler(localSection, new ConfigurationChangedEventHandler(OnConfigurationChanged));
            SystemConfigurationSource.Implementation.ConfigSourceChanged(localSectionSource);
            Assert.AreEqual(1, updatedSectionsTally[localSection]);
        }
        public static void CheckSource()
        {
            var source = new SystemConfigurationSource(false);
            object section = source.GetSection(localSection);

            if (section == null)
            {
                throw new Exception("could not get section");
            }
        }
Exemple #21
0
        public static void CheckSource()
        {
            var    source  = new SystemConfigurationSource(false);
            object section = source.GetSection(localSection);

            if (section == null)
            {
                throw new Exception("could not get section");
            }
        }
        public void RequestForNonexistentSectionCreatesNoWatcher()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);

            object section = source.GetSection(nonExistingSection);

            Assert.IsNull(section);
            Assert.AreEqual(1, source.WatchedConfigSources.Count); //watches only ConfigurationSourceSection.
            Assert.AreEqual(1, source.WatchedSections.Count);
        }
        public void RegisteredObjectIsNotifiedOfSectionChangesForExternalFile()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);

            source.GetSection(externalSection);
            source.AddSectionChangeHandler(externalSection, new ConfigurationChangedEventHandler(OnConfigurationChanged));

            source.ExternalConfigSourceChanged(externalSectionSource);

            Assert.AreEqual(1, updatedSectionsTally[externalSection]);
        }
        public void RegisteredObjectForExternalFileIsNotifiedOfSectionChangesForAppConfigIfConfigSourceForExternalSectionNotChanged()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);

            source.GetSection(externalSection);
            source.AddSectionChangeHandler(externalSection, new ConfigurationChangedEventHandler(OnConfigurationChanged));

            source.ConfigSourceChanged(localSectionSource);

            Assert.IsFalse(updatedSectionsTally.ContainsKey(externalSection));
        }
        public void SameImplementationSurvivesAcrossInstances()
        {
            SystemConfigurationSource source1 = new SystemConfigurationSource();
            object section = source1.GetSection(localSection);

            Assert.IsNotNull(section);
            Assert.AreEqual(1, SystemConfigurationSource.Implementation.WatchedSections.Count);
            Assert.IsTrue(SystemConfigurationSource.Implementation.WatchedSections.Contains(localSection));
            source1 = null;
            GC.Collect(3);
            Assert.AreEqual(1, SystemConfigurationSource.Implementation.WatchedSections.Count);
            Assert.IsTrue(SystemConfigurationSource.Implementation.WatchedSections.Contains("dummy.local"));
        }
        public void FirstRequestForSectionInExternalFileCreatesWatchersForExternalFileAndAppConfig()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);

            object section = source.GetSection(externalSection);

            Assert.IsNotNull(section);
            Assert.AreEqual(2, source.WatchedConfigSources.Count);
            Assert.IsTrue(source.WatchedConfigSources.Contains(localSectionSource));
            Assert.IsTrue(source.WatchedConfigSources.Contains(externalSectionSource));
            Assert.AreEqual(2, source.WatchedSections.Count); //watches  ConfigurationSourceSection + externalSection
            Assert.IsTrue(source.WatchedSections.Contains(externalSection));
        }
        public void FirstRequestForSectionInAppConfigCreatesWatcherForAppConfig()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(true);

            object section = source.GetSection(localSection);

            Assert.IsNotNull(section);
            Assert.AreEqual(1, source.WatchedConfigSources.Count);
            Assert.IsTrue(source.WatchedConfigSources.Contains(localSectionSource));
            Assert.AreEqual(2, source.WatchedSections.Count); //watches  ConfigurationSourceSection + localSection
            Assert.IsTrue(source.WatchedSections.Contains(localSection));

            Assert.IsNotNull(source.ConfigSourceWatcherMappings[localSectionSource].Watcher);
            Assert.AreEqual(source.ConfigSourceWatcherMappings[localSectionSource].Watcher.GetType(), typeof(ConfigurationChangeFileWatcher));

            ((IDisposable)source).Dispose();
        }
        public void CanAddAndRemoveHandlers()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);
            object section = source.GetSection(externalSection);

            Assert.IsNotNull(section);

            source.AddSectionChangeHandler(externalSection, new ConfigurationChangedEventHandler(OnConfigurationChanged));
            source.AddSectionChangeHandler(externalSection, new ConfigurationChangedEventHandler(OnConfigurationChanged));
            source.ExternalConfigSourceChanged(externalSectionSource);
            Assert.AreEqual(2, updatedSectionsTally[externalSection]);

            source.RemoveSectionChangeHandler(externalSection, new ConfigurationChangedEventHandler(OnConfigurationChanged));
            source.ExternalConfigSourceChanged(externalSectionSource);
            Assert.AreEqual(3, updatedSectionsTally[externalSection]);

            source.RemoveSectionChangeHandler(externalSection, new ConfigurationChangedEventHandler(OnConfigurationChanged));
            source.ExternalConfigSourceChanged(externalSectionSource);
            Assert.AreEqual(3, updatedSectionsTally[externalSection]);
        }
        public void RemovingAndAddingSection()
        {
            SystemConfigurationSource sysSource = new SystemConfigurationSource(false);

            DummySection dummySection = sysSource.GetSection(localSection) as DummySection;
            Assert.IsTrue(dummySection != null);

            System.Configuration.Configuration rwConfiguration =
                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            string fileName = rwConfiguration.FilePath;
            int numSections = rwConfiguration.Sections.Count;
            sysSource.Remove(localSection);

            rwConfiguration =
                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            Assert.AreEqual(rwConfiguration.Sections.Count, numSections - 1);
            sysSource.Add(localSection, new DummySection()); // can't be the same instance

            rwConfiguration =
                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            Assert.AreEqual(rwConfiguration.Sections.Count, numSections);
        }
        public void RemovingAndAddingSection()
        {
            SystemConfigurationSource sysSource = new SystemConfigurationSource(false);

            DummySection dummySection = sysSource.GetSection(localSection) as DummySection;

            Assert.IsTrue(dummySection != null);

            System.Configuration.Configuration rwConfiguration =
                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            string fileName    = rwConfiguration.FilePath;
            int    numSections = rwConfiguration.Sections.Count;

            sysSource.Remove(localSection);

            rwConfiguration =
                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            Assert.AreEqual(rwConfiguration.Sections.Count, numSections - 1);
            sysSource.Add(localSection, new DummySection()); // can't be the same instance

            rwConfiguration =
                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            Assert.AreEqual(rwConfiguration.Sections.Count, numSections);
        }
        public void AllRegisteredObjectsAreNotifiedOfSectionChangesForExternalFile()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);
            source.GetSection(externalSection);
            source.AddSectionChangeHandler(externalSection, new ConfigurationChangedEventHandler(OnConfigurationChanged));
            source.AddSectionChangeHandler(externalSection, new ConfigurationChangedEventHandler(OnConfigurationChanged));
            source.AddSectionChangeHandler(externalSection, new ConfigurationChangedEventHandler(OnConfigurationChanged));

            source.ExternalConfigSourceChanged(externalSectionSource);

            Assert.AreEqual(3, updatedSectionsTally[externalSection]);
        }
        public void RegisteredObjectForExternalFileIsNotifiedOfSectionChangesForAppConfigIfConfigSourceForExternalSectionNotChanged()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);
            source.GetSection(externalSection);
            source.AddSectionChangeHandler(externalSection, new ConfigurationChangedEventHandler(OnConfigurationChanged));

            source.ConfigSourceChanged(localSectionSource);

            Assert.IsFalse(updatedSectionsTally.ContainsKey(externalSection));
        }
        public void CanAddAndRemoveHandlers()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);
            object section = source.GetSection(externalSection);
            Assert.IsNotNull(section);

            source.AddSectionChangeHandler(externalSection, new ConfigurationChangedEventHandler(OnConfigurationChanged));
            source.AddSectionChangeHandler(externalSection, new ConfigurationChangedEventHandler(OnConfigurationChanged));
            source.ExternalConfigSourceChanged(externalSectionSource);
            Assert.AreEqual(2, updatedSectionsTally[externalSection]);

            source.RemoveSectionChangeHandler(externalSection, new ConfigurationChangedEventHandler(OnConfigurationChanged));
            source.ExternalConfigSourceChanged(externalSectionSource);
            Assert.AreEqual(3, updatedSectionsTally[externalSection]);

            source.RemoveSectionChangeHandler(externalSection, new ConfigurationChangedEventHandler(OnConfigurationChanged));
            source.ExternalConfigSourceChanged(externalSectionSource);
            Assert.AreEqual(3, updatedSectionsTally[externalSection]);
        }
        public void RestoredSectionGetsNotificationOnRestoreAndGetsFurtherNotifications()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);

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

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

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

            source.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();

            source.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();

            source.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();

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

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

            source.ConfigSourceChanged(localSectionSource);
            Assert.AreEqual(5, updatedSectionsTally[localSection]);
            Assert.AreEqual(4, updatedSectionsTally[localSection2]);
        }
        public void DifferentFileConfigurationSourcesDoNotShareEvents()
        {
            string otherConfigurationFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Other.config");
            File.Copy(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, otherConfigurationFile);

            try
            {
                bool sysSourceChanged = false;
                bool otherSourceChanged = false;

                using (SystemConfigurationSource systemSource = new SystemConfigurationSource(true, 50))
                using (FileConfigurationSource otherSource = new FileConfigurationSource(otherConfigurationFile, true, 50))
                {
                    DummySection sysDummySection = systemSource.GetSection(localSection) as DummySection;
                    DummySection otherDummySection = otherSource.GetSection(localSection) as DummySection;
                    Assert.IsTrue(sysDummySection != null);
                    Assert.IsTrue(otherDummySection != null);

                    systemSource.AddSectionChangeHandler(
                        localSection,
                        delegate(object o, ConfigurationChangedEventArgs args)
                        {
                            sysSourceChanged = true;
                        });

                    otherSource.AddSectionChangeHandler(
                        localSection,
                        delegate(object o, ConfigurationChangedEventArgs args)
                        {
                            Assert.AreEqual(12, ((DummySection)otherSource.GetSection(localSection)).Value);
                            otherSourceChanged = true;
                        });

                    DummySection rwSection = new DummySection();
                    System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenExeConfiguration(otherConfigurationFile);
                    rwConfiguration.Sections.Remove(localSection);
                    rwConfiguration.Sections.Add(localSection, rwSection = new DummySection());
                    rwSection.Name = localSection;
                    rwSection.Value = 12;
                    rwSection.SectionInformation.ConfigSource = localSectionSource;

                    rwConfiguration.SaveAs(otherConfigurationFile);

                    Thread.Sleep(500);

                    Assert.AreEqual(false, sysSourceChanged);
                    Assert.AreEqual(true, otherSourceChanged);
                }
            }
            finally
            {
                if (File.Exists(otherConfigurationFile))
                {
                    File.Delete(otherConfigurationFile);
                }
            }
        }
        public void RequestForNonexistentSectionCreatesNoWatcher()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);

            object section = source.GetSection(nonExistingSection);

            Assert.IsNull(section);
            Assert.AreEqual(1, source.WatchedConfigSources.Count); //watches only ConfigurationSourceSection.
            Assert.AreEqual(1, source.WatchedSections.Count);
        }
        public void FirstRequestForSectionInAppConfigCreatesWatcherForAppConfig()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(true);

            object section = source.GetSection(localSection);

            Assert.IsNotNull(section);
            Assert.AreEqual(1, source.WatchedConfigSources.Count);
            Assert.IsTrue(source.WatchedConfigSources.Contains(localSectionSource));
            Assert.AreEqual(2, source.WatchedSections.Count); //watches  ConfigurationSourceSection + localSection
            Assert.IsTrue(source.WatchedSections.Contains(localSection));

            Assert.IsNotNull(source.ConfigSourceWatcherMappings[localSectionSource].Watcher);
            Assert.AreEqual(source.ConfigSourceWatcherMappings[localSectionSource].Watcher.GetType(), typeof(ConfigurationChangeFileWatcher));

            ((IDisposable)source).Dispose();
        }
        public void SecondRequestForDifferentSectionInAppConfigDoesNotCreateSecondWatcherForAppConfig()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);

            object section1 = source.GetSection(localSection);
            object section2 = source.GetSection(localSection2);

            Assert.IsNotNull(section1);
            Assert.IsNotNull(section2);
            Assert.AreEqual(1, source.WatchedConfigSources.Count);
            Assert.IsTrue(source.WatchedConfigSources.Contains(localSectionSource));
            Assert.AreEqual(3, source.WatchedSections.Count); //watches  ConfigurationSourceSection. + localSection + localSection2
            Assert.IsTrue(source.WatchedSections.Contains(localSection));
            Assert.IsTrue(source.WatchedSections.Contains(localSection2));
        }
        public void WatchedExistingSectionInExternalFileIsNoLongerWatchedIfRemovedFromConfigurationAndExternalFileWatcherIsRemoved()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);
            DummySection dummySection1 = source.GetSection(localSection) as DummySection;
            DummySection dummySection2 = source.GetSection(externalSection) as DummySection;
            Assert.IsNotNull(dummySection1);
            Assert.IsNotNull(dummySection2);
            Assert.AreEqual(2, source.WatchedConfigSources.Count);
            Assert.IsTrue(source.WatchedConfigSources.Contains(localSectionSource));
            Assert.IsTrue(source.WatchedConfigSources.Contains(externalSectionSource));
            Assert.AreEqual(3, source.WatchedSections.Count); //watches  ConfigurationSourceSection + externalSection + localSection
            Assert.IsTrue(source.WatchedSections.Contains(localSection));
            Assert.IsTrue(source.WatchedSections.Contains(externalSection));

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

            source.ConfigSourceChanged(localSectionSource);

            Assert.AreEqual(2, source.WatchedConfigSources.Count);
            Assert.IsTrue(source.WatchedConfigSources.Contains(localSectionSource));
            Assert.IsTrue(source.WatchedConfigSources.Contains(SystemConfigurationSource.NullConfigSource));
            Assert.AreEqual(3, source.WatchedSections.Count); 
            Assert.IsTrue(source.WatchedSections.Contains(localSection));
            Assert.IsTrue(source.WatchedSections.Contains(externalSection));
            Assert.AreEqual(2, source.ConfigSourceWatcherMappings[string.Empty].WatchedSections.Count);
            Assert.IsTrue(source.ConfigSourceWatcherMappings[string.Empty].WatchedSections.Contains(localSection));
            Assert.AreEqual(1, source.ConfigSourceWatcherMappings[SystemConfigurationSource.NullConfigSource].WatchedSections.Count);
            Assert.IsTrue(source.ConfigSourceWatcherMappings[SystemConfigurationSource.NullConfigSource].WatchedSections.Contains(externalSection));
        }
        public void RequestsForAppConfigAndExternalFileCreatesWatchersForBoth()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);

            object section1 = source.GetSection(localSection);
            object section2 = source.GetSection(externalSection);

            Assert.IsNotNull(section1);
            Assert.IsNotNull(section2);
            Assert.AreEqual(2, source.WatchedConfigSources.Count);
            Assert.IsTrue(source.WatchedConfigSources.Contains(localSectionSource));
            Assert.IsTrue(source.WatchedConfigSources.Contains(externalSectionSource));
            Assert.AreEqual(3, source.WatchedSections.Count); //watches  ConfigurationSourceSection + externalSection + localSection
            Assert.IsTrue(source.WatchedSections.Contains(localSection));
            Assert.IsTrue(source.WatchedSections.Contains(externalSection));
        }
        public void AllRegisteredObjectsAreNotifiedOfDifferentSectionsChangesForAppConfig()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);
            source.GetSection(localSection);
            source.GetSection(localSection2);
            source.AddSectionChangeHandler(localSection, new ConfigurationChangedEventHandler(OnConfigurationChanged));
            source.AddSectionChangeHandler(localSection2, new ConfigurationChangedEventHandler(OnConfigurationChanged));

            source.ConfigSourceChanged(localSectionSource);

            Assert.AreEqual(1, updatedSectionsTally[localSection]);
            Assert.AreEqual(1, updatedSectionsTally[localSection2]);
        }
Exemple #42
0
        public void DifferentSqlConfigurationSourcesDoNotShareEvents()
        {
            ConfigurationChangeWatcher.SetDefaultPollDelayInMilliseconds(50);

            SqlConfigurationSource.ResetImplementation(data1, true);
            SystemConfigurationSource.ResetImplementation(true);


            bool sysSourceChanged   = false;
            bool otherSourceChanged = false;

            SystemConfigurationSource systemSource   = new SystemConfigurationSource();
            DummySection sysDummySerializableSection = systemSource.GetSection(localSection) as DummySection;

            SqlConfigurationSource otherSource = new SqlConfigurationSource(
                data1.ConnectionString,
                data1.GetStoredProcedure,
                data1.SetStoredProcedure,
                data1.RefreshStoredProcedure,
                data1.RemoveStoredProcedure);
            SqlConfigurationParameter parameter = new SqlConfigurationParameter(
                data1.ConnectionString,
                data1.GetStoredProcedure,
                data1.SetStoredProcedure,
                data1.RefreshStoredProcedure,
                data1.RemoveStoredProcedure);

            otherSource.Add(parameter, localSection, sysDummySerializableSection);

            DummySection otherDummySerializableSection = otherSource.GetSection(localSection) as DummySection;

            Assert.IsTrue(sysDummySerializableSection != null);
            Assert.IsTrue(otherDummySerializableSection != null);

            systemSource.AddSectionChangeHandler(localSection, delegate(object o, ConfigurationChangedEventArgs args)
            {
                sysSourceChanged = true;
            });

            otherSource.AddSectionChangeHandler(localSection, delegate(object o, ConfigurationChangedEventArgs args)
            {
                Assert.AreEqual(12, ((DummySection)otherSource.GetSection(localSection)).Value);
                otherSourceChanged = true;
            });

            DummySection rwSection = new DummySection();

            System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenMachineConfiguration();
            rwConfiguration.Sections.Remove(localSection);
            rwConfiguration.Sections.Add(localSection, rwSection = new DummySection());
            rwSection.Name  = localSection;
            rwSection.Value = 12;
            rwSection.SectionInformation.ConfigSource = data1.ConnectionString;

            SqlConfigurationManager.SaveSection(rwSection.Name, rwSection, data1);

            Thread.Sleep(200);

            Assert.AreEqual(false, sysSourceChanged);
            Assert.AreEqual(true, otherSourceChanged);
        }
        public void GetsNullIfSectionDoesNotExist()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);
            object section = source.GetSection(nonExistingSection);

            Assert.IsNull(section);
        }
        public void GetsNotifiedOfMultipleRetrievedSection()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);
            source.GetSection(localSection);
            source.GetSection(localSection2);
            source.SourceChanged += this.OnConfigurationSourceChanged;

            source.ConfigSourceChanged(localSectionSource);

            CollectionAssert.AreEquivalent(new[] { ConfigurationSourceSection.SectionName, localSection, localSection2 }, new List<string>(updatedSections));
        }
        public void SecondRequestForSameSectionInExternalFileDoesNotCreateWatcherForExternalFile()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);

            object section1 = source.GetSection(externalSection);
            object section2 = source.GetSection(externalSection);

            Assert.IsNotNull(section1);
            Assert.IsNotNull(section2);
            Assert.AreEqual(2, source.WatchedConfigSources.Count);
            Assert.IsTrue(source.WatchedConfigSources.Contains(localSectionSource));
            Assert.IsTrue(source.WatchedConfigSources.Contains(externalSectionSource));
            Assert.AreEqual(2, source.WatchedSections.Count);  //watches  ConfigurationSourceSection + externalSection
            Assert.IsTrue(source.WatchedSections.Contains(externalSection));
        }
        public void FirstRequestForSectionInExternalFileCreatesWatchersForExternalFileAndAppConfig()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);

            object section = source.GetSection(externalSection);

            Assert.IsNotNull(section);
            Assert.AreEqual(2, source.WatchedConfigSources.Count);
            Assert.IsTrue(source.WatchedConfigSources.Contains(localSectionSource));
            Assert.IsTrue(source.WatchedConfigSources.Contains(externalSectionSource));
            Assert.AreEqual(2, source.WatchedSections.Count); //watches  ConfigurationSourceSection + externalSection
            Assert.IsTrue(source.WatchedSections.Contains(externalSection));
        }
        public void WatchedSectionInExternalFileValuesAreUpdatedIfExternalFileChangesAndNotificationIsFired()
        {
            IConfigurationSourceTest source = new SystemConfigurationSource(false);

            object section1 = source.GetSection(externalSection);
            Assert.IsNotNull(section1);
            DummySection dummySection1 = section1 as DummySection;
            Assert.AreEqual(externalSection, dummySection1.Name);
            Assert.AreEqual(20, dummySection1.Value);

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

            source.ExternalConfigSourceChanged(externalSectionSource);

            section1 = source.GetSection(externalSection);
            Assert.IsNotNull(section1);
            dummySection1 = section1 as DummySection;
            Assert.AreEqual(externalSection, dummySection1.Name);
            Assert.AreEqual(25, dummySection1.Value);
        }