Index: Core/Common/test/Core.Common.Utils.Test/FileUtilsTest.cs =================================================================== diff -u -r5aa0269872b446cab9f68c55dead92b6c5f49a64 -r48d44fbda8f7ffb4c347aba4b4bb26d9c75487db --- Core/Common/test/Core.Common.Utils.Test/FileUtilsTest.cs (.../FileUtilsTest.cs) (revision 5aa0269872b446cab9f68c55dead92b6c5f49a64) +++ Core/Common/test/Core.Common.Utils.Test/FileUtilsTest.cs (.../FileUtilsTest.cs) (revision 48d44fbda8f7ffb4c347aba4b4bb26d9c75487db) @@ -219,7 +219,7 @@ [TestCase("")] [TestCase(" ")] [TestCase(null)] - public void ValidateFilePathIsWritable_FilePatNullOrWhiteSpace_ThrowsArgumentException(string filePath) + public void ValidateFilePathIsWritable_FilePathNullOrWhiteSpace_ThrowsArgumentException(string filePath) { // Call TestDelegate call = () => FileUtils.ValidateFilePathIsWritable(filePath); Index: Migration/Scripts/src/Migration.Scripts.Data/RingtoetsDatabaseFile.cs =================================================================== diff -u -r922df88501b79d938a774941d4a9eb60c91cd734 -r48d44fbda8f7ffb4c347aba4b4bb26d9c75487db --- Migration/Scripts/src/Migration.Scripts.Data/RingtoetsDatabaseFile.cs (.../RingtoetsDatabaseFile.cs) (revision 922df88501b79d938a774941d4a9eb60c91cd734) +++ Migration/Scripts/src/Migration.Scripts.Data/RingtoetsDatabaseFile.cs (.../RingtoetsDatabaseFile.cs) (revision 48d44fbda8f7ffb4c347aba4b4bb26d9c75487db) @@ -33,23 +33,24 @@ private SQLiteConnection connection; private bool disposed; + private readonly string filePath; + /// /// Creates a new instance of the class. /// - /// The path to the target file. - /// Thrown when : + /// 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. /// - public RingtoetsDatabaseFile(string filePath) + public RingtoetsDatabaseFile(string path) { - FileUtils.ValidateFilePathIsWritable(filePath); - - FilePath = filePath; + FileUtils.ValidateFilePathIsWritable(path); + filePath = path; } /// @@ -58,8 +59,8 @@ /// Creates the file if it does not exist. public void OpenDatabaseConnection() { - SQLiteConnection.CreateFile(FilePath); - connection = new SQLiteConnection(SqLiteConnectionStringBuilder.BuildSqLiteConnectionString(FilePath, false)); + SQLiteConnection.CreateFile(filePath); + connection = new SQLiteConnection(SqLiteConnectionStringBuilder.BuildSqLiteConnectionString(filePath, false)); connection.Open(); } @@ -93,9 +94,6 @@ GC.SuppressFinalize(this); } - - private string FilePath { get; } - private void PerformExecuteQuery(string query) { if (string.IsNullOrWhiteSpace(query)) Index: Migration/Scripts/test/Migration.Scripts.Data.Test/Migration.Scripts.Data.Test.csproj =================================================================== diff -u -r1a90e7c188f16e5e075c39bee9c715bfddbc00f3 -r48d44fbda8f7ffb4c347aba4b4bb26d9c75487db --- Migration/Scripts/test/Migration.Scripts.Data.Test/Migration.Scripts.Data.Test.csproj (.../Migration.Scripts.Data.Test.csproj) (revision 1a90e7c188f16e5e075c39bee9c715bfddbc00f3) +++ Migration/Scripts/test/Migration.Scripts.Data.Test/Migration.Scripts.Data.Test.csproj (.../Migration.Scripts.Data.Test.csproj) (revision 48d44fbda8f7ffb4c347aba4b4bb26d9c75487db) @@ -59,6 +59,7 @@ + Index: Migration/Scripts/test/Migration.Scripts.Data.Test/RingtoetsDatabaseFileTest.cs =================================================================== diff -u --- Migration/Scripts/test/Migration.Scripts.Data.Test/RingtoetsDatabaseFileTest.cs (revision 0) +++ Migration/Scripts/test/Migration.Scripts.Data.Test/RingtoetsDatabaseFileTest.cs (revision 48d44fbda8f7ffb4c347aba4b4bb26d9c75487db) @@ -0,0 +1,228 @@ +// Copyright (C) Stichting Deltares 2016. 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 Migration.Scripts.Data.Test +{ + [TestFixture] + public class RingtoetsDatabaseFileTest + { + [Test] + [TestCase("")] + [TestCase(" ")] + [TestCase(null)] + public void Constructor_FilePathNullOrWhiteSpace_ThrowsArgumentException(string filePath) + { + // Call + TestDelegate call = () => + { + using (new RingtoetsDatabaseFile(filePath)) {} + }; + + // Assert + ArgumentException 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 filename = Path.GetRandomFileName(); + string filePath = TestHelper.GetTestDataPath(TestDataPath.Migration.Core.Storage, filename); + + using (new FileDisposeHelper(filePath)) + { + FileAttributes attributes = File.GetAttributes(filePath); + File.SetAttributes(filePath, attributes | FileAttributes.ReadOnly); + + // 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); + } + } + + [Test] + [TestCase("")] + [TestCase(" ")] + [TestCase(null)] + public void CreateStructure_QueryIsNullOrWhiteSpace_ThrowsArgumentException(string query) + { + // Setup + string filename = Path.GetRandomFileName(); + string filePath = TestHelper.GetTestDataPath(TestDataPath.Migration.Core.Storage, filename); + + using (new FileDisposeHelper(filePath)) + using (var databaseFile = new RingtoetsDatabaseFile(filePath)) + { + databaseFile.OpenDatabaseConnection(); + + // Call + TestDelegate call = () => databaseFile.CreateStructure(query); + + // Assert + string paramName = Assert.Throws(call).ParamName; + Assert.AreEqual("query", paramName); + } + } + + [Test] + public void CreateStructure_InvalidQuery_ThrowsSQLiteException() + { + // Setup + string filename = Path.GetRandomFileName(); + string filePath = TestHelper.GetTestDataPath(TestDataPath.Migration.Core.Storage, filename); + + using (new FileDisposeHelper(filePath)) + using (var databaseFile = new RingtoetsDatabaseFile(filePath)) + { + databaseFile.OpenDatabaseConnection(); + + // Call + TestDelegate call = () => databaseFile.CreateStructure("THIS WILL FAIL"); + + // Assert + Assert.Throws(call); + } + } + + [Test] + public void CreateStructure_ValidQuery_CreatesFile() + { + // Setup + string filename = Path.GetRandomFileName(); + string filePath = TestHelper.GetTestDataPath(TestDataPath.Migration.Core.Storage, filename); + + using (var databaseFile = new RingtoetsDatabaseFile(filePath)) + { + databaseFile.OpenDatabaseConnection(); + + // Call + databaseFile.CreateStructure(";"); + + // Assert + Assert.IsTrue(File.Exists(filePath)); + } + + using (new FileDisposeHelper(filePath)) {} + } + + [Test] + [TestCase("")] + [TestCase(" ")] + [TestCase(null)] + public void ExecuteMigration_QueryIsNullOrWhiteSpace_ThrowsArgumentException(string query) + { + // Setup + string filename = Path.GetRandomFileName(); + string filePath = TestHelper.GetTestDataPath(TestDataPath.Migration.Core.Storage, filename); + + using (new FileDisposeHelper(filePath)) + using (var databaseFile = new RingtoetsDatabaseFile(filePath)) + { + databaseFile.OpenDatabaseConnection(); + + // Call + TestDelegate call = () => databaseFile.ExecuteMigration(query); + + // Assert + string paramName = Assert.Throws(call).ParamName; + Assert.AreEqual("query", paramName); + } + } + + [Test] + public void ExecuteMigration_InvalidQuery_ThrowsSQLiteException() + { + // Setup + string filename = Path.GetRandomFileName(); + string filePath = TestHelper.GetTestDataPath(TestDataPath.Migration.Core.Storage, filename); + + using (new FileDisposeHelper(filePath)) + using (var databaseFile = new RingtoetsDatabaseFile(filePath)) + { + databaseFile.OpenDatabaseConnection(); + + // Call + TestDelegate call = () => databaseFile.ExecuteMigration("THIS WILL FAIL"); + + // Assert + Assert.Throws(call); + } + } + + [Test] + public void ExecuteMigration_ValidQuery_CreatesFile() + { + // Setup + string filename = Path.GetRandomFileName(); + string filePath = TestHelper.GetTestDataPath(TestDataPath.Migration.Core.Storage, filename); + + using (var databaseFile = new RingtoetsDatabaseFile(filePath)) + { + databaseFile.OpenDatabaseConnection(); + + // Call + databaseFile.ExecuteMigration(";"); + + // Assert + Assert.IsTrue(File.Exists(filePath)); + } + + using (new FileDisposeHelper(filePath)) {} + } + + [Test] + public void Dispose_AlreadyDisposed_DoesNotThrowException() + { + // Setup + string filename = Path.GetRandomFileName(); + string filePath = TestHelper.GetTestDataPath(TestDataPath.Migration.Core.Storage, filename); + + // Call + TestDelegate call = () => + { + using (var databaseFile = new RingtoetsDatabaseFile(filePath)) + { + databaseFile.Dispose(); + } + }; + + using (new FileDisposeHelper(filePath)) + { + // Assert + Assert.DoesNotThrow(call); + } + } + } +} \ No newline at end of file