Index: Application/Ringtoets/test/Application.Ringtoets.Integration.Test/Application.Ringtoets.Integration.Test.csproj =================================================================== diff -u -r9e7b8344cc985249f312870b9030705371c82bfe -r28ab332db2d96ad9e766f19677af1c00fc88a644 --- Application/Ringtoets/test/Application.Ringtoets.Integration.Test/Application.Ringtoets.Integration.Test.csproj (.../Application.Ringtoets.Integration.Test.csproj) (revision 9e7b8344cc985249f312870b9030705371c82bfe) +++ Application/Ringtoets/test/Application.Ringtoets.Integration.Test/Application.Ringtoets.Integration.Test.csproj (.../Application.Ringtoets.Integration.Test.csproj) (revision 28ab332db2d96ad9e766f19677af1c00fc88a644) @@ -65,10 +65,6 @@ - - {f49bd8b2-332a-4c91-a196-8cce0a2c7d98} - Core.Common.Utils - {d749ee4c-ce50-4c17-bf01-9a953028c126} Core.Common.TestUtil Index: Application/Ringtoets/test/Application.Ringtoets.Migration.Core.Test/RingtoetsCreateScriptTest.cs =================================================================== diff -u -r9d68a734d8da9bfadd3bdd349ddb56fb70d461ec -r28ab332db2d96ad9e766f19677af1c00fc88a644 --- Application/Ringtoets/test/Application.Ringtoets.Migration.Core.Test/RingtoetsCreateScriptTest.cs (.../RingtoetsCreateScriptTest.cs) (revision 9d68a734d8da9bfadd3bdd349ddb56fb70d461ec) +++ Application/Ringtoets/test/Application.Ringtoets.Migration.Core.Test/RingtoetsCreateScriptTest.cs (.../RingtoetsCreateScriptTest.cs) (revision 28ab332db2d96ad9e766f19677af1c00fc88a644) @@ -103,8 +103,7 @@ const string query = ";"; string version = RingtoetsVersionHelper.GetCurrentDatabaseVersion(); - string targetFilename = Path.GetRandomFileName(); - string filePath = TestHelper.GetScratchPadPath(targetFilename); + string filePath = TestHelper.GetScratchPadPath(nameof(CreateEmptyVersionedFile_FileDoesNotExist_ReturnsVersionedFile)); var createScript = new RingtoetsCreateScript(version, query); // Call @@ -122,8 +121,7 @@ const string query = ";"; string version = RingtoetsVersionHelper.GetCurrentDatabaseVersion(); - string filename = Path.GetRandomFileName(); - string filePath = TestHelper.GetScratchPadPath(filename); + string filePath = TestHelper.GetScratchPadPath(nameof(CreateEmptyVersionedFile_FileExistsButNotWritable_ThrowsArgumentException)); var createScript = new RingtoetsCreateScript(version, query); using (new FileDisposeHelper(filePath)) @@ -148,8 +146,7 @@ const string query = "THIS WILL FAIL"; string version = RingtoetsVersionHelper.GetCurrentDatabaseVersion(); - string targetFilename = Path.GetRandomFileName(); - string filePath = TestHelper.GetScratchPadPath(targetFilename); + string filePath = TestHelper.GetScratchPadPath(nameof(CreateEmptyVersionedFile_QueryFails_ThrowsCriticalMigrationException)); var createScript = new RingtoetsCreateScript(version, query); // Call Index: Application/Ringtoets/test/Application.Ringtoets.Migration.Core.Test/RingtoetsDatabaseFileTest.cs =================================================================== diff -u -r9d68a734d8da9bfadd3bdd349ddb56fb70d461ec -r28ab332db2d96ad9e766f19677af1c00fc88a644 --- Application/Ringtoets/test/Application.Ringtoets.Migration.Core.Test/RingtoetsDatabaseFileTest.cs (.../RingtoetsDatabaseFileTest.cs) (revision 9d68a734d8da9bfadd3bdd349ddb56fb70d461ec) +++ Application/Ringtoets/test/Application.Ringtoets.Migration.Core.Test/RingtoetsDatabaseFileTest.cs (.../RingtoetsDatabaseFileTest.cs) (revision 28ab332db2d96ad9e766f19677af1c00fc88a644) @@ -51,29 +51,21 @@ public void Constructor_FileNotWritable_ThrowsArgumentException() { // Setup - string filename = Path.GetRandomFileName(); - string filePath = TestHelper.GetScratchPadPath(filename); + string filePath = TestHelper.GetScratchPadPath($"{nameof(RingtoetsDatabaseFileTest)}.{nameof(Constructor_FileNotWritable_ThrowsArgumentException)}"); - using (new FileDisposeHelper(filePath)) + using (var helper = new FileDisposeHelper(filePath)) { - FileAttributes attributes = File.GetAttributes(filePath); - File.SetAttributes(filePath, attributes | FileAttributes.ReadOnly); - try - { - // Call - TestDelegate call = () => - { - using (new RingtoetsDatabaseFile(filePath)) {} - }; + helper.LockFiles(); - // Assert - string expectedMessage = $"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'."; - TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, expectedMessage); - } - finally + // Call + TestDelegate call = () => { - File.SetAttributes(filePath, attributes); - } + using (new RingtoetsDatabaseFile(filePath)) {} + }; + + // Assert + string expectedMessage = $"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'."; + TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, expectedMessage); } } @@ -85,7 +77,7 @@ { // Setup string filename = Path.GetRandomFileName(); - string filePath = TestHelper.GetScratchPadPath(filename); + string filePath = TestHelper.GetScratchPadPath(nameof(ExecuteQuery_QueryIsNullOrWhiteSpace_ThrowsArgumentException) + filename); using (new FileDisposeHelper(filePath)) using (var databaseFile = new RingtoetsDatabaseFile(filePath)) @@ -105,7 +97,7 @@ { // Setup string filename = Path.GetRandomFileName(); - string filePath = TestHelper.GetScratchPadPath(filename); + string filePath = TestHelper.GetScratchPadPath(nameof(ExecuteQuery_InvalidQuery_ThrowsSQLiteException) + filename); using (new FileDisposeHelper(filePath)) using (var databaseFile = new RingtoetsDatabaseFile(filePath)) Index: Application/Ringtoets/test/Application.Ringtoets.Migration.Core.Test/RingtoetsUpgradeScriptTest.cs =================================================================== diff -u -r9d68a734d8da9bfadd3bdd349ddb56fb70d461ec -r28ab332db2d96ad9e766f19677af1c00fc88a644 --- Application/Ringtoets/test/Application.Ringtoets.Migration.Core.Test/RingtoetsUpgradeScriptTest.cs (.../RingtoetsUpgradeScriptTest.cs) (revision 9d68a734d8da9bfadd3bdd349ddb56fb70d461ec) +++ Application/Ringtoets/test/Application.Ringtoets.Migration.Core.Test/RingtoetsUpgradeScriptTest.cs (.../RingtoetsUpgradeScriptTest.cs) (revision 28ab332db2d96ad9e766f19677af1c00fc88a644) @@ -141,24 +141,27 @@ string fromVersion = RingtoetsVersionHelper.GetCurrentDatabaseVersion(); string toVersion = RingtoetsVersionHelper.GetCurrentDatabaseVersion(); + string fileLocation = TestHelper.GetScratchPadPath(nameof(Upgrade_UpgradeFails_ThrowsCriticalMigrationException)); var upgradeScript = new RingtoetsUpgradeScript(fromVersion, toVersion, "THIS WILL FAIL"); - // Call - TestDelegate call = () => upgradeScript.Upgrade("c:\\file.ext", "c:\\file.ext"); + using (new FileDisposeHelper(fileLocation)) + { + // Call + TestDelegate call = () => upgradeScript.Upgrade(fileLocation, fileLocation); - // Assert - CriticalMigrationException exception = Assert.Throws(call); - Assert.AreEqual($"Het migreren van het Ringtoets projectbestand van versie '{fromVersion}' naar '{fromVersion}' is mislukt.", - exception.Message); - Assert.IsInstanceOf(exception.InnerException); + // Assert + CriticalMigrationException exception = Assert.Throws(call); + Assert.AreEqual($"Het migreren van het Ringtoets projectbestand van versie '{fromVersion}' naar '{fromVersion}' is mislukt.", + exception.Message); + Assert.IsInstanceOf(exception.InnerException); + } } [Test] public void Upgrade_ValidParameters_ExpectedProperties() { // Setup - string filename = Path.GetRandomFileName(); - string filePath = TestHelper.GetScratchPadPath(filename); + string filePath = TestHelper.GetScratchPadPath(nameof(Upgrade_ValidParameters_ExpectedProperties)); string fromVersion = RingtoetsVersionHelper.GetCurrentDatabaseVersion(); string toVersion = RingtoetsVersionHelper.GetCurrentDatabaseVersion(); Index: Application/Ringtoets/test/Application.Ringtoets.Migration.Core.Test/RingtoetsVersionedFileTest.cs =================================================================== diff -u -r9d68a734d8da9bfadd3bdd349ddb56fb70d461ec -r28ab332db2d96ad9e766f19677af1c00fc88a644 --- Application/Ringtoets/test/Application.Ringtoets.Migration.Core.Test/RingtoetsVersionedFileTest.cs (.../RingtoetsVersionedFileTest.cs) (revision 9d68a734d8da9bfadd3bdd349ddb56fb70d461ec) +++ Application/Ringtoets/test/Application.Ringtoets.Migration.Core.Test/RingtoetsVersionedFileTest.cs (.../RingtoetsVersionedFileTest.cs) (revision 28ab332db2d96ad9e766f19677af1c00fc88a644) @@ -48,7 +48,7 @@ public void Constructor_ValidfilePath_ExpectedProperties() { // Setup - string filePath = "c:\\file.ext"; + string filePath = TestHelper.GetTestDataPath(nameof(Constructor_ValidfilePath_ExpectedProperties)); // Call var versionedFile = new RingtoetsVersionedFile(filePath); @@ -61,8 +61,7 @@ public void GetVersion_FileDoesNotExist_ThrowsCriticalFileReadException() { // Setup - string file = Path.GetRandomFileName(); - string filePath = TestHelper.GetTestDataPath(TestDataPath.Application.Ringtoets.Migration, file); + string filePath = TestHelper.GetTestDataPath(nameof(GetVersion_FileDoesNotExist_ThrowsCriticalFileReadException)); var sourceFile = new RingtoetsVersionedFile(filePath); Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/BackedUpFileWriterTest.cs =================================================================== diff -u -r9e7b8344cc985249f312870b9030705371c82bfe -r28ab332db2d96ad9e766f19677af1c00fc88a644 --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/BackedUpFileWriterTest.cs (.../BackedUpFileWriterTest.cs) (revision 9e7b8344cc985249f312870b9030705371c82bfe) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/BackedUpFileWriterTest.cs (.../BackedUpFileWriterTest.cs) (revision 28ab332db2d96ad9e766f19677af1c00fc88a644) @@ -31,20 +31,8 @@ [TestFixture] public class BackedUpFileWriterTest { - private readonly string testWorkDir = TestHelper.GetScratchPadPath("SafeOverwriteFileHelperTest"); + private readonly string testWorkDir = TestHelper.GetScratchPadPath(nameof(BackedUpFileWriterTest)); - [OneTimeSetUp] - public void SetUpFixture() - { - Directory.CreateDirectory(testWorkDir); - } - - [OneTimeTearDown] - public void TearDownFixture() - { - Directory.Delete(testWorkDir); - } - [Test] [TestCase("")] [TestCase(null)] @@ -63,21 +51,19 @@ public void Perform_ValidFile_TemporaryFileCreatedFromOriginalAndDeletedAfterwards() { // Setup - var writableDirectory = Path.Combine(testWorkDir, "Writable"); - var filePath = Path.Combine(writableDirectory, "iDoExist.txt"); - var temporaryFilePath = filePath + "~"; - var testContent = "Some test text to write into file."; + string writableDirectory = Path.Combine(testWorkDir, nameof(Perform_ValidFile_TemporaryFileCreatedFromOriginalAndDeletedAfterwards)); + string filePath = Path.Combine(writableDirectory, "iDoExist.txt"); + string temporaryFilePath = filePath + "~"; + const string testContent = "Some test text to write into file."; - Directory.CreateDirectory(writableDirectory); - File.WriteAllText(filePath, testContent); - - var writer = new BackedUpFileWriter(filePath); - - try + using (new DirectoryDisposeHelper(writableDirectory, nameof(Perform_ValidFile_TemporaryFileCreatedFromOriginalAndDeletedAfterwards))) + using (new FileDisposeHelper(filePath)) + using (new FileDisposeHelper(temporaryFilePath)) { - // Precondition - Assert.IsTrue(File.Exists(filePath)); + File.WriteAllText(filePath, testContent); + var writer = new BackedUpFileWriter(filePath); + // Call writer.Perform(() => { @@ -88,32 +74,27 @@ }); Assert.False(File.Exists(temporaryFilePath)); } - finally - { - Directory.Delete(writableDirectory, true); - } } [Test] public void Perform_ActionThrowsExceptionValidPathFileExists_OriginalFileRevertedAndExceptionThrown() { // Setup - var writableDirectory = Path.Combine(testWorkDir, "Writable"); - var filePath = Path.Combine(writableDirectory, "iDoExist.txt"); - var temporaryFilePath = filePath + "~"; - var testContent = "Some test text to write into file."; + string writableDirectory = Path.Combine(testWorkDir, nameof(Perform_ActionThrowsExceptionValidPathFileExists_OriginalFileRevertedAndExceptionThrown)); + string filePath = Path.Combine(writableDirectory, "iDoExist.txt"); + string temporaryFilePath = filePath + "~"; + const string testContent = "Some test text to write into file."; - Directory.CreateDirectory(writableDirectory); - File.WriteAllText(filePath, testContent); + using (new DirectoryDisposeHelper(writableDirectory, nameof(Perform_ActionThrowsExceptionValidPathFileExists_OriginalFileRevertedAndExceptionThrown))) + { + File.WriteAllText(filePath, testContent); - var writer = new BackedUpFileWriter(filePath); - var exception = new IOException(); + var writer = new BackedUpFileWriter(filePath); + var exception = new IOException(); - // Precondition - Assert.IsTrue(File.Exists(filePath)); + // Precondition + Assert.IsTrue(File.Exists(filePath)); - try - { // Call TestDelegate test = () => writer.Perform(() => { throw exception; }); @@ -124,159 +105,113 @@ Assert.IsTrue(File.Exists(filePath)); Assert.AreEqual(testContent, File.ReadAllText(filePath)); } - finally - { - Directory.Delete(writableDirectory, true); - } } [Test] public void Perform_ValidPathFileExistsTemporaryFileExistsAndCannotBeDeleted_ThrowsIOException() { // Setup - var filePath = Path.Combine(testWorkDir, "iDoExist.txt"); + string filePath = Path.Combine(testWorkDir, "iDoExist.txt"); var temporaryFilePath = filePath + "~"; - using (File.Create(filePath)) {} - var temporaryFileStream = File.Create(temporaryFilePath); + using (new FileDisposeHelper(filePath)) + using (var tempFileHelper = new FileDisposeHelper(temporaryFilePath)) + { + tempFileHelper.LockFiles(); - var writer = new BackedUpFileWriter(filePath); + var writer = new BackedUpFileWriter(filePath); - // Precondition - Assert.IsTrue(File.Exists(filePath)); - Assert.IsTrue(File.Exists(temporaryFilePath)); + // Call + TestDelegate test = () => writer.Perform(() => { }); - // Call - TestDelegate test = () => writer.Perform(() => { }); - - try - { // Assert var message = Assert.Throws(test).Message; var expectedMessage = $"Er bestaat al een tijdelijk bestand ({temporaryFilePath}) dat niet verwijderd kan worden. Dit bestand dient handmatig verwijderd te worden."; Assert.AreEqual(message, expectedMessage); - temporaryFileStream.Dispose(); } - finally - { - File.Delete(filePath); - File.Delete(temporaryFilePath); - } } [Test] public void Perform_ValidPathFileExistsDirectoryNotWritable_ThrowsIOException() { // Setup - var notWritableDirectory = Path.Combine(testWorkDir, "NotWritable"); - var filePath = Path.Combine(notWritableDirectory, "iDoExist.txt"); + string notWritableDirectory = Path.Combine(testWorkDir, nameof(Perform_ValidPathFileExistsDirectoryNotWritable_ThrowsIOException)); + string filePath = Path.Combine(notWritableDirectory, "iDoExist.txt"); - Directory.CreateDirectory(notWritableDirectory); - using (File.Create(filePath)) {} - var writer = new BackedUpFileWriter(filePath); + using (var directoryHelper = new DirectoryDisposeHelper(testWorkDir, nameof(Perform_ValidPathFileExistsDirectoryNotWritable_ThrowsIOException))) + using (new FileDisposeHelper(filePath)) + { + var writer = new BackedUpFileWriter(filePath); - // Precondition - Assert.IsTrue(File.Exists(filePath)); + // Call + TestDelegate test = () => writer.Perform(() => { }); - // Call - TestDelegate test = () => writer.Perform(() => { }); - - try - { - using (new DirectoryPermissionsRevoker(notWritableDirectory, FileSystemRights.Write)) - { - // Assert - var expectedMessage = $"Kan geen tijdelijk bestand maken van het originele bestand ({filePath})."; - var message = Assert.Throws(test).Message; - Assert.AreEqual(expectedMessage, message); - } + directoryHelper.LockDirectory(FileSystemRights.Write); + // Assert + var expectedMessage = $"Kan geen tijdelijk bestand maken van het originele bestand ({filePath})."; + var message = Assert.Throws(test).Message; + Assert.AreEqual(expectedMessage, message); } - finally - { - Directory.Delete(notWritableDirectory, true); - } } [Test] public void Perform_TargetFileDoesNotExistDeleteRightsRevoked_DoesNotThrow() { // Setup - var noAccessDirectory = Path.Combine(testWorkDir, "NoAccess"); - var filePath = Path.Combine(noAccessDirectory, "iDoNotExist.txt"); + string noAccessDirectory = Path.Combine(testWorkDir, nameof(Perform_TargetFileDoesNotExistDeleteRightsRevoked_DoesNotThrow)); + string filePath = Path.Combine(noAccessDirectory, "iDoNotExist.txt"); - Directory.CreateDirectory(noAccessDirectory); - var helper = new BackedUpFileWriter(filePath); + using (var directoryHelper = new DirectoryDisposeHelper(testWorkDir, nameof(Perform_TargetFileDoesNotExistDeleteRightsRevoked_DoesNotThrow))) + { + var helper = new BackedUpFileWriter(filePath); - // Call - TestDelegate test = () => helper.Perform(() => { }); + // Call + TestDelegate test = () => helper.Perform(() => { }); - try - { - using (new DirectoryPermissionsRevoker(noAccessDirectory, FileSystemRights.Delete)) - { - // Assert - Assert.DoesNotThrow(test); - } + directoryHelper.LockDirectory(FileSystemRights.Delete); + // Assert + Assert.DoesNotThrow(test); } - finally - { - Directory.Delete(noAccessDirectory, true); - } } [Test] public void Perform_TargetFileExistsCannotDeleteFile_ThrowsCannotDeleteBackupFileException() { // Setup - var noAccessDirectory = Path.Combine(testWorkDir, "Access"); - var filePath = Path.Combine(noAccessDirectory, "iDoExist.txt"); - var temporaryFilePath = filePath + "~"; + string filePath = Path.Combine(testWorkDir, nameof(Perform_TargetFileExistsCannotDeleteFile_ThrowsCannotDeleteBackupFileException)); + string temporaryFilePath = filePath + "~"; - Directory.CreateDirectory(noAccessDirectory); - using (File.Create(filePath)) {} - - var helper = new BackedUpFileWriter(filePath); - - FileStream fileStream = null; - - try + using (new FileDisposeHelper(filePath)) + using (var temporaryFileHelper = new FileDisposeHelper(temporaryFilePath)) { + var helper = new BackedUpFileWriter(filePath); + // Call - TestDelegate test = () => helper.Perform(() => { fileStream = File.Open(temporaryFilePath, FileMode.Open); }); + TestDelegate test = () => helper.Perform(() => { temporaryFileHelper.LockFiles(); }); // Assert var expectedMessage = $"Kan het tijdelijke bestand ({temporaryFilePath}) niet opruimen. Het tijdelijke bestand dient handmatig verwijderd te worden."; var message = Assert.Throws(test).Message; Assert.AreEqual(expectedMessage, message); } - finally - { - fileStream?.Dispose(); - Directory.Delete(noAccessDirectory, true); - } } [Test] public void Perform_ActionThrowsExceptionTargetFileExistsCannotDeleteFile_ThrowsIOException() { // Setup - var noAccessDirectory = Path.Combine(testWorkDir, "Access"); - var filePath = Path.Combine(noAccessDirectory, "iDoExist.txt"); - var temporaryFilePath = filePath + "~"; + string filePath = Path.Combine(testWorkDir, nameof(Perform_ActionThrowsExceptionTargetFileExistsCannotDeleteFile_ThrowsIOException)); + string temporaryFilePath = filePath + "~"; - Directory.CreateDirectory(noAccessDirectory); - using (File.Create(filePath)) {} - - var helper = new BackedUpFileWriter(filePath); - - FileStream fileStream = null; - - try + using (new FileDisposeHelper(filePath)) + using (var temporaryFileHelper = new FileDisposeHelper(temporaryFilePath)) { + var helper = new BackedUpFileWriter(filePath); + // Call TestDelegate test = () => helper.Perform(() => { - fileStream = File.Open(temporaryFilePath, FileMode.Open); + temporaryFileHelper.LockFiles(); throw new Exception(); }); @@ -285,32 +220,24 @@ var message = Assert.Throws(test).Message; Assert.AreEqual(expectedMessage, message); } - finally - { - fileStream?.Dispose(); - Directory.Delete(noAccessDirectory, true); - } } [Test] public void Perform_ActionThrowsExceptionTargetFileExistsCannotMoveFile_ThrowsIOException() { // Setup - var noAccessDirectory = Path.Combine(testWorkDir, "NoAccess"); - var filePath = Path.Combine(noAccessDirectory, "iDoExist.txt"); + string noAccessDirectory = Path.Combine(testWorkDir, nameof(Perform_ActionThrowsExceptionTargetFileExistsCannotMoveFile_ThrowsIOException)); + string filePath = Path.Combine(noAccessDirectory, "iDoExist.txt"); - Directory.CreateDirectory(noAccessDirectory); - using (File.Create(filePath)) {} - - var helper = new BackedUpFileWriter(filePath); - - DirectoryPermissionsRevoker fileRightsHelper = null; - try + using (var directoryHelper = new DirectoryDisposeHelper(testWorkDir, nameof(Perform_ActionThrowsExceptionTargetFileExistsCannotMoveFile_ThrowsIOException))) + using (new FileDisposeHelper(filePath)) { + var helper = new BackedUpFileWriter(filePath); + // Call TestDelegate test = () => helper.Perform(() => { - fileRightsHelper = new DirectoryPermissionsRevoker(noAccessDirectory, FileSystemRights.FullControl); + directoryHelper.LockDirectory(FileSystemRights.FullControl); throw new Exception(); }); @@ -319,11 +246,18 @@ var message = Assert.Throws(test).Message; Assert.AreEqual(expectedMessage, message); } - finally - { - fileRightsHelper?.Dispose(); - Directory.Delete(noAccessDirectory, true); - } } + + [OneTimeSetUp] + public void SetUpFixture() + { + Directory.CreateDirectory(testWorkDir); + } + + [OneTimeTearDown] + public void TearDownFixture() + { + Directory.Delete(testWorkDir, true); + } } } \ No newline at end of file Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/RingtoetsDatabaseCreatorTest.cs =================================================================== diff -u -r9e7b8344cc985249f312870b9030705371c82bfe -r28ab332db2d96ad9e766f19677af1c00fc88a644 --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/RingtoetsDatabaseCreatorTest.cs (.../RingtoetsDatabaseCreatorTest.cs) (revision 9e7b8344cc985249f312870b9030705371c82bfe) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/RingtoetsDatabaseCreatorTest.cs (.../RingtoetsDatabaseCreatorTest.cs) (revision 28ab332db2d96ad9e766f19677af1c00fc88a644) @@ -31,7 +31,7 @@ [Explicit("Creates a new Ringtoets.rtd file in the root of the Application.Ringtoets.Storage")] public class RingtoetsDatabaseCreatorTest { - private static readonly string rootPath = TestHelper.GetScratchPadPath(); + private static readonly string pathToStorageProject = Path.Combine(Path.GetDirectoryName(TestHelper.SolutionRoot), "Application", "Ringtoets", "src", "Application.Ringtoets.Storage"); /// /// Creates a new Ringtoets.rtd file in the root of the , @@ -41,7 +41,7 @@ public void RingtoetsDatabaseCreator_Explicit_CreatesRingtoetsProjectDatabaseFile() { // Setup - var storageFile = Path.Combine(rootPath, "Ringtoets.rtd"); + var storageFile = Path.Combine(pathToStorageProject, "Ringtoets.rtd"); if (File.Exists(storageFile)) { TestDelegate precondition = () => File.Delete(storageFile); Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/StorageSqliteCreatorTest.cs =================================================================== diff -u -rd18995d638a6c3f99cb7a8419107bb4148420301 -r28ab332db2d96ad9e766f19677af1c00fc88a644 --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/StorageSqliteCreatorTest.cs (.../StorageSqliteCreatorTest.cs) (revision d18995d638a6c3f99cb7a8419107bb4148420301) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/StorageSqliteCreatorTest.cs (.../StorageSqliteCreatorTest.cs) (revision 28ab332db2d96ad9e766f19677af1c00fc88a644) @@ -105,19 +105,16 @@ [Test] public void CreateDatabaseStructure_ValidExistingFile_ThrowsStorageException() { - string tempRingtoetsFile = TestHelper.GetScratchPadPath("tempProjectFile.rtd"); - using (new FileDisposeHelper(tempRingtoetsFile)) + string tempRingtoetsFile = TestHelper.GetScratchPadPath(nameof(CreateDatabaseStructure_ValidExistingFile_ThrowsStorageException)); + using (var disposeHelper = new FileDisposeHelper(tempRingtoetsFile)) { + disposeHelper.LockFiles(); + // Call TestDelegate call = () => StorageSqliteCreator.CreateDatabaseStructure(tempRingtoetsFile); - ArgumentException exception; - using (File.Create(tempRingtoetsFile)) // Locks file - { - exception = Assert.Throws(call); - } - // Assert + var exception = Assert.Throws(call); var expectedMessage = $@"File '{tempRingtoetsFile}' already exists."; Assert.AreEqual(expectedMessage, exception.Message); } Index: Application/Ringtoets/test/Application.Ringtoets.Storage.TestUtil.Test/SqLiteDatabaseHelperTest.cs =================================================================== diff -u -r27a3de13b3b94459fc79664d521de3790dd6b0df -r28ab332db2d96ad9e766f19677af1c00fc88a644 --- Application/Ringtoets/test/Application.Ringtoets.Storage.TestUtil.Test/SqLiteDatabaseHelperTest.cs (.../SqLiteDatabaseHelperTest.cs) (revision 27a3de13b3b94459fc79664d521de3790dd6b0df) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.TestUtil.Test/SqLiteDatabaseHelperTest.cs (.../SqLiteDatabaseHelperTest.cs) (revision 28ab332db2d96ad9e766f19677af1c00fc88a644) @@ -48,7 +48,7 @@ public void CreateCorruptDatabaseFile_ValidArguments_CreatedDatabaseFile() { // Setup - string validPath = TestHelper.GetScratchPadPath("tempFile.rtd"); + string validPath = TestHelper.GetScratchPadPath(nameof(CreateCorruptDatabaseFile_ValidArguments_CreatedDatabaseFile)); using (new FileDisposeHelper(validPath)) { @@ -122,7 +122,7 @@ public void CreateCompleteDatabaseFileEmpty_ValidArguments_CreatedDatabaseFile() { // Setup - string validPath = TestHelper.GetScratchPadPath("tempFile.rtd"); + string validPath = TestHelper.GetScratchPadPath(nameof(CreateCompleteDatabaseFileEmpty_ValidArguments_CreatedDatabaseFile)); using (new FileDisposeHelper(validPath)) { @@ -172,7 +172,7 @@ public void CreateDatabaseFile_ValidPathInvalidScript_ThrowsSQLiteException() { // Setup - string validPath = TestHelper.GetScratchPadPath("tempFile.rtd"); + string validPath = TestHelper.GetScratchPadPath(nameof(CreateDatabaseFile_ValidPathInvalidScript_ThrowsSQLiteException)); const string invalidScript = "SELECT '' FROM *;"; using (new FileDisposeHelper(validPath)) @@ -189,7 +189,7 @@ public void CreateDatabaseFile_ValidPathValidScript_CreatesValidFile() { // Setup - string validPath = TestHelper.GetScratchPadPath("tempFile.rtd"); + string validPath = TestHelper.GetScratchPadPath(nameof(CreateDatabaseFile_ValidPathValidScript_CreatesValidFile)); const string validScript = "select * from sqlite_master;"; using (new FileDisposeHelper(validPath)) @@ -207,7 +207,7 @@ public void CreateDatabaseFile_FileAlreadyExists_OverwriteFile() { // Setup - string validPath = TestHelper.GetScratchPadPath("tempFile.rtd"); + string validPath = TestHelper.GetScratchPadPath(nameof(CreateDatabaseFile_FileAlreadyExists_OverwriteFile)); const string validScript = "select * from sqlite_master;"; using (new FileDisposeHelper(validPath)) @@ -247,7 +247,7 @@ public void CreateValidRingtoetsDatabase_ValidArguments_SavesProjectToFile() { // Setup - string validPath = TestHelper.GetScratchPadPath("tempFile.rtd"); + string validPath = TestHelper.GetScratchPadPath(nameof(CreateValidRingtoetsDatabase_ValidArguments_SavesProjectToFile)); var project = new RingtoetsProject(); FileDisposeHelper fileDisposeHelper = new FileDisposeHelper(validPath); Index: Migration/Scripts/test/Migration.Scripts.Data.Test/CreateScriptTest.cs =================================================================== diff -u -rd18995d638a6c3f99cb7a8419107bb4148420301 -r28ab332db2d96ad9e766f19677af1c00fc88a644 --- Migration/Scripts/test/Migration.Scripts.Data.Test/CreateScriptTest.cs (.../CreateScriptTest.cs) (revision d18995d638a6c3f99cb7a8419107bb4148420301) +++ Migration/Scripts/test/Migration.Scripts.Data.Test/CreateScriptTest.cs (.../CreateScriptTest.cs) (revision 28ab332db2d96ad9e766f19677af1c00fc88a644) @@ -62,8 +62,7 @@ // Setup const string version = "Valid version"; - string targetFilename = Path.GetRandomFileName(); - string filePath = TestHelper.GetScratchPadPath(targetFilename); + string filePath = TestHelper.GetScratchPadPath(nameof(CreateEmptyVersionedFile_FileDoesNotExist_ReturnsVersionedFile)); var createScript = new TestCreateScript(version); // Call @@ -98,22 +97,19 @@ // Setup const string version = "Valid version"; - string filename = Path.GetRandomFileName(); - string filePath = TestHelper.GetScratchPadPath(filename); + string filePath = TestHelper.GetScratchPadPath(nameof(CreateEmptyVersionedFile_FileExistsButNotWritable_ThrowsArgumentException)); var createScript = new TestCreateScript(version); - using (new FileDisposeHelper(filePath)) + using (var disposeHelper = new FileDisposeHelper(filePath)) { - FileAttributes attributes = File.GetAttributes(filePath); - File.SetAttributes(filePath, attributes | FileAttributes.ReadOnly); + disposeHelper.LockFiles(); // Call TestDelegate call = () => createScript.CreateEmptyVersionedFile(filePath); // Assert ArgumentException exception = Assert.Throws(call); Assert.AreEqual("path", exception.ParamName); - File.SetAttributes(filePath, attributes); } } } Index: Migration/Scripts/test/Migration.Scripts.Data.Test/FileMigrationScriptTest.cs =================================================================== diff -u -rc7bd36bfc84e3d9111ee0676888bb58a054bad2a -r28ab332db2d96ad9e766f19677af1c00fc88a644 --- Migration/Scripts/test/Migration.Scripts.Data.Test/FileMigrationScriptTest.cs (.../FileMigrationScriptTest.cs) (revision c7bd36bfc84e3d9111ee0676888bb58a054bad2a) +++ Migration/Scripts/test/Migration.Scripts.Data.Test/FileMigrationScriptTest.cs (.../FileMigrationScriptTest.cs) (revision 28ab332db2d96ad9e766f19677af1c00fc88a644) @@ -20,7 +20,6 @@ // All rights reserved. using System; -using System.IO; using Core.Common.TestUtil; using Migration.Scripts.Data.TestUtil; using NUnit.Framework; @@ -105,13 +104,14 @@ var upgradeScript = new TestUpgradeScript("1", "2"); var migrationScript = new FileMigrationScript(createScript, upgradeScript); - // Call - IVersionedFile upgradedFile = migrationScript.Upgrade(versionedFile); + using (new FileDisposeHelper(filePath)) + { + // Call + IVersionedFile upgradedFile = migrationScript.Upgrade(versionedFile); - // Assert - Assert.IsNotNull(upgradedFile); - File.Delete(upgradedFile.Location); - + // Assert + Assert.IsNotNull(upgradedFile); + } mockRepository.VerifyAll(); } } Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/ReferenceLines/ReferenceLineExporterTest.cs =================================================================== diff -u -rd18995d638a6c3f99cb7a8419107bb4148420301 -r28ab332db2d96ad9e766f19677af1c00fc88a644 --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/ReferenceLines/ReferenceLineExporterTest.cs (.../ReferenceLineExporterTest.cs) (revision d18995d638a6c3f99cb7a8419107bb4148420301) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/ReferenceLines/ReferenceLineExporterTest.cs (.../ReferenceLineExporterTest.cs) (revision 28ab332db2d96ad9e766f19677af1c00fc88a644) @@ -39,7 +39,7 @@ { // Setup var referenceLine = new ReferenceLine(); - string filePath = TestHelper.GetScratchPadPath("test.shp"); + string filePath = TestHelper.GetScratchPadPath(nameof(ParameteredConstructor_ValidParameters_ExpectedValues)); // Call var referenceLineExporter = new ReferenceLineExporter(referenceLine, "anId", filePath); @@ -72,25 +72,19 @@ new Point2D(11.11, 22.22) }); - string directoryPath = TestHelper.GetScratchPadPath("Export_ValidData_ReturnTrue"); - Directory.CreateDirectory(directoryPath); - string filePath = Path.Combine(directoryPath, "test.shp"); + string directoryPath = TestHelper.GetScratchPadPath(nameof(Export_ValidData_ReturnTrue)); + using (new DirectoryDisposeHelper(TestHelper.GetScratchPadPath(), nameof(Export_ValidData_ReturnTrue))) + { + string filePath = Path.Combine(directoryPath, "test.shp"); - var exporter = new ReferenceLineExporter(referenceLine, "anId", filePath); + var exporter = new ReferenceLineExporter(referenceLine, "anId", filePath); - bool isExported; - try - { // Call - isExported = exporter.Export(); - } - finally - { - Directory.Delete(directoryPath, true); - } + bool isExported = exporter.Export(); - // Assert - Assert.IsTrue(isExported); + // Assert + Assert.IsTrue(isExported); + } } [Test] @@ -104,30 +98,24 @@ new Point2D(11.11, 22.22) }); - string directoryPath = TestHelper.GetScratchPadPath("Export_InvalidDirectoryRights_LogErrorAndReturnFalse"); - Directory.CreateDirectory(directoryPath); - string filePath = Path.Combine(directoryPath, "test.shp"); + string directoryPath = TestHelper.GetScratchPadPath(nameof(Export_InvalidDirectoryRights_LogErrorAndReturnFalse)); + using (var disposeHelper = new DirectoryDisposeHelper(TestHelper.GetScratchPadPath(), nameof(Export_InvalidDirectoryRights_LogErrorAndReturnFalse))) + { + string filePath = Path.Combine(directoryPath, "test.shp"); + var exporter = new ReferenceLineExporter(referenceLine, "anId", filePath); - var exporter = new ReferenceLineExporter(referenceLine, "anId", filePath); + disposeHelper.LockDirectory(FileSystemRights.Write); + var isExported = true; - try - { - using (new DirectoryPermissionsRevoker(directoryPath, FileSystemRights.Write)) - { - // Call - bool isExported = true; - Action call = () => isExported = exporter.Export(); + // Call - // Assert - string expectedMessage = string.Format("Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{0}'.\r\nEr is geen referentielijn geëxporteerd.", filePath); - TestHelper.AssertLogMessageIsGenerated(call, expectedMessage); - Assert.IsFalse(isExported); - } + Action call = () => isExported = exporter.Export(); + + // Assert + string expectedMessage = $"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'.\r\nEr is geen referentielijn geëxporteerd."; + TestHelper.AssertLogMessageIsGenerated(call, expectedMessage); + Assert.IsFalse(isExported); } - finally - { - Directory.Delete(directoryPath, true); - } } } } \ No newline at end of file Index: Ringtoets/Common/test/Ringtoets.Common.IO.Test/ReferenceLines/ReferenceLineWriterTest.cs =================================================================== diff -u -rd18995d638a6c3f99cb7a8419107bb4148420301 -r28ab332db2d96ad9e766f19677af1c00fc88a644 --- Ringtoets/Common/test/Ringtoets.Common.IO.Test/ReferenceLines/ReferenceLineWriterTest.cs (.../ReferenceLineWriterTest.cs) (revision d18995d638a6c3f99cb7a8419107bb4148420301) +++ Ringtoets/Common/test/Ringtoets.Common.IO.Test/ReferenceLines/ReferenceLineWriterTest.cs (.../ReferenceLineWriterTest.cs) (revision 28ab332db2d96ad9e766f19677af1c00fc88a644) @@ -37,7 +37,7 @@ public void WriteReferenceLine_NullReferenceLine_ThrowArgumentNullException() { // Setup - string filePath = TestHelper.GetScratchPadPath(Path.Combine("WriteReferenceLine_NullReferenceLine_ThrowArgumentNullException", + string filePath = TestHelper.GetScratchPadPath(Path.Combine(nameof(WriteReferenceLine_NullReferenceLine_ThrowArgumentNullException), "test.shp")); var writer = new ReferenceLineWriter(); @@ -60,7 +60,7 @@ new Point2D(11.11, 22.22) }); - string filePath = TestHelper.GetScratchPadPath(Path.Combine("WriteReferenceLine_NullId_ThrowArgumentNullException", + string filePath = TestHelper.GetScratchPadPath(Path.Combine(nameof(WriteReferenceLine_NullId_ThrowArgumentNullException), "test.shp")); var writer = new ReferenceLineWriter(); @@ -86,7 +86,7 @@ new Point2D(11.11, 22.22) }); - string filePath = TestHelper.GetScratchPadPath(Path.Combine("WriteReferenceLine_InvalidId_ThrowArgumentException", + string filePath = TestHelper.GetScratchPadPath(Path.Combine(nameof(WriteReferenceLine_InvalidId_ThrowArgumentException), "test.shp")); var writer = new ReferenceLineWriter(); @@ -129,29 +129,25 @@ new Point2D(11.11, 22.22) }); - string directoryPath = TestHelper.GetScratchPadPath("WriteReferenceLine_ValidData_WritesShapeFile"); - Directory.CreateDirectory(directoryPath); - string filePath = Path.Combine(directoryPath, "test.shp"); - var baseName = "test"; + string directoryPath = TestHelper.GetScratchPadPath(nameof(WriteReferenceLine_ValidData_WritesShapeFile)); + using (new DirectoryDisposeHelper(TestHelper.GetScratchPadPath(), nameof(WriteReferenceLine_ValidData_WritesShapeFile))) + { + Directory.CreateDirectory(directoryPath); + string filePath = Path.Combine(directoryPath, "test.shp"); + var baseName = "test"; - var writer = new ReferenceLineWriter(); + var writer = new ReferenceLineWriter(); - // Precondition - AssertEssentialShapefileExists(directoryPath, baseName, false); + // Precondition + AssertEssentialShapefileExists(directoryPath, baseName, false); - // Call - try - { + // Call writer.WriteReferenceLine(referenceLine, "anId", filePath); // Assert AssertEssentialShapefileExists(directoryPath, baseName, true); AssertEssentialShapefileMd5Hashes(directoryPath, baseName); } - finally - { - Directory.Delete(directoryPath, true); - } } private static void AssertEssentialShapefileExists(string directoryPath, string baseName, bool shouldExist) Index: Ringtoets/DuneErosion/test/Ringtoets.DuneErosion.IO.Test/DuneLocationsExporterTest.cs =================================================================== diff -u -rd18995d638a6c3f99cb7a8419107bb4148420301 -r28ab332db2d96ad9e766f19677af1c00fc88a644 --- Ringtoets/DuneErosion/test/Ringtoets.DuneErosion.IO.Test/DuneLocationsExporterTest.cs (.../DuneLocationsExporterTest.cs) (revision d18995d638a6c3f99cb7a8419107bb4148420301) +++ Ringtoets/DuneErosion/test/Ringtoets.DuneErosion.IO.Test/DuneLocationsExporterTest.cs (.../DuneLocationsExporterTest.cs) (revision 28ab332db2d96ad9e766f19677af1c00fc88a644) @@ -40,7 +40,7 @@ public void Constructor_ExpectedValues() { // Setup - var filePath = TestHelper.GetScratchPadPath(Path.Combine("DuneLocationsExporter", "test.bnd")); + var filePath = TestHelper.GetScratchPadPath(Path.Combine(nameof(DuneLocationsExporterTest), "test.bnd")); // Call var exporter = new DuneLocationsExporter(Enumerable.Empty(), filePath); @@ -52,11 +52,8 @@ [Test] public void Constructor_LocationsNull_ThrowArgumentNullException() { - // Setup - var filePath = TestHelper.GetScratchPadPath(Path.Combine("DuneLocationsExporter", "test.bnd")); - // Call - TestDelegate test = () => new DuneLocationsExporter(null, filePath); + TestDelegate test = () => new DuneLocationsExporter(null, "IAmValid.bnd"); // Assert var exception = Assert.Throws(test); @@ -95,14 +92,13 @@ locationCalculatedOutput }; - string directoryPath = TestHelper.GetScratchPadPath("Export_ValidData_ReturnTrue"); - Directory.CreateDirectory(directoryPath); - string filePath = Path.Combine(directoryPath, "test.bnd"); + string directoryPath = TestHelper.GetScratchPadPath(nameof(Export_ValidData_ReturnTrueAndWritesFile)); + using (new DirectoryDisposeHelper(TestHelper.GetScratchPadPath(), nameof(Export_ValidData_ReturnTrueAndWritesFile))) + { + string filePath = Path.Combine(directoryPath, "test.bnd"); - var exporter = new DuneLocationsExporter(duneLocations, filePath); + var exporter = new DuneLocationsExporter(duneLocations, filePath); - try - { // Call bool isExported = exporter.Export(); @@ -117,10 +113,6 @@ "11\t9771.3\t5.89\t8.53\t14.11\t*\t0.000134\r\n", fileContent); } - finally - { - Directory.Delete(directoryPath, true); - } } [Test] @@ -139,31 +131,24 @@ } }; - string directoryPath = TestHelper.GetScratchPadPath("Export_InvalidDirectoryRights_LogErrorAndReturnFalse"); - Directory.CreateDirectory(directoryPath); - string filePath = Path.Combine(directoryPath, "test.bnd"); + string directoryPath = TestHelper.GetScratchPadPath(nameof(Export_InvalidDirectoryRights_LogErrorAndReturnFalse)); + using (var disposeHelper = new DirectoryDisposeHelper(TestHelper.GetScratchPadPath(), nameof(Export_InvalidDirectoryRights_LogErrorAndReturnFalse))) + { + string filePath = Path.Combine(directoryPath, "test.bnd"); + var exporter = new DuneLocationsExporter(duneLocations, filePath); - var exporter = new DuneLocationsExporter(duneLocations, filePath); + disposeHelper.LockDirectory(FileSystemRights.Write); + bool isExported = true; - try - { - using (new DirectoryPermissionsRevoker(directoryPath, FileSystemRights.Write)) - { - // Call - bool isExported = true; - Action call = () => isExported = exporter.Export(); + // Call + Action call = () => isExported = exporter.Export(); - // Assert - string expectedMessage = $"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'. " - + "Er zijn geen hydraulische randvoorwaarden locaties geëxporteerd."; - TestHelper.AssertLogMessageIsGenerated(call, expectedMessage); - Assert.IsFalse(isExported); - } + // Assert + string expectedMessage = $"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'. " + + "Er zijn geen hydraulische randvoorwaarden locaties geëxporteerd."; + TestHelper.AssertLogMessageIsGenerated(call, expectedMessage); + Assert.IsFalse(isExported); } - finally - { - Directory.Delete(directoryPath, true); - } } private static DuneLocationOutput CreateDuneLocationOutputForExport(double waterLevel, double wavePeriod, double waveHeight) Index: Ringtoets/DuneErosion/test/Ringtoets.DuneErosion.IO.Test/DuneLocationsWriterTest.cs =================================================================== diff -u -r27a3de13b3b94459fc79664d521de3790dd6b0df -r28ab332db2d96ad9e766f19677af1c00fc88a644 --- Ringtoets/DuneErosion/test/Ringtoets.DuneErosion.IO.Test/DuneLocationsWriterTest.cs (.../DuneLocationsWriterTest.cs) (revision 27a3de13b3b94459fc79664d521de3790dd6b0df) +++ Ringtoets/DuneErosion/test/Ringtoets.DuneErosion.IO.Test/DuneLocationsWriterTest.cs (.../DuneLocationsWriterTest.cs) (revision 28ab332db2d96ad9e766f19677af1c00fc88a644) @@ -91,27 +91,20 @@ public void WriteDuneLocations_InvalidDirectoryRights_ThrowCriticalFileWriteException() { // Setup - string directoryPath = TestHelper.GetScratchPadPath("WriteDuneLocations_InvalidDirectoryRights_ThrowCriticalFileWriteException"); - Directory.CreateDirectory(directoryPath); - string filePath = Path.Combine(directoryPath, "test.bnd"); + string directoryPath = TestHelper.GetScratchPadPath(nameof(WriteDuneLocations_InvalidDirectoryRights_ThrowCriticalFileWriteException)); + using (var disposeHelper = new DirectoryDisposeHelper(TestHelper.GetScratchPadPath(), nameof(WriteDuneLocations_InvalidDirectoryRights_ThrowCriticalFileWriteException))) + { + string filePath = Path.Combine(directoryPath, "test.bnd"); - // Call - TestDelegate call = () => DuneLocationsWriter.WriteDuneLocations(Enumerable.Empty(), filePath); + // Call + TestDelegate call = () => DuneLocationsWriter.WriteDuneLocations(Enumerable.Empty(), filePath); - try - { - using (new DirectoryPermissionsRevoker(directoryPath, FileSystemRights.Write)) - { - // Assert - var exception = Assert.Throws(call); - Assert.AreEqual($"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'.", exception.Message); - Assert.IsInstanceOf(exception.InnerException); - } + disposeHelper.LockDirectory(FileSystemRights.Write); + // Assert + var exception = Assert.Throws(call); + Assert.AreEqual($"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'.", exception.Message); + Assert.IsInstanceOf(exception.InnerException); } - finally - { - Directory.Delete(directoryPath, true); - } } [Test] Index: Ringtoets/GrassCoverErosionOutwards/test/Ringtoets.GrassCoverErosionOutwards.IO.Test/GrassCoverErosionOutwardsWaveConditionsExporterTest.cs =================================================================== diff -u -rd18995d638a6c3f99cb7a8419107bb4148420301 -r28ab332db2d96ad9e766f19677af1c00fc88a644 --- Ringtoets/GrassCoverErosionOutwards/test/Ringtoets.GrassCoverErosionOutwards.IO.Test/GrassCoverErosionOutwardsWaveConditionsExporterTest.cs (.../GrassCoverErosionOutwardsWaveConditionsExporterTest.cs) (revision d18995d638a6c3f99cb7a8419107bb4148420301) +++ Ringtoets/GrassCoverErosionOutwards/test/Ringtoets.GrassCoverErosionOutwards.IO.Test/GrassCoverErosionOutwardsWaveConditionsExporterTest.cs (.../GrassCoverErosionOutwardsWaveConditionsExporterTest.cs) (revision 28ab332db2d96ad9e766f19677af1c00fc88a644) @@ -30,7 +30,7 @@ [TestFixture] public class GrassCoverErosionOutwardsWaveConditionsExporterTest { - private readonly string testFilePath = TestHelper.GetScratchPadPath("test.csv"); + private readonly string testFilePath = TestHelper.GetScratchPadPath($"{nameof(GrassCoverErosionOutwardsWaveConditionsExporterTest)}.csv"); [Test] public void Constructor_ValidParameters_ExpectedValues() Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/Exporters/PipingConfigurationExporterTest.cs =================================================================== diff -u -rd18995d638a6c3f99cb7a8419107bb4148420301 -r28ab332db2d96ad9e766f19677af1c00fc88a644 --- Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/Exporters/PipingConfigurationExporterTest.cs (.../PipingConfigurationExporterTest.cs) (revision d18995d638a6c3f99cb7a8419107bb4148420301) +++ Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/Exporters/PipingConfigurationExporterTest.cs (.../PipingConfigurationExporterTest.cs) (revision 28ab332db2d96ad9e766f19677af1c00fc88a644) @@ -76,7 +76,7 @@ public void Export_ValidData_ReturnTrueAndWritesFile() { // Setup - string filePath = TestHelper.GetScratchPadPath("test.xml"); + string filePath = TestHelper.GetScratchPadPath($"{nameof(Export_ValidData_ReturnTrueAndWritesFile)}.xml"); PipingCalculation calculation = PipingTestDataGenerator.GetPipingCalculation(); calculation.InputParameters.EntryPointL = (RoundedDouble) 0.1; @@ -158,31 +158,25 @@ } }; - string directoryPath = TestHelper.GetScratchPadPath("Export_InvalidDirectoryRights_LogErrorAndReturnFalse"); - Directory.CreateDirectory(directoryPath); - string filePath = Path.Combine(directoryPath, "test.xml"); + string directoryPath = TestHelper.GetScratchPadPath(nameof(Export_InvalidDirectoryRights_LogErrorAndReturnFalse)); + using (var disposeHelper = new DirectoryDisposeHelper(TestHelper.GetScratchPadPath(), nameof(Export_InvalidDirectoryRights_LogErrorAndReturnFalse))) + { + string filePath = Path.Combine(directoryPath, "test.xml"); - var exporter = new PipingConfigurationExporter(calculationGroup, filePath); + var exporter = new PipingConfigurationExporter(calculationGroup, filePath); - try - { - using (new DirectoryPermissionsRevoker(directoryPath, FileSystemRights.Write)) - { - // Call - var isExported = true; - Action call = () => isExported = exporter.Export(); + disposeHelper.LockDirectory(FileSystemRights.Write); - // Assert - string expectedMessage = $"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'. " - + "Er is geen configuratie geëxporteerd."; - TestHelper.AssertLogMessageIsGenerated(call, expectedMessage); - Assert.IsFalse(isExported); - } + // Call + var isExported = true; + Action call = () => isExported = exporter.Export(); + + // Assert + string expectedMessage = $"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'. " + + "Er is geen configuratie geëxporteerd."; + TestHelper.AssertLogMessageIsGenerated(call, expectedMessage); + Assert.IsFalse(isExported); } - finally - { - Directory.Delete(directoryPath, true); - } } } } \ No newline at end of file Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/Exporters/PipingConfigurationWriterTest.cs =================================================================== diff -u -raa30731a0eabb25918a308f15c7d3083e38b0bd8 -r28ab332db2d96ad9e766f19677af1c00fc88a644 --- Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/Exporters/PipingConfigurationWriterTest.cs (.../PipingConfigurationWriterTest.cs) (revision aa30731a0eabb25918a308f15c7d3083e38b0bd8) +++ Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/Exporters/PipingConfigurationWriterTest.cs (.../PipingConfigurationWriterTest.cs) (revision 28ab332db2d96ad9e766f19677af1c00fc88a644) @@ -123,27 +123,20 @@ public void Write_InvalidDirectoryRights_ThrowCriticalFileWriteException() { // Setup - string directoryPath = TestHelper.GetScratchPadPath("Write_InvalidDirectoryRights_ThrowCriticalFileWriteException"); - Directory.CreateDirectory(directoryPath); - string filePath = Path.Combine(directoryPath, "test.xml"); - - // Call - TestDelegate call = () => PipingConfigurationWriter.Write(new CalculationGroup(), filePath); - - try + string directoryPath = TestHelper.GetScratchPadPath(nameof(Write_InvalidDirectoryRights_ThrowCriticalFileWriteException)); + using (var disposeHelper = new DirectoryDisposeHelper(TestHelper.GetScratchPadPath(), nameof(Write_InvalidDirectoryRights_ThrowCriticalFileWriteException))) { - using (new DirectoryPermissionsRevoker(directoryPath, FileSystemRights.Write)) - { - // Assert - var exception = Assert.Throws(call); - Assert.AreEqual($"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'.", exception.Message); - Assert.IsInstanceOf(exception.InnerException); - } + string filePath = Path.Combine(directoryPath, "test.xml"); + disposeHelper.LockDirectory(FileSystemRights.Write); + + // Call + TestDelegate call = () => PipingConfigurationWriter.Write(new CalculationGroup(), filePath); + + // Assert + var exception = Assert.Throws(call); + Assert.AreEqual($"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'.", exception.Message); + Assert.IsInstanceOf(exception.InnerException); } - finally - { - Directory.Delete(directoryPath, true); - } } [Test] Index: Ringtoets/Piping/test/Ringtoets.Piping.Plugin.Test/FileImporter/PipingSurfaceLinesCsvImporterTest.cs =================================================================== diff -u -re6afafed09cf1face1cacd89329da45ff93485a9 -r28ab332db2d96ad9e766f19677af1c00fc88a644 --- Ringtoets/Piping/test/Ringtoets.Piping.Plugin.Test/FileImporter/PipingSurfaceLinesCsvImporterTest.cs (.../PipingSurfaceLinesCsvImporterTest.cs) (revision e6afafed09cf1face1cacd89329da45ff93485a9) +++ Ringtoets/Piping/test/Ringtoets.Piping.Plugin.Test/FileImporter/PipingSurfaceLinesCsvImporterTest.cs (.../PipingSurfaceLinesCsvImporterTest.cs) (revision 28ab332db2d96ad9e766f19677af1c00fc88a644) @@ -528,7 +528,7 @@ public void Import_FileDeletedDuringRead_AbortImportAndLog() { // Setup - var copyTargetPath = TestHelper.GetScratchPadPath("Import_FileDeletedDuringRead_AbortImportAndLog.csv"); + var copyTargetPath = TestHelper.GetScratchPadPath($"{nameof(Import_FileDeletedDuringRead_AbortImportAndLog)}.csv"); string validFilePath = Path.Combine(ioTestDataPath, "TwoValidSurfaceLines.csv"); File.Copy(validFilePath, copyTargetPath); Index: Ringtoets/Revetment/test/Ringtoets.Revetment.IO.Test/WaveConditionsExporterBaseTest.cs =================================================================== diff -u -rd18995d638a6c3f99cb7a8419107bb4148420301 -r28ab332db2d96ad9e766f19677af1c00fc88a644 --- Ringtoets/Revetment/test/Ringtoets.Revetment.IO.Test/WaveConditionsExporterBaseTest.cs (.../WaveConditionsExporterBaseTest.cs) (revision d18995d638a6c3f99cb7a8419107bb4148420301) +++ Ringtoets/Revetment/test/Ringtoets.Revetment.IO.Test/WaveConditionsExporterBaseTest.cs (.../WaveConditionsExporterBaseTest.cs) (revision 28ab332db2d96ad9e766f19677af1c00fc88a644) @@ -39,7 +39,7 @@ public void Constructor_ValidParameters_ExpectedValues() { // Setup - string filePath = TestHelper.GetScratchPadPath("test.csv"); + string filePath = TestHelper.GetScratchPadPath($"{nameof(Constructor_ValidParameters_ExpectedValues)}.csv"); // Call var waveConditionsExporter = new TestWaveConditionsExporter(new ExportableWaveConditions[0], filePath); @@ -52,7 +52,7 @@ public void Constructor_ExportableWaveConditionsCollectionNull_ThrowArgumentNullException() { // Setup - string filePath = TestHelper.GetScratchPadPath("test.csv"); + string filePath = TestHelper.GetScratchPadPath($"{nameof(Constructor_ExportableWaveConditionsCollectionNull_ThrowArgumentNullException)}.csv"); // Call TestDelegate call = () => new TestWaveConditionsExporter(null, filePath); @@ -96,12 +96,12 @@ public void Export_ValidData_ReturnTrue() { // Setup - string directoryPath = TestHelper.GetScratchPadPath("Export_ValidData_ReturnTrue"); - Directory.CreateDirectory(directoryPath); - string filePath = Path.Combine(directoryPath, "test.csv"); + string directoryPath = TestHelper.GetScratchPadPath(nameof(Export_ValidData_ReturnTrue)); - try + using (new DirectoryDisposeHelper(TestHelper.GetScratchPadPath(), nameof(Export_ValidData_ReturnTrue))) { + string filePath = Path.Combine(directoryPath, "test.csv"); + var waveConditionsExporter = new TestWaveConditionsExporter(new ExportableWaveConditions[0], filePath); // Call @@ -110,10 +110,6 @@ // Assert Assert.IsTrue(isExported); } - finally - { - Directory.Delete(directoryPath, true); - } } [Test] @@ -148,12 +144,11 @@ }, CreateWaveConditionsOutputForExport(3.33333, 1.11111, 4.44444, 2.2, 6.66666), CoverType.StoneCoverColumns) }; - string directoryPath = TestHelper.GetScratchPadPath("Export_ValidData_ValidFile"); - Directory.CreateDirectory(directoryPath); - string filePath = Path.Combine(directoryPath, "test.csv"); - - try + string directoryPath = TestHelper.GetScratchPadPath(nameof(Export_ValidData_ValidFile)); + using (new DirectoryDisposeHelper(TestHelper.GetScratchPadPath(), nameof(Export_ValidData_ValidFile))) { + string filePath = Path.Combine(directoryPath, "test.csv"); + // Call WaveConditionsWriter.WriteWaveConditions(exportableWaveConditions, filePath); @@ -165,10 +160,6 @@ "columnsName, aLocation, 44.000, 123.456, , nee, nee, Steen (zuilen), 3.33, 1.11, 4.44, 2.20, 6.67\r\n", fileContent); } - finally - { - Directory.Delete(directoryPath, true); - } } private class TestWaveConditionsExporter : WaveConditionsExporterBase Index: Ringtoets/Revetment/test/Ringtoets.Revetment.IO.Test/WaveConditionsWriterTest.cs =================================================================== diff -u -r27a3de13b3b94459fc79664d521de3790dd6b0df -r28ab332db2d96ad9e766f19677af1c00fc88a644 --- Ringtoets/Revetment/test/Ringtoets.Revetment.IO.Test/WaveConditionsWriterTest.cs (.../WaveConditionsWriterTest.cs) (revision 27a3de13b3b94459fc79664d521de3790dd6b0df) +++ Ringtoets/Revetment/test/Ringtoets.Revetment.IO.Test/WaveConditionsWriterTest.cs (.../WaveConditionsWriterTest.cs) (revision 28ab332db2d96ad9e766f19677af1c00fc88a644) @@ -168,12 +168,11 @@ }, CreateWaveConditionsOutputForExport(3.33333, 1.11111, 4.44444, 2.2, 6.66666), CoverType.StoneCoverColumns) }; - string directoryPath = TestHelper.GetScratchPadPath("WriteWaveConditions_ValidData_ValidFile"); - Directory.CreateDirectory(directoryPath); - string filePath = Path.Combine(directoryPath, "test.csv"); - - try + string directoryPath = TestHelper.GetScratchPadPath(nameof(WriteWaveConditions_ValidData_ValidFile)); + using (new DirectoryDisposeHelper(TestHelper.GetScratchPadPath(), nameof(WriteWaveConditions_ValidData_ValidFile))) { + string filePath = Path.Combine(directoryPath, "test.csv"); + // Call WaveConditionsWriter.WriteWaveConditions(exportableWaveConditions, filePath); @@ -185,10 +184,6 @@ "columnsName, aLocation, 44.000, 123.456, , nee, nee, Steen (zuilen), 3.33, 1.11, 4.44, 2.20, 6.67\r\n", fileContent); } - finally - { - Directory.Delete(directoryPath, true); - } } private static WaveConditionsOutput CreateWaveConditionsOutputForExport(double waterLevel, double waveHeight, Index: Ringtoets/StabilityStoneCover/test/Ringtoets.StabilityStoneCover.IO.Test/StabilityStoneCoverWaveConditionsExporterTest.cs =================================================================== diff -u -rd18995d638a6c3f99cb7a8419107bb4148420301 -r28ab332db2d96ad9e766f19677af1c00fc88a644 --- Ringtoets/StabilityStoneCover/test/Ringtoets.StabilityStoneCover.IO.Test/StabilityStoneCoverWaveConditionsExporterTest.cs (.../StabilityStoneCoverWaveConditionsExporterTest.cs) (revision d18995d638a6c3f99cb7a8419107bb4148420301) +++ Ringtoets/StabilityStoneCover/test/Ringtoets.StabilityStoneCover.IO.Test/StabilityStoneCoverWaveConditionsExporterTest.cs (.../StabilityStoneCoverWaveConditionsExporterTest.cs) (revision 28ab332db2d96ad9e766f19677af1c00fc88a644) @@ -68,19 +68,18 @@ public void Export_CalculationsWithoutOutput_FileWithOnlyHeader() { // Setup - string directoryPath = TestHelper.GetScratchPadPath("Export_CalculationsWithoutOutput_FileWithOnlyHeader"); - Directory.CreateDirectory(directoryPath); - string filePath = Path.Combine(directoryPath, "test.csv"); - - var calculationsWithoutOutput = new[] + string directoryPath = TestHelper.GetScratchPadPath(nameof(Export_CalculationsWithoutOutput_FileWithOnlyHeader)); + using (new DirectoryDisposeHelper(TestHelper.GetScratchPadPath(), nameof(Export_CalculationsWithoutOutput_FileWithOnlyHeader))) { - new StabilityStoneCoverWaveConditionsCalculation() - }; + string filePath = Path.Combine(directoryPath, "test.csv"); - var exporter = new StabilityStoneCoverWaveConditionsExporter(calculationsWithoutOutput, filePath); + var calculationsWithoutOutput = new[] + { + new StabilityStoneCoverWaveConditionsCalculation() + }; - try - { + var exporter = new StabilityStoneCoverWaveConditionsExporter(calculationsWithoutOutput, filePath); + // Call bool isExported = exporter.Export(); @@ -90,32 +89,27 @@ string fileContent = File.ReadAllText(filePath); Assert.AreEqual("Naam berekening, Naam HR locatie, X HR locatie (RD) [m], Y HR locatie (RD) [m], Naam voorlandprofiel, Dam gebruikt, Voorlandgeometrie gebruikt, Type bekleding, Waterstand [m+NAP], Golfhoogte (Hs) [m], Golfperiode (Tp) [s], Golfrichting t.o.v. dijknormaal [°], Golfrichting t.o.v. Noord [°]\r\n", fileContent); } - finally - { - Directory.Delete(directoryPath, true); - } } [Test] public void Export_CalculationsWithoutHydraulicBoundaryLocation_FileWithOnlyHeader() { // Setup - string directoryPath = TestHelper.GetScratchPadPath("Export_CalculationsWithoutHydraulicBoundaryLocation_FileWithOnlyHeader"); - Directory.CreateDirectory(directoryPath); - string filePath = Path.Combine(directoryPath, "test.csv"); - - var calculations = new[] + string directoryPath = TestHelper.GetScratchPadPath(nameof(Export_CalculationsWithoutHydraulicBoundaryLocation_FileWithOnlyHeader)); + using (new DirectoryDisposeHelper(TestHelper.GetScratchPadPath(), nameof(Export_CalculationsWithoutHydraulicBoundaryLocation_FileWithOnlyHeader))) { - new StabilityStoneCoverWaveConditionsCalculation + string filePath = Path.Combine(directoryPath, "test.csv"); + + var calculations = new[] { - Output = new StabilityStoneCoverWaveConditionsOutput(Enumerable.Empty(), Enumerable.Empty()), - } - }; + new StabilityStoneCoverWaveConditionsCalculation + { + Output = new StabilityStoneCoverWaveConditionsOutput(Enumerable.Empty(), Enumerable.Empty()), + } + }; - var exporter = new StabilityStoneCoverWaveConditionsExporter(calculations, filePath); + var exporter = new StabilityStoneCoverWaveConditionsExporter(calculations, filePath); - try - { // Call bool isExported = exporter.Export(); @@ -125,51 +119,47 @@ string fileContent = File.ReadAllText(filePath); Assert.AreEqual("Naam berekening, Naam HR locatie, X HR locatie (RD) [m], Y HR locatie (RD) [m], Naam voorlandprofiel, Dam gebruikt, Voorlandgeometrie gebruikt, Type bekleding, Waterstand [m+NAP], Golfhoogte (Hs) [m], Golfperiode (Tp) [s], Golfrichting t.o.v. dijknormaal [°], Golfrichting t.o.v. Noord [°]\r\n", fileContent); } - finally - { - Directory.Delete(directoryPath, true); - } } [Test] public void Export_ValidData_ValidFile() { // Setup - string directoryPath = TestHelper.GetScratchPadPath("Export_ValidData_ValidFile"); - Directory.CreateDirectory(directoryPath); - string filePath = Path.Combine(directoryPath, "test.csv"); - - var calculations = new[] + string subFolder = $"{nameof(StabilityStoneCoverWaveConditionsExporterTest)}.{nameof(Export_ValidData_ValidFile)}"; + string directoryPath = TestHelper.GetScratchPadPath(subFolder); + using (new DirectoryDisposeHelper(TestHelper.GetScratchPadPath(), subFolder)) { - new StabilityStoneCoverWaveConditionsCalculation + string filePath = Path.Combine(directoryPath, "test.csv"); + + var calculations = new[] { - Name = "aCalculation", - InputParameters = + new StabilityStoneCoverWaveConditionsCalculation { - HydraulicBoundaryLocation = new HydraulicBoundaryLocation(8, "aLocation", 44, 123.456) + Name = "aCalculation", + InputParameters = { - DesignWaterLevelOutput = new TestHydraulicBoundaryLocationOutput(28.36844) + HydraulicBoundaryLocation = new HydraulicBoundaryLocation(8, "aLocation", 44, 123.456) + { + DesignWaterLevelOutput = new TestHydraulicBoundaryLocationOutput(28.36844) + }, + LowerBoundaryRevetment = (RoundedDouble) 1.384, + UpperBoundaryRevetment = (RoundedDouble) 11.54898963, + StepSize = WaveConditionsInputStepSize.Half, + LowerBoundaryWaterLevels = (RoundedDouble) 1.98699, + UpperBoundaryWaterLevels = (RoundedDouble) 84.26548 }, - LowerBoundaryRevetment = (RoundedDouble) 1.384, - UpperBoundaryRevetment = (RoundedDouble) 11.54898963, - StepSize = WaveConditionsInputStepSize.Half, - LowerBoundaryWaterLevels = (RoundedDouble) 1.98699, - UpperBoundaryWaterLevels = (RoundedDouble) 84.26548 - }, - Output = new StabilityStoneCoverWaveConditionsOutput(new[] - { - new TestWaveConditionsOutput() - }, new[] - { - new TestWaveConditionsOutput() - }) - } - }; + Output = new StabilityStoneCoverWaveConditionsOutput(new[] + { + new TestWaveConditionsOutput() + }, new[] + { + new TestWaveConditionsOutput() + }) + } + }; - var exporter = new StabilityStoneCoverWaveConditionsExporter(calculations, filePath); + var exporter = new StabilityStoneCoverWaveConditionsExporter(calculations, filePath); - try - { // Call bool isExported = exporter.Export(); @@ -182,10 +172,6 @@ "aCalculation, aLocation, 44.000, 123.456, , nee, nee, Steen (blokken), 1.10, 2.20, 3.30, 4.40, 5.50\r\n", fileContent); } - finally - { - Directory.Delete(directoryPath, true); - } } } } \ No newline at end of file