public void SetValue(string AttributeName, string Value) { XmlDocument = new XmlDocument(); XmlDocument.Load(this.ConfigFileName); Query = "configuration/" + SectionName; AppSettingsNode = (XmlElement)this.XmlDocument.SelectSingleNode(this.Query); if (AppSettingsNode == null) { return; } Query += "/add[@key='" + AttributeName.ToString() + "']"; XmlNodeList = this.XmlDocument.SelectNodes(this.Query); if (XmlNodeList.Count > 0) { Node = (XmlElement)XmlNodeList[0]; } else { Node = this.XmlDocument.CreateElement("add"); XmlAttribute1 = this.XmlDocument.CreateAttribute("key"); XmlAttribute1.Value = AttributeName.ToString(); Node.Attributes.SetNamedItem(XmlAttribute1); XmlAttribute2 = this.XmlDocument.CreateAttribute("value"); Node.Attributes.SetNamedItem(XmlAttribute2); AppSettingsNode.AppendChild(Node); } Node.Attributes["value"].Value = Value.ToString(); this.XmlDocument.Save(this.ConfigFileName); }
public void FilePropertyWillBeDisplayedOnNode() { string fileName = "a:\random\filename.txt"; AppSettingsSection appSettingsSection = new AppSettingsSection(); appSettingsSection.File = fileName; AppSettingsNode appSettingsNode = new AppSettingsNode(appSettingsSection); Assert.AreEqual(fileName, appSettingsNode.File); }
public void NodeBuilderAddsNodesWithoutConfigurationSourceAsReadWrite() { AppSettingsSection appSettingsSection = new AppSettingsSection(); appSettingsSection.Settings.Add("key", "value"); AppSettingsNodeBuilder builder = new AppSettingsNodeBuilder(ServiceProvider, appSettingsSection); AppSettingsNode appSettingsNode = builder.Build(); Assert.AreEqual(1, appSettingsNode.Nodes.Count); Assert.AreEqual(typeof(AppSettingNode), appSettingsNode.Nodes[0].GetType()); }
public void NodeBuilderAddsChildNodesForEverySettingInAppSettings() { AppSettingsSection appSettingsSection = new AppSettingsSection(); appSettingsSection.Settings.Add("key1", "value1"); appSettingsSection.Settings.Add("key2", "value2"); appSettingsSection.Settings.Add("key3", "value3"); AppSettingsNodeBuilder builder = new AppSettingsNodeBuilder(ServiceProvider, appSettingsSection); AppSettingsNode appSettingsNode = builder.Build(); Assert.AreEqual(3, appSettingsNode.Nodes.Count); }
public void SettingsFromConfigSectionAreAddedAsReadWriteNodes() { ExeConfigurationFileMap filemap = new ExeConfigurationFileMap(); filemap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AppSettingsWithConfigSource.config"); System.Configuration.Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(filemap, ConfigurationUserLevel.None); AppSettingsNodeBuilder builder = new AppSettingsNodeBuilder(ServiceProvider, configuration.GetSection("appSettings") as AppSettingsSection); AppSettingsNode appSettingsNode = builder.Build(); Assert.IsNotNull(appSettingsNode); Assert.AreEqual(1, appSettingsNode.Nodes.Count); Assert.AreEqual(typeof(AppSettingNode), appSettingsNode.Nodes[0].GetType()); }
public void AppSettingIsAddForEverySettingNode() { AppSettingsNode settingsNode = new AppSettingsNode(); base.ApplicationNode.AddNode(settingsNode); settingsNode.AddNode(new AppSettingNode("key", "value")); AppSettingsBuilder settingsBuilder = new AppSettingsBuilder(settingsNode); AppSettingsSection settingsSection = settingsBuilder.Build(); Assert.IsNotNull(settingsSection); Assert.AreEqual(1, settingsSection.Settings.Count); Assert.AreEqual("key", settingsSection.Settings.AllKeys[0]); Assert.AreEqual("value", settingsSection.Settings["key"].Value); }
public void AddedAppSettingsAreSaved() { using (new ConfigFileSnapshot()) { ApplicationNode.Hierarchy.Load(); Assert.AreEqual(0, ErrorLogService.ConfigurationErrorCount); ApplicationNode.Hierarchy.Open(); Assert.AreEqual(0, ErrorLogService.ConfigurationErrorCount); AppSettingsNode appSettingsNode = (AppSettingsNode)ApplicationNode.Hierarchy.FindNodeByType(typeof(AppSettingsNode)); Assert.IsNotNull(appSettingsNode); appSettingsNode.AddNode(new AppSettingNode("addedKey", "value")); ApplicationNode.Hierarchy.Save(); Assert.AreEqual(0, ErrorLogService.ConfigurationErrorCount); string fileContentsAfterSave = File.ReadAllText(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); Assert.IsTrue(fileContentsAfterSave.Contains("addedKey")); } }
public void SavingUntouchedSettingsDoesntChangeFile() { using (new ConfigFileSnapshot()) { string originalFileContents = File.ReadAllText(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); ApplicationNode.Hierarchy.Load(); Assert.AreEqual(0, ErrorLogService.ConfigurationErrorCount); ApplicationNode.Hierarchy.Open(); Assert.AreEqual(0, ErrorLogService.ConfigurationErrorCount); AppSettingsNode appSettingsNode = (AppSettingsNode)ApplicationNode.Hierarchy.FindNodeByType(typeof(AppSettingsNode)); Assert.IsNotNull(appSettingsNode); ApplicationNode.Hierarchy.Save(); Assert.AreEqual(0, ErrorLogService.ConfigurationErrorCount); string fileContentsAfterSave = File.ReadAllText(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); Assert.AreEqual(originalFileContents, fileContentsAfterSave); } }
public void RemovedAppSettingsAreRemoved() { using (new ConfigFileSnapshot()) { ApplicationNode.Hierarchy.Load(); Assert.AreEqual(0, ErrorLogService.ConfigurationErrorCount); ApplicationNode.Hierarchy.Open(); Assert.AreEqual(0, ErrorLogService.ConfigurationErrorCount); AppSettingsNode appSettingsNode = (AppSettingsNode)ApplicationNode.Hierarchy.FindNodeByType(typeof(AppSettingsNode)); Assert.IsNotNull(appSettingsNode); AppSettingNode removeThisNode = appSettingsNode.Nodes["removethissetting"] as AppSettingNode; Assert.IsNotNull(removeThisNode); removeThisNode.Remove(); ApplicationNode.Hierarchy.Save(); Assert.AreEqual(0, ErrorLogService.ConfigurationErrorCount); string fileContentsAfterSave = File.ReadAllText(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); Assert.IsFalse(fileContentsAfterSave.Contains("removethissetting")); } }
public void AppSettingsNodeHasProperName() { AppSettingsNode appSettingsNode = new AppSettingsNode(); Assert.AreEqual("Application Settings", appSettingsNode.Name); }