From 8aab88261d9f299274e0aeb9af001c03b0e36eed Mon Sep 17 00:00:00 2001 From: lukeIam <2lukeiam@gmail.com> Date: Sat, 24 Mar 2018 11:37:06 +0100 Subject: [PATCH] Add test for CLogFileRoller --- Tests/Tests.csproj | 1 + .../Log/Rolling/CLogFileRollerTest.cs | 110 ++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 Tests/VocaluxeLib/Log/Rolling/CLogFileRollerTest.cs diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 0e058173..960e30c2 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -43,6 +43,7 @@ + diff --git a/Tests/VocaluxeLib/Log/Rolling/CLogFileRollerTest.cs b/Tests/VocaluxeLib/Log/Rolling/CLogFileRollerTest.cs new file mode 100644 index 00000000..b8089613 --- /dev/null +++ b/Tests/VocaluxeLib/Log/Rolling/CLogFileRollerTest.cs @@ -0,0 +1,110 @@ +#region license +// This file is part of Vocaluxe. +// +// Vocaluxe is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Vocaluxe is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Vocaluxe. If not, see . +#endregion + +using System; +using System.IO; +using NUnit.Framework; +using VocaluxeLib.Log.Rolling; + +namespace Tests.VocaluxeLib.Log.Rolling +{ + [TestFixture] + public class CLogFileRollerTest + { + private const string _TestFileName = "Test.log"; + private string _TestFolder; + + #region Setup methods + + [SetUp] + public void SetUp() + { + _TestFolder = _GetTemporaryDirectory(); + } + + [TearDown] + public void TearDown() + { + Directory.Delete(_TestFolder, true); + } + + #endregion + + #region Tests + + [Test] + public void RollFilesTest([Range(0,4)] int numFilesExisting, [Range(0, 3)] int numOldFilesToKeep) + { + // Create existing files + if (numFilesExisting > 0) + { + CreateFile(_TestFolder, _TestFileName, 0); + } + + for (var i = 1; i < numFilesExisting; i++) + { + CreateFile(_TestFolder, GetFileName(i), i); + } + + + // Roll the files + CLogFileRoller.RollLogs(Path.Combine(_TestFolder, _TestFileName), numOldFilesToKeep); + + + // Check main file + Assert.IsFalse(File.Exists(Path.Combine(_TestFolder, _TestFileName)), "Main file was not deleted."); + //Check other files + for (int i = 1; i <= Math.Max(numOldFilesToKeep, numFilesExisting); i++) + { + var fileToCheck = Path.Combine(_TestFolder, GetFileName(i)); + if (i <= Math.Min(numFilesExisting,numOldFilesToKeep)) + { + Assert.IsTrue(File.Exists(fileToCheck), $"File {GetFileName(i)} is missing."); + Assert.AreEqual((i - 1).ToString(), File.ReadAllText(fileToCheck), "Rotation is wrong"); + } + else + { + Assert.IsFalse(File.Exists(fileToCheck)); + } + } + } + + #endregion + + #region Helper methods + + private string _GetTemporaryDirectory() + { + string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); + Directory.CreateDirectory(tempDirectory); + return tempDirectory; + } + + private void CreateFile(string folder, string file, int i) + { + File.WriteAllText(Path.Combine(folder, file), i.ToString()); + } + + private string GetFileName(int i) + { + return _TestFileName.Replace(".", $"_{i}."); + } + + #endregion + + } +}