Index: Core/Common/src/Core.Common.IO/Core.Common.IO.csproj =================================================================== diff -u -r968645e02cdc2c21c899fa8099589c779138be54 -r9a4c74a9d2cc49987adf0049919baafa6e12a618 --- Core/Common/src/Core.Common.IO/Core.Common.IO.csproj (.../Core.Common.IO.csproj) (revision 968645e02cdc2c21c899fa8099589c779138be54) +++ Core/Common/src/Core.Common.IO/Core.Common.IO.csproj (.../Core.Common.IO.csproj) (revision 9a4c74a9d2cc49987adf0049919baafa6e12a618) @@ -51,7 +51,7 @@ - + Index: Core/Common/src/Core.Common.IO/Readers/StreamReaderHelper.cs =================================================================== diff -u --- Core/Common/src/Core.Common.IO/Readers/StreamReaderHelper.cs (revision 0) +++ Core/Common/src/Core.Common.IO/Readers/StreamReaderHelper.cs (revision 9a4c74a9d2cc49987adf0049919baafa6e12a618) @@ -0,0 +1,65 @@ +// 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 Lesser 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser 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.Utils.Builders; +using Core.Common.Utils.Properties; + +namespace Core.Common.IO.Readers +{ + /// + /// This class provides helper functions for reading UTF8 encoded files. + /// + public static class StreamReaderHelper + { + /// + /// Initializes the stream reader for a UTF8 encoded file. + /// + /// The path to the file to be read. + /// A UTF8 encoding configured stream reader opened on . + /// File/directory cannot be found or + /// some other I/O related problem occurred. + public static StreamReader InitializeStreamReader(string path) + { + try + { + return new StreamReader(path); + } + catch (FileNotFoundException e) + { + string message = new FileReaderErrorMessageBuilder(path).Build(Resources.Error_File_does_not_exist); + throw new CriticalFileReadException(message, e); + } + catch (DirectoryNotFoundException e) + { + string message = new FileReaderErrorMessageBuilder(path).Build(Resources.Error_Directory_missing); + throw new CriticalFileReadException(message, e); + } + catch (IOException e) + { + var message = new FileReaderErrorMessageBuilder(path).Build(String.Format(Resources.Error_General_IO_ErrorMessage_0_, e.Message)); + throw new CriticalFileReadException(message, e); + } + } + } +} \ No newline at end of file Fisheye: Tag 9a4c74a9d2cc49987adf0049919baafa6e12a618 refers to a dead (removed) revision in file `Core/Common/src/Core.Common.IO/StreamReaderHelper.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Core/Common/test/Core.Common.IO.Test/Core.Common.IO.Test.csproj =================================================================== diff -u -r16fef01c5d2d8ef8d15c652585efa85125ba7b25 -r9a4c74a9d2cc49987adf0049919baafa6e12a618 --- Core/Common/test/Core.Common.IO.Test/Core.Common.IO.Test.csproj (.../Core.Common.IO.Test.csproj) (revision 16fef01c5d2d8ef8d15c652585efa85125ba7b25) +++ Core/Common/test/Core.Common.IO.Test/Core.Common.IO.Test.csproj (.../Core.Common.IO.Test.csproj) (revision 9a4c74a9d2cc49987adf0049919baafa6e12a618) @@ -53,6 +53,7 @@ + Index: Core/Common/test/Core.Common.IO.Test/Readers/StreamReaderHelperTest.cs =================================================================== diff -u --- Core/Common/test/Core.Common.IO.Test/Readers/StreamReaderHelperTest.cs (revision 0) +++ Core/Common/test/Core.Common.IO.Test/Readers/StreamReaderHelperTest.cs (revision 9a4c74a9d2cc49987adf0049919baafa6e12a618) @@ -0,0 +1,82 @@ +// 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 Lesser 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser 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.IO; +using Core.Common.IO.Exceptions; +using Core.Common.IO.Readers; +using Core.Common.TestUtil; +using NUnit.Framework; + +namespace Core.Common.IO.Test.Readers +{ + [TestFixture] + public class StreamReaderHelperTest + { + private readonly string testDataPath = TestHelper.GetTestDataPath(TestDataPath.Core.Common.IO, "StreamReaderHelper"); + private readonly string notExistingTestDataPath = TestHelper.GetTestDataPath(TestDataPath.Core.Common.IO, "NotExistingFolder"); + + [Test] + public void InitializeStreamReader_ValidFile_ReturnsStreamReader() + { + // Setup + var filePath = Path.Combine(testDataPath, "empty.csv"); + + // Call + using (var streamReader = StreamReaderHelper.InitializeStreamReader(filePath)) + { + // Assert + Assert.IsInstanceOf(streamReader); + } + } + + [Test] + public void InitializeStreamReader_NotExistingFile_ThrowsCriticalFileReadExceptionWithInnerFileNotFoundException() + { + // Setup + var filePath = "nothing"; + var expectedMessage = string.Format("Fout bij het lezen van bestand '{0}': Het bestand bestaat niet.", filePath); + + // Call + TestDelegate call = () => StreamReaderHelper.InitializeStreamReader(filePath); + + // Assert + var exception = Assert.Throws(call); + Assert.AreEqual(expectedMessage, exception.Message); + Assert.IsInstanceOf(exception.InnerException); + } + + [Test] + public void InitializeStreamReader_NotExistingFolder_ThrowsCriticalFileReadExceptionWithInnerDirectoryNotFoundException() + { + // Setup + var filePath = Path.Combine(notExistingTestDataPath, "empty.csv"); + var expectedMessage = string.Format("Fout bij het lezen van bestand '{0}': Het bestandspad verwijst naar een map die niet bestaat.", filePath); + + // Call + TestDelegate call = () => StreamReaderHelper.InitializeStreamReader(filePath); + + // Assert + var exception = Assert.Throws(call); + Assert.AreEqual(expectedMessage, exception.Message); + Assert.IsInstanceOf(exception.InnerException); + } + } +} Index: Core/Common/test/Core.Common.IO.Test/test-data/StreamReaderHelper/empty.csv =================================================================== diff -u --- Core/Common/test/Core.Common.IO.Test/test-data/StreamReaderHelper/empty.csv (revision 0) +++ Core/Common/test/Core.Common.IO.Test/test-data/StreamReaderHelper/empty.csv (revision 9a4c74a9d2cc49987adf0049919baafa6e12a618) @@ -0,0 +1 @@ \ No newline at end of file Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/IO/HydraRingSettingsCsvReader.cs =================================================================== diff -u -rfe9af51d0ecee9dc64884d9370cf951fe787a8bc -r9a4c74a9d2cc49987adf0049919baafa6e12a618 --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/IO/HydraRingSettingsCsvReader.cs (.../HydraRingSettingsCsvReader.cs) (revision fe9af51d0ecee9dc64884d9370cf951fe787a8bc) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/IO/HydraRingSettingsCsvReader.cs (.../HydraRingSettingsCsvReader.cs) (revision 9a4c74a9d2cc49987adf0049919baafa6e12a618) @@ -28,13 +28,13 @@ namespace Ringtoets.HydraRing.Calculation.IO { /// - /// The reader for the HydraRingSettings in csv format. + /// The reader for the Hydra- Ring settings in csv format. /// public class HydraRingSettingsCsvReader { private const char separator = ';'; - private readonly string filePath; + private readonly string fileContents; private readonly IDictionary>> settings = new Dictionary>>(); @@ -49,14 +49,14 @@ {formRelaxationFactorKey, 6}, {formEpsBetaKey, 7}, {formEpsHohKey, 8}, - {formEpsZFunc, 9}, - {dsStartMethod, 10}, - {dsMinNumberOfIterations, 11}, - {dsMaxNumberOfIterations, 12}, - {dsVarCoefficient, 13}, - {niUMin, 14}, - {niUMax, 15}, - {niNumberSteps, 16} + {formEpsZFuncKey, 9}, + {dsStartMethodKey, 10}, + {dsMinNumberOfIterationsKey, 11}, + {dsMaxNumberOfIterationsKey, 12}, + {dsVarCoefficientKey, 13}, + {niUMinKey, 14}, + {niUMaxKey, 15}, + {niNumberStepsKey, 16} }; /// @@ -71,7 +71,7 @@ throw new ArgumentNullException("file", "A file must be set."); } - filePath = file; + fileContents = file; } /// @@ -80,7 +80,7 @@ /// A with the settings. public IDictionary>> ReadSettings() { - string[] lines = filePath.Split('\n'); + string[] lines = fileContents.Split('\n'); foreach (string line in lines.Skip(1).Where(s => !string.IsNullOrEmpty(s))) { @@ -90,8 +90,7 @@ return settings; } - - private void CreateSetting(string[] line) + private void CreateSetting(IList line) { // Get failure mechanism int failureMechanismType = GetFailureMechanismType(line); @@ -123,7 +122,7 @@ return GetIntValueFromElement(line[columns[mechanismIdKey]]); } - private int GetSubMechanismType(string[] line) + private int GetSubMechanismType(IList line) { return GetIntValueFromElement(line[columns[subMechanismIdKey]]); } @@ -141,14 +140,14 @@ GetDoubleValueFromElement(line[columns[formRelaxationFactorKey]]), GetDoubleValueFromElement(line[columns[formEpsBetaKey]]), GetDoubleValueFromElement(line[columns[formEpsHohKey]]), - GetDoubleValueFromElement(line[columns[formEpsZFunc]]), - GetIntValueFromElement(line[columns[dsStartMethod]]), - GetIntValueFromElement(line[columns[dsMinNumberOfIterations]]), - GetIntValueFromElement(line[columns[dsMaxNumberOfIterations]]), - GetDoubleValueFromElement(line[columns[dsVarCoefficient]]), - GetDoubleValueFromElement(line[columns[niUMin]]), - GetDoubleValueFromElement(line[columns[niUMax]]), - GetIntValueFromElement(line[columns[niNumberSteps]])); + GetDoubleValueFromElement(line[columns[formEpsZFuncKey]]), + GetIntValueFromElement(line[columns[dsStartMethodKey]]), + GetIntValueFromElement(line[columns[dsMinNumberOfIterationsKey]]), + GetIntValueFromElement(line[columns[dsMaxNumberOfIterationsKey]]), + GetDoubleValueFromElement(line[columns[dsVarCoefficientKey]]), + GetDoubleValueFromElement(line[columns[niUMinKey]]), + GetDoubleValueFromElement(line[columns[niUMaxKey]]), + GetIntValueFromElement(line[columns[niNumberStepsKey]])); } private static int GetIntValueFromElement(string element) @@ -183,14 +182,14 @@ private const string formRelaxationFactorKey = "FORM_RelaxationFactor"; private const string formEpsBetaKey = "FORM_EpsBeta"; private const string formEpsHohKey = "FORM_EpsHOH"; - private const string formEpsZFunc = "FORM_EpsZFunc"; - private const string dsStartMethod = "Ds_StartMethod"; - private const string dsMinNumberOfIterations = "Ds_Min"; - private const string dsMaxNumberOfIterations = "Ds_Max"; - private const string dsVarCoefficient = "Ds_VarCoefficient"; - private const string niUMin = "NI_UMin"; - private const string niUMax = "NI_Umax"; - private const string niNumberSteps = "NI_NumberSteps"; + private const string formEpsZFuncKey = "FORM_EpsZFunc"; + private const string dsStartMethodKey = "Ds_StartMethod"; + private const string dsMinNumberOfIterationsKey = "Ds_Min"; + private const string dsMaxNumberOfIterationsKey = "Ds_Max"; + private const string dsVarCoefficientKey = "Ds_VarCoefficient"; + private const string niUMinKey = "NI_UMin"; + private const string niUMaxKey = "NI_Umax"; + private const string niNumberStepsKey = "NI_NumberSteps"; #endregion } Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Ringtoets.HydraRing.Calculation.csproj =================================================================== diff -u -r890690b5780ce9721f94934d5c5788ea188d2203 -r9a4c74a9d2cc49987adf0049919baafa6e12a618 --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Ringtoets.HydraRing.Calculation.csproj (.../Ringtoets.HydraRing.Calculation.csproj) (revision 890690b5780ce9721f94934d5c5788ea188d2203) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.Calculation/Ringtoets.HydraRing.Calculation.csproj (.../Ringtoets.HydraRing.Calculation.csproj) (revision 9a4c74a9d2cc49987adf0049919baafa6e12a618) @@ -68,14 +68,6 @@ - - {E344867E-9AC9-44C8-88A5-8185681679A9} - Core.Common.IO - - - {F49BD8B2-332A-4C91-A196-8CCE0A2C7D98} - Core.Common.Utils - {c90b77da-e421-43cc-b82e-529651bc21ac} Core.Common.Version Index: Ringtoets/HydraRing/test/Ringtoets.HydraRing.Calculation.Test/IO/HydraRingSettingsCsvReaderTest.cs =================================================================== diff -u -rfe9af51d0ecee9dc64884d9370cf951fe787a8bc -r9a4c74a9d2cc49987adf0049919baafa6e12a618 --- Ringtoets/HydraRing/test/Ringtoets.HydraRing.Calculation.Test/IO/HydraRingSettingsCsvReaderTest.cs (.../HydraRingSettingsCsvReaderTest.cs) (revision fe9af51d0ecee9dc64884d9370cf951fe787a8bc) +++ Ringtoets/HydraRing/test/Ringtoets.HydraRing.Calculation.Test/IO/HydraRingSettingsCsvReaderTest.cs (.../HydraRingSettingsCsvReaderTest.cs) (revision 9a4c74a9d2cc49987adf0049919baafa6e12a618) @@ -60,50 +60,52 @@ // Setup var testFile = Path.Combine(testDataPath, "HydraRingSettingsTest.csv"); - var streamReader = new StreamReader(testFile); - var fileContents = streamReader.ReadToEnd(); + using (var streamReader = new StreamReader(testFile)) + { + var fileContents = streamReader.ReadToEnd(); - var reader = new HydraRingSettingsCsvReader(fileContents); - var expectedDictionary = GetDictionary(); + var reader = new HydraRingSettingsCsvReader(fileContents); + var expectedDictionary = GetDictionary(); - // Call - var settings = reader.ReadSettings(); + // Call + var settings = reader.ReadSettings(); - // Assert - Assert.IsInstanceOf>>>(settings); - Assert.AreEqual(2, settings.Count); + // Assert + Assert.IsInstanceOf>>>(settings); + Assert.AreEqual(2, settings.Count); - foreach (KeyValuePair>> expectedMechanism in expectedDictionary) - { - Assert.IsTrue(settings.ContainsKey(expectedMechanism.Key)); - Assert.IsInstanceOf>>(settings[expectedMechanism.Key]); - - foreach (KeyValuePair> expectedSubMechanism in expectedMechanism.Value) + foreach (KeyValuePair>> expectedMechanism in expectedDictionary) { - Assert.IsTrue(settings[expectedMechanism.Key].ContainsKey(expectedSubMechanism.Key)); - Assert.IsInstanceOf>(settings[expectedMechanism.Key][expectedSubMechanism.Key]); + Assert.IsTrue(settings.ContainsKey(expectedMechanism.Key)); + Assert.IsInstanceOf>>(settings[expectedMechanism.Key]); - foreach (KeyValuePair expectedSubMechanismSettings in expectedSubMechanism.Value) + foreach (KeyValuePair> expectedSubMechanism in expectedMechanism.Value) { - Assert.IsTrue(settings[expectedMechanism.Key][expectedSubMechanism.Key].ContainsKey(expectedSubMechanismSettings.Key)); - Assert.IsInstanceOf(settings[expectedMechanism.Key][expectedSubMechanism.Key][expectedSubMechanismSettings.Key]); + Assert.IsTrue(settings[expectedMechanism.Key].ContainsKey(expectedSubMechanism.Key)); + Assert.IsInstanceOf>(settings[expectedMechanism.Key][expectedSubMechanism.Key]); - SubMechanismSettings setting = settings[expectedMechanism.Key][expectedSubMechanism.Key][expectedSubMechanismSettings.Key]; + foreach (KeyValuePair expectedSubMechanismSettings in expectedSubMechanism.Value) + { + Assert.IsTrue(settings[expectedMechanism.Key][expectedSubMechanism.Key].ContainsKey(expectedSubMechanismSettings.Key)); + Assert.IsInstanceOf(settings[expectedMechanism.Key][expectedSubMechanism.Key][expectedSubMechanismSettings.Key]); - Assert.AreEqual(expectedSubMechanismSettings.Value.CalculationTechniqueId, setting.CalculationTechniqueId); - Assert.AreEqual(expectedSubMechanismSettings.Value.FormStartMethod, setting.FormStartMethod); - Assert.AreEqual(expectedSubMechanismSettings.Value.FormNumberOfIterations, setting.FormNumberOfIterations); - Assert.AreEqual(expectedSubMechanismSettings.Value.FormRelaxationFactor, setting.FormRelaxationFactor); - Assert.AreEqual(expectedSubMechanismSettings.Value.FormEpsBeta, setting.FormEpsBeta); - Assert.AreEqual(expectedSubMechanismSettings.Value.FormEpsHoh, setting.FormEpsHoh); - Assert.AreEqual(expectedSubMechanismSettings.Value.FormEpsZFunc, setting.FormEpsZFunc); - Assert.AreEqual(expectedSubMechanismSettings.Value.DsStartMethod, setting.DsStartMethod); - Assert.AreEqual(expectedSubMechanismSettings.Value.DsMaxNumberOfIterations, setting.DsMaxNumberOfIterations); - Assert.AreEqual(expectedSubMechanismSettings.Value.DsMinNumberOfIterations, setting.DsMinNumberOfIterations); - Assert.AreEqual(expectedSubMechanismSettings.Value.DsVarCoefficient, setting.DsVarCoefficient); - Assert.AreEqual(expectedSubMechanismSettings.Value.NiNumberSteps, setting.NiNumberSteps); - Assert.AreEqual(expectedSubMechanismSettings.Value.NiUMax, setting.NiUMax); - Assert.AreEqual(expectedSubMechanismSettings.Value.NiUMin, setting.NiUMin); + SubMechanismSettings setting = settings[expectedMechanism.Key][expectedSubMechanism.Key][expectedSubMechanismSettings.Key]; + + Assert.AreEqual(expectedSubMechanismSettings.Value.CalculationTechniqueId, setting.CalculationTechniqueId); + Assert.AreEqual(expectedSubMechanismSettings.Value.FormStartMethod, setting.FormStartMethod); + Assert.AreEqual(expectedSubMechanismSettings.Value.FormNumberOfIterations, setting.FormNumberOfIterations); + Assert.AreEqual(expectedSubMechanismSettings.Value.FormRelaxationFactor, setting.FormRelaxationFactor); + Assert.AreEqual(expectedSubMechanismSettings.Value.FormEpsBeta, setting.FormEpsBeta); + Assert.AreEqual(expectedSubMechanismSettings.Value.FormEpsHoh, setting.FormEpsHoh); + Assert.AreEqual(expectedSubMechanismSettings.Value.FormEpsZFunc, setting.FormEpsZFunc); + Assert.AreEqual(expectedSubMechanismSettings.Value.DsStartMethod, setting.DsStartMethod); + Assert.AreEqual(expectedSubMechanismSettings.Value.DsMaxNumberOfIterations, setting.DsMaxNumberOfIterations); + Assert.AreEqual(expectedSubMechanismSettings.Value.DsMinNumberOfIterations, setting.DsMinNumberOfIterations); + Assert.AreEqual(expectedSubMechanismSettings.Value.DsVarCoefficient, setting.DsVarCoefficient); + Assert.AreEqual(expectedSubMechanismSettings.Value.NiNumberSteps, setting.NiNumberSteps); + Assert.AreEqual(expectedSubMechanismSettings.Value.NiUMax, setting.NiUMax); + Assert.AreEqual(expectedSubMechanismSettings.Value.NiUMin, setting.NiUMin); + } } } } Index: Ringtoets/Piping/src/Ringtoets.Piping.IO/SurfaceLines/CharacteristicPointsCsvReader.cs =================================================================== diff -u -rcd17802422ec70791daa96e1f536bf505ecfe954 -r9a4c74a9d2cc49987adf0049919baafa6e12a618 --- Ringtoets/Piping/src/Ringtoets.Piping.IO/SurfaceLines/CharacteristicPointsCsvReader.cs (.../CharacteristicPointsCsvReader.cs) (revision cd17802422ec70791daa96e1f536bf505ecfe954) +++ Ringtoets/Piping/src/Ringtoets.Piping.IO/SurfaceLines/CharacteristicPointsCsvReader.cs (.../CharacteristicPointsCsvReader.cs) (revision 9a4c74a9d2cc49987adf0049919baafa6e12a618) @@ -27,6 +27,7 @@ using Core.Common.Base.Geometry; using Core.Common.IO; using Core.Common.IO.Exceptions; +using Core.Common.IO.Readers; using Core.Common.Utils; using Core.Common.Utils.Builders; using Ringtoets.Piping.IO.Properties; Index: Ringtoets/Piping/src/Ringtoets.Piping.IO/SurfaceLines/PipingSurfaceLinesCsvReader.cs =================================================================== diff -u -r968645e02cdc2c21c899fa8099589c779138be54 -r9a4c74a9d2cc49987adf0049919baafa6e12a618 --- Ringtoets/Piping/src/Ringtoets.Piping.IO/SurfaceLines/PipingSurfaceLinesCsvReader.cs (.../PipingSurfaceLinesCsvReader.cs) (revision 968645e02cdc2c21c899fa8099589c779138be54) +++ Ringtoets/Piping/src/Ringtoets.Piping.IO/SurfaceLines/PipingSurfaceLinesCsvReader.cs (.../PipingSurfaceLinesCsvReader.cs) (revision 9a4c74a9d2cc49987adf0049919baafa6e12a618) @@ -27,6 +27,7 @@ using Core.Common.Base.Geometry; using Core.Common.IO; using Core.Common.IO.Exceptions; +using Core.Common.IO.Readers; using Core.Common.Utils; using Core.Common.Utils.Builders; using Ringtoets.Piping.Data;