Index: Core/Components/src/Core.Components.DotSpatial/BaseMap.cs =================================================================== diff -u -rd0615029ee52b3dbe0eda73c8cf9ba40e4353ee4 -r8605d66cdc1266af3a372af46c9d32008f1261e4 --- Core/Components/src/Core.Components.DotSpatial/BaseMap.cs (.../BaseMap.cs) (revision d0615029ee52b3dbe0eda73c8cf9ba40e4353ee4) +++ Core/Components/src/Core.Components.DotSpatial/BaseMap.cs (.../BaseMap.cs) (revision 8605d66cdc1266af3a372af46c9d32008f1261e4) @@ -2,6 +2,7 @@ using System.IO; using System.Windows.Forms; using Core.Components.DotSpatial.Data; +using Core.Components.DotSpatial.Exceptions; using Core.Components.DotSpatial.Properties; using DotSpatial.Controls; using log4net; @@ -30,15 +31,15 @@ /// /// Thrown when is null. /// Thrown when does not exist. - /// Thrown when has an unaccepted extension. + /// Thrown when the data in is not valid. public void SetMapData(MapData mapData) { if (IsDisposed) { return; } - - if(mapData == null) + + if (mapData == null) { throw new ArgumentNullException("mapData", "MapData is required when adding shapeFiles"); } @@ -48,6 +49,10 @@ data = mapData; LoadData(); } + else + { + throw new MapDataException("The data available in MapData is not valid."); + } } /// Index: Core/Components/src/Core.Components.DotSpatial/Core.Components.DotSpatial.csproj =================================================================== diff -u -r71ce26f5bde53bd1173fbe6ea09510e30a6a1166 -r8605d66cdc1266af3a372af46c9d32008f1261e4 --- Core/Components/src/Core.Components.DotSpatial/Core.Components.DotSpatial.csproj (.../Core.Components.DotSpatial.csproj) (revision 71ce26f5bde53bd1173fbe6ea09510e30a6a1166) +++ Core/Components/src/Core.Components.DotSpatial/Core.Components.DotSpatial.csproj (.../Core.Components.DotSpatial.csproj) (revision 8605d66cdc1266af3a372af46c9d32008f1261e4) @@ -96,6 +96,7 @@ Component + True Index: Core/Components/src/Core.Components.DotSpatial/Data/MapData.cs =================================================================== diff -u -r04c70d10f22ba007f8210813d3d02697e42f8a5d -r8605d66cdc1266af3a372af46c9d32008f1261e4 --- Core/Components/src/Core.Components.DotSpatial/Data/MapData.cs (.../MapData.cs) (revision 04c70d10f22ba007f8210813d3d02697e42f8a5d) +++ Core/Components/src/Core.Components.DotSpatial/Data/MapData.cs (.../MapData.cs) (revision 8605d66cdc1266af3a372af46c9d32008f1261e4) @@ -1,6 +1,7 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.IO; +using System.Linq; +using Core.Components.DotSpatial.Exceptions; using Core.Components.DotSpatial.Properties; namespace Core.Components.DotSpatial.Data @@ -34,53 +35,56 @@ } /// - /// Adds the shape file to the list. Each should be unique. + /// Adds the shape file to the list. Duplicates are ignored. /// /// The path to the file. - /// Thrown when is null. - /// Thrown when does not exist. - /// Thrown when has an unaccepted extension. + /// Thrown when + /// + /// The is null. + /// The file at does not exist. + /// The extension of is not accepted. + /// + /// /// True when added to the list. False when it is not added, for example when is not unique. public bool AddShapeFile(string filePath) { - IsPathValid(filePath); + string validationMessage = ValidateFilePath(filePath); + if (validationMessage != string.Empty) + { + throw new MapDataException(validationMessage); + } + return filePaths.Add(filePath); } /// /// Checks if the given paths are valid. /// - /// Throwns when value in is null. - /// Thrown when value in does not exist. - /// Thrown when value has an unaccepted extension. - /// + /// True when all filePaths are valid. False otherwise. public bool IsValid() { - foreach (var path in filePaths) - { - IsPathValid(path); - } - - return true; + return filePaths.Count != 0 && filePaths.All(fp => ValidateFilePath(fp) == string.Empty); } - private void IsPathValid(string path) + private string ValidateFilePath(string path) { if (path == null) { - throw new ArgumentNullException("path", "A path is required when adding shape files"); + return "A path is required when adding shape files"; } if (!File.Exists(path)) { - throw new FileNotFoundException(string.Format(Resources.MapData_IsPathValid_File_on_path__0__does_not_exist, path)); + return string.Format(Resources.MapData_IsPathValid_File_on_path__0__does_not_exist, path); } if (!CheckExtension(path)) { - throw new ArgumentException(string.Format(Resources.MapData_IsPathValid_File_on_path__0__does_not_have_the_shp_extension, path)); + return string.Format(Resources.MapData_IsPathValid_File_on_path__0__does_not_have_the_shp_extension, path); } + + return string.Empty; } private bool CheckExtension(string path) Index: Core/Components/src/Core.Components.DotSpatial/Exceptions/MapDataException.cs =================================================================== diff -u --- Core/Components/src/Core.Components.DotSpatial/Exceptions/MapDataException.cs (revision 0) +++ Core/Components/src/Core.Components.DotSpatial/Exceptions/MapDataException.cs (revision 8605d66cdc1266af3a372af46c9d32008f1261e4) @@ -0,0 +1,32 @@ +using System; + +namespace Core.Components.DotSpatial.Exceptions +{ + /// + /// The exception that is thrown when an entity is not found. + /// + public class MapDataException : Exception + { + /// + /// Initializes a new instance of the class. + /// + public MapDataException() {} + + /// + /// Initializes a new instance of the class + /// with a specified error message. + /// + /// The error message that explains the reason for the exception. + public MapDataException(string message) : base(message) {} + + /// + /// Initializes a new instance of the class + /// with a specified error message and a reference to the inner exception that is + /// the cause of this exception. + /// + /// The error message that explains the reason for the exception. + /// The exception that is the cause of the current exception, + /// or a null reference if no inner exception is specified. + public MapDataException(string message, Exception inner) : base(message, inner) {} + } +} \ No newline at end of file Index: Core/Components/test/Core.Components.DotSpatial.Test/BaseMapTest.cs =================================================================== diff -u -r04c70d10f22ba007f8210813d3d02697e42f8a5d -r8605d66cdc1266af3a372af46c9d32008f1261e4 --- Core/Components/test/Core.Components.DotSpatial.Test/BaseMapTest.cs (.../BaseMapTest.cs) (revision 04c70d10f22ba007f8210813d3d02697e42f8a5d) +++ Core/Components/test/Core.Components.DotSpatial.Test/BaseMapTest.cs (.../BaseMapTest.cs) (revision 8605d66cdc1266af3a372af46c9d32008f1261e4) @@ -4,6 +4,7 @@ using Core.Common.TestUtil; using Core.Common.Utils.Reflection; using Core.Components.DotSpatial.Data; +using Core.Components.DotSpatial.Exceptions; using Core.Components.DotSpatial.Properties; using DotSpatial.Controls; using NUnit.Framework; @@ -54,16 +55,25 @@ var map = new BaseMap(); var data = new MapData(); var filePath = string.Format("{0}\\Resources\\DR10_binnenteen.shp", Environment.CurrentDirectory); + var newPath = string.Format("{0}\\Resources\\DR10_teen.shp", Environment.CurrentDirectory); data.AddShapeFile(filePath); - File.Delete(filePath); + RenameFile(newPath, filePath); // Call TestDelegate testDelegate = () => map.SetMapData(data); - // Assert - Assert.Throws(testDelegate); + try + { + // Assert + Assert.Throws(testDelegate); + } + finally + { + // Place the original file back for other tests. + RenameFile(filePath, newPath); + } } [Test] @@ -81,7 +91,7 @@ // Assert Assert.DoesNotThrow(setDataDelegate); } - + [Test] public void SetDataOnMap_Succeeds_AddOneMapLayerAndWriteLog() { @@ -95,7 +105,7 @@ data.AddShapeFile(filePath); var mapComponent = TypeUtils.GetField(map, "map"); - + // Pre-condition var preLayerCount = mapComponent.GetLayers().Count; @@ -104,7 +114,13 @@ // Assert TestHelper.AssertLogMessageIsGenerated(action, excpectedLog); - Assert.AreEqual(preLayerCount+1, mapComponent.GetLayers().Count); + Assert.AreEqual(preLayerCount + 1, mapComponent.GetLayers().Count); } + + private static void RenameFile(string newPath, string path) + { + File.Delete(newPath); + File.Move(path, newPath); + } } } \ No newline at end of file Index: Core/Components/test/Core.Components.DotSpatial.Test/Data/MapDataTest.cs =================================================================== diff -u -r04c70d10f22ba007f8210813d3d02697e42f8a5d -r8605d66cdc1266af3a372af46c9d32008f1261e4 --- Core/Components/test/Core.Components.DotSpatial.Test/Data/MapDataTest.cs (.../MapDataTest.cs) (revision 04c70d10f22ba007f8210813d3d02697e42f8a5d) +++ Core/Components/test/Core.Components.DotSpatial.Test/Data/MapDataTest.cs (.../MapDataTest.cs) (revision 8605d66cdc1266af3a372af46c9d32008f1261e4) @@ -3,8 +3,8 @@ using System.IO; using Core.Common.Utils.Reflection; using Core.Components.DotSpatial.Data; +using Core.Components.DotSpatial.Exceptions; using NUnit.Framework; -using Rhino.Mocks; namespace Core.Components.DotSpatial.Test.Data { @@ -20,7 +20,7 @@ // Assert Assert.IsNotNull(filePaths); - Assert.IsInstanceOf>(data.FilePaths); + CollectionAssert.IsEmpty(data.FilePaths); } [Test] @@ -35,6 +35,10 @@ // Assert Assert.IsTrue(succeeded); + CollectionAssert.AreEquivalent(data.FilePaths, new[] + { + filePath + }); } [Test] @@ -51,10 +55,14 @@ // Assert Assert.IsTrue(succeededFirst); Assert.IsFalse(succeededSecond); + CollectionAssert.AreEquivalent(data.FilePaths, new[] + { + filePath + }); } [Test] - public void AddShapeFile_FileDoesNotExist_ThrowsFileNotFoundException() + public void AddShapeFile_FileDoesNotExist_ThrowsMapDataException() { // Setup var data = new MapData(); @@ -64,11 +72,13 @@ TestDelegate testDelegate = () => data.AddShapeFile(filePath); // Assert - Assert.Throws(testDelegate); + MapDataException exception = Assert.Throws(testDelegate); + string expectedMessage = string.Format("Bestand op pad {0} bestaat niet.", filePath); + Assert.AreEqual(expectedMessage, exception.Message); } [Test] - public void AddShapeFile_ExtensionIsInvalid_ThrowsArgumentException() + public void AddShapeFile_ExtensionIsInvalid_ThrowsMapDataException() { // Setup var data = new MapData(); @@ -78,20 +88,24 @@ TestDelegate testDelegate = () => data.AddShapeFile(filePath); // Assert - Assert.Throws(testDelegate); + MapDataException exception = Assert.Throws(testDelegate); + string expectedMessage = string.Format("Bestand op pad {0} heeft niet de juiste .shp extensie.", filePath); + Assert.AreEqual(expectedMessage, exception.Message); } [Test] - public void AddShapeFile_IsNull_ThrowsArgumentNullException() + public void AddShapeFile_IsNull_ThrowsMapDataException() { // Setup var data = new MapData(); - + // Call TestDelegate testDelegate = () => data.AddShapeFile(null); // Assert - Assert.Throws(testDelegate); + MapDataException exception = Assert.Throws(testDelegate); + string expectedMessage = "A path is required when adding shape files"; + Assert.AreEqual(expectedMessage, exception.Message); } [Test] @@ -114,8 +128,110 @@ addedPaths.Add(path); // Assert - Assert.AreEqual(data.FilePaths, addedPaths); + CollectionAssert.AreEqual(data.FilePaths, addedPaths); } } + + [Test] + public void IsValid_FilePathsEmpty_ReturnsFalse() + { + // Setup + var data = new MapData(); + + // Call + bool result = data.IsValid(); + + // Assert + Assert.IsFalse(result); + CollectionAssert.AreEqual(new string[] + {}, data.FilePaths); + } + + [Test] + public void IsValid_FilePathValid_ReturnsTrue() + { + // Setup + var data = new MapData(); + var path = string.Format("{0}\\Resources\\DR10_binnenteen.shp", Environment.CurrentDirectory); + data.AddShapeFile(path); + + // Call + bool result = data.IsValid(); + + // Assert + Assert.IsTrue(result); + CollectionAssert.AreEqual(data.FilePaths, new[] + { + path + }); + } + + [Test] + public void IsValid_FilePathsNotValid_ReturnsFalse() + { + // Setup + var data = new MapData(); + var path = string.Format("{0}\\Resources\\DR10_binnenteen.shp", Environment.CurrentDirectory); + var newPath = string.Format("{0}\\Resources\\DR10_teen.shp", Environment.CurrentDirectory); + + data.AddShapeFile(path); + + // Pre-condition + bool result = data.IsValid(); + + RenameFile(newPath, path); + + // Call + bool resultAfterDelete = data.IsValid(); + + // Place original file back for other tests. + RenameFile(path, newPath); + + // Assert + Assert.AreNotEqual(result, resultAfterDelete); + Assert.IsTrue(result); + Assert.IsFalse(resultAfterDelete); + } + + [Test] + public void IsValid_OneFilePathIsInvalid_ReturnsFalse() + { + // Setup + var data = new MapData(); + var paths = new[] + { + string.Format("{0}\\Resources\\DR10_binnenteen.shp", Environment.CurrentDirectory), + string.Format("{0}\\Resources\\DR10_dijkvakgebieden.shp", Environment.CurrentDirectory), + }; + var newPath = string.Format("{0}\\Resources\\DR10_teen.shp", Environment.CurrentDirectory); + + foreach (var path in paths) + { + data.AddShapeFile(path); + } + + // Pre-condition + bool result = data.IsValid(); + + RenameFile(newPath, paths[0]); + + // Call + bool resultAfterDelete = data.IsValid(); + + // Place the original file back for other tests. + RenameFile(paths[0], newPath); + + // Assert + Assert.AreNotEqual(result, resultAfterDelete); + Assert.IsTrue(result); + Assert.IsFalse(resultAfterDelete); + CollectionAssert.AreEquivalent(paths, data.FilePaths); + } + + private static void RenameFile(string newPath, string path) + { + File.Delete(newPath); + File.Move(path, newPath); + } } } \ No newline at end of file