The ChannelAdam XML Library

Overview

An open source, .NET Standard 2.0 library that provides helpful XML functionality that makes XML validation, serialisation and type conversion easy.

Below are the main features of the library. See the linked code for details.

Please contact me if you have any questions.

Getting Started

NuGet Package Installation

To install the ChannelAdam.Xml NuGet package run the following command in the Package Manager Console:

1PM> Install-Package ChannelAdam.Xml

Usage

 1using ChannelAdam.Xml;
 2...
 3
 4    [XmlRoot(Namespace = "uri:normal:namespace")]
 5    public class ObjectForXmlStuff
 6    {
 7        public string MyStringProperty { get; set; }
 8
 9        [XmlElement(Namespace = "uri:different:namespace")]
10        public string MyStringPropertyWithDifferentNamespace { get; set; }
11
12        public int MyIntProperty { get; set; }
13    }
14
15    public class Stuff
16    {
17        public void SerialiseAndDeserialseXml()
18        {
19            var xmlObject = new ObjectForXmlStuff();
20            xmlObject.MyStringProperty = "hello";
21
22            string xml = xmlObject.SerialiseToXml();
23
24            var xmlObject2 = xml.DeserialiseFromXml<ObjectForXmlStuff>();
25        }
26    }

See XmlSerialisationUnitTestSteps.cs for more examples.

Reference

The ChannelAdam Test Framework XML Library

Overview

This is an open source, .NET Standard 2.0 library that provides helpers for comparing text and flat files (using XMLUnit.NET), optionally in conjunction with one of the ChannelAdam Test Framework Libraries:

Below are the main features of the library. See the linked code for details.

Please contact me if you have any questions.

Getting Started

NuGet Package Installation

To install the ChannelAdam.TestFramework.Xml NuGet package run the following command in the Package Manager Console:

1PM> Install-Package ChannelAdam.TestFramework.Xml

Usage

 1using ChannelAdam.TestFramework.Xml;
 2...
 3    [TextFixture]
 4    public class MyUnitTests : MoqTestFixture
 5    {
 6        private XmlTester _xmlTester;
 7
 8        [SetUp]
 9        public Setup()
10        {
11            _xmlTester = new XmlTester(base.LogAssert);
12        }
13
14        [Test]
15        public void MyTestMethod()
16        {
17            // ARRANGE
18            _xmlTester.ArrangeExpectedXml(@"<a><b>hi</b></a>");
19
20            // ACT
21            Logger.Log("About to do the ACT part of blah blah... so the test output reads like a story");
22            _xmlTester.ArrangeActualXml(@"<a><c>c</c></a>");
23
24            // ASSERT
25            Logger.Log("Comparing...");
26            LogAssert.IsTrue("XML samples are different", _xmlTester.IsEqual());
27            // OR
28            _xmlTester.AssertActualXmlEqualsExpectedXml();
29            // The test output will have a helpful Diff.
30        }
31    }

See XmlTestingUnitSteps.cs for an example of using the XmlTester helper.

Reference

XML Tester

The XmlTester class is used in tests for asserting whether the actual XML is similar enough to the given expected XML.

Under the covers, it uses the XMLUnit.NET open source library to identify differences.

The XmlTester class has the following properties:

  • XElement ActualXml
  • XElement ExpectedXml
  • Diff Differences

The XmlTester class has the following Arrange methods:

  • void ArrangeActualXml(Assembly assembly, string resourceName) - to arrange the actual XML from an embedded resource in the given assembly
  • void ArrangeActualXml(XElement xmlElement) - to arrange the actual XML from a given XElement
  • void ArrangeActualXml(object valueToSerialise) - to arrange the actual XML by serialising the given object
  • void ArrangeActualXml(object valueToSerialise, XmlRootAttribute xmlRootAttribute) - to arrange the actual XML by serialising the given object and applying the given XmlRootAttribute during the serialisation
  • void ArrangeActualXml(object valueToSerialise, XmlAttributeOverrides xmlAttributeOverrides) - to arrange the actual XML by serialising the given object and applying the given XmlAttributeOverrides during the serialisation
  • void ArrangeActualXml(string xmlValue) - to arrange the actual XML from the given XML string
  • void ArrangeExpectedXml(Assembly assembly, string resourceName) - to arrange the expected XML from an embedded resource in the given assembly
  • void ArrangeExpectedXml(XElement xmlElement) - to arrange the expected XML from a given XElement
  • void ArrangeExpectedXml(object valueToSerialise) - to arrange the expected XML by serialising the given object
  • void ArrangeExpectedXml(object valueToSerialise, XmlRootAttribute xmlRootAttribute) - to arrange the expected XML by serialising the given object and applying the given XmlRootAttribute during the serialisation
  • void ArrangeExpectedXml(object valueToSerialise, XmlAttributeOverrides xmlAttributeOverrides) - to arrange the expected XML by serialising the given object and applying the given XmlAttributeOverrides during the serialisation
  • void ArrangeExpectedXml(string xmlValue) - to arrange the expected XML from the given XML string

The XmlTester class has the following Assert methods:

  • virtual void AssertActualXmlEqualsExpectedXml() - to assert the actual XML against the expected XML
  • virtual void AssertActualXmlEqualsExpectedXml(IXmlFilter xmlFilter) - to assert the actual XML against the expected XML, ignoring items listed in the given IXmlFilter (see below)
  • bool IsEqual() - determines if the given actual and expected XML is equivalent
  • bool IsEqual(XNode expected, XNode actual) - determines if the given actual and expected XNode XML is equivalent
  • virtual bool IsEqual(XmlNode expected, XmlNode actual) - determines if the given actual and expected XmlNode XML is equivalent

Ignoring / Filtering XML Elements

The AssertActualXmlEqualsExpectedXml(IXmlFilter xmlFilter) override allows you to specify an IXmlFilter interface which allows you to specify a list of the local names of XML elements to ignore in comparisons, and/or a list of XPath expressions to ignore in comparisons.

Examples

For a quick example, please see the Behaviour Specification for the XmlTester class and the XML tester code

XML Filter

The XmlFilter class allows you to specify a list of the local names of XML elements to ignore in comparisons, and/or a list of XPath expressions to ignore in comparisons.

Two constructor methods allow you to easily specify these lists.

  • XmlFilter(IList<string> elementLocalNamesToIgnore)
  • XmlFilter(IList<string> elementLocalNamesToIgnore, IList<string> xpathsToIgnore)

XML Asserter

The XmlAsserter class provides utility methods for performing assertions on XML values. It has the following methods:

  • void XPathValueEquals(string description, string xpath, XNode rootElement, string expected) - to assert that the given XPath of the provided XNode has the given expected string value
  • void XPathValueEquals(string description, string xpath, XNode rootElement, XmlNamespaceManager namespaceManager, string expected) - uses the provided namespace manager to assert that the given XPath of the provided XNode has the given expected string value
  • void XPathValuesAreEqual(string description, string xpath, XNode expectedElements, XNode actualElements) - to assert that the value of the XPath in the given actual XNode equals the corresponding value in the given expected XNode
  • void XPathValuesAreEqual(string description, string expectedXpath, XNode expectedElements, string actualXpath, XNode actualElements) - to assert that the value of the actual XPath in the actual XNode is the same as the value of the expected XPath in the expected XNode
  • void XPathValuesAreEqual(string description, string expectedXpath, XNode expectedElements, XmlNamespaceManager expectedNamespaceManager, string actualXpath, XNode actualElements, XmlNamespaceManager actualNamespaceManager) - to assert that the value of the actual XPath in the actual XNode is the same as the value of the expected XPath in the expected XNode, using the provided XML namespace managers
  • void AreEqual(XElement expectedXml, XElement actualXml) - to assert that the given expected XElement is equivalent to the actual XElement
  • void AreEqual(XElement expectedXml, XElement actualXml, IXmlFilter xmlFilter) - to assert that the given expected XElement is equivalent to the actual XElement - using the given IXmlFilter

Mapping Testers

There are 3 map tester classes to be used depending on the type of input and output of your map:

The MappingFromXmlToXmlTester class has the following method overrides for arranging the input XML.

  • void ArrangeInputXml(Assembly assembly, string resourceName) - to arrange the input XML from an embedded resource in the given assembly
  • void ArrangeInputXml(XElement xmlElement) - to arrange the input XML from a given XElement
  • void ArrangeInputXml(object valueToSerialise) - to arrange the input XML by serialising the given object
  • void ArrangeInputXml(object valueToSerialise, XmlRootAttribute xmlRootAttribute) - to arrange the input XML by serialising the given object and applying the given XmlRootAttribute during the serialisation
  • void ArrangeInputXml(object valueToSerialise, XmlAttributeOverrides xmlAttributeOverrides) - to arrange the input XML by serialising the given object and applying the given XmlAttributeOverrides during the serialisation
  • void ArrangeInputXml(string xmlValue) - to arrange the input XML from the given XML string
  • void ArrangeExpectedOutputXml(Assembly assembly, string resourceName) - to arrange the expected output XML from an embedded resource in the given assembly
  • void ArrangeExpectedOutputXml(XElement xmlElement) - to arrange the expected output XML from a given XElement
  • void ArrangeExpectedOutputXml(object valueToSerialise) - to arrange the expected output XML by serialising the given object
  • void ArrangeExpectedOutputXml(object valueToSerialise, XmlRootAttribute xmlRootAttribute) - to arrange the expected output XML by serialising the given object and applying the given XmlRootAttribute during the serialisation
  • void ArrangeExpectedOutputXml(object valueToSerialise, XmlAttributeOverrides xmlAttributeOverrides) - to arrange the expected output XML by serialising the given object and applying the given XmlAttributeOverrides during the serialisation
  • void ArrangeExpectedOutputXml(string xmlValue) - to arrange the expected output XML from the given XML string

Input can be arranged from an embedded resource, XElement, object that is XML serialisable (for which you can also override the root XML namespace when serialised) or simply a string.

This same pattern is followed for arranging the expected output of the map, and for arranging the contents of flat files with the other map tester classes.

Assert - Compare the Actual Output Against the Expected Output

The MappingFromXmlToXmlTester class has the following method overrides for asserting the actual output from the map against the expected output that was arranged.

  • void AssertActualOutputXmlEqualsExpectedOutputXml()
  • AssertActualOutputXmlEqualsExpectedOutputXml(IXmlFilter xmlFilter) (see below)

All the comparison and logging and formatting of any differences is done for you. Easy!

Ignoring / Filtering XML Elements

The AssertActualOutputXmlEqualsExpectedOutputXml(IXmlFilter xmlFilter) override allows you to specify an IXmlFilter which allows you to specify a list of the local names of XML elements to ignore in comparisons, and/or a list of XPath expressions to ignore in comparisons.

Two constructor methods on XmlFilter allow you to easily specify these lists.

  • XmlFilter(IList<string> elementLocalNamesToIgnore)
  • XmlFilter(IList<string> elementLocalNamesToIgnore, IList<string> xpathsToIgnore)

The ChannelAdam Core Library

IMPORTANT: DEPRECATION NOTICE

This full .NET Framework library has been deprecated.

The functionality has been refactored out into the following ChannelAdam .NET Standard libraries.

Overview

The open source, full .NET Framework, ChannelAdam Core Library provides some helpful .NET functionality - such as the implementation of various design patterns. This functionality also just happens to be used in the other ChannelAdam code libraries…

Below are the main features of the library. See the linked code for details.

Please contact me if you have any questions.

Getting Started

NuGet Package Installation

To install the ChannelAdam.Core NuGet package run the following command in the Package Manager Console:

1PM> Install-Package ChannelAdam.Core

Features

Disposable Pattern

Have you ever thought that the disposable pattern was complicated to implement or understand? Inherit this and it’s easy!

Command Pattern

Have you ever needed to implement a Command Pattern?

Command Action

A Command Action has an Execute method with a void return parameter.

Command Function

A Command Function has an Execute method with a specified return parameter.

Reversible Commands

Need to execute some commands and be able to undo them? This Reversible Command implementation is for you!

Use the Reversible Command Manager to manage the stack of commands that can be reversed.

Set Property - Command Implementation

A command implementation for setting a property on an object.

Weak Event Pattern

Need a Weak Event in specific circumstances to prevent memory leaks in your event listeners?

Simple Logger

A very simple interface for logging or writing out to the console.

Embedded Resources

Easily retreive embedded resources from their manifest resource stream.

Real Proxies

Getting low-level with a Real Proxy? These are disposable and have retry capability!

Retry Policies (for Transient Fault Handling)

Some handy interfaces to describe retry policies.

XML

XML validation, serialisation and type conversion made easy.