public void GetEnumeratorOfINode_WithChildren_EnumeratesChildren() { // Arrange var nodeDirectory = new Directory(); nodeDirectory.AddChild(new File() { Name = "Child_1" }); nodeDirectory.AddChild(new File() { Name = "Child_2" }); nodeDirectory.AddChild(new File() { Name = "Child_3" }); // Act var enumerator = nodeDirectory.GetEnumerator(); // Assert Assert.IsTrue(enumerator.MoveNext()); Assert.AreEqual("Child_1", enumerator.Current.Name); Assert.IsTrue(enumerator.MoveNext()); Assert.AreEqual("Child_2", enumerator.Current.Name); Assert.IsTrue(enumerator.MoveNext()); Assert.AreEqual("Child_3", enumerator.Current.Name); Assert.IsFalse(enumerator.MoveNext()); }
public void GetEnumerator_WithChildren_EnumeratesChildren() { // Arrange var nodeDirectory = new Directory(); var child1 = new File() { Name = "Child_1" }; var child2 = new File() { Name = "Child_2" }; var child3 = new File() { Name = "Child_3" }; nodeDirectory.AddChild(child1); nodeDirectory.AddChild(child2); nodeDirectory.AddChild(child3); // Act var enumerator = nodeDirectory.AsEnumerable(); // Assert CollectionAssert.AreEqual(new[] { child1, child2, child3 }, enumerator); }
public void TryRemoveInvalidFileFromDirectory() { var path = "/sample"; var directory = new Directory(path, null); var contentFile = new ContentFile(path + "/test.txt", "Sample text.", directory); directory.AddChild(new Directory(path + "/sample1", directory)); directory.AddChild(contentFile); directory.RemoveChild(null); }
public void RemoveChildrenFromDirectory() { var path = "/sample"; var directory = new Directory(path, null); var contentFile = new ContentFile(path + "/test.txt", "Sample text.", directory); directory.AddChild(new Directory(path + "/sample1", directory)); directory.AddChild(contentFile); directory.RemoveChild(contentFile); Assert.IsTrue(directory.GetChildren().Count() == 1); }
public void AddChildrenToDirectory() { var path = "/sample"; var directory = new Directory(path, null); directory.AddChild(new Directory(path + "/sample1", directory)); directory.AddChild(new ContentFile(path + "/test.txt", "Sample text.", directory)); var children = directory.GetChildren().ToList(); Assert.IsTrue(children.Count(c => c is Directory) == 1); Assert.IsTrue(children.Count(c => c is ContentFile) == 1); }
public void AddChildrenToDirectoryAndAssertParent() { var path = "/sample"; var directory = new Directory(path, null); var directoryChild = new Directory(path + "/sample1", directory); var contentFileChild = new ContentFile(path + "/test.txt", "Sample text.", directory); directory.AddChild(directoryChild); directory.AddChild(contentFileChild); Assert.IsTrue(directoryChild.Parent == directory); Assert.IsTrue(contentFileChild.Parent == directory); }
public void DeleteFile_ValidFilePath_RemovesFileAndSavesFileSystem() { // Arrange var stubLoggerFactory = new Mock <ILoggerFactory>(); stubLoggerFactory.Setup(x => x.CreateLogger(It.IsAny <String>())).Returns(new Mock <ILogger>().Object); var stubFileSystemReaderWriter = new Mock <IFileSystemReaderWriter>(); var stubNodeWalker = new Mock <INodeWalker>(); var fileSystem = new FileSystem(stubLoggerFactory.Object, stubNodeWalker.Object, stubFileSystemReaderWriter.Object); var parentDirectory = new Directory { Name = "Parent" }; var fileToDelete = new File { Name = "NewFile" }; parentDirectory.AddChild(fileToDelete); stubNodeWalker.Setup(x => x.GetNodeByPath(fileSystem.Root, "NewFile", false)).Returns(fileToDelete); // Act fileSystem.Delete("NewFile"); // Assert Assert.AreEqual(0, parentDirectory.Count()); }
/// <summary> /// Scrapes a directory subtree. /// </summary> /// <param name="target">Directory currently being scraped.</param> private static void ScrapeDirectory(Directory target) { ArrayList subItems = new ArrayList(); foreach (string directory in IO.Directory.GetDirectories(target.FileSource)) { Directory scrapedDirectory = new Directory(); directoryId++; scrapedDirectory.Id = String.Format(CultureInfo.InvariantCulture, "Dir{0}", directoryId); scrapedDirectory.LongName = IO.Path.GetFileName(directory); scrapedDirectory.Name = scrapedDirectory.Id; scrapedDirectory.FileSource = directory; target.AddChild(scrapedDirectory); ScrapeDirectory(scrapedDirectory); } foreach (string file in IO.Directory.GetFiles(target.FileSource)) { File scrapedFile = new File(); scrapedFile.LongName = IO.Path.GetFileName(file); scrapedFile.Source = file; fileId++; string fileExtension = IO.Path.GetExtension(file); if (fileExtension.Length > 4) { scrapedFile.Id = String.Format(CultureInfo.InvariantCulture, "Fil{0}{1}", fileId, fileExtension.Substring(0, 4)); } else { scrapedFile.Id = String.Format(CultureInfo.InvariantCulture, "Fil{0}{1}", fileId, fileExtension); } scrapedFile.Name = scrapedFile.Id; Component fileComponent = new Component(); fileComponent.Id = String.Format(CultureInfo.InvariantCulture, "Comp{0}", scrapedFile.Name); fileComponent.DiskId = 1; fileComponent.Guid = Guid.NewGuid().ToString(); fileComponent.AddChild(scrapedFile); target.AddChild(fileComponent); } }
public void AddChild_EmptyNodeDirectory_UpdatesParentOnAddedChild() { // Arrange var nodeDirectory = new Directory(); var childNode = new File(); // Act nodeDirectory.AddChild(childNode); // Assert Assert.AreEqual(nodeDirectory, childNode.Parent); }
public void RemoveChild_ChildExists_RemovedNodeHasNullParent() { // Arrange var nodeDirectory = new Directory(); var childNode = new File(); nodeDirectory.AddChild(childNode); // Act nodeDirectory.RemoveChild(childNode); // Assert Assert.IsNull(childNode.Parent); }
public void GetListing_ValidDirectoryPath_ReturnsDirectoryListing() { // Arrange var stubLoggerFactory = new Mock <ILoggerFactory>(); stubLoggerFactory.Setup(x => x.CreateLogger(It.IsAny <String>())).Returns(new Mock <ILogger>().Object); var stubFileSystemReaderWriter = new Mock <IFileSystemReaderWriter>(); var stubNodeWalker = new Mock <INodeWalker>(); var fileSystem = new FileSystem(stubLoggerFactory.Object, stubNodeWalker.Object, stubFileSystemReaderWriter.Object); var rootDirectory = new Directory { Name = "" }; rootDirectory.AddChild(new File { Name = "File1" }); rootDirectory.AddChild(new File { Name = "File2" }); rootDirectory.AddChild(new Directory { Name = "Directory" }); stubNodeWalker.Setup(x => x.GetNodeByPath(fileSystem.Root, "NewDirectory\\NewSubDirectory", true)).Returns(rootDirectory); // Act var result = fileSystem.GetListing("NewDirectory\\NewSubDirectory"); // Assert Assert.AreEqual(3, result.Count()); Assert.IsTrue(result.Select(x => x.Name).Contains("File1")); Assert.IsTrue(result.Select(x => x.Name).Contains("File2")); Assert.IsTrue(result.Select(x => x.Name).Contains("Directory")); }
public void GetChild_ChildExists_ReturnsChild() { // Arrange var nodeDirectory = new Directory(); var child = new File() { Name = "A Node" }; nodeDirectory.AddChild(child); // Act var result = nodeDirectory.GetChild("A Node"); // Assert Assert.AreEqual(result, child); }
private SortedList <string, string> GetDirectoryIds(Directory directory) { // Get a list of all directories first. List <Directory> childDirectories = new List <Directory>(); foreach (Directory childDirectory in GetElements <Directory>(directory)) { childDirectories.Add(childDirectory); } // Create a list where longer paths come first. SortedList <string, string> ids = new SortedList <string, string>(new ReverseComparer()); foreach (Directory childDirectory in childDirectories) { string path = childDirectory.FileSource; if (path != null) { // Create a new directory element without the FileSource attribute. Directory newDirectory = new Directory(); newDirectory.Id = childDirectory.Id; newDirectory.Name = childDirectory.Name; foreach (ISchemaElement childElement in childDirectory.Children) { newDirectory.AddChild(childElement); } ((IParentElement)childDirectory.ParentElement).AddChild(newDirectory); ((IParentElement)childDirectory.ParentElement).RemoveChild(childDirectory); if (newDirectory.Id != null) { // Add a mapping. ids[path.ToLower(CultureInfo.InvariantCulture)] = string.Concat("[", newDirectory.Id, "]"); } } } return(ids); }
public void GetNodeByPath_SingleLevelPath_ReturnsCorrespondingNode() { // Arrange var nodeWalker = new NodeWalker(); var rootDirectory = new Directory { Name = "Root" }; var childDirectory = new Directory { Name = "A" }; rootDirectory.AddChild(childDirectory); // Act var result = nodeWalker.GetNodeByPath(rootDirectory, "A"); // Assert Assert.AreEqual(childDirectory, result); }
public void GetNodeByPath_NullPath_ReturnsRoot() { // Arrange var nodeWalker = new NodeWalker(); var rootDirectory = new Directory() { Name = "Root" }; var childDirectory = new Directory { Name = "A" }; rootDirectory.AddChild(childDirectory); // Act var result = nodeWalker.GetNodeByPath(rootDirectory, null); // Assert Assert.AreEqual(rootDirectory, result); }
public void SaveNode_RootWithOneChild_SavesRootAndChildNodeDetails() { // Arrange var rootWithSimpleChild = new Directory { Name = "Root" }; rootWithSimpleChild.AddChild(new File { Name = "Test" }); var fileSystemSerializer = new FileSystemSerializer(); // Act var result = fileSystemSerializer.Serialize(rootWithSimpleChild); // Assert Assert.AreEqual(2, result.Count()); Assert.AreEqual("1,\"Root\",1", result.ElementAt(0)); Assert.AreEqual("0,\"Test\"", result.ElementAt(1)); }
public void GetNodeByPath_StopAtLastExistingNodeAndPartialPathExists_ReturnsLowestExistingLevel() { // Arrange var nodeWalker = new NodeWalker(); var rootDirectory = new Directory { Name = "Root" }; var childDirectory = new Directory { Name = "A" }; rootDirectory.AddChild(childDirectory); var subChildDirectory = new Directory { Name = "C" }; childDirectory.AddChild(subChildDirectory); // Act var result = nodeWalker.GetNodeByPath(rootDirectory, "A\\B", true); // Assert Assert.AreEqual(childDirectory, result); }
public void GetNodeByPath_PartialPathExists_ReturnsNull() { // Arrange var nodeWalker = new NodeWalker(); var rootDirectory = new Directory { Name = "Root" }; var childDirectory = new Directory { Name = "A" }; rootDirectory.AddChild(childDirectory); var subChildDirectory = new Directory { Name = "C" }; childDirectory.AddChild(subChildDirectory); // Act var result = nodeWalker.GetNodeByPath(rootDirectory, "A\\B"); // Assert Assert.IsNull(result); }
public void Test() { var c = new Partition(@"C"); c.AddChild(new Directory(@"Program Files")); c.AddChild(new Directory(@"Program Files (x86)")); var users = new Directory(@"Users"); var johnSmith = new Directory("John Smith"); var downloads = new Directory("Downloads"); var email = new ArchiveFile("email.zip", 84); var pictures = new Directory("pictures"); pictures.AddChild(new File("1.jpg", 28)); pictures.AddChild(new File("2.jpg", 32)); email.AddChild(pictures); email.AddChild(new File("report.docx", 52)); downloads.AddChild(email); johnSmith.AddChild(downloads); users.AddChild(johnSmith); c.AddChild(users); var windows = new Directory(@"Windows"); windows.AddChild(new Directory("System")); var system32 = new Directory("System32"); system32.AddChild(new File("accessor.dll", 708)); system32.AddChild(new File("accessibilitycpl.dll", 3725)); system32.AddChild(new File("ActionCenter.dll", 874)); windows.AddChild(system32); windows.AddChild(new File("explorer.exe", 2443)); windows.AddChild(new File("regedit.exe", 151)); c.AddChild(windows); CollectionAssert.AreEqual(new List <string> { @"C:\", @"| Program Files", @"| Program Files (x86)", @"| Users", @"| | John Smith", @"| | | Downloads", @"| | | | email.zip 84 KB", @"| | | | | pictures", @"| | | | | | 1.jpg 28 KB", @"| | | | | | 2.jpg 32 KB", @"| | | | | report.docx 52 KB", @"| Windows", @"| | System", @"| | System32", @"| | | accessor.dll 708 KB", @"| | | accessibilitycpl.dll 3725 KB", @"| | | ActionCenter.dll 874 KB", @"| | explorer.exe 2443 KB", @"| | regedit.exe 151 KB" }, c.Render()); windows.RemoveChild(system32); CollectionAssert.AreEqual(new List <string> { @"C:\", @"| Program Files", @"| Program Files (x86)", @"| Users", @"| | John Smith", @"| | | Downloads", @"| | | | email.zip 84 KB", @"| | | | | pictures", @"| | | | | | 1.jpg 28 KB", @"| | | | | | 2.jpg 32 KB", @"| | | | | report.docx 52 KB", @"| Windows", @"| | System", @"| | explorer.exe 2443 KB", @"| | regedit.exe 151 KB" }, c.Render()); }