// Copyright (C) Stichting Deltares 2024. All rights reserved. // // This file is part of the application DAM - UI. // // DAM - UI 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 System.Threading; using NUnit.Framework; namespace Deltares.Maps.Tests.Services { [TestFixture] public class ShapeFileCreatorTest { const string OutputFolder = "ShapeFileTestFiles"; private FeatureRepository repository; private string fileName; private string shpFile; private string dbfFile; private string shxFile; private string shpFilePath; private string shxFilePath; private string dbfFilePath; [Test] public void CreateFile_ValidPathAndNoFeatures_Throws() { Assert.That(() => ShapeFileCreator.Create(@"", fileName, repository), Throws.ArgumentException); } [Test] public void CreateFile_InvalidName_Throws() { Assert.That(() => ShapeFileCreator.Create(@"", "", repository), Throws.ArgumentNullException); } [Test] [Ignore("Need to fix shape file writing")] public void CreateFile_RepositoryContainsNotSupportedFeature_Throws() { repository.Add(Feature.Create("POLYGON (( 10 10, 10 20, 20 20, 20 15, 10 10))")); Assert.That(() => ShapeFileCreator.Create(@"", fileName, repository), Throws.InstanceOf().With.Message.EqualTo("The repository contains geometry types which are currently not supported")); } [Test] [Ignore("Need to fix shape file writing")] public void CreateFile_RepositoryContainsDifferentGeometryTypes_Throws() { repository.Add(Feature.Create("LINESTRING EMPTY")); repository.Add(Feature.Create("POINT(0 0)")); Assert.That(() => ShapeFileCreator.Create(@"", fileName, repository), Throws.InstanceOf().With.Message.EqualTo("The repository contains different geometry types. Currently only one type per repository is supported ")); } [Test] [Ignore("Need to fix shape file writing")] public void CreateFile_ValidPathAndRepositoryContainsOneLineString_ShapeFileIsCreated() { repository.Add(Feature.Create("LINESTRING (10 10, 10 20)")); ShapeFileCreator.Create(OutputFolder, fileName, repository); AssertThatMandatoryFilesExist(); var shapeFile = new ShapeFileLocation(shpFilePath); IFeatureRepository retrievedRepository = FeatureRepository.CreateFromShapeFile(shapeFile); Assert.That(retrievedRepository, Is.Not.Null); Assert.That(retrievedRepository.Count, Is.EqualTo(1)); Assert.That(repository.Features.ElementAt(0).Geometry.GeometryType, Is.EqualTo("LineString")); } [Test] [Ignore("Need to fix shape file writing")] public void CreateFile_ValidPathAndRepositoryContainsOneLineStringAndOneMultiLineString_ShapeFileIsCreated() { repository.Add(Feature.Create("LINESTRING (10 10, 10 20)")); repository.Add(Feature.Create("MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))")); ShapeFileCreator.Create(OutputFolder, fileName, repository); AssertThatMandatoryFilesExist(); var shapeFile = new ShapeFileLocation(shpFilePath); IFeatureRepository retrievedRepository = FeatureRepository.CreateFromShapeFile(shapeFile); Assert.That(retrievedRepository, Is.Not.Null); Assert.That(retrievedRepository.Count, Is.EqualTo(2)); Assert.That(repository.Features.Any(f => f.Geometry.GeometryType == "LineString"), Is.True); Assert.That(repository.Features.Any(f => f.Geometry.GeometryType == "MultiLineString"), Is.True); } [Test] [Ignore("Need to fix shape file writing")] public void CreateFile_ValidPathAndRepositoryContainsOnePoint_ShapeFileIsCreated() { repository.Add(Feature.Create("POINT(0 0)")); ShapeFileCreator.Create(OutputFolder, fileName, repository); AssertThatMandatoryFilesExist(); var shapeFile = new ShapeFileLocation(shpFilePath); IFeatureRepository retrievedRepository = FeatureRepository.CreateFromShapeFile(shapeFile); Assert.That(retrievedRepository, Is.Not.Null); Assert.That(retrievedRepository.Count, Is.EqualTo(1)); Assert.That(repository.Features.ElementAt(0).Geometry.GeometryType, Is.EqualTo("GeometryPoint")); } /* [Test] public void CreateFile_ValidPathAndRepositoryContainsMultyPoint_ShapeFileIsCreated() { //repository.Add(Feature.Create("POINT(0 0)")); repository.Add(Feature.Create("MULTIPOINT (10 40, 40 30, 20 20, 30 10)")); ShapeFileCreator.Create(OutputFolder, fileName, repository); AssertThatMandatoryFilesExist(); var shapeFile = new ShapeFile(this.shpFilePath); var retrievedRepository = FeatureRepository.CreateFromShapeFile(shapeFile); Assert.IsNotNull(retrievedRepository); Assert.IsTrue(retrievedRepository.Count == 2); Assert.IsTrue(repository.Features.Any(f => f.Geometry.GeometryType == GeometryType2.MultiPoint)); Assert.IsTrue(repository.Features.Any(f => f.Geometry.GeometryType == GeometryType2.GeometryPoint)); }*/ private void AssertThatMandatoryFilesExist() { Assert.That(File.Exists(shpFilePath), Is.True, $"{shpFile} does not exist"); Assert.That(File.Exists(dbfFilePath), Is.True, $"{dbfFile} does not exist"); Assert.That(File.Exists(shxFilePath), Is.True, $"{shxFile} does not exist"); } #region Setup [SetUp] public void FixtureSetup() { if (Directory.Exists(OutputFolder)) { //Directory.Delete(OutputFolder, true); // TODO: Disabled to prevent files from being deleted in test data dir (although copied there for other tests) } Directory.CreateDirectory(OutputFolder); } [TearDown] public void FixtureTearDown() {} [SetUp] public void TestSetup() { fileName = "TestFile_" + Guid.NewGuid().ToString().Replace("-", "_"); shpFile = fileName + ".shp"; dbfFile = fileName + ".dbf"; shxFile = fileName + ".shx"; shpFilePath = Path.Combine(OutputFolder, shpFile); shxFilePath = Path.Combine(OutputFolder, shxFile); dbfFilePath = Path.Combine(OutputFolder, dbfFile); if (File.Exists(shpFilePath)) { File.Delete(shpFilePath); } if (File.Exists(shxFilePath)) { File.Delete(shxFilePath); } if (File.Exists(dbfFilePath)) { File.Delete(dbfFilePath); } repository = new FeatureRepository(); } [TearDown] public void TestTearDown() { if (File.Exists(shpFilePath)) { File.Delete(shpFilePath); } if (File.Exists(shxFilePath)) { File.Delete(shxFilePath); } if (File.Exists(dbfFilePath)) { // disposing the dbf file could take longer for (var i = 0; i < 3; i++) { try { File.Delete(dbfFilePath); } catch (IOException) { Thread.Sleep(200); } } } } #endregion } }