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 SerializeAndDeserializeSqlTestConfiguration() { StringBuilder output = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings(); using (XmlWriter writer = XmlWriter.Create(output, settings)) { // Add a testObject and serialize it the configuration section configSection.Value = 20; configSection.WriteXml(writer); Assert.IsNotNull(writer); writer.Close(); Assert.IsTrue(output.Length > 0); writer.Flush(); } DummySection configSection2 = new DummySection(); XmlReaderSettings settings2 = new XmlReaderSettings(); StringReader input = new StringReader(output.ToString()); using (XmlReader reader = XmlReader.Create(input, settings2)) { //deserialize the configuration section configSection2.ReadXml(reader); Assert.AreEqual(configSection2.Value, configSection.Value); } }
public void Setup() { System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); DummySection rwSection; rwConfiguration.Sections.Remove(localSection); rwConfiguration.Sections.Add(localSection, rwSection = new DummySection()); rwSection.Name = localSection; rwSection.Value = 10; rwSection.SectionInformation.ConfigSource = localSectionSource; rwConfiguration.Sections.Remove(externalSection); rwConfiguration.Sections.Add(externalSection, rwSection = new DummySection()); rwSection.Name = externalSection; rwSection.Value = 20; rwSection.SectionInformation.ConfigSource = externalSectionSource; rwConfiguration.Sections.Remove(localSection2); rwConfiguration.Sections.Add(localSection2, rwSection = new DummySection()); rwSection.Name = localSection2; rwSection.Value = 30; rwSection.SectionInformation.ConfigSource = localSectionSource; rwConfiguration.Save(); ConfigurationManager.RefreshSection(localSection); ConfigurationManager.RefreshSection(localSection2); ConfigurationManager.RefreshSection(externalSection); updatedSectionsTally = new Dictionary<string, int>(0); updatedSections = null; }
public void AddIsReflectedInNextRequestWithoutRefresh() { string otherConfigurationFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Other.config"); try { File.Copy(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath, otherConfigurationFile); using (FileConfigurationSource otherSource = new FileConfigurationSource(otherConfigurationFile, false)) { DummySection otherSection = otherSource.GetSection(localSection) as DummySection; // update twice, just to make sure DummySection newSection = new DummySection(); newSection.Value = 13; otherSource.Add(localSection, newSection); newSection = new DummySection(); newSection.Value = 12; otherSource.Add(localSection, newSection); otherSection = otherSource.GetSection(localSection) as DummySection; Assert.IsNotNull(otherSection); Assert.AreEqual(12, otherSection.Value); } } finally { if (File.Exists(otherConfigurationFile)) { File.Delete(otherConfigurationFile); } } }
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 Setup() { SystemConfigurationSource.ResetImplementation(false); System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); DummySection rwSection; rwConfiguration.Sections.Remove(localSection); rwConfiguration.Sections.Add(localSection, rwSection = new DummySection()); rwSection.Name = localSection; rwSection.Value = 10; rwSection.SectionInformation.ConfigSource = localSectionSource; rwConfiguration.Sections.Remove(externalSection); rwConfiguration.Sections.Add(externalSection, rwSection = new DummySection()); rwSection.Name = externalSection; rwSection.Value = 20; rwSection.SectionInformation.ConfigSource = externalSectionSource; rwConfiguration.Sections.Remove(localSection2); rwConfiguration.Sections.Add(localSection2, rwSection = new DummySection()); rwSection.Name = localSection2; rwSection.Value = 30; rwSection.SectionInformation.ConfigSource = localSectionSource; rwConfiguration.Save(); ConfigurationManager.RefreshSection(localSection); ConfigurationManager.RefreshSection(localSection2); ConfigurationManager.RefreshSection(externalSection); ConfigurationChangeFileWatcher.ResetDefaultPollDelay(); updatedSectionsTally = new Dictionary <string, int>(0); }
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 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 SectionsCanBeAccessedThroughFileConfigurationSource() { string fullConfigurationFilepath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; string otherConfigurationFilepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Other.config"); File.Copy(fullConfigurationFilepath, otherConfigurationFilepath); try { using (FileConfigurationSource otherConfiguration = new FileConfigurationSource(otherConfigurationFilepath, false)) { DummySection dummySection = otherConfiguration.GetSection(localSection) as DummySection; Assert.IsNotNull(dummySection); } } finally { if (File.Exists(otherConfigurationFilepath)) { File.Delete(otherConfigurationFilepath); } } }
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 Setup() { SystemConfigurationSource.ResetImplementation(false); System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); DummySection rwSection; rwConfiguration.Sections.Remove(localSection); rwConfiguration.Sections.Add(localSection, rwSection = new DummySection()); rwSection.Name = localSection; rwSection.Value = 10; rwSection.SectionInformation.ConfigSource = localSectionSource; rwConfiguration.Sections.Remove(externalSection); rwConfiguration.Sections.Add(externalSection, rwSection = new DummySection()); rwSection.Name = externalSection; rwSection.Value = 20; rwSection.SectionInformation.ConfigSource = externalSectionSource; rwConfiguration.Sections.Remove(localSection2); rwConfiguration.Sections.Add(localSection2, rwSection = new DummySection()); rwSection.Name = localSection2; rwSection.Value = 30; rwSection.SectionInformation.ConfigSource = localSectionSource; rwConfiguration.Save(); ConfigurationManager.RefreshSection(localSection); ConfigurationManager.RefreshSection(localSection2); ConfigurationManager.RefreshSection(externalSection); ConfigurationChangeFileWatcher.ResetDefaultPollDelay(); updatedSectionsTally = new Dictionary<string, int>(0); }
public void AddIsReflectedInNextRequestWithoutRefresh() { string otherConfigurationFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Other.config"); FileConfigurationParameter parameter = new FileConfigurationParameter(otherConfigurationFile); FileConfigurationSource.ResetImplementation(otherConfigurationFile, false); try { File.Copy(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, otherConfigurationFile); FileConfigurationSource otherSource = new FileConfigurationSource(otherConfigurationFile); DummySection otherSection = otherSource.GetSection(localSection) as DummySection; // update twice, just to make sure DummySection newSection = new DummySection(); newSection.Value = 13; otherSource.Add(parameter, localSection, newSection); newSection = new DummySection(); newSection.Value = 12; otherSource.Add(parameter, localSection, newSection); otherSection = otherSource.GetSection(localSection) as DummySection; Assert.IsNotNull(otherSection); Assert.AreEqual(12, otherSection.Value); } finally { FileConfigurationSource.ResetImplementation(otherConfigurationFile, true); if (File.Exists(otherConfigurationFile)) { File.Delete(otherConfigurationFile); } } }
public void RemoveIsReflectedInNextRequestWithoutRefresh() { string otherConfigurationFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Other.config"); try { File.Copy(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, otherConfigurationFile); using (FileConfigurationSource otherSource = new FileConfigurationSource(otherConfigurationFile, false)) { DummySection otherSection = otherSource.GetSection(localSection) as DummySection; DummySection newSection = new DummySection(); newSection.Value = 13; otherSource.Add(localSection, newSection); otherSource.Remove(localSection); otherSection = otherSource.GetSection(localSection) as DummySection; Assert.IsNull(otherSection); } } finally { if (File.Exists(otherConfigurationFile)) { File.Delete(otherConfigurationFile); } } }
public void CanUseRelativePathToBaseDirectory() { var currentDirectory = Environment.CurrentDirectory; Environment.CurrentDirectory = Path.GetTempPath(); string fullConfigurationFilepath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; string otherConfigurationFilepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Other.config"); File.Copy(fullConfigurationFilepath, otherConfigurationFilepath); try { using (FileConfigurationSource otherConfiguration = new FileConfigurationSource("Other.config", false)) { DummySection dummySection = otherConfiguration.GetSection(localSection) as DummySection; Assert.IsNotNull(dummySection); } } finally { Environment.CurrentDirectory = currentDirectory; if (File.Exists(otherConfigurationFilepath)) { File.Delete(otherConfigurationFilepath); } } }
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); }
public void SerializeAndDeserializeSqlTestConfiguration() { StringBuilder output = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings(); using (XmlWriter writer = XmlWriter.Create(output, settings)) { // Add a testObject and serialize it the configuration section configSection.Value = 20; configSection.WriteXml(writer); Assert.IsNotNull(writer); writer.Close(); Assert.IsTrue(output.Length > 0); writer.Flush(); } DummySection configSection2 = new DummySection(); XmlReaderSettings settings2 = new XmlReaderSettings(); StringReader input = new StringReader(output.ToString()); using (XmlReader reader = XmlReader.Create(input, settings2)) { //deserialize the configuration section configSection2.ReadXml(reader); Assert.AreEqual(configSection2.Value, configSection.Value); } }
public void Setup() { System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); DummySection rwSection; rwConfiguration.Sections.Remove(localSection); rwConfiguration.Sections.Add(localSection, rwSection = new DummySection()); rwSection.Name = localSection; rwSection.Value = 10; rwSection.SectionInformation.ConfigSource = localSectionSource; rwConfiguration.Sections.Remove(externalSection); rwConfiguration.Sections.Add(externalSection, rwSection = new DummySection()); rwSection.Name = externalSection; rwSection.Value = 20; rwSection.SectionInformation.ConfigSource = externalSectionSource; rwConfiguration.Sections.Remove(localSection2); rwConfiguration.Sections.Add(localSection2, rwSection = new DummySection()); rwSection.Name = localSection2; rwSection.Value = 30; rwSection.SectionInformation.ConfigSource = localSectionSource; rwConfiguration.Save(); ConfigurationManager.RefreshSection(localSection); ConfigurationManager.RefreshSection(localSection2); ConfigurationManager.RefreshSection(externalSection); updatedSectionsTally = new Dictionary <string, int>(0); updatedSections = null; }
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); } } }
public void SavingWithEmptyProtectionProviderThrowsArgumentException() { string configurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; FileConfigurationSource fileConfigSource = new FileConfigurationSource(configurationFile, false); DummySection newSection = new DummySection(); fileConfigSource.Add(localSection, newSection, string.Empty); }
public void SavingWithEmptyProtectionProviderThrowsArgumentException() { string configurationFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath; FileConfigurationSource fileConfigSource = new FileConfigurationSource(configurationFile, false); DummySection newSection = new DummySection(); fileConfigSource.Add(localSection, newSection, string.Empty); }
public void AddingASectionIsReflectedInMemoryAndOnDisk_Bug2931() { SystemConfigurationSource sysSource = new SystemConfigurationSource(false); var originalSection = (DummySection)(sysSource.GetSection(addedSectionName)); Assert.IsNull(originalSection); var newSection = new DummySection(); sysSource.Add(addedSectionName, newSection); var returnedSection = (DummySection)(sysSource.GetSection(addedSectionName)); Assert.IsNotNull(returnedSection); sysSource.Remove(addedSectionName); }
public void Setup() { SystemConfigurationSource.ResetImplementation(false); ConfigurationChangeWatcher.SetDefaultPollDelayInMilliseconds(50); System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); DummySection rwSection; rwConfiguration.Sections.Remove(localSection); rwConfiguration.Sections.Add(localSection, rwSection = new DummySection()); rwSection.Name = localSection; rwSection.Value = 10; rwSection.SectionInformation.ConfigSource = localSectionSource; updatedSectionsTally = new Dictionary <string, int>(0); }
public void AddingUnprotectedSectionsWithProtectionProviderWillProtectThem() { DummySection dummySection = new DummySection(); string configurationFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath; FileConfigurationSource fileConfigSource = new FileConfigurationSource(configurationFile, false); fileConfigSource.Add(protectedSection, dummySection, ProtectedConfiguration.DefaultProvider); ConfigurationSection section = fileConfigSource.GetSection(protectedSection); Assert.IsTrue(section.SectionInformation.IsProtected); Assert.IsNotNull(section.SectionInformation); Assert.AreEqual(ProtectedConfiguration.DefaultProvider, section.SectionInformation.ProtectionProvider.Name); }
public void AddingUnprotectedSectionsWithProtectionProviderWillProtectThem() { DummySection dummySection = new DummySection(); string configurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; FileConfigurationSource fileConfigSource = new FileConfigurationSource(configurationFile, false); fileConfigSource.Add(protectedSection, dummySection, ProtectedConfiguration.DefaultProvider); ConfigurationSection section = fileConfigSource.GetSection(protectedSection); Assert.IsTrue(section.SectionInformation.IsProtected); Assert.IsNotNull(section.SectionInformation); Assert.AreEqual(ProtectedConfiguration.DefaultProvider, section.SectionInformation.ProtectionProvider.Name); }
public void Setup() { SystemConfigurationSource.ResetImplementation(false); ConfigurationChangeWatcher.SetDefaultPollDelayInMilliseconds(50); System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); DummySection rwSection; rwConfiguration.Sections.Remove(localSection); rwConfiguration.Sections.Add(localSection, rwSection = new DummySection()); rwSection.Name = localSection; rwSection.Value = 10; rwSection.SectionInformation.ConfigSource = localSectionSource; updatedSectionsTally = new Dictionary<string, int>(0); }
public void AddingASectionIsReflectedInMemoryAndOnDisk_Bug2931() { SystemConfigurationSource sysSource = new SystemConfigurationSource(false); var originalSection = (DummySection)(sysSource.GetSection(addedSectionName)); Assert.IsNull(originalSection); var newSection = new DummySection(); sysSource.Add(addedSectionName, newSection); var returnedSection = (DummySection)(sysSource.GetSection(addedSectionName)); Assert.IsNotNull(returnedSection); sysSource.Remove(addedSectionName); }
public void ChangeInExternalConfigSourceIsDetected() { ConfigurationChangeWatcher.SetDefaultPollDelayInMilliseconds(50); string otherConfigurationFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Other.config"); FileConfigurationParameter parameter = new FileConfigurationParameter(otherConfigurationFile); FileConfigurationSource.ResetImplementation(otherConfigurationFile, true); try { File.Copy(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, otherConfigurationFile); FileConfigurationSource otherSource = new FileConfigurationSource(otherConfigurationFile); DummySection rwSection; System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenExeConfiguration(otherConfigurationFile); rwConfiguration.Sections.Remove(externalSection); rwConfiguration.Sections.Add(externalSection, rwSection = new DummySection()); rwSection.Name = externalSection; rwSection.Value = 12; rwSection.SectionInformation.ConfigSource = externalSectionSource; rwConfiguration.Save(ConfigurationSaveMode.Full); DummySection otherSection = otherSource.GetSection(externalSection) as DummySection; Assert.AreEqual(12, otherSection.Value); rwSection.Value = 13; rwConfiguration.Save(ConfigurationSaveMode.Modified); Thread.Sleep(150); otherSection = otherSource.GetSection(externalSection) as DummySection; Assert.AreEqual(13, otherSection.Value); } finally { ConfigurationChangeWatcher.ResetDefaultPollDelay(); FileConfigurationSource.ResetImplementation(otherConfigurationFile, true); if (File.Exists(otherConfigurationFile)) { File.Delete(otherConfigurationFile); } } }
public void FirstRequestForSectionGetsFreshInformation() { DummySection section1, section2; section1 = ConfigurationManager.GetSection(localSection) as DummySection; Assert.AreEqual(10, section1.Value); System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); DummySection rwSection1 = rwConfiguration.GetSection(localSection) as DummySection; rwSection1.Value = 15; DummySection rwSection2 = rwConfiguration.GetSection(localSection2) as DummySection; rwSection2.Value = 25; rwConfiguration.Save(); section1 = ConfigurationManager.GetSection(localSection) as DummySection; section2 = ConfigurationManager.GetSection(localSection2) as DummySection; Assert.AreEqual(10, section1.Value); // gets old value for cached section Assert.AreEqual(25, section2.Value); // gets new value for fresh section }
public void ChangeInExternalConfigSourceIsDetected() { string otherConfigurationFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Other.config"); try { File.Copy(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, otherConfigurationFile); using (FileConfigurationSource otherSource = new FileConfigurationSource(otherConfigurationFile, true, 50)) { DummySection rwSection; System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenExeConfiguration(otherConfigurationFile); rwConfiguration.Sections.Remove(externalSection); rwConfiguration.Sections.Add(externalSection, rwSection = new DummySection()); rwSection.Name = externalSection; rwSection.Value = 12; rwSection.SectionInformation.ConfigSource = externalSectionSource; rwConfiguration.Save(ConfigurationSaveMode.Full); DummySection otherSection = otherSource.GetSection(externalSection) as DummySection; Assert.AreEqual(12, otherSection.Value); rwSection.Value = 13; rwConfiguration.Save(ConfigurationSaveMode.Modified); Thread.Sleep(300); otherSection = otherSource.GetSection(externalSection) as DummySection; Assert.AreEqual(13, otherSection.Value); } } finally { if (File.Exists(otherConfigurationFile)) { File.Delete(otherConfigurationFile); } } }
public void RemovingSectionCausesChangeNotification() { string otherConfigurationFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Other.config"); File.Copy(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, otherConfigurationFile); try { bool otherSourceChanged = false; using (FileConfigurationSource otherSource = new FileConfigurationSource(otherConfigurationFile, true, 50)) { DummySection otherDummySection = otherSource.GetSection(localSection) as DummySection; Assert.IsTrue(otherDummySection != null); otherSource.AddSectionChangeHandler( localSection, delegate(object o, ConfigurationChangedEventArgs args) { Assert.IsNull(otherSource.GetSection(localSection)); otherSourceChanged = true; }); otherSource.Remove(localSection); Thread.Sleep(300); Assert.AreEqual(true, otherSourceChanged); } } finally { if (File.Exists(otherConfigurationFile)) { File.Delete(otherConfigurationFile); } } }
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); }
// this test relied on lazily initialization of the Configuration object, and would only work if changes // happened before any section was requested public void ReadsLatestVersionOnFirstRequest() { string otherConfigurationFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Other.config"); try { File.Copy(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, otherConfigurationFile); using (FileConfigurationSource otherSource = new FileConfigurationSource(otherConfigurationFile, false)) { DummySection rwSection = null; ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(); fileMap.ExeConfigFilename = otherConfigurationFile; System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); rwConfiguration.Sections.Remove(localSection); rwConfiguration.Sections.Add(localSection, rwSection = new DummySection()); rwSection.Name = localSection; rwSection.Value = 12; rwSection.SectionInformation.ConfigSource = localSectionSource; rwConfiguration.Save(); DummySection otherSection = otherSource.GetSection(localSection) as DummySection; Assert.IsNotNull(otherSection); Assert.AreEqual(12, otherSection.Value); } } finally { if (File.Exists(otherConfigurationFile)) { File.Delete(otherConfigurationFile); } } }
public void RemoveIsReflectedInNextRequestWithoutRefresh() { string otherConfigurationFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Other.config"); try { File.Copy(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, otherConfigurationFile); using (FileConfigurationSource otherSource = new FileConfigurationSource(otherConfigurationFile, false)) { DummySection otherSection = otherSource.GetSection(localSection) as DummySection; DummySection newSection = new DummySection(); newSection.Value = 13; otherSource.Add(localSection, newSection); otherSource.Remove(localSection); otherSection = otherSource.GetSection(localSection) as DummySection; Assert.IsNull(otherSection); } } finally { if (File.Exists(otherConfigurationFile)) { File.Delete(otherConfigurationFile); } } }
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]); }
protected override void Arrange() { base.Arrange(); var customSourceContents = new Dictionary<string, System.Configuration.ConfigurationSection>(); customSourceContents.Add(CustomSourceDummySectionName, sectionInCustomSource = new DummySection { Value = 12, Name = CustomSourceDummySectionName }); TestConfigurationSource.ConfigurationSourceContents = customSourceContents; }
protected override void Arrange() { base.Arrange(); section = new DummySection { Name = "NewSection", Value = 21 }; }
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); } } }
protected override void Act() { section = (DummySection)MergedSource.GetSection(FileSourceDummySectionName); }
public void TestInitialize() { configSection = new DummySection(); }
// this test relied on lazily initialization of the Configuration object, and would only work if changes // happened before any section was requested public void ReadsLatestVersionOnFirstRequest() { string otherConfigurationFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Other.config"); try { File.Copy(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, otherConfigurationFile); using (FileConfigurationSource otherSource = new FileConfigurationSource(otherConfigurationFile, false)) { DummySection rwSection = null; ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(); fileMap.ExeConfigFilename = otherConfigurationFile; System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); rwConfiguration.Sections.Remove(localSection); rwConfiguration.Sections.Add(localSection, rwSection = new DummySection()); rwSection.Name = localSection; rwSection.Value = 12; rwSection.SectionInformation.ConfigSource = localSectionSource; rwConfiguration.Save(); DummySection otherSection = otherSource.GetSection(localSection) as DummySection; Assert.IsNotNull(otherSection); Assert.AreEqual(12, otherSection.Value); } } finally { if (File.Exists(otherConfigurationFile)) { File.Delete(otherConfigurationFile); } } }
protected override void Act() { section = (DummySection)CompositeSource.GetSection(FileSourceDummySectionName); }
public void ChangeInExternalConfigSourceIsDetected() { string otherConfigurationFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Other.config"); try { File.Copy(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, otherConfigurationFile); using (FileConfigurationSource otherSource = new FileConfigurationSource(otherConfigurationFile, true, 50)) { DummySection rwSection; System.Configuration.Configuration rwConfiguration = ConfigurationManager.OpenExeConfiguration(otherConfigurationFile); rwConfiguration.Sections.Remove(externalSection); rwConfiguration.Sections.Add(externalSection, rwSection = new DummySection()); rwSection.Name = externalSection; rwSection.Value = 12; rwSection.SectionInformation.ConfigSource = externalSectionSource; rwConfiguration.Save(ConfigurationSaveMode.Full); DummySection otherSection = otherSource.GetSection(externalSection) as DummySection; Assert.AreEqual(12, otherSection.Value); rwSection.Value = 13; rwConfiguration.Save(ConfigurationSaveMode.Modified); Thread.Sleep(300); otherSection = otherSource.GetSection(externalSection) as DummySection; Assert.AreEqual(13, otherSection.Value); } } finally { if (File.Exists(otherConfigurationFile)) { File.Delete(otherConfigurationFile); } } }
protected override void Act() { section = CompositeSource.GetSection(FileSourceDummySectionName) as DummySection; File.SetLastWriteTime(@"ExternalFileSource.config", DateTime.Now); // Wait for at least two events Assert.IsTrue(waitForChangedEvents.Wait(30000), "timed out"); // And give it a little more time in case more come in Thread.Sleep(3000); }
public void AddingUnprotectedSectionsWithProtectionProviderWillProtectThem() { DummySection dummySection = new DummySection(); string configurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; FileConfigurationSource fileConfigSource = new FileConfigurationSource(configurationFile, false); fileConfigSource.Add(protectedSection, dummySection, ProtectedConfiguration.DefaultProvider); ConfigurationSection section = fileConfigSource.GetSection(protectedSection); Assert.IsTrue(section.SectionInformation.IsProtected); Assert.IsNotNull(section.SectionInformation); Assert.AreEqual(ProtectedConfiguration.DefaultProvider, section.SectionInformation.ProtectionProvider.Name); }
public void SavingWithEmptyProtectionProviderThrowsArgumentException() { string configurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; FileConfigurationSource fileConfigSource = new FileConfigurationSource(configurationFile, false); DummySection newSection = new DummySection(); fileConfigSource.Add(localSection, newSection, string.Empty); }
public void TestInitialize() { configSection = new DummySection(); }