Skip to content

Commit

Permalink
Refactoring and some corrections.
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantina Chremmou <[email protected]>
  • Loading branch information
kc284 authored and danilo-delbusso committed Sep 22, 2023
1 parent 76bd441 commit 680a9cd
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 266 deletions.
138 changes: 69 additions & 69 deletions XenAdminTests/ArchiveTests/ArchiveIteratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

using System;
using System.IO;
using System.Linq;
using System.Text;
using NUnit.Framework;
using XenCenterLib;
Expand All @@ -57,7 +56,7 @@ public Stream ExtractedFileReturn
disposed = false;
}
}
public string CurrentFileNameReturn { private get; set; }
public string CurrentFileNameReturn { get; set; }
public long CurrentFileSizeReturn { private get; set; }
public DateTime ModTimeReturn { private get; set; }
public bool IsDirectoryReturn { private get; set; }
Expand Down Expand Up @@ -99,10 +98,10 @@ public override void ExtractCurrentFile(Stream extractedFileContents, Action can

public override string CurrentFileName()
{
if (String.IsNullOrEmpty(CurrentFileNameReturn))
if (string.IsNullOrEmpty(CurrentFileNameReturn))
return CurrentFileNameReturn;

return NumberOfCallsLeftReturn + CurrentFileNameReturn;
return CurrentFileNameReturn + NumberOfCallsLeftReturn;
}

public override long CurrentFileSize()
Expand All @@ -123,19 +122,15 @@ public override bool IsDirectory()
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if( !disposed )
{
if( disposing )
{
if(extractedFile != null)
extractedFile.Dispose();
}
}

if (!disposed && disposing)
extractedFile?.Dispose();
}
}
#endregion

private ArchiveIteratorFake fakeIterator;
private string tempPath;

[OneTimeSetUp]
public void Setup()
Expand All @@ -152,96 +147,101 @@ public void TearDown()
[SetUp]
public void TestSetup()
{
tempPath = null;
fakeIterator.Reset();
}

[TearDown]
public void TestTearDown()
{
if (Directory.Exists(tempPath))
Directory.Delete(tempPath, true);
}


[Test]
public void AnExceptionIsThrownForNullArgumentWhenCallingExtractAllContents()
public void TestExtractToNullDestinationPath()
{
Assert.Throws(typeof(ArgumentNullException), () => fakeIterator.ExtractAllContents(null));
}

[Test]
public void AnExceptionIsThrownForANullFileNameWhenCallingExtractAllContents()
public void TestExtractNullFile()
{
fakeIterator.CurrentFileNameReturn = null;
Assert.Throws(typeof(ArgumentNullException), () => fakeIterator.ExtractAllContents(Path.GetTempPath()));
Assert.Throws(typeof(NullReferenceException), () => fakeIterator.ExtractAllContents(Path.GetTempPath()));
}

[Test]
public void VerifyAFileIsWrittenWhenCallingExtractAllContents()

[TestCase(true, true)]
[TestCase(true, false)]
[TestCase(false, true)]
[TestCase(false, false)]
public void TestExtractFile(bool longDirectoryPath, bool longFilePath)
{
string tempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
tempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

var dirCharNumber = (longDirectoryPath ? 248 : 247) - tempPath.Length - 2;
//2 was removed for the combining slash between tempPath and dir, and the combining slash between dir and filename
var dir = new string('A', dirCharNumber);
var fileCharNumber = (longFilePath ? 260 : 259) - Path.Combine(tempPath, dir).Length - 2;
//2 was removed for the combining slash between the full dir path and filename, and the NumberOfCallsLeftReturn
var fileName = new string('B', fileCharNumber);

const int numberOfFiles = 3;
fakeIterator.NumberOfCallsLeftReturn = numberOfFiles;
fakeIterator.CurrentFileNameReturn = Path.Combine(dir, fileName);
fakeIterator.ExtractAllContents(tempPath);

//Test file has been created
string targetFile = Path.Combine(tempPath, fakeIterator.CurrentFileName());
Assert.IsTrue(File.Exists(targetFile), "File Exists");
for (var i = 0; i < 3; i++)
{
string targetFile = Path.Combine(tempPath, fakeIterator.CurrentFileNameReturn + i);

Assert.IsTrue(File.ReadAllBytes(targetFile).Length > 1, "File length > 1");
if (longDirectoryPath || longFilePath)
targetFile = StringUtility.ToLongWindowsPathUnchecked(targetFile);

//Check recursively that there are only the correct number of files
Assert.IsTrue(Directory.GetFiles(tempPath, "*.*", SearchOption.AllDirectories).Length == numberOfFiles, "File number is correct");
Assert.IsTrue(File.Exists(targetFile), "File should exist");
Assert.IsNotEmpty(File.ReadAllBytes(targetFile), "File should not be empty");

Assert.IsFalse((File.GetAttributes(targetFile) & FileAttributes.Directory) == FileAttributes.Directory,
"It should not have directory attributes");
}

Assert.IsFalse((File.GetAttributes(targetFile) & FileAttributes.Directory) == FileAttributes.Directory, "Is not a dir");
//Check recursively that there are only the correct number of files
var actualFileNumber = Directory.GetFiles(tempPath, "*.*", SearchOption.AllDirectories).Length;
Assert.AreEqual(numberOfFiles, actualFileNumber, $"There should be {numberOfFiles}");

Directory.Delete(tempPath,true);
if (longDirectoryPath || longFilePath)
tempPath = StringUtility.ToLongWindowsPathUnchecked(tempPath);
}

[Test]
public void VerifyADirectoryIsWrittenWhenCallingExtractAllContents()

[TestCase(true)]
[TestCase(false)]
public void TestExtractDirectory(bool longDirectoryPath)
{
string tempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
tempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
var dirCharNumber = (longDirectoryPath ? 248 : 247) - tempPath.Length - 2;
//2 was removed for the combining slash between tempPath and dir, and the NumberOfCallsLeftReturn
var dir = new string('A', dirCharNumber);

fakeIterator.IsDirectoryReturn = true;
fakeIterator.CurrentFileNameReturn = "FakePath" + Path.DirectorySeparatorChar;
fakeIterator.CurrentFileNameReturn = dir;
fakeIterator.ExtractAllContents(tempPath);

//Test file has been created
string targetPath = Path.Combine(tempPath, fakeIterator.CurrentFileName());
Assert.IsFalse(File.Exists(targetPath), "No files exist");
Assert.IsTrue(Directory.Exists(targetPath), "Directories exist");

//No files - just a directory
Assert.IsTrue(Directory.GetFiles(tempPath).Length < 1, "No file in the directory" );

//Check it's a directory
Assert.IsTrue((File.GetAttributes(targetPath) & FileAttributes.Directory) == FileAttributes.Directory, "Has directory attributes");

Directory.Delete(tempPath, true);
}

[TestCase(true, true)]
[TestCase(true, true)]
[TestCase(false, true)]
[TestCase(false, false)]
public void VerifyADirectoryIsWrittenWhenCallingExtractAllContentsOnLongPath(bool longDirectoryPath, bool longFilePath)
{
var rootPath = Path.GetTempPath();
var (relativeDirectoryPath, fileName) = ArchiveTestsUtils.GetDirectoryAndFilePaths(rootPath, longDirectoryPath, longFilePath);

var directoryPath = Path.Combine(rootPath, relativeDirectoryPath);
var filePath = Path.Combine(directoryPath, fileName);

var longTempPath = StringUtility.ToLongWindowsPath(filePath);
fakeIterator.IsDirectoryReturn = true;
fakeIterator.CurrentFileNameReturn = "FakePath" + Path.DirectorySeparatorChar;
fakeIterator.ExtractAllContents(longTempPath);

//Test file has been created
var targetPath = StringUtility.ToLongWindowsPath(Path.Combine(longTempPath, fakeIterator.CurrentFileName()));
Assert.IsFalse(File.Exists(targetPath), "No files exist");
Assert.IsTrue(Directory.Exists(targetPath), "Directories exist");
if (longDirectoryPath)
targetPath = StringUtility.ToLongWindowsPathUnchecked(targetPath);

//No files - just a directory
Assert.IsTrue(Directory.GetFiles(longTempPath).Length < 1, "No file in the directory");
Assert.IsFalse(File.Exists(targetPath), "Files should not exist");
Assert.IsEmpty(Directory.GetFiles(tempPath), "Directory should not have files");

//Check it's a directory
Assert.IsTrue((File.GetAttributes(targetPath) & FileAttributes.Directory) == FileAttributes.Directory, "Has directory attributes");
Assert.IsTrue(Directory.Exists(targetPath), "Directory should exist");
Assert.IsTrue((File.GetAttributes(targetPath) & FileAttributes.Directory) == FileAttributes.Directory,
"It should have directory attributes");

Directory.Delete(longTempPath, true);
if (longDirectoryPath)
tempPath = StringUtility.ToLongWindowsPathUnchecked(tempPath);
}
}
}
46 changes: 0 additions & 46 deletions XenAdminTests/ArchiveTests/ArchiveTestsUtils.cs

This file was deleted.

Loading

0 comments on commit 680a9cd

Please sign in to comment.