// Copyright (C) Stichting Deltares 2017. 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 System.Linq; using Core.Common.Base.Geometry; using Core.Common.Base.IO; using Core.Common.TestUtil; using NUnit.Framework; using Ringtoets.Common.Data.AssessmentSection; using Ringtoets.Common.IO.ReferenceLines; namespace Ringtoets.Common.IO.Test.ReferenceLines { [TestFixture] public class ReferenceLineReaderTest { [Test] public void ReadReferenceLine_FileHasOneValidLineInShape_ReturnReferenceLine() { // Setup string validReferenceLineShapeFile = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, Path.Combine("ReferenceLine", "traject_10-2.shp")); var reader = new ReferenceLineReader(); // Call ReferenceLine referenceLine = reader.ReadReferenceLine(validReferenceLineShapeFile); // Assert Point2D[] point2Ds = referenceLine.Points.ToArray(); Assert.AreEqual(803, point2Ds.Length); Assert.AreEqual(193527.156, point2Ds[465].X, 1e-6); Assert.AreEqual(511438.281, point2Ds[465].Y, 1e-6); Assert.AreEqual(191144.375, point2Ds[800].X, 1e-6); Assert.AreEqual(508840.469, point2Ds[800].Y, 1e-6); } [Test] [TestCase("")] [TestCase(" ")] [TestCase(null)] public void ReadReferenceLine_NoFilePath_ThrowArgumentException(string invalidFilePath) { // Setup var reader = new ReferenceLineReader(); // Call TestDelegate call = () => reader.ReadReferenceLine(invalidFilePath); // Assert string expectedMessage = string.Format("Fout bij het lezen van bestand '{0}': bestandspad mag niet leeg of ongedefinieerd zijn.", invalidFilePath); TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, expectedMessage); } [Test] public void ReadReferenceLine_FilePathHasInvalidPathCharacter_ThrowArgumentException() { // Setup char[] invalidPathChars = Path.GetInvalidPathChars(); string validFilePath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, "traject_10-1.shp"); string invalidFilePath = validFilePath.Replace("_", invalidPathChars[3].ToString()); var reader = new ReferenceLineReader(); // Call TestDelegate call = () => reader.ReadReferenceLine(invalidFilePath); // Assert string expectedMessage = $"Fout bij het lezen van bestand '{invalidFilePath}': " + "er zitten ongeldige tekens in het bestandspad. Alle tekens in het bestandspad moeten geldig zijn."; TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, expectedMessage); } [Test] public void ReadReferenceLine_FilePathIsActuallyDirectoryPath_ThrowArgumentException() { // Setup string invalidFilePath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, Path.DirectorySeparatorChar.ToString()); var reader = new ReferenceLineReader(); // Call TestDelegate call = () => reader.ReadReferenceLine(invalidFilePath); // Assert string expectedMessage = string.Format("Fout bij het lezen van bestand '{0}': bestandspad mag niet verwijzen naar een lege bestandsnaam.", invalidFilePath); TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, expectedMessage); } [Test] public void ReadReferenceLine_ShapefileDoesntExist_ThrowCriticalFileReadException() { // Setup string invalidFilePath = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, "I_do_not_exist.shp"); var reader = new ReferenceLineReader(); // Call TestDelegate call = () => reader.ReadReferenceLine(invalidFilePath); // Assert string expectedMessage = string.Format("Fout bij het lezen van bestand '{0}': het bestand bestaat niet.", invalidFilePath); string message = Assert.Throws(call).Message; Assert.AreEqual(expectedMessage, message); } [Test] [TestCase("Multiple_Point_with_ID.shp")] [TestCase("Multiple_Polygon_with_ID.shp")] [TestCase("Single_Multi-Polygon_with_ID.shp")] [TestCase("Single_Point_with_ID.shp")] [TestCase("Single_Polygon_with_ID.shp")] public void ReadReferenceLine_ShapefileContainsOtherThanPolyline_ThrowCriticalFileReadException(string shapeFileName) { // Setup string invalidFilePath = TestHelper.GetTestDataPath(TestDataPath.Core.Components.Gis.IO, shapeFileName); var reader = new ReferenceLineReader(); // Call TestDelegate call = () => reader.ReadReferenceLine(invalidFilePath); // Assert string expectedMessage = string.Format("Fout bij het lezen van bestand '{0}': kon geen lijnen vinden in dit bestand.", invalidFilePath); string message = Assert.Throws(call).Message; Assert.AreEqual(expectedMessage, message); } [Test] [TestCase("Empty_PolyLine_with_ID.shp")] [TestCase("Multiple_PolyLine_with_ID.shp")] public void ReadReferenceLine_ShapefileDoesNotHaveSinglePolyline_ThrowCriticalFileReadException(string shapeFileName) { // Setup string invalidFilePath = TestHelper.GetTestDataPath(TestDataPath.Core.Components.Gis.IO, shapeFileName); var reader = new ReferenceLineReader(); // Call TestDelegate call = () => reader.ReadReferenceLine(invalidFilePath); // Assert string expectedMessage = string.Format("Fout bij het lezen van bestand '{0}': het bestand moet exact één gehele polylijn bevatten.", invalidFilePath); string message = Assert.Throws(call).Message; Assert.AreEqual(expectedMessage, message); } [Test] public void ReadReferenceLine_ShapefileHasSingleMultiPolyline_ThrowCriticalFileReadException() { // Setup string invalidFilePath = TestHelper.GetTestDataPath(TestDataPath.Core.Components.Gis.IO, "Single_Multi-PolyLine_with_ID.shp"); var reader = new ReferenceLineReader(); // Call TestDelegate call = () => reader.ReadReferenceLine(invalidFilePath); // Assert string expectedMessage = string.Format("Fout bij het lezen van bestand '{0}': het bestand bevat een multi-polylijn. Multi-polylijnen worden niet ondersteund.", invalidFilePath); string message = Assert.Throws(call).Message; Assert.AreEqual(expectedMessage, message); } [Test] public void ReadReferenceLine_FileInUse_ThrowCriticalFileReadException() { // Setup var reader = new ReferenceLineReader(); string path = TestHelper.GetScratchPadPath(nameof(ReadReferenceLine_FileInUse_ThrowCriticalFileReadException)); using (var fileDisposeHelper = new FileDisposeHelper(path)) { fileDisposeHelper.LockFiles(); // Call TestDelegate call = () => reader.ReadReferenceLine(path); // Assert string expectedMessage = $"Fout bij het lezen van bestand '{path}': het bestand kon niet worden geopend. Mogelijk is het bestand corrupt of in gebruik door een andere applicatie."; var exception = Assert.Throws(call); Assert.AreEqual(expectedMessage, exception.Message); Assert.IsInstanceOf(exception.InnerException); } } } }