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 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 NonExistentSectionReturnsNullThroughFileConfigurationSource()
        {
            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))
                {
                    object wrongSection = otherConfiguration.GetSection("wrong section");

                    Assert.IsNull(wrongSection);
                }
            }
            finally
            {
                if (File.Exists(otherConfigurationFilepath))
                {
                    File.Delete(otherConfigurationFilepath);
                }
            }
        }
        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 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 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);
                }
            }
        }
        // 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 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 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 bool EnsureCanSaveConfigurationFile(string configurationFile)
        {
            if (!File.Exists(configurationFile))
            {
                using (var textWriter = File.CreateText(configurationFile))
                {
                    textWriter.WriteLine("<configuration />");
                    textWriter.Close();
                }
            }
            else
            {
                var fileIsInvalidConfigurationFile = false;
                try
                {
                    using (var source = new FileConfigurationSource(configurationFile))
                    {
                        source.GetSection("applicationSettings");
                    }
                }
                catch (Exception ex)
                {
                    ConfigurationLogWriter.LogException(ex);
                    var warningResults = uiService.ShowMessageWpf(
                        string.Format(CultureInfo.CurrentCulture, Resources.PromptSaveOverFileThatCannotBeReadFromWarningMessage,
                                      configurationFile),
                        Resources.PromptSaveOverFileThatCannotBeReadFromWarningTitle,
                        MessageBoxButton.OKCancel);

                    if (warningResults == MessageBoxResult.Cancel)
                    {
                        return(false);
                    }
                    fileIsInvalidConfigurationFile = true;
                }

                var attributes = File.GetAttributes(configurationFile);
                if ((attributes | FileAttributes.ReadOnly) == attributes)
                {
                    var overwriteReadonlyDialogResult = uiService.ShowMessageWpf(
                        string.Format(CultureInfo.CurrentCulture, Resources.PromptOverwriteReadonlyFileMessage,
                                      configurationFile),
                        Resources.PromptOverwriteReadonlyFiletitle,
                        MessageBoxButton.YesNoCancel);

                    switch (overwriteReadonlyDialogResult)
                    {
                    case MessageBoxResult.No:

                        return(SaveAs());


                    case MessageBoxResult.Yes:

                        File.SetAttributes(configurationFile, attributes ^ FileAttributes.ReadOnly);

                        break;

                    default:

                        return(false);
                    }
                }

                if (fileIsInvalidConfigurationFile)
                {
                    File.Delete(configurationFile);
                    using (var textWriter = File.CreateText(configurationFile))
                    {
                        textWriter.WriteLine("<configuration />");
                    }
                }
            }
            return(true);
        }
        public void then_environmental_overrides_is_not_part_of_main_file()
        {
            FileConfigurationSource source = new FileConfigurationSource(mainFile);

            Assert.IsNull(source.GetSection(EnvironmentalOverridesSection.EnvironmentallyOverriddenProperties));
        }
Example #12
0
        public void then_attributes_with_omitted_values_are_overridden()
        {
            FileConfigurationSource             source = new FileConfigurationSource(TargetFile);
            InstrumentationConfigurationSection instrumentationSettings = (InstrumentationConfigurationSection)source.GetSection(InstrumentationConfigurationSection.SectionName);

            Assert.AreEqual("Overriden Application", instrumentationSettings.ApplicationInstanceName);
            Assert.IsTrue(instrumentationSettings.EventLoggingEnabled);
            Assert.IsTrue(instrumentationSettings.PerformanceCountersEnabled);
        }
Example #13
0
        private static void LoadConfiguration()
        {
            var configurationSource = new FileConfigurationSource(ConfigurationTestHelper.ConfigurationFileName, false);

            configurationSource.GetSection(LoggingSettings.SectionName);
        }
        private static void ValidateConfigurationFile(XDocument resultDocument)
        {
            string tempValidationFilePath = TempRandomConfigFilePath;

            resultDocument.SaveToFile(tempValidationFilePath);

            try
            {
                IConfigurationSource testConfigSource = new FileConfigurationSource(tempValidationFilePath);

                foreach (XElement sectionElement in resultDocument.Root.Element("configSections").Elements())
                {
                    if (sectionElement.Attribute("name") != null)
                    {
                        string sectionName = sectionElement.Attribute("name").Value;

                        try
                        {
                            testConfigSource.GetSection(sectionName);
                        }
                        catch (ConfigurationErrorsException exception)
                        {
                            if(exception.InnerException != null &&
                               exception.InnerException is AppCodeTypeNotFoundConfigurationException)
                            {
                                // App_Code classes aren't compiled during package installation, therefore related exceptions are ignored
                            }
                            else
                            {
                                throw;
                            }
                        }
                    }
                }

                DeleteTempConfigurationFile(tempValidationFilePath);
            }
            catch (Exception)
            {
                DeleteTempConfigurationFile(tempValidationFilePath);

                throw;
            }
        }