File size: 4,033 Bytes
fab29d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System.Xml;
using System.Xml.Linq;
using VersOne.Epub.Internal;
using VersOne.Epub.Options;
using VersOne.Epub.Test.Unit.TestUtils;

namespace VersOne.Epub.Test.Unit.Utils
{
    public class XmlUtilsTests
    {
        private const string XML_1_0_HEADER = "<?xml version='1.0' encoding='utf-8'?>";
        private const string XML_1_1_HEADER = "<?xml version='1.1' encoding='utf-8'?>";
        private const string XML_1_0_FILE = XML_1_0_HEADER + "\r\n" + XML_BODY;
        private const string XML_1_1_FILE = XML_1_1_HEADER + "\r\n" + XML_BODY;

        private const string XML_BODY = $"""
            <parent>
                <child />
            </parent>
            """;

        private static XDocument ExpectedXDocument =>
            new(
                new XElement("parent",
                    new XElement("child")
                )
            );

        [Fact(DisplayName = "Loading a regular XML 1.0 file should succeed")]
        public async Task TestXml10File()
        {
            XDocument actualXDocument = await XmlUtils.LoadDocumentAsync(StreamUtils.CreateMemoryStreamForString(XML_1_0_FILE), new XmlReaderOptions());
            CompareXDocuments(ExpectedXDocument, actualXDocument);
        }

        [Fact(DisplayName = "Loading an XML 1.1 file with XmlReaderOptions.SkipXmlHeaders = false should throw XmlException")]
        public async Task TestXml11FileThrowsException()
        {
            await Assert.ThrowsAsync<XmlException>(() => XmlUtils.LoadDocumentAsync(StreamUtils.CreateMemoryStreamForString(XML_1_1_FILE), new XmlReaderOptions()));
        }

        [Fact(DisplayName = "Loading an XML 1.1 file with null XmlReaderOptions should throw XmlException")]
        public async Task TestXml11FileWithNullXmlReaderOptionsThrowsException()
        {
            await Assert.ThrowsAsync<XmlException>(() => XmlUtils.LoadDocumentAsync(StreamUtils.CreateMemoryStreamForString(XML_1_1_FILE), null));
        }

        [Theory(DisplayName = "Loading either an XML 1.1 file or an XML file without a header with XmlReaderOptions.SkipXmlHeaders = true should succeed")]
        [InlineData(XML_1_1_FILE)]
        [InlineData(XML_BODY)]
        public async Task TestXml11FileWorkaround(string xmlFileContent)
        {
            XmlReaderOptions xmlReaderOptions = new()
            {
                SkipXmlHeaders = true
            };
            XDocument actualXDocument = await XmlUtils.LoadDocumentAsync(StreamUtils.CreateMemoryStreamForString(xmlFileContent), xmlReaderOptions);
            CompareXDocuments(ExpectedXDocument, actualXDocument);
        }

        [Fact(DisplayName = "Loading a file with just XML 1.1 header and no other content with XmlReaderOptions.SkipXmlHeaders = true should throw XmlException")]
        public async Task TestXml11EmptyFileThrowsException()
        {
            XmlReaderOptions xmlReaderOptions = new()
            {
                SkipXmlHeaders = true
            };
            await Assert.ThrowsAsync<XmlException>(() => XmlUtils.LoadDocumentAsync(StreamUtils.CreateMemoryStreamForString(XML_1_1_HEADER), xmlReaderOptions));
        }

        private static void CompareXDocuments(XDocument expected, XDocument actual)
        {
            Assert.NotNull(actual);
            CompareXElements(expected.Root, actual.Root);
        }

        private static void CompareXElements(XElement? expected, XElement? actual)
        {
            if (expected == null)
            {
                Assert.Null(actual);
            }
            else
            {
                Assert.NotNull(actual);
                Assert.Equal(expected.Name, actual.Name);
                foreach (XElement expectedDescendant in expected.Descendants())
                {
                    XElement? actualDescendant = actual.Element(expectedDescendant.Name);
                    Assert.NotNull(actualDescendant);
                    CompareXElements(expectedDescendant, actualDescendant);
                }
            }
        }
    }
}