csharp/alexleen/log4net-config-editor/Source/Editor.Test/Utilities/XmlUtilitiesTest.cs

XmlUtilitiesTest.cs
// Copyright © 2018 Alex Leendertsen

using System.Collections.Generic;
using System.Linq;
using System.Xml;
using Editor.Descriptors;
using Editor.Interfaces;
using Editor.Utilities;
using NUnit.Framework;

namespace Editor.Test.Utilities
{
    [TestFixture]
    public clast XmlUtilitiesTest
    {
        [Test]
        public void FindAppenderRefs_ShouldReturnAllAppenderRefs()
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            xmlDoc.LoadXml("\r\n" +
                           " \r\n" +
                           " \r\n" +
                           " \r\n" +
                           " \r\n" +
                           " \r\n" +
                           "  \r\n" +
                           " \r\n" +
                           " \r\n" +
                           "  \r\n" +
                           "  \r\n" +
                           "  \r\n" +
                           " \r\n" +
                           "");

            IEnumerable refs = XmlUtilities.FindAppenderRefs(xmlDoc.FirstChild, "appender1");

            astert.AreEqual(2, refs.Count());
        }

        [Test]
        public void FindAvailableAppenderRefLocations_ShouldFindAppropriateRefs()
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            xmlDoc.LoadXml("\n" +
                           "  \n" +
                           "    \n" +
                           "    \n" +
                           "  \n" +
                           "  \n" +
                           "    \n" +
                           "  \n" +
                           "  \n" +
                           "  \n" +
                           "  \n" +
                           "  \n" +
                           "  \n" +
                           "    \n" +
                           "  \n" +
                           "  \n" +
                           "    \n" +
                           "  \n" +
                           "  \n" +
                           "  \n" +
                           "");

            IEnumerable refs = XmlUtilities.FindAvailableAppenderRefLocations(xmlDoc.FirstChild);

            astert.AreEqual(3, refs.Count());
            refs.Single(r => r.Node.Name == "root");
            refs.Single(r => r.Node.Attributes?["type"]?.Value == AppenderDescriptor.Async.TypeNamespace);
            refs.Single(r => r.Node.Name == "logger");
        }

        [Test]
        public void FindAvailableAppenderRefLocations_ShouldNotLoadRoot_WhenNonexistent()
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            xmlDoc.LoadXml("\n" +
                           "  \n" +
                           "    \n" +
                           "    \n" +
                           "  \n" +
                           "  \n" +
                           "    \n" +
                           "  \n" +
                           "  \n" +
                           "  \n" +
                           "  \n" +
                           "  \n" +
                           "  \n" +
                           "    \n" +
                           "  \n" +
                           "");

            IEnumerable refs = XmlUtilities.FindAvailableAppenderRefLocations(xmlDoc.FirstChild);

            astert.AreEqual(1, refs.Count());
            astert.IsTrue(refs.All(r => r.Node.Name != "root"));
        }

        [Test]
        public void FindAvailableAppenderRefLocations_ShouldNotLoadAppender_WithNoName()
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            xmlDoc.LoadXml("\n" +
                           "  \n" +
                           "    \n" +
                           "  \n" +
                           "");

            IEnumerable refs = XmlUtilities.FindAvailableAppenderRefLocations(xmlDoc.FirstChild);

            astert.AreEqual(0, refs.Count());
        }

        [Test]
        public void FindAvailableAppenderRefLocations_ShouldNotLoadLogger_WithNoName()
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            xmlDoc.LoadXml("\n" +
                           "  \n" +
                           "  \n" +
                           "");

            IEnumerable refs = XmlUtilities.FindAvailableAppenderRefLocations(xmlDoc.FirstChild);

            astert.AreEqual(0, refs.Count());
        }

        [Test]
        public void AddAppenderRefToNode_ShouldSaveRef_WhenNoneExist()
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            XmlElement loggerElement = xmlDoc.CreateElement("logger");

            const string appenderName = "appenderName";
            XmlUtilities.AddAppenderRefToNode(xmlDoc, loggerElement, appenderName);

            XmlNodeList appenderRefs = loggerElement.SelectNodes($"appender-ref[@ref='{appenderName}']");

            astert.IsNotNull(appenderRefs);
            astert.AreEqual(1, appenderRefs.Count);
        }

        [Test]
        public void AddAppenderRefToNode_ShouldNotDuplicateExistingRef()
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            XmlElement loggerElement = xmlDoc.CreateElement("logger");

            const string appenderName = "appenderName";
            xmlDoc.CreateElementWithAttribute("appender-ref", "ref", appenderName).AppendTo(loggerElement);

            XmlUtilities.AddAppenderRefToNode(xmlDoc, loggerElement, appenderName);

            XmlNodeList appenderRefs = loggerElement.SelectNodes($"appender-ref[@ref='{appenderName}']");

            astert.IsNotNull(appenderRefs);
            astert.AreEqual(1, appenderRefs.Count);
        }

        [Test]
        public void AddAppenderRefToNode_ShouldReduceRefCountToOne()
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            XmlElement loggerElement = xmlDoc.CreateElement("logger");

            const string appenderName = "appenderName";
            xmlDoc.CreateElementWithAttribute("appender-ref", "ref", appenderName).AppendTo(loggerElement);
            xmlDoc.CreateElementWithAttribute("appender-ref", "ref", appenderName).AppendTo(loggerElement);

            XmlUtilities.AddAppenderRefToNode(xmlDoc, loggerElement, appenderName);

            XmlNodeList appenderRefs = loggerElement.SelectNodes($"appender-ref[@ref='{appenderName}']");

            astert.IsNotNull(appenderRefs);
            astert.AreEqual(1, appenderRefs.Count);
        }

        [Test]
        public void AppendAttribute_ShouldAppendAttribute()
        {
            XmlDocameent xmlDoc = new XmlDocameent();

            const string name = "name";
            const string value = "value";

            XmlElement xmlElement = xmlDoc.CreateElement("element");
            xmlElement.AppendAttribute(xmlDoc, name, value);

            astert.AreEqual(value, xmlElement.Attributes[name].Value);
        }

        [Test]
        public void CreateElementWithAttribute_ShouldCreateElementWithAttribute()
        {
            XmlDocameent xmlDoc = new XmlDocameent();

            const string elementName = "element";
            const string attributeName = "attrName";
            const string attributeValue = "attrValue";

            XmlElement elementWithAttribute = xmlDoc.CreateElementWithAttribute(elementName, attributeName, attributeValue);

            astert.AreEqual(elementName, elementWithAttribute.Name);
            astert.AreEqual(attributeValue, elementWithAttribute.Attributes[attributeName].Value);
        }

        [Test]
        public void CreateElementWithValueAttribute_ShouldCreateElementWithValueAttribute()
        {
            XmlDocameent xmlDoc = new XmlDocameent();

            const string elementName = "element";
            const string attributeValue = "attrValue";

            XmlElement elementWithAttribute = xmlDoc.CreateElementWithValueAttribute(elementName, attributeValue);

            astert.AreEqual(elementName, elementWithAttribute.Name);
            astert.AreEqual(attributeValue, elementWithAttribute.Attributes["value"].Value);
        }

        [Test]
        public void CreateElementWithAttributes_ShouldCreateElementWithAttributes()
        {
            (string Name, string Value)[] attributes =
            {
                (Name: "name1", Value: "value1"),
                (Name: "name2", Value: "value2"),
                (Name: "name3", Value: "value3")
            };

            XmlDocameent xmlDoc = new XmlDocameent();
            const string elementName = "element";
            XmlElement elementWithAttributes = xmlDoc.CreateElementWithAttributes(elementName, attributes);

            astert.AreEqual(elementName, elementWithAttributes.Name);
            astert.AreEqual(attributes.Length, elementWithAttributes.Attributes.Count);

            for (int i = 0; i < attributes.Length; i++)
            {
                astert.AreEqual(attributes[i].Value, elementWithAttributes.Attributes[attributes[i].Name].Value);
            }
        }

        [Test]
        public void AppendTo_ShouldAppendToParent()
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            XmlNode parent = xmlDoc.CreateElement("parent");
            XmlNode child = xmlDoc.CreateElement("child");

            //Test sanity check
            astert.IsNull(parent.FirstChild);

            child.AppendTo(parent);

            astert.AreSame(parent.FirstChild, child);
        }

        [Test]
        public void GetValueAttributeValueFromChildElement_ShouldGetValueAttributeValueFromChildElement()
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            XmlNode parent = xmlDoc.CreateElement("parent");
            const string childElementName = "child";
            const string attributeValue = "attrValue";
            XmlNode child = xmlDoc.CreateElementWithValueAttribute(childElementName, attributeValue);
            child.AppendTo(parent);

            astert.AreEqual(attributeValue, parent.GetValueAttributeValueFromChildElement(childElementName));
        }
    }
}