[mapguide-commits] r5199 - in sandbox/maestro-3.0: Maestro.Base/Commands/SiteExplorer Maestro.Base/Commands/Test MaestroAPITests OSGeo.MapGuide.MaestroAPI OSGeo.MapGuide.MaestroAPI/Resource

svn_mapguide at osgeo.org svn_mapguide at osgeo.org
Tue Sep 28 05:26:53 EDT 2010


Author: jng
Date: 2010-09-28 09:26:53 +0000 (Tue, 28 Sep 2010)
New Revision: 5199

Added:
   sandbox/maestro-3.0/MaestroAPITests/ValidationTests.cs
   sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/Check.cs
   sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/Resource/ValidationResultSet.cs
Modified:
   sandbox/maestro-3.0/Maestro.Base/Commands/SiteExplorer/ValidateCommand.cs
   sandbox/maestro-3.0/Maestro.Base/Commands/Test/SelectFdoProviderCommand.cs
   sandbox/maestro-3.0/MaestroAPITests/HttpConnectionTests.cs
   sandbox/maestro-3.0/MaestroAPITests/MaestroAPITests.csproj
   sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/OSGeo.MapGuide.MaestroAPI.csproj
   sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/Resource/IResourceValidator.cs
   sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/Resource/ResourceValidatorSet.cs
Log:
Implement #1448 for the 3.0 sandbox:

- Implement equality APIs (Equals() and GetHashCode()) for ValidationIssue
- Add a ValidationResultSet class that does the weeding out of redundant issues.
- Add some unit tests to verify issue uniqueness

Modified: sandbox/maestro-3.0/Maestro.Base/Commands/SiteExplorer/ValidateCommand.cs
===================================================================
--- sandbox/maestro-3.0/Maestro.Base/Commands/SiteExplorer/ValidateCommand.cs	2010-09-28 08:20:48 UTC (rev 5198)
+++ sandbox/maestro-3.0/Maestro.Base/Commands/SiteExplorer/ValidateCommand.cs	2010-09-28 09:26:53 UTC (rev 5199)
@@ -121,7 +121,7 @@
 
             worker.ReportProgress(0);
 
-            var issues = new List<ValidationIssue>();
+            var set = new ValidationResultSet();
             int i = 0;
             foreach (string s in documents)
             {
@@ -132,18 +132,18 @@
                     //TODO: This will validate resources multiple times, if they are referenced by
                     //resources inside the folder
                     item = _conn.ResourceService.GetResource(s);
-                    issues.AddRange(ResourceValidatorSet.Validate(item, true));
+                    set.AddIssues(ResourceValidatorSet.Validate(item, true));
                 }
                 catch (Exception ex)
                 {
                     string msg = NestedExceptionMessageProcessor.GetFullMessage(ex);
-                    issues.Add(new ValidationIssue(item, ValidationStatus.Error, string.Format(Properties.Resources.ValidationResourceLoadFailed, msg)));
+                    set.AddIssue(new ValidationIssue(item, ValidationStatus.Error, string.Format(Properties.Resources.ValidationResourceLoadFailed, msg)));
                 }
                 i++;
                 worker.ReportProgress((int)((i / (double)documents.Count) * 100), s);
             }
 
-            return issues.ToArray();
+            return set.GetAllIssues();
         }
     }
 }

Modified: sandbox/maestro-3.0/Maestro.Base/Commands/Test/SelectFdoProviderCommand.cs
===================================================================
--- sandbox/maestro-3.0/Maestro.Base/Commands/Test/SelectFdoProviderCommand.cs	2010-09-28 08:20:48 UTC (rev 5198)
+++ sandbox/maestro-3.0/Maestro.Base/Commands/Test/SelectFdoProviderCommand.cs	2010-09-28 09:26:53 UTC (rev 5199)
@@ -36,7 +36,7 @@
             var conn = mgr.GetConnection(exp.ConnectionName);
 
             var providers = conn.FeatureService.FeatureProviders;
-            var provider = GenericItemSelectionDialog.SelectItem("Select FDO provider", "Select an FDO provider", providers, "DisplayName", "Name");
+            var provider = GenericItemSelectionDialog.SelectItem("Select FDO provider", "Select an FDO provider", providers, "DisplayName", "Name"); //LOCALIZEME
             if (provider != null)
             {
                 MessageService.ShowMessage(provider.DisplayName);

Modified: sandbox/maestro-3.0/MaestroAPITests/HttpConnectionTests.cs
===================================================================
--- sandbox/maestro-3.0/MaestroAPITests/HttpConnectionTests.cs	2010-09-28 08:20:48 UTC (rev 5198)
+++ sandbox/maestro-3.0/MaestroAPITests/HttpConnectionTests.cs	2010-09-28 09:26:53 UTC (rev 5199)
@@ -60,17 +60,28 @@
             var props = isvc.GetCustomPropertyNames();
 
             Assert.IsNotNull(props);
-            Assert.AreEqual(props.Length, 1);
+            Assert.AreEqual(props.Length, 2);
             Assert.IsTrue(Array.IndexOf<string>(props, "UserAgent") >= 0);
+            Assert.IsTrue(Array.IndexOf<string>(props, "BaseUrl") >= 0);
 
             //It is of type string
             var type = isvc.GetCustomPropertyType("UserAgent");
             Assert.AreEqual(type, typeof(string));
+            type = isvc.GetCustomPropertyType("BaseUrl");
+            Assert.AreEqual(type, typeof(string));
 
             //We can set and get it
             isvc.SetCustomProperty("UserAgent", "MapGuide Maestro API Unit Test Fixture");
             var agent = (string)isvc.GetCustomProperty("UserAgent");
             Assert.AreEqual(agent, "MapGuide Maestro API Unit Test Fixture");
+
+            //BaseUrl is read-only
+            try
+            {
+                isvc.SetCustomProperty("BaseUrl", "http://mylocalhost/mapguide");
+                Assert.Fail("Should've thrown exception");
+            }
+            catch { } 
         }
 
         [Test]

Modified: sandbox/maestro-3.0/MaestroAPITests/MaestroAPITests.csproj
===================================================================
--- sandbox/maestro-3.0/MaestroAPITests/MaestroAPITests.csproj	2010-09-28 08:20:48 UTC (rev 5198)
+++ sandbox/maestro-3.0/MaestroAPITests/MaestroAPITests.csproj	2010-09-28 09:26:53 UTC (rev 5199)
@@ -3,7 +3,7 @@
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
     <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
-    <ProductVersion>9.0.21022</ProductVersion>
+    <ProductVersion>9.0.30729</ProductVersion>
     <SchemaVersion>2.0</SchemaVersion>
     <ProjectGuid>{351D49A3-2E4A-4EC3-AFC2-D56598F44F51}</ProjectGuid>
     <OutputType>Library</OutputType>
@@ -57,6 +57,7 @@
     <Compile Include="ResourceTests.cs" />
     <Compile Include="RuntimeMapTests.cs" />
     <Compile Include="SerializationTests.cs" />
+    <Compile Include="ValidationTests.cs" />
   </ItemGroup>
   <ItemGroup>
     <ProjectReference Include="..\OSGeo.MapGuide.MaestroAPI.Http\OSGeo.MapGuide.MaestroAPI.Http.csproj">

Added: sandbox/maestro-3.0/MaestroAPITests/ValidationTests.cs
===================================================================
--- sandbox/maestro-3.0/MaestroAPITests/ValidationTests.cs	                        (rev 0)
+++ sandbox/maestro-3.0/MaestroAPITests/ValidationTests.cs	2010-09-28 09:26:53 UTC (rev 5199)
@@ -0,0 +1,82 @@
+#region Disclaimer / License
+// Copyright (C) 2010, Jackie Ng
+// http://trac.osgeo.org/mapguide/wiki/maestro, jumpinjackie at gmail.com
+// 
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// 
+// This library 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
+// Lesser General Public License for more details.
+// 
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+// 
+#endregion
+using System;
+using System.Collections.Generic;
+using System.Text;
+using NUnit.Framework;
+using OSGeo.MapGuide.MaestroAPI.Resource;
+using NMock2;
+
+namespace MaestroAPITests
+{
+    [TestFixture]
+    public class ValidationTests
+    {
+        [Test]
+        public void TestUniqueIssues()
+        {
+            //Test for the purposes of equality and hashing that any two issues with
+            //the same set of resource id, status and message are identical.
+
+            var mocks = new Mockery();
+            var res1 = mocks.NewMock<IResource>();
+            Expect.AtLeastOnce.On(res1).GetProperty("ResourceID").Will(Return.Value("Library://Test.FeatureSource"));
+            var res2 = mocks.NewMock<IResource>();
+            Expect.AtLeastOnce.On(res2).GetProperty("ResourceID").Will(Return.Value("Library://Test.FeatureSource"));
+            var res3 = mocks.NewMock<IResource>();
+            Expect.AtLeastOnce.On(res3).GetProperty("ResourceID").Will(Return.Value("Library://Test.FeatureSource"));
+
+            var msg1 = new ValidationIssue(res1, ValidationStatus.Error, "Epic Fail");
+            var msg2 = new ValidationIssue(res2, ValidationStatus.Error, "Epic Fail");
+            var msg3 = new ValidationIssue(res3, ValidationStatus.Error, "Not so epic fail");
+
+            Assert.AreEqual(msg1.GetHashCode(), msg2.GetHashCode());
+            Assert.IsTrue(msg1.Equals(msg2));
+
+            Assert.AreNotEqual(msg1.GetHashCode(), msg3.GetHashCode());
+            Assert.AreNotEqual(msg2.GetHashCode(), msg3.GetHashCode());
+            Assert.IsFalse(msg1.Equals(msg3));
+            Assert.IsFalse(msg2.Equals(msg3));
+        }
+
+        [Test]
+        public void TestValidationResultSet()
+        {
+            var mocks = new Mockery();
+            var res1 = mocks.NewMock<IResource>();
+            Expect.AtLeastOnce.On(res1).GetProperty("ResourceID").Will(Return.Value("Library://Test.FeatureSource"));
+            var res2 = mocks.NewMock<IResource>();
+            Expect.AtLeastOnce.On(res2).GetProperty("ResourceID").Will(Return.Value("Library://Test.FeatureSource"));
+            var res3 = mocks.NewMock<IResource>();
+            Expect.AtLeastOnce.On(res3).GetProperty("ResourceID").Will(Return.Value("Library://Test.FeatureSource"));
+
+            var msg1 = new ValidationIssue(res1, ValidationStatus.Error, "Epic Fail");
+            var msg2 = new ValidationIssue(res2, ValidationStatus.Error, "Epic Fail");
+            var msg3 = new ValidationIssue(res3, ValidationStatus.Error, "Not so epic fail");
+
+            var set = new ValidationResultSet();
+            set.AddIssues(new ValidationIssue[] { msg1, msg2, msg3 });
+
+            var totalIssues = set.GetAllIssues();
+            Assert.AreEqual(totalIssues.Length, 2);
+            Assert.AreEqual(set.ResourceIDs.Length, 1);
+        }
+    }
+}

Added: sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/Check.cs
===================================================================
--- sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/Check.cs	                        (rev 0)
+++ sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/Check.cs	2010-09-28 09:26:53 UTC (rev 5199)
@@ -0,0 +1,40 @@
+#region Disclaimer / License
+// Copyright (C) 2010, Jackie Ng
+// http://trac.osgeo.org/mapguide/wiki/maestro, jumpinjackie at gmail.com
+// 
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// 
+// This library 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
+// Lesser General Public License for more details.
+// 
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+// 
+#endregion
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace OSGeo.MapGuide.MaestroAPI
+{
+    public static class Check
+    {
+        public static void NotNull<T>(T obj, string arg) where T : class
+        {
+            if (obj == null)
+                throw new ArgumentNullException(arg);
+        }
+
+        public static void NotEmpty(string msg, string arg) 
+        {
+            if (string.IsNullOrEmpty(msg))
+                throw new ArgumentException("Empty value", arg); //LOCALIZEME
+        }
+    }
+}

Modified: sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/OSGeo.MapGuide.MaestroAPI.csproj
===================================================================
--- sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/OSGeo.MapGuide.MaestroAPI.csproj	2010-09-28 08:20:48 UTC (rev 5198)
+++ sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/OSGeo.MapGuide.MaestroAPI.csproj	2010-09-28 09:26:53 UTC (rev 5199)
@@ -160,6 +160,7 @@
     </Compile>
     <Compile Include="BindingListExtensions.cs" />
     <Compile Include="Capability\ConnectionCapabilities.cs" />
+    <Compile Include="Check.cs" />
     <Compile Include="Commands\CommandType.cs" />
     <Compile Include="Commands\ExecuteLoadProcedure.cs" />
     <Compile Include="Commands\GetCapabilities.cs" />
@@ -220,6 +221,7 @@
     <Compile Include="Resource\ResourceTypeDescriptor.cs" />
     <Compile Include="Resource\ResourceContentVersionChecker.cs" />
     <Compile Include="Resource\ResourceValidatorSet.cs" />
+    <Compile Include="Resource\ValidationResultSet.cs" />
     <Compile Include="Serialization\Enums.cs" />
     <Compile Include="Serialization\IBinarySerializeable.cs" />
     <Compile Include="Serialization\MgBinaryDeserializer.cs" />

Modified: sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/Resource/IResourceValidator.cs
===================================================================
--- sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/Resource/IResourceValidator.cs	2010-09-28 08:20:48 UTC (rev 5198)
+++ sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/Resource/IResourceValidator.cs	2010-09-28 09:26:53 UTC (rev 5199)
@@ -37,6 +37,9 @@
     {
         public ValidationIssue(IResource res, ValidationStatus stat, string msg)
         {
+            Check.NotNull(res, "res");
+            Check.NotEmpty(msg, "msg");
+
             this.Resource = res;
             this.Status = stat;
             this.Message = msg;
@@ -56,6 +59,34 @@
         /// Gets the resource this issue pertains to
         /// </summary>
         public IResource Resource { get; private set; }
+
+        public override bool Equals(object obj)
+        {
+            if (obj == null)
+                return false;
+
+            if (!typeof(ValidationIssue).IsAssignableFrom(obj.GetType()))
+                return false;
+
+            ValidationIssue vi = (ValidationIssue)obj;
+            return this.Resource.ResourceID.Equals(vi.Resource.ResourceID) &&
+                   this.Message.Equals(vi.Message) &&
+                   this.Status == vi.Status;
+        }
+
+        public override int GetHashCode()
+        {
+            //http://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-systemobjectgethashcode
+            unchecked
+            {
+                int hash = 17;
+                hash = hash * 23 + this.Resource.ResourceID.GetHashCode();
+                hash = hash * 23 + this.Message.GetHashCode();
+                hash = hash * 23 + this.Status.GetHashCode();
+
+                return hash;
+            }
+        }
     }
 
     /// <summary>

Modified: sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/Resource/ResourceValidatorSet.cs
===================================================================
--- sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/Resource/ResourceValidatorSet.cs	2010-09-28 08:20:48 UTC (rev 5198)
+++ sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/Resource/ResourceValidatorSet.cs	2010-09-28 09:26:53 UTC (rev 5199)
@@ -49,10 +49,11 @@
 
         public static ValidationIssue[] Validate(IResource item, bool recurse)
         {
+            var issueSet = new ValidationResultSet();
             List<ValidationIssue> issues = new List<ValidationIssue>();
             if (!HasValidator(item.ResourceType, item.ResourceVersion))
             {
-                issues.Add(new ValidationIssue(item, ValidationStatus.Warning, string.Format(Properties.Resources.ERR_NO_REGISTERED_VALIDATOR, item.ResourceType, item.ResourceVersion)));
+                issueSet.AddIssue(new ValidationIssue(item, ValidationStatus.Warning, string.Format(Properties.Resources.ERR_NO_REGISTERED_VALIDATOR, item.ResourceType, item.ResourceVersion)));
             }
             else
             {
@@ -62,7 +63,7 @@
                     {
                         ValidationIssue[] tmp = v.Validate(item, recurse);
                         if (tmp != null)
-                            issues.AddRange(tmp);
+                            issueSet.AddIssues(tmp);
                     }
                     catch (Exception ex)
                     {
@@ -71,7 +72,7 @@
                     }
                 }
             }
-            return issues.ToArray();
+            return issueSet.GetAllIssues();
         }
 
         public static bool HasValidator(ResourceTypes resourceTypes, Version version)

Added: sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/Resource/ValidationResultSet.cs
===================================================================
--- sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/Resource/ValidationResultSet.cs	                        (rev 0)
+++ sandbox/maestro-3.0/OSGeo.MapGuide.MaestroAPI/Resource/ValidationResultSet.cs	2010-09-28 09:26:53 UTC (rev 5199)
@@ -0,0 +1,85 @@
+#region Disclaimer / License
+// Copyright (C) 2010, Jackie Ng
+// http://trac.osgeo.org/mapguide/wiki/maestro, jumpinjackie at gmail.com
+// 
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// 
+// This library 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
+// Lesser General Public License for more details.
+// 
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+// 
+#endregion
+using System;
+using System.Collections.Generic;
+using System.Text;
+using OSGeo.MapGuide.MaestroAPI.Resource;
+using OSGeo.MapGuide.MaestroAPI;
+
+namespace OSGeo.MapGuide.MaestroAPI.Resource
+{
+    /// <summary>
+    /// A "bucket" class that filters out redundant validation messages
+    /// </summary>
+    public class ValidationResultSet
+    {
+        //HACK: Abusing the Key component of Dictionary<K, V> because there is no
+        //Set collection in .net fx 2.0!
+        private Dictionary<string, Dictionary<ValidationIssue, ValidationIssue>> _issues;
+
+        public ValidationResultSet()
+        {
+            _issues = new Dictionary<string, Dictionary<ValidationIssue, ValidationIssue>>();
+        }
+
+        public ValidationResultSet(List<ValidationIssue> issues) : this()
+        {
+            Check.NotNull(issues, "issues");
+
+            AddIssues(issues);
+        }
+
+        public string[] ResourceIDs
+        {
+            get { return new List<string>(_issues.Keys).ToArray(); }
+        }
+
+        public ICollection<ValidationIssue> GetIssuesForResource(string resourceId)
+        {
+            return _issues[resourceId].Keys;
+        }
+
+        public ValidationIssue[] GetAllIssues()
+        {
+            var issues = new List<ValidationIssue>();
+            foreach (string resId in _issues.Keys)
+            {
+                issues.AddRange(_issues[resId].Keys);
+            }
+            return issues.ToArray();
+        }
+
+        public void AddIssue(ValidationIssue issue)
+        {
+            if (!_issues.ContainsKey(issue.Resource.ResourceID))
+                _issues[issue.Resource.ResourceID] = new Dictionary<ValidationIssue, ValidationIssue>();
+
+            _issues[issue.Resource.ResourceID][issue] = issue;
+        }
+
+        public void AddIssues(IEnumerable<ValidationIssue> issues)
+        {
+            foreach (var issue in issues)
+            {
+                AddIssue(issue);
+            }
+        }
+    }
+}



More information about the mapguide-commits mailing list