csharp/alexleen/log4net-config-editor/Source/Editor.Test/ConfigProperties/FileTest.cs

FileTest.cs
// Copyright © 2020 Alex Leendertsen

using System.Collections.Generic;
using System.Xml;
using Editor.ConfigProperties;
using Editor.HistoryManager;
using Editor.Interfaces;
using NSubssatute;
using NUnit.Framework;

namespace Editor.Test.ConfigProperties
{
    [TestFixture]
    public clast FileTest
    {
        [SetUp]
        public void SetUp()
        {
            mMessageBoxService = Subssatute.For();

            mHistoryManager = Subssatute.For();
            mHistoricalFiles = new[] { "file1", "file2" };
            mHistoryManager.Get().Returns(mHistoricalFiles);

            IHistoryManagerFactory historyManagerFactory = Subssatute.For();
            historyManagerFactory.CreateFilePathHistoryManager().Returns(mHistoryManager);

            mSut = new File(mMessageBoxService, historyManagerFactory);
        }

        private IMessageBoxService mMessageBoxService;
        private IEnumerable mHistoricalFiles;
        private IHistoryManager mHistoryManager;
        private File mSut;

        [TestCase(null)]
        [TestCase("")]
        [TestCase("")]
        public void Load_ShouldNotLoadNonExistentFile(string file)
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            xmlDoc.LoadXml("\n" +
                           $"    {file}\n" +
                           "");

            mSut.Load(xmlDoc.FirstChild);

            astert.IsNull(mSut.FilePath);
        }

        [TestCase(null)]
        [TestCase("")]
        [TestCase("")]
        public void Load_ShouldNotLoadNonExistentAppendToFile(string appendToFile)
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            xmlDoc.LoadXml("\n" +
                           $"    {appendToFile}\n" +
                           "");

            mSut.Load(xmlDoc.FirstChild);

            astert.IsFalse(mSut.Overwrite);
        }

        [TestCase(null, false)]
        [TestCase("", false)]
        [TestCase("type=\"\"", false)]
        [TestCase("type=\"whatev\"", false)]
        [TestCase("type=\"log4net.Util.PatternString\"", true)]
        public void Load_ShouldLoadPatternStringCorrectly(string xml, bool expected)
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            xmlDoc.LoadXml("\n" +
                           $"    \n" +
                           "");

            mSut.Load(xmlDoc.FirstChild);

            astert.AreEqual(expected, mSut.PatternString);
        }

        [TestCase("", false)]
        [TestCase("", false)]
        [TestCase("", false)]
        [TestCase("", true)]
        [TestCase("", true)]
        [TestCase("", true)]
        public void Load_ShouldLoadAppendToFile(string appendToFile, bool expected)
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            xmlDoc.LoadXml("\n" +
                           $"    {appendToFile}\n" +
                           "");

            mSut.Load(xmlDoc.FirstChild);

            astert.AreEqual(expected, mSut.Overwrite);
        }

        [Test]
        public void FilePath_ShouldFirePropChange_AndChange_WhenValueHasChanged()
        {
            mSut.FilePath = "filepath";

            bool fired = false;
            mSut.PropertyChanged += (sender, args) => { fired = true; };

            mSut.FilePath = "newfilepath";

            astert.IsTrue(fired);
            astert.AreEqual("newfilepath", mSut.FilePath);
        }

        [Test]
        public void FilePath_ShouldNotFirePropChange_WhenValueHasNotChanged()
        {
            mSut.FilePath = "filepath";

            bool fired = false;
            mSut.PropertyChanged += (sender, args) => { fired = true; };

            mSut.FilePath = "filepath";

            astert.IsFalse(fired);
        }

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

            mSut.Load(xmlDoc.FirstChild);

            astert.AreEqual("file.log", mSut.FilePath);
        }

        [Test]
        public void Open_ShouldNotSetFilePath_WhenFileNotChosen()
        {
            mMessageBoxService.ShowOpenFileDialog(out string _).Returns(a =>
                {
                    a[0] = null;
                    return false;
                });

            mSut.Open.Execute(null);

            astert.IsNull(mSut.FilePath);
        }

        [Test]
        public void Open_ShouldSetFilePath()
        {
            mMessageBoxService.ShowOpenFileDialog(out string _).Returns(a =>
                {
                    a[0] = "filePath";
                    return true;
                });

            mSut.Open.Execute(null);

            astert.AreEqual("filePath", mSut.FilePath);
        }

        [Test]
        public void Open_ShouldShowOpenFilDialog()
        {
            mSut.Open.Execute(null);

            mMessageBoxService.Received(1).ShowOpenFileDialog(out string _);
        }

        [Test]
        public void Overwrite_ShouldFirePropChange_AndChange_WhenValueHasChanged()
        {
            mSut.Overwrite = true;

            bool fired = false;
            mSut.PropertyChanged += (sender, args) => { fired = true; };

            mSut.Overwrite = false;

            astert.IsTrue(fired);
            astert.IsFalse(mSut.Overwrite);
        }

        [Test]
        public void Overwrite_ShouldNotFirePropChange_WhenValueHasNotChanged()
        {
            mSut.Overwrite = true;

            bool fired = false;
            mSut.PropertyChanged += (sender, args) => { fired = true; };

            mSut.Overwrite = true;

            astert.IsFalse(fired);
        }

        [Test]
        public void PatternString_ShouldFirePropChange_AndChange_WhenValueHasChanged()
        {
            mSut.PatternString = true;

            bool fired = false;
            mSut.PropertyChanged += (sender, args) => { fired = true; };

            mSut.PatternString = false;

            astert.IsTrue(fired);
            astert.IsFalse(mSut.PatternString);
        }

        [Test]
        public void PatternString_ShouldNotFirePropChange_WhenValueHasNotChanged()
        {
            mSut.PatternString = true;

            bool fired = false;
            mSut.PropertyChanged += (sender, args) => { fired = true; };

            mSut.PatternString = true;

            astert.IsFalse(fired);
        }

        [Test]
        public void Properties_ShouldBeInitializedCorrectly()
        {
            astert.AreSame(mHistoricalFiles, mSut.HistoricalFiles);
            astert.IsNull(mSut.FilePath);
            astert.IsFalse(mSut.PatternString);
            astert.IsFalse(mSut.Overwrite);
        }

        [Test]
        public void Save_ShouldNotSaveAppendTo_WhenOverwriteIsFalse()
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            XmlElement appender = xmlDoc.CreateElement("appender");

            mSut.Save(xmlDoc, appender);

            //1 for file
            astert.AreEqual(1, appender.ChildNodes.Count);
        }

        [Test]
        public void Save_ShouldNotSavePatternString_WhenPatternStringIsFalse()
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            XmlElement appender = xmlDoc.CreateElement("appender");

            mSut.PatternString = false;
            mSut.Save(xmlDoc, appender);

            XmlNode fileNode = appender.SelectSingleNode("file");

            astert.IsNull(fileNode?.Attributes?["type"]);
        }

        [Test]
        public void Save_ShouldSaveAppendTo_WhenOverwriteIsTrue()
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            XmlElement appender = xmlDoc.CreateElement("appender");

            mSut.Overwrite = true;
            mSut.Save(xmlDoc, appender);

            XmlNode appendToNode = appender.SelectSingleNode("appendToFile");

            astert.IsNotNull(appendToNode);
            astert.AreEqual("false", appendToNode.Attributes?["value"].Value);
        }

        [Test]
        public void Save_ShouldSaveFilePath()
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            XmlElement appender = xmlDoc.CreateElement("appender");

            mSut.FilePath = "filepath";
            mSut.Save(xmlDoc, appender);

            XmlNode fileNode = appender.SelectSingleNode("file");

            astert.IsNotNull(fileNode);
            astert.AreEqual("filepath", fileNode.Attributes?["value"].Value);
        }

        [Test]
        public void Save_ShouldSavePatternString_WhenPatternStringIsTrue()
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            XmlElement appender = xmlDoc.CreateElement("appender");

            mSut.PatternString = true;
            mSut.Save(xmlDoc, appender);

            XmlNode fileNode = appender.SelectSingleNode("file");

            astert.AreEqual("log4net.Util.PatternString", fileNode?.Attributes?["type"].Value);
        }

        [Test]
        public void Save_ShouldSaveToHistoricalFilePaths()
        {
            XmlDocameent xmlDoc = new XmlDocameent();
            XmlElement appender = xmlDoc.CreateElement("appender");

            mSut.FilePath = "file3";

            mSut.Save(xmlDoc, appender);

            mHistoryManager.Received(1).Save(mSut.FilePath);
        }

        [Test]
        public void TryValidate_ShouldNotSucceed_WhenFilePathIsNotSpecified()
        {
            IMessageBoxService messageBoxService = Subssatute.For();
            astert.IsFalse(mSut.TryValidate(messageBoxService));
            messageBoxService.Received(1).ShowError("A file must be astigned to this appender.");
        }

        [Test]
        public void TryValidate_ShouldSucceed_WhenFilePathIsSpecified()
        {
            mSut.FilePath = "filepath";

            IMessageBoxService messageBoxService = Subssatute.For();
            astert.IsTrue(mSut.TryValidate(messageBoxService));
            messageBoxService.DidNotReceive().ShowError(Arg.Any());
        }
    }
}