Index: Migration/Console/test/Migration.Console.TestUtil.Test/TestEnvironmentControlTest.cs =================================================================== diff -u -r111d72b3e14a3dddfaacddbae3f06ae214049f91 -r1b7e132c53e245b576e31cf9b6526dde6461f9e5 --- Migration/Console/test/Migration.Console.TestUtil.Test/TestEnvironmentControlTest.cs (.../TestEnvironmentControlTest.cs) (revision 111d72b3e14a3dddfaacddbae3f06ae214049f91) +++ Migration/Console/test/Migration.Console.TestUtil.Test/TestEnvironmentControlTest.cs (.../TestEnvironmentControlTest.cs) (revision 1b7e132c53e245b576e31cf9b6526dde6461f9e5) @@ -20,8 +20,8 @@ // All rights reserved. using System; -using NUnit.Framework; using Core.Common.TestUtil; +using NUnit.Framework; namespace Migration.Console.TestUtil.Test { Index: Migration/Core/src/Migration.Core.Storage/VersionedFileMigrator.cs =================================================================== diff -u -raa12af0eac754d1d32412e8c3b5fd780fe32b8e4 -r1b7e132c53e245b576e31cf9b6526dde6461f9e5 --- Migration/Core/src/Migration.Core.Storage/VersionedFileMigrator.cs (.../VersionedFileMigrator.cs) (revision aa12af0eac754d1d32412e8c3b5fd780fe32b8e4) +++ Migration/Core/src/Migration.Core.Storage/VersionedFileMigrator.cs (.../VersionedFileMigrator.cs) (revision 1b7e132c53e245b576e31cf9b6526dde6461f9e5) @@ -19,6 +19,7 @@ // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -112,7 +113,7 @@ File.Copy(upgradedVersionFile.Location, newFileLocation, true); File.Delete(upgradedVersionFile.Location); } - catch (IOException exception) + catch (Exception exception) when (exception is IOException || exception is UnauthorizedAccessException) { var message = string.Format(Resources.Migrate_Unable_To_Move_From_Location_0_To_Location_1, upgradedVersionFile.Location, newFileLocation); Index: Migration/Core/test/Migration.Core.Storage.Test/VersionedFileMigratorTest.cs =================================================================== diff -u -raa12af0eac754d1d32412e8c3b5fd780fe32b8e4 -r1b7e132c53e245b576e31cf9b6526dde6461f9e5 --- Migration/Core/test/Migration.Core.Storage.Test/VersionedFileMigratorTest.cs (.../VersionedFileMigratorTest.cs) (revision aa12af0eac754d1d32412e8c3b5fd780fe32b8e4) +++ Migration/Core/test/Migration.Core.Storage.Test/VersionedFileMigratorTest.cs (.../VersionedFileMigratorTest.cs) (revision 1b7e132c53e245b576e31cf9b6526dde6461f9e5) @@ -19,6 +19,7 @@ // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. +using System; using System.IO; using Core.Common.TestUtil; using Migration.Scripts.Data; @@ -143,13 +144,48 @@ TestDelegate call = () => migrator.Migrate(fromVersionedFile, newVersion, targetFilePath); // Assert - string message = Assert.Throws(call).Message; - Assert.That(message.StartsWith("Er is een onverwachte fout opgetreden tijdens het verplaatsen van het gemigreerde bestand '")); - Assert.That(message.EndsWith($"' naar '{targetFilePath}'.")); + CriticalDatabaseMigrationException exception = Assert.Throws(call); + Assert.That(exception.Message.StartsWith("Er is een onverwachte fout opgetreden tijdens het verplaatsen van het gemigreerde bestand '")); + Assert.That(exception.Message.EndsWith($"' naar '{targetFilePath}'.")); + Assert.IsInstanceOf(exception.InnerException); } } [Test] + public void Migrate_TargetFileNotWritable_ThrowsCriticalDatabaseMigrationException() + { + // Setup + const string newVersion = "17.1"; + string sourceFilePath = TestHelper.GetTestDataPath(TestDataPath.Migration.Core.Storage, "Demo164.rtd"); + var fromVersionedFile = new VersionedFile(sourceFilePath); + + string targetFilePath = TestHelper.GetTestDataPath(TestDataPath.Migration.Core.Storage, Path.GetRandomFileName()); + var migrator = new VersionedFileMigrator(); + + using (new FileDisposeHelper(targetFilePath)) + { + FileAttributes attributes = File.GetAttributes(targetFilePath); + File.SetAttributes(targetFilePath, attributes | FileAttributes.ReadOnly); + + try + { + // Call + TestDelegate call = () => migrator.Migrate(fromVersionedFile, newVersion, targetFilePath); + + // Assert + CriticalDatabaseMigrationException exception = Assert.Throws(call); + Assert.That(exception.Message.StartsWith("Er is een onverwachte fout opgetreden tijdens het verplaatsen van het gemigreerde bestand '")); + Assert.That(exception.Message.EndsWith($"' naar '{targetFilePath}'.")); + Assert.IsInstanceOf(exception.InnerException); + } + finally + { + File.SetAttributes(targetFilePath, attributes); + } + } + } + + [Test] public void Migrate_InvalidToVersion_ThrowsCriticalDatabaseMigrationException() { // Setup Index: Migration/Scripts/test/Migration.Scripts.Data.Test/RingtoetsDatabaseFileTest.cs =================================================================== diff -u -r48d44fbda8f7ffb4c347aba4b4bb26d9c75487db -r1b7e132c53e245b576e31cf9b6526dde6461f9e5 --- Migration/Scripts/test/Migration.Scripts.Data.Test/RingtoetsDatabaseFileTest.cs (.../RingtoetsDatabaseFileTest.cs) (revision 48d44fbda8f7ffb4c347aba4b4bb26d9c75487db) +++ Migration/Scripts/test/Migration.Scripts.Data.Test/RingtoetsDatabaseFileTest.cs (.../RingtoetsDatabaseFileTest.cs) (revision 1b7e132c53e245b576e31cf9b6526dde6461f9e5) @@ -58,17 +58,22 @@ { FileAttributes attributes = File.GetAttributes(filePath); File.SetAttributes(filePath, attributes | FileAttributes.ReadOnly); - - // Call - TestDelegate call = () => + try { - using (new RingtoetsDatabaseFile(filePath)) {} - }; + // Call + TestDelegate call = () => + { + using (new RingtoetsDatabaseFile(filePath)) {} + }; - // Assert - string expectedMessage = $"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'."; - TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, expectedMessage); - File.SetAttributes(filePath, attributes); + // Assert + string expectedMessage = $"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'."; + TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, expectedMessage); + } + finally + { + File.SetAttributes(filePath, attributes); + } } }