[mapguide-commits] r7171 - in trunk/Tools/Maestro: MaestroAPITests OSGeo.MapGuide.MaestroAPI OSGeo.MapGuide.MaestroAPI/ObjectModels
svn_mapguide at osgeo.org
svn_mapguide at osgeo.org
Tue Oct 30 21:02:34 PDT 2012
Author: jng
Date: 2012-10-30 21:02:33 -0700 (Tue, 30 Oct 2012)
New Revision: 7171
Modified:
trunk/Tools/Maestro/MaestroAPITests/HttpSiteTests.cs
trunk/Tools/Maestro/MaestroAPITests/ResourceTests.cs
trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/Check.cs
trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/ObjectModels/MapDefinition.cs
trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/ObjectModels/MapDefinitionInterfaces.cs
Log:
#2148: Add a new InsertLayer API to IMapDefinition. Also improve the test coverage with some more MapDefinition unit tests
Modified: trunk/Tools/Maestro/MaestroAPITests/HttpSiteTests.cs
===================================================================
--- trunk/Tools/Maestro/MaestroAPITests/HttpSiteTests.cs 2012-10-29 16:42:50 UTC (rev 7170)
+++ trunk/Tools/Maestro/MaestroAPITests/HttpSiteTests.cs 2012-10-31 04:02:33 UTC (rev 7171)
@@ -50,7 +50,8 @@
conn.ResourceService.SetResourceXmlData("Library://UnitTests/Layers/HydrographicPolygons.LayerDefinition", File.OpenRead("TestData/MappingService/UT_HydrographicPolygons.ldf"));
- mdf.AddLayer("foo", "bar", "Library://UnitTests/Layers/HydrographicPolygons.LayerDefinition");
+ var layer = mdf.AddLayer(null, "bar", "Library://UnitTests/Layers/HydrographicPolygons.LayerDefinition");
+ layer.Group = "foo"; //AddLayer no longer lets us put in bogus group names
context = new ResourceValidationContext(conn.ResourceService, conn.FeatureService);
issues = ResourceValidatorSet.Validate(context, mdf, false);
Assert.True(issues.Any(x => x.StatusCode == ValidationStatusCode.Error_MapDefinition_LayerWithNonExistentGroup));
Modified: trunk/Tools/Maestro/MaestroAPITests/ResourceTests.cs
===================================================================
--- trunk/Tools/Maestro/MaestroAPITests/ResourceTests.cs 2012-10-29 16:42:50 UTC (rev 7170)
+++ trunk/Tools/Maestro/MaestroAPITests/ResourceTests.cs 2012-10-31 04:02:33 UTC (rev 7171)
@@ -693,7 +693,178 @@
}
}
+ private static void SetupMapDefinitionForTest(IMapDefinition mdf)
+ {
+ mdf.AddGroup("Group1");
+ mdf.AddLayer("Group1", "Parcels", "Library://UnitTests/Layers/Parcels.LayerDefinition");
+ mdf.AddLayer("Group1", "Rail", "Library://UnitTests/Layers/Rail.LayerDefinition");
+ mdf.AddGroup("Group2");
+ }
+
[Test]
+ public void TestMapDefinitionLayerInsert()
+ {
+ var conn = _mocks.NewMock<IServerConnection>();
+ var mdf = ObjectFactory.CreateMapDefinition(conn, new Version(1, 0, 0), "TestMapDefinitionLayerInsert");
+ SetupMapDefinitionForTest(mdf);
+ int layerCount = mdf.GetLayerCount();
+
+ Assert.Throws<PreconditionException>(() => { mdf.InsertLayer(-1, null, "Hydro", "Library://UnitTests/Layers/HydrographicPolygons.LayerDefinition"); });
+ Assert.Throws<PreconditionException>(() => { mdf.InsertLayer(layerCount + 1, null, "Hydro", "Library://UnitTests/Layers/HydrographicPolygons.LayerDefinition"); });
+ Assert.Throws<PreconditionException>(() => { mdf.InsertLayer(0, null, "", ""); });
+ Assert.Throws<PreconditionException>(() => { mdf.InsertLayer(0, null, null, ""); });
+ Assert.Throws<PreconditionException>(() => { mdf.InsertLayer(0, null, "", null); });
+ Assert.Throws<PreconditionException>(() => { mdf.InsertLayer(0, null, null, null); });
+ Assert.Throws<PreconditionException>(() => { mdf.InsertLayer(0, null, "Hydro", "Library://UnitTests/Layers/HydrographicPolygons.FeatureSource"); });
+ Assert.Throws<PreconditionException>(() => { mdf.InsertLayer(0, null, "Hydro", "Garbage"); });
+
+ IMapLayer layer = mdf.InsertLayer(0, null, "Hydro", "Library://UnitTests/Layers/HydrographicPolygons.LayerDefinition");
+ Assert.AreEqual(0, mdf.GetIndex(layer));
+ Assert.True(layer == mdf.GetLayerByName("Hydro"));
+
+ layerCount = mdf.GetLayerCount();
+ IMapLayer layer1 = mdf.InsertLayer(layerCount, null, "Hydro2", "Library://UnitTests/Layers/HydrographicPolygons.LayerDefinition");
+ Assert.AreEqual(layerCount, mdf.GetIndex(layer1));
+ Assert.True(layer1 == mdf.GetLayerByName("Hydro2"));
+ }
+
+ [Test]
+ public void TestMapDefinitionLayerAdd()
+ {
+ var conn = _mocks.NewMock<IServerConnection>();
+ var mdf = ObjectFactory.CreateMapDefinition(conn, new Version(1, 0, 0), "TestMapDefinitionLayerAdd");
+ SetupMapDefinitionForTest(mdf);
+ int layerCount = mdf.GetLayerCount();
+
+ Assert.Throws<PreconditionException>(() => { mdf.AddLayer("IDontExist", "Hydro", "Library://UnitTests/Layers/HydrographicPolygons.LayerDefinition"); });
+ Assert.Throws<PreconditionException>(() => { mdf.AddLayer(null, "", ""); });
+ Assert.Throws<PreconditionException>(() => { mdf.AddLayer(null, null, ""); });
+ Assert.Throws<PreconditionException>(() => { mdf.AddLayer(null, "", null); });
+ Assert.Throws<PreconditionException>(() => { mdf.AddLayer(null, null, null); });
+ Assert.Throws<PreconditionException>(() => { mdf.AddLayer(null, "Hydro", "Library://UnitTests/Layers/HydrographicPolygons.FeatureSource"); });
+ Assert.Throws<PreconditionException>(() => { mdf.AddLayer(null, "Hydro", "Garbage"); });
+
+ IMapLayer layer = mdf.AddLayer(null, "Hydro", "Library://UnitTests/Layers/HydrographicPolygons.LayerDefinition");
+ Assert.AreEqual(0, mdf.GetIndex(layer));
+ Assert.True(layer == mdf.GetLayerByName("Hydro"));
+ Assert.AreEqual(layerCount + 1, mdf.GetLayerCount());
+ }
+
+ [Test]
+ public void TestMapDefinitionLayerRemove()
+ {
+ var conn = _mocks.NewMock<IServerConnection>();
+ var mdf = ObjectFactory.CreateMapDefinition(conn, new Version(1, 0, 0), "TestMapDefinitionLayerRemove");
+ SetupMapDefinitionForTest(mdf);
+ int layerCount = mdf.GetLayerCount();
+
+ IMapLayer layer = mdf.AddLayer(null, "Hydro", "Library://UnitTests/Layers/HydrographicPolygons.LayerDefinition");
+ Assert.AreEqual(0, mdf.GetIndex(layer));
+ Assert.True(layer == mdf.GetLayerByName("Hydro"));
+ Assert.AreEqual(layerCount + 1, mdf.GetLayerCount());
+
+ mdf.RemoveLayer(layer);
+ Assert.True(mdf.GetIndex(layer) < 0);
+ Assert.AreEqual(layerCount, mdf.GetLayerCount());
+ }
+
+ [Test]
+ public void TestMapDefinitionLayerReordering()
+ {
+ var conn = _mocks.NewMock<IServerConnection>();
+ var mdf = ObjectFactory.CreateMapDefinition(conn, new Version(1, 0, 0), "TestMapDefinitionLayerReordering");
+ SetupMapDefinitionForTest(mdf);
+ int layerCount = mdf.GetLayerCount();
+
+ Assert.Throws<PreconditionException>(() => { mdf.MoveDown(null); });
+ Assert.Throws<PreconditionException>(() => { mdf.MoveUp(null); });
+ Assert.Throws<PreconditionException>(() => { mdf.SetTopDrawOrder(null); });
+ Assert.Throws<PreconditionException>(() => { mdf.SetBottomDrawOrder(null); });
+
+ IMapLayer layer = mdf.AddLayer(null, "Hydro", "Library://UnitTests/Layers/HydrographicPolygons.LayerDefinition");
+ Assert.AreEqual(0, mdf.GetIndex(layer));
+ Assert.True(layer == mdf.GetLayerByName("Hydro"));
+ Assert.AreEqual(layerCount + 1, mdf.GetLayerCount());
+
+ int value = mdf.MoveUp(layer);
+ Assert.AreEqual(0, value); //Already at top
+ value = mdf.MoveDown(layer);
+ Assert.AreEqual(1, value);
+ mdf.SetBottomDrawOrder(layer);
+ value = mdf.GetIndex(layer);
+ Assert.AreEqual(mdf.GetLayerCount() - 1, value);
+ value = mdf.MoveDown(layer);
+ Assert.AreEqual(mdf.GetLayerCount() - 1, value); //Already at bottom
+ value = mdf.MoveUp(layer);
+ Assert.AreEqual(mdf.GetLayerCount() - 2, value);
+ mdf.SetTopDrawOrder(layer);
+ value = mdf.GetIndex(layer);
+ Assert.AreEqual(0, value);
+ }
+
+ [Test]
+ public void TestMapDefinitionGroupAdd()
+ {
+ var conn = _mocks.NewMock<IServerConnection>();
+ var mdf = ObjectFactory.CreateMapDefinition(conn, new Version(1, 0, 0), "TestMapDefinitionGroupAdd");
+ SetupMapDefinitionForTest(mdf);
+ int layerCount = mdf.GetLayerCount();
+ int groupCount = mdf.GetGroupCount();
+
+ Assert.Throws<PreconditionException>(() => { mdf.AddGroup(null); });
+ Assert.Throws<PreconditionException>(() => { mdf.AddGroup(""); });
+ Assert.Throws<PreconditionException>(() => { mdf.AddGroup("Group1"); });
+
+ IMapLayerGroup group = mdf.AddGroup("Test");
+ Assert.AreEqual(groupCount + 1, mdf.GetGroupCount());
+ Assert.NotNull(mdf.GetGroupByName("Test"));
+ Assert.True(group == mdf.GetGroupByName("Test"));
+ }
+
+ [Test]
+ public void TestMapDefinitionGroupRemove()
+ {
+ var conn = _mocks.NewMock<IServerConnection>();
+ var mdf = ObjectFactory.CreateMapDefinition(conn, new Version(1, 0, 0), "TestMapDefinitionGroupRemove");
+ SetupMapDefinitionForTest(mdf);
+ int layerCount = mdf.GetLayerCount();
+ int groupCount = mdf.GetGroupCount();
+
+ IMapLayerGroup group = mdf.AddGroup("Test");
+ Assert.AreEqual(groupCount + 1, mdf.GetGroupCount());
+ Assert.NotNull(mdf.GetGroupByName("Test"));
+ Assert.True(group == mdf.GetGroupByName("Test"));
+
+ mdf.RemoveGroup(group);
+ Assert.AreEqual(groupCount, mdf.GetGroupCount());
+ Assert.Null(mdf.GetGroupByName("Test"));
+ }
+
+ [Test]
+ public void TestMapDefinitionGroupReordering()
+ {
+ var conn = _mocks.NewMock<IServerConnection>();
+ var mdf = ObjectFactory.CreateMapDefinition(conn, new Version(1, 0, 0), "TestMapDefinitionGroupReordering");
+ SetupMapDefinitionForTest(mdf);
+ int groupCount = mdf.GetGroupCount();
+
+ Assert.Throws<PreconditionException>(() => { mdf.MoveDown(null); });
+ Assert.Throws<PreconditionException>(() => { mdf.MoveUp(null); });
+ Assert.Throws<PreconditionException>(() => { mdf.SetTopDrawOrder(null); });
+ Assert.Throws<PreconditionException>(() => { mdf.SetBottomDrawOrder(null); });
+
+ IMapLayerGroup group = mdf.AddGroup("Test");
+ Assert.AreEqual(groupCount, mdf.GetIndex(group));
+ Assert.True(group == mdf.GetGroupByName("Test"));
+ Assert.AreEqual(groupCount + 1, mdf.GetGroupCount());
+
+ int value = mdf.MoveUpGroup(group);
+ Assert.AreEqual(groupCount - 1, value); //Already at top
+ value = mdf.MoveDownGroup(group);
+ Assert.AreEqual(groupCount, value);
+ }
+
+ [Test]
public void TestMapDefinitionConversions()
{
var conn = _mocks.NewMock<IServerConnection>();
Modified: trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/Check.cs
===================================================================
--- trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/Check.cs 2012-10-29 16:42:50 UTC (rev 7170)
+++ trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/Check.cs 2012-10-31 04:02:33 UTC (rev 7171)
@@ -117,5 +117,25 @@
if (!condition)
throw new PreconditionException(Strings.PrecondFailure + msg);
}
+
+ /// <summary>
+ /// Check that the given integer is between the specified range
+ /// </summary>
+ /// <param name="value">The value to check</param>
+ /// <param name="min">The lower bound</param>
+ /// <param name="max">The upper bound</param>
+ /// <param name="bInclusive">Determines whether the range is inclusive. If false, the range is exclusive</param>
+ /// <param name="msg">The message to include for precondition failure</param>
+ public static void IntBetween(int value, int min, int max, bool bInclusive, string msg)
+ {
+ bool bInRange = false;
+ if (bInclusive)
+ bInRange = (value >= min && value <= max);
+ else
+ bInRange = (value > min && value < max);
+
+ if (!bInRange)
+ throw new PreconditionException(Strings.PrecondFailure + msg);
+ }
}
}
Modified: trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/ObjectModels/MapDefinition.cs
===================================================================
--- trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/ObjectModels/MapDefinition.cs 2012-10-29 16:42:50 UTC (rev 7170)
+++ trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/ObjectModels/MapDefinition.cs 2012-10-31 04:02:33 UTC (rev 7171)
@@ -218,6 +218,8 @@
/// <param name="layer"></param>
void IMapDefinition.InsertLayer(int idx, IMapLayer layer)
{
+ Check.IntBetween(idx, 0, this.MapLayer.Count, true, "idx"); //NOXLATE
+ Check.NotNull(layer, "layer"); //NOXLATE
var li = layer as MapLayerType;
if (li != null)
{
@@ -251,6 +253,8 @@
public IMapLayerGroup AddGroup(string groupName)
{
+ Check.NotEmpty(groupName, "groupName"); //NOXLATE
+ Check.Precondition(this.GetGroupByName(groupName) == null, "<groupName> does not already exist"); //NOXLATE
if (this.MapLayerGroup == null)
this.MapLayerGroup = new System.ComponentModel.BindingList<MapLayerGroupType>();
@@ -269,19 +273,26 @@
return group;
}
- public IMapLayer AddLayer(string groupName, string layerName, string resourceId)
- {
+ public IMapLayer AddLayer(string groupName, string layerName, string layerDefinitionId)
+ {
+ Check.NotEmpty(layerName, "layerName"); //NOXLATE
+ Check.NotEmpty(layerDefinitionId, "layerDefinitionId"); //NOXLATE
+ Check.Precondition(ResourceIdentifier.Validate(layerDefinitionId), "ResourceIdentifier.Validate(layerDefinitionId)"); //NOXLATE
+ Check.Precondition(ResourceIdentifier.GetResourceType(layerDefinitionId) == ResourceTypes.LayerDefinition, "ResourceIdentifier.GetResourceType(layerDefinitionId) == ResourceTypes.LayerDefinition"); //NOXLATE
+ if (!string.IsNullOrEmpty(groupName))
+ {
+ Check.Precondition(this.GetGroupByName(groupName) != null, "There should be an existing group for <groupName>"); //NOXLATE
+ }
var layer = new MapLayerType() {
Parent = this,
ExpandInLegend = true,
LegendLabel = layerName,
Name = layerName,
- ResourceId = resourceId,
+ ResourceId = layerDefinitionId,
ShowInLegend = true,
Visible = true,
Selectable = true
};
- //TODO: Throw exception if adding to non-existent group?
layer.Group = string.IsNullOrEmpty(groupName) ? string.Empty : groupName;
this.MapLayer.Insert(0, layer);
@@ -331,21 +342,54 @@
}
}
+ public IMapLayer InsertLayer(int index, string groupName, string layerName, string layerDefinitionId)
+ {
+ Check.IntBetween(index, 0, this.MapLayer.Count, true, "index");
+ Check.NotEmpty(layerName, "layerName"); //NOXLATE
+ Check.NotEmpty(layerDefinitionId, "layerDefinitionId"); //NOXLATE
+ Check.Precondition(ResourceIdentifier.Validate(layerDefinitionId), "ResourceIdentifier.Validate(layerDefinitionId)"); //NOXLATE
+ Check.Precondition(ResourceIdentifier.GetResourceType(layerDefinitionId) == ResourceTypes.LayerDefinition, "ResourceIdentifier.GetResourceType(layerDefinitionId) == ResourceTypes.LayerDefinition"); //NOXLATE
+ if (!string.IsNullOrEmpty(groupName))
+ {
+ Check.NotNull(this.GetGroupByName(groupName), "Group for <groupName>"); //NOXLATE
+ }
+ var layer = new MapLayerType()
+ {
+ Parent = this,
+ ExpandInLegend = true,
+ LegendLabel = layerName,
+ Name = layerName,
+ ResourceId = layerDefinitionId,
+ ShowInLegend = true,
+ Visible = true,
+ Selectable = true
+ };
+ layer.Group = string.IsNullOrEmpty(groupName) ? string.Empty : groupName;
+ this.MapLayer.Insert(index, layer);
+ return layer;
+ }
- public IMapLayer AddLayer(IMapLayer layerToInsertAbove, string groupName, string layerName, string resourceId)
+ public IMapLayer AddLayer(IMapLayer layerToInsertAbove, string groupName, string layerName, string layerDefinitionId)
{
+ Check.NotEmpty(layerName, "layerName"); //NOXLATE
+ Check.NotEmpty(layerDefinitionId, "layerDefinitionId"); //NOXLATE
+ Check.Precondition(ResourceIdentifier.Validate(layerDefinitionId), "ResourceIdentifier.Validate(layerDefinitionId)"); //NOXLATE
+ Check.Precondition(ResourceIdentifier.GetResourceType(layerDefinitionId) == ResourceTypes.LayerDefinition, "ResourceIdentifier.GetResourceType(layerDefinitionId) == ResourceTypes.LayerDefinition"); //NOXLATE
+ if (!string.IsNullOrEmpty(groupName))
+ {
+ Check.NotNull(this.GetGroupByName(groupName), "Group for <groupName>"); //NOXLATE
+ }
var layer = new MapLayerType()
{
Parent = this,
ExpandInLegend = true,
LegendLabel = layerName,
Name = layerName,
- ResourceId = resourceId,
+ ResourceId = layerDefinitionId,
ShowInLegend = true,
Visible = true,
Selectable = true
};
- //TODO: Throw exception if adding to non-existent group?
layer.Group = string.IsNullOrEmpty(groupName) ? string.Empty : groupName;
if (layerToInsertAbove != null)
@@ -527,6 +571,10 @@
return idst;
}
+ else if (isrc == 0)
+ {
+ return isrc; //Unchanged
+ }
return -1;
}
@@ -549,6 +597,10 @@
return idst;
}
+ else if (isrc == this.MapLayer.Count - 1)
+ {
+ return this.MapLayer.Count - 1; //Unchanged
+ }
return -1;
}
@@ -574,6 +626,10 @@
return idst;
}
+ else if (idx == 0)
+ {
+ return idx; //Unchanged
+ }
}
return -1;
@@ -600,6 +656,10 @@
return idst;
}
+ else if (idx == this.MapLayerGroup.Count - 1)
+ {
+ return this.MapLayerGroup.Count - 1; //Unchanged
+ }
}
return -1;
Modified: trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/ObjectModels/MapDefinitionInterfaces.cs
===================================================================
--- trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/ObjectModels/MapDefinitionInterfaces.cs 2012-10-29 16:42:50 UTC (rev 7170)
+++ trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/ObjectModels/MapDefinitionInterfaces.cs 2012-10-31 04:02:33 UTC (rev 7171)
@@ -123,10 +123,24 @@
IEnumerable<IMapLayer> MapLayer { get; }
/// <summary>
+ /// Inserts a layer into this map at the specified index in the map's layer collection
+ /// </summary>
+ /// <param name="index"></param>
+ /// <param name="groupName"></param>
+ /// <param name="layerName"></param>
+ /// <param name="layerDefinitionId"></param>
+ /// <returns></returns>
+ IMapLayer InsertLayer(int index, string groupName, string layerName, string layerDefinitionId);
+
+ /// <summary>
/// Adds a layer to this map. If this is the first layer to be added, the coordinate system
/// of this map and its extents will be set to the coordinate system and extents of this layer
/// if this has not been set already.
/// </summary>
+ /// <remarks>
+ /// The layer is added to the beginning of the list. That is, if you called <see cref="M:OSGeo.MapGuide.ObjectModels.MapDefinition.IMapDefinition.GetIndex(OSGeo.MapGuide.ObjectModels.MapDefinition.IMapLayer)"/>
+ /// on your newly added layer, it will return 0. From a display perspective, your newly added layer will be at the top of the map's draw order when you create a runtime map from this map definition
+ /// </remarks>
/// <param name="groupName"></param>
/// <param name="layerName"></param>
/// <param name="resourceId"></param>
@@ -162,14 +176,14 @@
/// Moves the layer up the draw order
/// </summary>
/// <param name="layer">The layer.</param>
- /// <returns></returns>
+ /// <returns>The new index of the moved layer. -1 is returned if the layer does not belong to the map</returns>
int MoveUp(IMapLayer layer);
/// <summary>
/// Moves the layer down the draw order.
/// </summary>
/// <param name="layer">The layer.</param>
- /// <returns></returns>
+ /// <returns>The new index of the moved layer. -1 is returned if the layer does not belong to the map</returns>
int MoveDown(IMapLayer layer);
/// <summary>
@@ -179,7 +193,7 @@
IEnumerable<IMapLayerGroup> MapLayerGroup { get; }
/// <summary>
- /// Adds the group.
+ /// Adds the group. The group will be added to the end of the list
/// </summary>
/// <param name="groupName">Name of the group.</param>
/// <returns></returns>
@@ -221,12 +235,14 @@
/// Moves a Map Group down the presentation order
/// </summary>
/// <param name="group"></param>
+ /// <returns>The new index of the moved group. -1 is returned if the group does not belong to the map</returns>
int MoveDownGroup(IMapLayerGroup group);
/// <summary>
/// Moves a Map Group up the presentation order
/// </summary>
/// <param name="group"></param>
+ /// <returns>The new index of the moved group. -1 is returned if the group does not belong to the map</returns>
int MoveUpGroup(IMapLayerGroup group);
/// <summary>
More information about the mapguide-commits
mailing list