public void Perform_ValidTargetFileContext_ExpectedTemporaryFileCreatedAndTargetFileContextRestoredAfterwards(bool performWithExistingTargetFile) { // Setup string writableDirectory = Path.Combine(testWorkDir, nameof(Perform_ValidTargetFileContext_ExpectedTemporaryFileCreatedAndTargetFileContextRestoredAfterwards)); string targetFilePath = Path.Combine(writableDirectory, "targetFile.txt"); string temporaryFilePath = Path.ChangeExtension(targetFilePath, temporaryFileExtension); using (new DirectoryDisposeHelper(testWorkDir, nameof(Perform_ValidTargetFileContext_ExpectedTemporaryFileCreatedAndTargetFileContextRestoredAfterwards))) { if (performWithExistingTargetFile) { File.WriteAllText(targetFilePath, testContent); } var writer = new SafeFileWriter(targetFilePath, temporaryFileExtension); // Call writer.Perform(() => { // Assert Assert.IsFalse(File.Exists(targetFilePath)); Assert.IsTrue(File.Exists(temporaryFilePath)); Assert.AreEqual(performWithExistingTargetFile ? testContent : string.Empty, ReadAllText(temporaryFilePath)); }); Assert.False(File.Exists(temporaryFilePath)); } }
public void Perform_TemporaryFileInUse_ExpectedExceptionThrown(bool performWithExistingTargetFile) { // Setup string writableDirectory = Path.Combine(testWorkDir, nameof(Perform_TemporaryFileInUse_ExpectedExceptionThrown)); string targetFilePath = Path.Combine(writableDirectory, "targetFile.txt"); string temporaryFilePath = Path.ChangeExtension(targetFilePath, temporaryFileExtension); using (new DirectoryDisposeHelper(testWorkDir, nameof(Perform_TemporaryFileInUse_ExpectedExceptionThrown))) using (var fileDisposeHelper = new FileDisposeHelper(temporaryFilePath)) { fileDisposeHelper.LockFiles(); if (performWithExistingTargetFile) { File.WriteAllText(targetFilePath, testContent); } var writer = new SafeFileWriter(targetFilePath, temporaryFileExtension); // Call var exception = Assert.Throws <IOException>(() => writer.Perform(() => {})); // Assert Assert.AreEqual("Het doelbestand is momenteel in gebruik.", exception.Message); } }
public void Perform_TargetFileReadOnly_ExpectedExceptionThrown() { // Setup string writableDirectory = Path.Combine(testWorkDir, nameof(Perform_TargetFileReadOnly_ExpectedExceptionThrown)); string targetFilePath = Path.Combine(writableDirectory, "targetFile.txt"); using (new DirectoryDisposeHelper(testWorkDir, nameof(Perform_TargetFileReadOnly_ExpectedExceptionThrown))) using (new FileDisposeHelper(targetFilePath)) { var fileInfo = new FileInfo(targetFilePath) { IsReadOnly = true }; try { var writer = new SafeFileWriter(targetFilePath, temporaryFileExtension); // Call var exception = Assert.Throws <IOException>(() => writer.Perform(() => {})); // Assert Assert.AreEqual("Onvoldoende rechten voor het schrijven van het doelbestand.", exception.Message); } finally { fileInfo.IsReadOnly = false; } } }
public void Perform_TargetFilePathTooLong_ExpectedExceptionThrown() { // Setup string writableDirectory = Path.Combine(testWorkDir, nameof(Perform_TargetFilePathTooLong_ExpectedExceptionThrown)); string targetFilePath = Path.Combine(writableDirectory, new string('x', 500) + ".txt"); using (new DirectoryDisposeHelper(testWorkDir, nameof(Perform_TargetFilePathTooLong_ExpectedExceptionThrown))) { var writer = new SafeFileWriter(targetFilePath, temporaryFileExtension); // Call var exception = Assert.Throws <IOException>(() => writer.Perform(() => {})); // Assert Assert.AreEqual("Het pad van het doelbestand is te lang.", exception.Message); } }
public void Perform_InsufficientAccessRights_ExpectedExceptionThrown() { // Setup string writableDirectory = Path.Combine(testWorkDir, nameof(Perform_InsufficientAccessRights_ExpectedExceptionThrown)); string targetFilePath = Path.Combine(writableDirectory, "targetFile.txt"); using (var directoryDisposeHelper = new DirectoryDisposeHelper(testWorkDir, nameof(Perform_InsufficientAccessRights_ExpectedExceptionThrown))) { directoryDisposeHelper.LockDirectory(FileSystemRights.Write); var writer = new SafeFileWriter(targetFilePath, temporaryFileExtension); // Call var exception = Assert.Throws <IOException>(() => writer.Perform(() => {})); // Assert Assert.AreEqual("Onvoldoende rechten voor het schrijven van het doelbestand.", exception.Message); } }