Index: Core/Common/src/Core.Common.IO/Readers/SqLiteDatabaseReaderBase.cs =================================================================== diff -u -r733d0d51adf737c52704cd80705507fdb34a2ba3 -r0415d97c8733babb1a31bc11de23ee4b0c5038b6 --- Core/Common/src/Core.Common.IO/Readers/SqLiteDatabaseReaderBase.cs (.../SqLiteDatabaseReaderBase.cs) (revision 733d0d51adf737c52704cd80705507fdb34a2ba3) +++ Core/Common/src/Core.Common.IO/Readers/SqLiteDatabaseReaderBase.cs (.../SqLiteDatabaseReaderBase.cs) (revision 0415d97c8733babb1a31bc11de23ee4b0c5038b6) @@ -84,11 +84,7 @@ /// public virtual void Dispose() { - if (Connection != null) - { - Connection.Close(); - Connection.Dispose(); - } + CloseConnection(); } /// @@ -128,6 +124,16 @@ } } + protected void CloseConnection() + { + if (Connection != null) + { + Connection.Close(); + Connection.Dispose(); + Connection = null; + } + } + /// /// Opens the connection with the . /// Index: Ringtoets/Piping/src/Ringtoets.Piping.IO/Ringtoets.Piping.IO.csproj =================================================================== diff -u -r25dd6ff5b74aec73e47df4488c05439a143711e1 -r0415d97c8733babb1a31bc11de23ee4b0c5038b6 --- Ringtoets/Piping/src/Ringtoets.Piping.IO/Ringtoets.Piping.IO.csproj (.../Ringtoets.Piping.IO.csproj) (revision 25dd6ff5b74aec73e47df4488c05439a143711e1) +++ Ringtoets/Piping/src/Ringtoets.Piping.IO/Ringtoets.Piping.IO.csproj (.../Ringtoets.Piping.IO.csproj) (revision 0415d97c8733babb1a31bc11de23ee4b0c5038b6) @@ -56,6 +56,7 @@ + Index: Ringtoets/Piping/src/Ringtoets.Piping.IO/SoilProfile/SoilDatabaseVersionReader.cs =================================================================== diff -u --- Ringtoets/Piping/src/Ringtoets.Piping.IO/SoilProfile/SoilDatabaseVersionReader.cs (revision 0) +++ Ringtoets/Piping/src/Ringtoets.Piping.IO/SoilProfile/SoilDatabaseVersionReader.cs (revision 0415d97c8733babb1a31bc11de23ee4b0c5038b6) @@ -0,0 +1,89 @@ +// 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; +using System.Data.SQLite; +using Core.Common.IO.Exceptions; +using Core.Common.IO.Readers; +using Core.Common.Utils.Builders; +using Ringtoets.Piping.IO.Builders; +using Ringtoets.Piping.IO.Properties; + +namespace Ringtoets.Piping.IO.SoilProfile +{ + /// + /// This class reads a soil database file and reads version from this database. + /// + public class SoilDatabaseVersionReader : SqLiteDatabaseReaderBase + { + private const string databaseRequiredVersion = "15.0.5.0"; + + /// + /// Creates a new instance of , + /// which will use the as its source. + /// + /// The path of the database file to open. + /// Thrown when: + /// The contains invalid characters. + /// No file could be found at . + /// The database version could not be read. + /// The database version is incorrect. + /// + public SoilDatabaseVersionReader(string databaseFilePath) : base(databaseFilePath) {} + + /// + /// Verifies if the database has the required version. + /// + /// Thrown when: + /// The database version could not be read. + /// The database version is incorrect. + /// + public void VerifyVersion() + { + var checkVersionQuery = SoilDatabaseQueryBuilder.GetCheckVersionQuery(); + var sqliteParameter = new SQLiteParameter + { + DbType = DbType.String, + ParameterName = String.Format("@{0}", MetaDataDatabaseColumns.Value), + Value = databaseRequiredVersion + }; + + try + { + using (SQLiteDataReader dataReader = CreateDataReader(checkVersionQuery, sqliteParameter)) + { + if (!dataReader.HasRows) + { + throw new CriticalFileReadException(String.Format( + Resources.PipingSoilProfileReader_Database_incorrect_version_requires_Version_0_, + databaseRequiredVersion)); + } + } + } + catch (SQLiteException exception) + { + var message = new FileReaderErrorMessageBuilder(Path).Build(Resources.StochasticSoilModelDatabaseReader_failed_to_read_database); + throw new CriticalFileReadException(message, exception); + } + } + } +} \ No newline at end of file Index: Ringtoets/Piping/src/Ringtoets.Piping.IO/SoilProfile/StochasticSoilModelDatabaseReader.cs =================================================================== diff -u -ra05bae3ae80a2099ae733f4349e468692b98d8f0 -r0415d97c8733babb1a31bc11de23ee4b0c5038b6 --- Ringtoets/Piping/src/Ringtoets.Piping.IO/SoilProfile/StochasticSoilModelDatabaseReader.cs (.../StochasticSoilModelDatabaseReader.cs) (revision a05bae3ae80a2099ae733f4349e468692b98d8f0) +++ Ringtoets/Piping/src/Ringtoets.Piping.IO/SoilProfile/StochasticSoilModelDatabaseReader.cs (.../StochasticSoilModelDatabaseReader.cs) (revision 0415d97c8733babb1a31bc11de23ee4b0c5038b6) @@ -39,7 +39,6 @@ /// public class StochasticSoilModelDatabaseReader : SqLiteDatabaseReaderBase { - private const string databaseRequiredVersion = "15.0.5.0"; private const string pipingMechanismName = "Piping"; private static readonly ILog log = LogManager.GetLogger(typeof(StochasticSoilModelDatabaseReader)); @@ -59,7 +58,18 @@ /// public StochasticSoilModelDatabaseReader(string databaseFilePath) : base(databaseFilePath) { - VerifyVersion(); + using (var versionReader = new SoilDatabaseVersionReader(databaseFilePath)) + { + try + { + versionReader.VerifyVersion(); + } + catch (CriticalFileReadException) + { + CloseConnection(); + throw; + } + } } /// @@ -118,13 +128,13 @@ } // Read SoilModels - var segmentSoilModel = ReadSoilModels(dataReader); + var segmentSoilModel = ReadStochasticSoilModelSegment(dataReader); if (currentStochasticSoilModelSegment == null || segmentSoilModel.SegmentSoilModelId != currentStochasticSoilModelSegment.SegmentSoilModelId) { currentStochasticSoilModelSegment = segmentSoilModel; - var probabilityList = ReadProbability(currentStochasticSoilModelSegment.SegmentSoilModelId); + var probabilityList = ReadStochasticSoilProfileProbability(currentStochasticSoilModelSegment.SegmentSoilModelId); if (probabilityList == null) { // Probability could not be read, ignore StochasticSoilModelSegment @@ -156,7 +166,7 @@ return true; } - private IEnumerable ReadProbability(long stochasticSoilModelId) + private IEnumerable ReadStochasticSoilProfileProbability(long stochasticSoilModelId) { var probabilityList = new List(); try @@ -167,7 +177,12 @@ currentStochasticSoilModelId = ReadStochasticSoilModelId(stochasticSoilProfilesDataReader); if (currentStochasticSoilModelId == stochasticSoilModelId) { - probabilityList.Add(ReadStochasticSoilProfileProbability(stochasticSoilProfilesDataReader)); + var stochasticSoilProfileProbability = ReadStochasticSoilProfileProbability(stochasticSoilProfilesDataReader); + if (stochasticSoilProfileProbability == null) + { + return null; + } + probabilityList.Add(stochasticSoilProfileProbability); } if (currentStochasticSoilModelId <= stochasticSoilModelId) { @@ -210,52 +225,22 @@ var soilProfileId = Convert.ToInt64(valueSoilProfile2DId); return new StochasticSoilProfileProbability(probability, SoilProfileType.SoilProfile2D, soilProfileId); } + log.Warn(Resources.StochasticSoilModelDatabaseReader_failed_to_read_soil_model); return null; } - private StochasticSoilModelSegment ReadSoilModels(SQLiteDataReader dataReader) + private static StochasticSoilModelSegment ReadStochasticSoilModelSegment(SQLiteDataReader dataReader) { var stochasticSoilModelId = Convert.ToInt64(dataReader[StochasticSoilModelDatabaseColumns.StochasticSoilModelId]); var stochasticSoilModelName = Convert.ToString(dataReader[StochasticSoilModelDatabaseColumns.StochasticSoilModelName]); var segmentName = Convert.ToString(dataReader[SegmentDatabaseColumns.SegmentName]); return new StochasticSoilModelSegment(stochasticSoilModelId, stochasticSoilModelName, segmentName); } - private void VerifyVersion() + private static Point2D ReadSegmentPoint(SQLiteDataReader dataReader) { - var checkVersionQuery = SoilDatabaseQueryBuilder.GetCheckVersionQuery(); - var sqliteParameter = new SQLiteParameter - { - DbType = DbType.String, - ParameterName = String.Format("@{0}", MetaDataDatabaseColumns.Value), - Value = databaseRequiredVersion - }; - try { - using (SQLiteDataReader dataReader = CreateDataReader(checkVersionQuery, sqliteParameter)) - { - if (!dataReader.HasRows) - { - Dispose(); - throw new CriticalFileReadException(String.Format( - Resources.PipingSoilProfileReader_Database_incorrect_version_requires_Version_0_, - databaseRequiredVersion)); - } - } - } - catch (SQLiteException exception) - { - Dispose(); - var message = new FileReaderErrorMessageBuilder(Path).Build(Resources.StochasticSoilModelDatabaseReader_failed_to_read_database); - throw new CriticalFileReadException(message, exception); - } - } - - private Point2D ReadSegmentPoint(SQLiteDataReader dataReader) - { - try - { double coordinateX = Convert.ToDouble(dataReader[SegmentPointsDatabaseColumns.CoordinateX]); double coordinateY = Convert.ToDouble(dataReader[SegmentPointsDatabaseColumns.CoordinateY]); return new Point2D(coordinateX, coordinateY); Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/Ringtoets.Piping.IO.Test.csproj =================================================================== diff -u -r25dd6ff5b74aec73e47df4488c05439a143711e1 -r0415d97c8733babb1a31bc11de23ee4b0c5038b6 --- Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/Ringtoets.Piping.IO.Test.csproj (.../Ringtoets.Piping.IO.Test.csproj) (revision 25dd6ff5b74aec73e47df4488c05439a143711e1) +++ Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/Ringtoets.Piping.IO.Test.csproj (.../Ringtoets.Piping.IO.Test.csproj) (revision 0415d97c8733babb1a31bc11de23ee4b0c5038b6) @@ -70,6 +70,7 @@ + Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/SoilProfile/PipingSoilProfileReaderTest.cs =================================================================== diff -u -r14db78eb3fc065c42f2d92c60d9d6b4b43dc9bd7 -r0415d97c8733babb1a31bc11de23ee4b0c5038b6 --- Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/SoilProfile/PipingSoilProfileReaderTest.cs (.../PipingSoilProfileReaderTest.cs) (revision 14db78eb3fc065c42f2d92c60d9d6b4b43dc9bd7) +++ Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/SoilProfile/PipingSoilProfileReaderTest.cs (.../PipingSoilProfileReaderTest.cs) (revision 0415d97c8733babb1a31bc11de23ee4b0c5038b6) @@ -49,7 +49,7 @@ var testFile = Path.Combine(testDataPath, "none.soil"); // Call - TestDelegate test = () => new PipingSoilProfileReader(testFile).Dispose(); + TestDelegate test = () => { using (new PipingSoilProfileReader(testFile)) {} }; // Assert var exception = Assert.Throws(test); @@ -63,7 +63,7 @@ public void Constructor_FileNullOrEmpty_ThrowsCriticalFileReadException(string fileName) { // Call - TestDelegate test = () => new PipingSoilProfileReader(fileName).Dispose(); + TestDelegate test = () => { using (new PipingSoilProfileReader(fileName)) {} }; // Assert var exception = Assert.Throws(test); @@ -84,7 +84,7 @@ Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile), "Precondition: file can be opened for edits."); // Call - TestDelegate test = () => new PipingSoilProfileReader(dbFile).Dispose(); + TestDelegate test = () => { using (new PipingSoilProfileReader(dbFile)) {} }; // Assert var exception = Assert.Throws(test); @@ -136,7 +136,7 @@ Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile), "Precondition: file can be opened for edits."); // Call - TestDelegate test = () => new PipingSoilProfileReader(dbFile).Dispose(); + TestDelegate test = () => { using (new PipingSoilProfileReader(dbFile)) {} }; // Assert var exception = Assert.Throws(test); Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/SoilProfile/SoilDatabaseVersionReaderTest.cs =================================================================== diff -u --- Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/SoilProfile/SoilDatabaseVersionReaderTest.cs (revision 0) +++ Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/SoilProfile/SoilDatabaseVersionReaderTest.cs (revision 0415d97c8733babb1a31bc11de23ee4b0c5038b6) @@ -0,0 +1,94 @@ +// 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.IO; +using Core.Common.IO.Exceptions; +using Core.Common.TestUtil; +using Core.Common.Utils.Builders; +using NUnit.Framework; +using Ringtoets.Piping.IO.Properties; +using Ringtoets.Piping.IO.SoilProfile; +using UtilsResources = Core.Common.Utils.Properties.Resources; + +namespace Ringtoets.Piping.IO.Test.SoilProfile +{ + [TestFixture] + public class SoilDatabaseVersionReaderTest + { + private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Piping.IO, "StochasticSoilModelDatabaseReader"); + + [Test] + public void Constructor_NonExistingPath_ThrowsCriticalFileReadException() + { + // Setup + var testFile = Path.Combine(testDataPath, "none.soil"); + + // Call + TestDelegate test = () => { using (new SoilDatabaseVersionReader(testFile)) {} }; + + // Assert + var exception = Assert.Throws(test); + var expectedMessage = new FileReaderErrorMessageBuilder(testFile).Build(UtilsResources.Error_File_does_not_exist); + Assert.AreEqual(expectedMessage, exception.Message); + } + + [Test] + [TestCase(null)] + [TestCase("")] + public void Constructor_FileNullOrEmpty_ThrowsCriticalFileReadException(string fileName) + { + // Setup + var expectedMessage = String.Format("Fout bij het lezen van bestand '{0}': {1}", + fileName, UtilsResources.Error_Path_must_be_specified); + // Call + TestDelegate test = () => { using (new SoilDatabaseVersionReader(fileName)) {} }; + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual(expectedMessage, exception.Message); + } + + [Test] + public void Constructor_IncorrectVersion_ThrowsCriticalFileReadException() + { + // Setup + const string version = "15.0.5.0"; + string expectedVersionMessage = String.Format(Resources.PipingSoilProfileReader_Database_incorrect_version_requires_Version_0_, version); + const string dbName = "incorrectversion.soil"; + var dbFile = Path.Combine(testDataPath, dbName); + + // Precondition + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile), "Precondition: file can be opened for edits."); + + using (var versionReader = new SoilDatabaseVersionReader(dbFile)) + { + // Call + TestDelegate test = () => versionReader.VerifyVersion(); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual(expectedVersionMessage, exception.Message); + } + Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile)); + } + } +} \ No newline at end of file Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/SoilProfile/StochasticSoilModelDatabaseReaderTest.cs =================================================================== diff -u -r25dd6ff5b74aec73e47df4488c05439a143711e1 -r0415d97c8733babb1a31bc11de23ee4b0c5038b6 --- Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/SoilProfile/StochasticSoilModelDatabaseReaderTest.cs (.../StochasticSoilModelDatabaseReaderTest.cs) (revision 25dd6ff5b74aec73e47df4488c05439a143711e1) +++ Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/SoilProfile/StochasticSoilModelDatabaseReaderTest.cs (.../StochasticSoilModelDatabaseReaderTest.cs) (revision 0415d97c8733babb1a31bc11de23ee4b0c5038b6) @@ -125,7 +125,7 @@ Assert.IsTrue(TestHelper.CanOpenFileForWrite(dbFile), "Precondition: file can be opened for edits."); // Call - TestDelegate test = () => new StochasticSoilModelDatabaseReader(dbFile).Dispose(); + TestDelegate test = () => { using (var s = new StochasticSoilModelDatabaseReader(dbFile)) {} }; // Assert var exception = Assert.Throws(test); @@ -250,7 +250,7 @@ } [Test] - public void GetStochasticSoilModelSegmentOfPiping_InvalidSegmentPoint_SkipsPoint() + public void GetStochasticSoilModelSegmentOfPiping_InvalidSegmentPoint_SkipsSegmentPoint() { // Setup var dbName = "invalidSegmentPoint.soil"; Index: Ringtoets/Piping/test/Ringtoets.Piping.IO.Test/test-data/StochasticSoilModelDatabaseReader/invalidSegmentPoint.soil =================================================================== diff -u -r14db78eb3fc065c42f2d92c60d9d6b4b43dc9bd7 -r0415d97c8733babb1a31bc11de23ee4b0c5038b6 Binary files differ