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; #region Setup [TestFixtureSetUp] 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); } [TestFixtureTearDown] 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 (int i = 0; i < 3; i++) { try { File.Delete(dbfFilePath); } catch (IOException) { Thread.Sleep(200); } } } } #endregion [Test] [ExpectedException(typeof(ArgumentException))] public void CreateFile_ValidPathAndNoFeatures_Throws() { ShapeFileCreator.Create(@"", fileName, repository); } [Test] [ExpectedException(typeof(ArgumentNullException))] public void CreateFile_InvalidName_Throws() { ShapeFileCreator.Create(@"", "", repository); } [Test] [Ignore("Need to fix shape file writing")] [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The repository contains geometry types which are currently not supported")] public void CreateFile_RepositoryContainsNotSupportedFeature_Throws() { repository.Add(Feature.Create("POLYGON (( 10 10, 10 20, 20 20, 20 15, 10 10))")); ShapeFileCreator.Create(@"", fileName, repository); } [Test] [Ignore("Need to fix shape file writing")] [ExpectedException(typeof(NotSupportedException), ExpectedMessage = "The repository contains different geometry types. Currently only one type per repository is supported ")] public void CreateFile_RepositoryContainsDifferentGeometryTypes_Throws() { repository.Add(Feature.Create("LINESTRING EMPTY")); repository.Add(Feature.Create("POINT(0 0)")); ShapeFileCreator.Create(@"", fileName, repository); } [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.IsNotNull(retrievedRepository); Assert.IsTrue(retrievedRepository.Count == 1); Assert.AreEqual("LineString", repository.Features.ElementAt(0).Geometry.GeometryType); } [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.IsNotNull(retrievedRepository); Assert.IsTrue(retrievedRepository.Count == 2); Assert.IsTrue(repository.Features.Any(f => f.Geometry.GeometryType == "LineString")); Assert.IsTrue(repository.Features.Any(f => f.Geometry.GeometryType == "MultiLineString")); } [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.IsNotNull(retrievedRepository); Assert.IsTrue(retrievedRepository.Count == 1); Assert.AreEqual("GeometryPoint", repository.Features.ElementAt(0).Geometry.GeometryType); } /* [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.IsTrue(File.Exists(shpFilePath), string.Format("{0} does not exist", shpFile)); Assert.IsTrue(File.Exists(dbfFilePath), string.Format("{0} does not exist", dbfFile)); Assert.IsTrue(File.Exists(shxFilePath), string.Format("{0} does not exist", shxFile)); } } }