Index: Ringtoets/Migration/src/Riskeer.Migration.Core/ProjectCreateScript.cs =================================================================== diff -u -r2f76d4cf01e6bc7599e0b5182bf05f0edda73cde -r42c365ba568e89bb0267c3788a2c46b816539244 --- Ringtoets/Migration/src/Riskeer.Migration.Core/ProjectCreateScript.cs (.../ProjectCreateScript.cs) (revision 2f76d4cf01e6bc7599e0b5182bf05f0edda73cde) +++ Ringtoets/Migration/src/Riskeer.Migration.Core/ProjectCreateScript.cs (.../ProjectCreateScript.cs) (revision 42c365ba568e89bb0267c3788a2c46b816539244) @@ -61,7 +61,7 @@ { try { - using (var databaseFile = new RingtoetsDatabaseFile(location)) + using (var databaseFile = new ProjectDatabaseFile(location)) { databaseFile.OpenDatabaseConnection(); databaseFile.ExecuteQuery(createQuery); Index: Ringtoets/Migration/src/Riskeer.Migration.Core/ProjectDatabaseFile.cs =================================================================== diff -u --- Ringtoets/Migration/src/Riskeer.Migration.Core/ProjectDatabaseFile.cs (revision 0) +++ Ringtoets/Migration/src/Riskeer.Migration.Core/ProjectDatabaseFile.cs (revision 42c365ba568e89bb0267c3788a2c46b816539244) @@ -0,0 +1,111 @@ +// Copyright (C) Stichting Deltares 2018. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using System.Data.SQLite; +using Core.Common.IO; +using Core.Common.Util; + +namespace Riskeer.Migration.Core +{ + /// + /// Class that provides methods for the migration of a project database target file. + /// + public class ProjectDatabaseFile : IDisposable + { + private readonly string filePath; + private SQLiteConnection connection; + private bool disposed; + + /// + /// Creates a new instance of the class. + /// + /// The path to the target file. + /// Thrown when : + /// + /// is not empty or null, + /// does not consist out of only whitespace characters, + /// does not contain an invalid character, + /// does not end with a directory or path separator (empty file name), + /// is not writable. + /// + /// Creates the file if it does not exist. + public ProjectDatabaseFile(string path) + { + IOUtils.CreateFileIfNotExists(path); + filePath = path; + } + + /// + /// Opens the connection to the project database file. + /// + public void OpenDatabaseConnection() + { + connection = new SQLiteConnection(SqLiteConnectionStringBuilder.BuildSqLiteConnectionString(filePath, false)); + connection.Open(); + } + + /// + /// Executes the on the project database file. + /// + /// Create structure query to execute. + /// Thrown when is null + /// or consist out of only whitespace characters. + /// Thrown when executing failed. + public void ExecuteQuery(string query) + { + if (string.IsNullOrWhiteSpace(query)) + { + throw new ArgumentException(@"Parameter must be a valid database script.", nameof(query)); + } + + using (var command = new SQLiteCommand(connection) + { + CommandText = query + }) + { + command.ExecuteNonQuery(); + } + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (disposed) + { + return; + } + + if (disposing) + { + connection?.Dispose(); + connection = null; + } + + disposed = true; + } + } +} \ No newline at end of file Fisheye: Tag 42c365ba568e89bb0267c3788a2c46b816539244 refers to a dead (removed) revision in file `Ringtoets/Migration/src/Riskeer.Migration.Core/RingtoetsDatabaseFile.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Ringtoets/Migration/src/Riskeer.Migration.Core/RingtoetsUpgradeScript.cs =================================================================== diff -u -reb0ce91f3f0fb3c543ef2b1875e5ad9e6356fa08 -r42c365ba568e89bb0267c3788a2c46b816539244 --- Ringtoets/Migration/src/Riskeer.Migration.Core/RingtoetsUpgradeScript.cs (.../RingtoetsUpgradeScript.cs) (revision eb0ce91f3f0fb3c543ef2b1875e5ad9e6356fa08) +++ Ringtoets/Migration/src/Riskeer.Migration.Core/RingtoetsUpgradeScript.cs (.../RingtoetsUpgradeScript.cs) (revision 42c365ba568e89bb0267c3788a2c46b816539244) @@ -69,7 +69,7 @@ try { string query = string.Format(upgradeQuery, sourceLocation, logDatabaseLocation); - using (var databaseFile = new RingtoetsDatabaseFile(targetLocation)) + using (var databaseFile = new ProjectDatabaseFile(targetLocation)) { databaseFile.OpenDatabaseConnection(); databaseFile.ExecuteQuery(query); Index: Ringtoets/Migration/src/Riskeer.Migration.Core/Riskeer.Migration.Core.csproj =================================================================== diff -u -r2f76d4cf01e6bc7599e0b5182bf05f0edda73cde -r42c365ba568e89bb0267c3788a2c46b816539244 --- Ringtoets/Migration/src/Riskeer.Migration.Core/Riskeer.Migration.Core.csproj (.../Riskeer.Migration.Core.csproj) (revision 2f76d4cf01e6bc7599e0b5182bf05f0edda73cde) +++ Ringtoets/Migration/src/Riskeer.Migration.Core/Riskeer.Migration.Core.csproj (.../Riskeer.Migration.Core.csproj) (revision 42c365ba568e89bb0267c3788a2c46b816539244) @@ -26,7 +26,7 @@ Resources.resx - + Index: Ringtoets/Migration/test/Riskeer.Migration.Core.Test/ProjectDatabaseFileTest.cs =================================================================== diff -u --- Ringtoets/Migration/test/Riskeer.Migration.Core.Test/ProjectDatabaseFileTest.cs (revision 0) +++ Ringtoets/Migration/test/Riskeer.Migration.Core.Test/ProjectDatabaseFileTest.cs (revision 42c365ba568e89bb0267c3788a2c46b816539244) @@ -0,0 +1,159 @@ +// Copyright (C) Stichting Deltares 2018. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using System.Data.SQLite; +using System.IO; +using Core.Common.TestUtil; +using NUnit.Framework; + +namespace Riskeer.Migration.Core.Test +{ + [TestFixture] + public class ProjectDatabaseFileTest + { + [Test] + [TestCase("")] + [TestCase(" ")] + [TestCase(null)] + public void Constructor_FilePathNullOrWhiteSpace_ThrowsArgumentException(string filePath) + { + // Call + TestDelegate call = () => + { + using (new ProjectDatabaseFile(filePath)) {} + }; + + // Assert + var exception = Assert.Throws(call); + Assert.AreEqual($"Fout bij het lezen van bestand '{filePath}': bestandspad mag niet leeg of ongedefinieerd zijn.", exception.Message); + } + + [Test] + public void Constructor_FileNotWritable_ThrowsArgumentException() + { + // Setup + string filePath = TestHelper.GetScratchPadPath($"{nameof(ProjectDatabaseFileTest)}.{nameof(Constructor_FileNotWritable_ThrowsArgumentException)}"); + + using (var helper = new FileDisposeHelper(filePath)) + { + helper.LockFiles(); + + // Call + TestDelegate call = () => + { + using (new ProjectDatabaseFile(filePath)) {} + }; + + // Assert + string expectedMessage = $"Er is een onverwachte fout opgetreden tijdens het schrijven van het bestand '{filePath}'."; + TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, expectedMessage); + } + } + + [Test] + [TestCase("")] + [TestCase(" ")] + [TestCase(null)] + public void ExecuteQuery_QueryIsNullOrWhiteSpace_ThrowsArgumentException(string query) + { + // Setup + string filename = Path.GetRandomFileName(); + string filePath = TestHelper.GetScratchPadPath(nameof(ExecuteQuery_QueryIsNullOrWhiteSpace_ThrowsArgumentException) + filename); + + using (new FileDisposeHelper(filePath)) + using (var databaseFile = new ProjectDatabaseFile(filePath)) + { + databaseFile.OpenDatabaseConnection(); + + // Call + TestDelegate call = () => databaseFile.ExecuteQuery(query); + + // Assert + TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, "Parameter must be a valid database script."); + } + } + + [Test] + public void ExecuteQuery_InvalidQuery_ThrowsSQLiteException() + { + // Setup + string filename = Path.GetRandomFileName(); + string filePath = TestHelper.GetScratchPadPath(nameof(ExecuteQuery_InvalidQuery_ThrowsSQLiteException) + filename); + + using (new FileDisposeHelper(filePath)) + using (var databaseFile = new ProjectDatabaseFile(filePath)) + { + databaseFile.OpenDatabaseConnection(); + + // Call + TestDelegate call = () => databaseFile.ExecuteQuery("THIS WILL FAIL"); + + // Assert + Assert.Throws(call); + } + } + + [Test] + public void ExecuteQuery_ValidQuery_CreatesFile() + { + // Setup + string filename = Path.GetRandomFileName(); + string filePath = TestHelper.GetScratchPadPath(filename); + + using (var databaseFile = new ProjectDatabaseFile(filePath)) + { + databaseFile.OpenDatabaseConnection(); + + // Call + databaseFile.ExecuteQuery(";"); + + // Assert + Assert.IsTrue(File.Exists(filePath)); + } + + using (new FileDisposeHelper(filePath)) {} + } + + [Test] + public void Dispose_AlreadyDisposed_DoesNotThrowException() + { + // Setup + string filename = Path.GetRandomFileName(); + string filePath = TestHelper.GetScratchPadPath(filename); + + // Call + TestDelegate call = () => + { + using (var databaseFile = new ProjectDatabaseFile(filePath)) + { + databaseFile.Dispose(); + } + }; + + using (new FileDisposeHelper(filePath)) + { + // Assert + Assert.DoesNotThrow(call); + } + } + } +} \ No newline at end of file Fisheye: Tag 42c365ba568e89bb0267c3788a2c46b816539244 refers to a dead (removed) revision in file `Ringtoets/Migration/test/Riskeer.Migration.Core.Test/RingtoetsDatabaseFileTest.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Ringtoets/Migration/test/Riskeer.Migration.Core.Test/Riskeer.Migration.Core.Test.csproj =================================================================== diff -u -r2f76d4cf01e6bc7599e0b5182bf05f0edda73cde -r42c365ba568e89bb0267c3788a2c46b816539244 --- Ringtoets/Migration/test/Riskeer.Migration.Core.Test/Riskeer.Migration.Core.Test.csproj (.../Riskeer.Migration.Core.Test.csproj) (revision 2f76d4cf01e6bc7599e0b5182bf05f0edda73cde) +++ Ringtoets/Migration/test/Riskeer.Migration.Core.Test/Riskeer.Migration.Core.Test.csproj (.../Riskeer.Migration.Core.Test.csproj) (revision 42c365ba568e89bb0267c3788a2c46b816539244) @@ -23,7 +23,7 @@ - + Index: Ringtoets/Migration/test/Riskeer.Migration.Integration.Test/MigrationTo172IntegrationTest.cs =================================================================== diff -u -r92827b7879fc5c2544c51a84a7a16c96d9a2103d -r42c365ba568e89bb0267c3788a2c46b816539244 --- Ringtoets/Migration/test/Riskeer.Migration.Integration.Test/MigrationTo172IntegrationTest.cs (.../MigrationTo172IntegrationTest.cs) (revision 92827b7879fc5c2544c51a84a7a16c96d9a2103d) +++ Ringtoets/Migration/test/Riskeer.Migration.Integration.Test/MigrationTo172IntegrationTest.cs (.../MigrationTo172IntegrationTest.cs) (revision 42c365ba568e89bb0267c3788a2c46b816539244) @@ -134,7 +134,7 @@ ); var fromVersionedFile = new RingtoetsVersionedFile(sourceFilePath); - using (var databaseFile = new RingtoetsDatabaseFile(sourceFilePath)) + using (var databaseFile = new ProjectDatabaseFile(sourceFilePath)) { databaseFile.OpenDatabaseConnection(); @@ -219,7 +219,7 @@ ); var fromVersionedFile = new RingtoetsVersionedFile(sourceFilePath); - using (var databaseFile = new RingtoetsDatabaseFile(sourceFilePath)) + using (var databaseFile = new ProjectDatabaseFile(sourceFilePath)) { databaseFile.OpenDatabaseConnection();