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