public void XmlSection_ExtendedSettingsTest()
        {
            try
            {
                Settings settings = XmlSection <Settings> .Deserialize("ExtendedSettingsSection");

                settings.IntValue = 345;

                settings = XmlSection <Settings> .Deserialize("ExtendedSettingsSection");

                ExtendedSettings exSettings = settings as ExtendedSettings;

                Assert.IsNotNull(exSettings);
                Assert.AreEqual(10, exSettings.IntValue);
                Assert.AreEqual("Value", exSettings.StringValue);
                List <int> intList = new List <int>(new int[] { 1, 2, 3, 4, 5, 6 });
                CollectionAssert.AreEqual(intList, exSettings.IntList);
                string[] array = { "first", "second", "third" };
                CollectionAssert.AreEqual(array, exSettings.StringArray);
                Assert.AreEqual("The new property", exSettings.NewProperty);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }
Ejemplo n.º 2
0
        public String Get(Configuration.ConfFolder folder, Configuration.ConfEntry entry, String defaultValue)
        {
            String result = defaultValue;

            try
            {
                lock (this.sections)
                {
                    XmlSection section = this.sections.FirstOrDefault((x) => String.Equals(x.Name, folder.ToString()));
                    if (section != null)
                    {
                        XmlSectionEntry sectionEntry = section.Entries.FirstOrDefault((x) => String.Equals(x.Key, entry.ToString()));
                        if (sectionEntry != null)
                        {
                            result = sectionEntry.Value;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                LOG.Error(e);
            }

            return(result);
        }
        public void XmlSection_AppSettingsTest()
        {
            AppSettings settings = XmlSection <AppSettings> .Deserialize("AppSettingsTest");

            Assert.IsTrue(settings.Classes[0] is InheritedClass1);
            Assert.IsTrue(settings.Classes[1] is InheritedClass2);
        }
Ejemplo n.º 4
0
        public void ElementsWithAttributes_Test1()
        {
            ElementsWithAttributesSettings settings = XmlSection <ElementsWithAttributesSettings> .Deserialize("ElementsWithAttributesTest");

            Assert.AreEqual("One", settings.AClasses[0].Value);
            Assert.AreEqual("Two", settings.AClasses[1].Value);
        }
        public string GetAttribute(string xPath, string attName, string defaultValue)
        {
            if (XmlSection == null)
            {
                return(defaultValue);
            }

            return(GetAttribute(XmlSection.SelectSingleNode(xPath), attName, defaultValue));
        }
        public XmlNode GetNode(string xPath)
        {
            if (XmlSection == null)
            {
                return(null);
            }

            return(XmlSection.SelectSingleNode(xPath));
        }
        public XmlNodeList GetNodes(string xPath)
        {
            if (XmlSection == null)
            {
                return(null);
            }

            return(XmlSection.SelectNodes(xPath));
        }
        public void XmlSection_DefaultSettings()
        {
            Settings settings = XmlSection <Settings> .Deserialize("bogus_section");

            Assert.IsNotNull(settings);
            Assert.AreEqual(999, settings.IntValue);
            Assert.AreEqual("The String Value", settings.StringValue);
            Assert.IsNull(settings.IntList);
            Assert.IsNotNull(settings.StringArray);
            Assert.AreEqual(3, settings.StringArray.Length);
            CollectionAssert.AreEqual(new string[] { "one", "two", "three" }, settings.StringArray);
        }
        public void XmlSection_InvalidBaseTypeTest()
        {
            try
            {
                XmlSection <Settings> .Deserialize("InvalidBaseType", false);

                Assert.Fail("Failed to throw InvalidOperationException");
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is ConfigurationErrorsException);
            }
        }
 public void XmlSection_MissingSectionTest()
 {
     try
     {
         XmlSection <Settings> .Deserialize("bogussection");
     }
     catch (ConfigurationMissingSectionException ex)
     {
         Assert.AreEqual("The section, bogussection, could not be found within the configuration file", ex.Message);
     }
     catch (Exception ex)
     {
         Assert.Fail(ex.Message);
     }
 }
        public void XmlSection_InvalidReferenceTest()
        {
            try
            {
                Settings settings = XmlSection <Settings> .Deserialize("InvalidReference", false);

                Assert.Fail("Failed to throw ConfigurationErrorsException");
            }
            catch (Exception ex)
            {
                Assert.AreEqual("Failed to load assembly (D:\\Steve\\gitrepos\\tnt.libraries\\TNT.Configuration\\Tests\\bin\\Debug\\Tests.dll.config line 161)", ex.Message);
                Assert.IsTrue(ex is ConfigurationErrorsException);
                Assert.IsTrue(ex.InnerException is FileNotFoundException);
            }
        }
Ejemplo n.º 12
0
        private void WriteSection(XElement element, XmlSection section)
        {
            var xElement = new XElement("Section");
            xElement.SetAttributeValue("Name", section.Name);

            foreach (var childSection in section.Sections())
            {
                WriteSection(xElement, childSection);
            }

            foreach (var entry in section.Entries())
            {
                WriteEntry(xElement, entry.Key, entry.Value);
            }
            element.Add(xElement);
        }
        public void XmlSection_SettingsTest()
        {
            try
            {
                Settings settings = XmlSection <Settings> .Deserialize("SettingsSection");

                Assert.AreEqual(10, settings.IntValue);
                Assert.AreEqual("Value", settings.StringValue);
                List <int> intList = new List <int>(new int[] { 1, 2, 3, 4, 5, 6 });
                CollectionAssert.AreEqual(intList, settings.IntList);
                string[] array = { "first", "second", "third" };
                CollectionAssert.AreEqual(array, settings.StringArray);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }
Ejemplo n.º 14
0
        public bool Set(Configuration.ConfFolder folder, Configuration.ConfEntry entry, String value)
        {
            if (value == null)
            {
                return(false);
            }

            try
            {
                lock (this.sections)
                {
                    XmlSection section = this.sections.FirstOrDefault((x) => String.Equals(x.Name, folder.ToString()));
                    if (section == null)
                    {
                        section = new XmlSection(folder.ToString());
                        this.sections.Add(section);
                    }

                    XmlSectionEntry sectionEntry = section.Entries.FirstOrDefault((x) => String.Equals(x.Key, entry.ToString()));
                    if (sectionEntry == null)
                    {
                        sectionEntry = new XmlSectionEntry(entry.ToString(), value);
                        section.Entries.Add(sectionEntry);
                    }
                    else
                    {
                        sectionEntry.Value = value;
                    }
                }
            }
            catch (Exception e)
            {
                LOG.Error("Failed to set value into registry", e);
                return(false);
            }

            // Trigger
            EventHandlerTrigger.TriggerEvent <ConfigurationEventArgs>(this.onConfigurationEvent, this, new ConfigurationEventArgs(folder, entry, value));

            this.DeferredSave();

            return(true);
        }
Ejemplo n.º 15
0
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                List <Plugin> plugins = XmlSection <List <Plugin> > .Deserialize("PluginSection");

                foreach (Plugin plugin in plugins)
                {
                    plugin.Merge(Controls);

                    plugin.MenuItem.Click += MenuItem_Click;
                    plugin.Button.Click   += MenuItem_Click;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void XmlSection_SettingsListTest()
        {
            try
            {
                List <Settings> setting = XmlSection <List <Settings> > .Deserialize("SettingsList");

                Assert.IsNotNull(setting);
                Assert.AreEqual(3, setting.Count);
                Assert.IsTrue(setting[0] is Settings);
                Assert.IsTrue(setting[1] is ExtendedSettings);
                Assert.AreEqual(1, setting[0].IntValue);
                Assert.AreEqual(2, setting[1].IntValue);
                Assert.AreEqual("The new property", (setting[1] as ExtendedSettings).NewProperty);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }
        internal FilterInfo GetFilterInfo(string name)
        {
            if (XmlSection == null)
            {
                return(null);
            }

            var xPath   = string.Format("./filters/filter[@name='{0}']", name);
            var objNode = XmlSection.SelectSingleNode(xPath);

            if (objNode == null)
            {
                return(null);
            }

            // validate info
            var sMode = GetAttribute(objNode, "mode", "include").Trim().ToLower();

            if (!(sMode == "include" || sMode == "exclude"))
            {
                return(null);
            }

            var mode   = sMode == "include" ? FilterMode.Include : FilterMode.Exclude;
            var filter = new FilterInfo(name.Trim().ToLower(), mode);

            foreach (XmlNode childNode in objNode.ChildNodes)
            {
                if (childNode.Name != "method" || childNode.Attributes["type"] == null)
                {
                    continue;
                }

                string[] methods = childNode.Attributes["type"].Value.Split(',');
                foreach (string method in methods)
                {
                    filter.IDs.Add(method.Trim());
                }
            }

            return(filter);
        }
        internal List <RIInstance> LoadLogManagerInstances()
        {
            var instances = new List <RIInstance>();

            if (XmlSection == null)
            {
                return(instances);
            }

            var objNode = XmlSection.SelectSingleNode("./logManager");

            if (objNode == null)
            {
                return(instances);
            }

            foreach (XmlNode node in objNode.ChildNodes)
            {
                if (node.Name != "instance")
                {
                    continue;
                }
                if (node.Attributes["name"] == null)
                {
                    continue;
                }

                string name     = node.Attributes["name"].Value;
                string category = node.Attributes["category"] != null ? node.Attributes["category"].Value : string.Empty;
                string bkColor  = node.Attributes["bkColor"] != null ? node.Attributes["bkColor"].Value : string.Empty;
                string destinationBindingGroup = node.Attributes["destinationBindingGroup"] != null ? node.Attributes["destinationBindingGroup"].Value : string.Empty;

                instances.Add(new RIInstance(name, category, bkColor, destinationBindingGroup));
            }

            return(instances);
        }
        internal Hashtable LoadMessageColors()
        {
            var messageColors = new Hashtable();

            if (XmlSection == null)
            {
                return(messageColors);
            }

            var objNode = XmlSection.SelectSingleNode("./messageColors");

            if (objNode == null)
            {
                return(messageColors);
            }

            foreach (XmlNode node in objNode.ChildNodes)
            {
                if (node.Name != "message")
                {
                    continue;
                }
                if (node.Attributes["type"] == null)
                {
                    continue;
                }
                if (node.Attributes["bkColor"] == null)
                {
                    continue;
                }

                messageColors[node.Attributes["type"].Value] = node.Attributes["bkColor"].Value;
            }

            return(messageColors);
        }
        internal List <ListenerGroup> LoadListenerGroups()
        {
            var groups = new List <ListenerGroup>();

            if (XmlSection == null)
            {
                return(groups);
            }

            var objNode = XmlSection.SelectSingleNode("./listenerGroups");

            if (objNode == null)
            {
                return(groups);
            }

            foreach (XmlNode gNode in objNode.ChildNodes)
            {
                if (gNode.Name != "group")
                {
                    continue;
                }
                if (gNode.Attributes["name"] == null)
                {
                    continue;
                }

                var name     = gNode.Attributes["name"].Value;
                var bEnabled = (gNode.Attributes["enabled"] != null ? gNode.Attributes["enabled"].Value : "true") == "true";
                var bMask    = (gNode.Attributes["maskIdentities"] != null ? gNode.Attributes["maskIdentities"].Value : "false") == "true";

                var group = new ListenerGroup(name, bEnabled, bMask);
                groups.Add(group);

                // let's add the destinations
                XmlNode destNodes = gNode.SelectSingleNode("./destinations");
                if (destNodes != null)
                {
                    foreach (XmlNode dNode in destNodes.ChildNodes)
                    {
                        if (dNode.Name != "destination")
                        {
                            continue;
                        }

                        if (dNode.Attributes["name"] == null || dNode.Attributes["details"] == null)
                        {
                            continue;
                        }

                        bEnabled = (dNode.Attributes["enabled"] != null ? dNode.Attributes["enabled"].Value : "true") == "true";
                        var filter = dNode.Attributes["filter"] != null ? dNode.Attributes["filter"].Value : string.Empty;

                        group.AddDestination(dNode.Attributes["name"].Value, dNode.Attributes["details"].Value, bEnabled, filter);
                    }
                }

                // let's add the destination binding groups
                XmlNode destinationBindingGroupNodes = gNode.SelectSingleNode("./destinationBindingGroups");
                if (destinationBindingGroupNodes != null)
                {
                    foreach (XmlNode bNode in destinationBindingGroupNodes.ChildNodes)
                    {
                        if (bNode.Name != "destinationBindingGroup")
                        {
                            continue;
                        }

                        var bindingGroupName = bNode.Attributes["name"] != null ? bNode.Attributes["name"].Value : null;
                        var bindingGroup     = group.GetDestinationBindingGroup(bindingGroupName);
                        if (bindingGroup == null)
                        {
                            bindingGroup = group.AddDestinationBindingGroup(bindingGroupName);
                        }
                        else
                        {
                            bindingGroup.ClearDestinationBindings();
                        }

                        foreach (XmlNode dbNode in bNode.ChildNodes)
                        {
                            if (dbNode.Name != "destination")
                            {
                                continue;
                            }

                            if (dbNode.Attributes["name"] == null)
                            {
                                continue;
                            }

                            bindingGroup.AddDestinationBinding(dbNode.Attributes["name"].Value);
                        }
                    }
                }
            }

            return(groups);
        }