lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ccurr...@apache.org
Subject svn commit: r1304573 - in /incubator/lucene.net/trunk: src/core/Util/Parameter.cs test/core/Lucene.Net.Test.csproj test/core/Util/TestParameter.cs
Date Fri, 23 Mar 2012 19:43:16 GMT
Author: ccurrens
Date: Fri Mar 23 19:43:16 2012
New Revision: 1304573

URL: http://svn.apache.org/viewvc?rev=1304573&view=rev
Log:
[LUCENENET-472] - Added check for null on all operators and equals

Added:
    incubator/lucene.net/trunk/test/core/Util/TestParameter.cs
Modified:
    incubator/lucene.net/trunk/src/core/Util/Parameter.cs
    incubator/lucene.net/trunk/test/core/Lucene.Net.Test.csproj

Modified: incubator/lucene.net/trunk/src/core/Util/Parameter.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/src/core/Util/Parameter.cs?rev=1304573&r1=1304572&r2=1304573&view=diff
==============================================================================
--- incubator/lucene.net/trunk/src/core/Util/Parameter.cs (original)
+++ incubator/lucene.net/trunk/src/core/Util/Parameter.cs Fri Mar 23 19:43:16 2012
@@ -23,18 +23,18 @@ namespace Lucene.Net.Util
 	
 	/// <summary> A serializable Enum class.</summary>
 	[Serializable]
-    public abstract class Parameter 
+    public abstract class Parameter : IEquatable<Parameter>
 	{
         internal static Dictionary<string, Parameter> allParameters = new Dictionary<string,
Parameter>();
 		
-		private System.String name;
+		private readonly string name;
 		
 		private Parameter()
 		{
 			// typesafe enum pattern, no public constructor
 		}
 		
-		protected internal Parameter(System.String name)
+		protected internal Parameter(string name)
 		{
 			// typesafe enum pattern, no public constructor
 			this.name = name;
@@ -46,7 +46,7 @@ namespace Lucene.Net.Util
 			allParameters[key] = this;
 		}
 		
-		private string MakeKey(System.String name)
+		private string MakeKey(string name)
 		{
 			return GetType() + " " + name;
 		}
@@ -75,8 +75,30 @@ namespace Lucene.Net.Util
 
         public override bool Equals(object obj)
         {
-            if (obj.GetType() != this.GetType()) return false;
-            return this.name.Equals((obj as Parameter).name);
+		    if (ReferenceEquals(null, obj)) return false;
+		    if (ReferenceEquals(this, obj)) return true;
+		    if (obj.GetType() != GetType()) return false;
+		    return Equals((Parameter) obj);
         }
+
+	    public bool Equals(Parameter other)
+	    {
+	        return string.Equals(name, other.name);
+	    }
+
+	    public override int GetHashCode()
+	    {
+	        return (name != null ? name.GetHashCode() : 0);
+	    }
+
+	    public static bool operator ==(Parameter left, Parameter right)
+	    {
+	        return Equals(left, right);
+	    }
+
+	    public static bool operator !=(Parameter left, Parameter right)
+	    {
+	        return !Equals(left, right);
+	    }
 	}
 }
\ No newline at end of file

Modified: incubator/lucene.net/trunk/test/core/Lucene.Net.Test.csproj
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/test/core/Lucene.Net.Test.csproj?rev=1304573&r1=1304572&r2=1304573&view=diff
==============================================================================
--- incubator/lucene.net/trunk/test/core/Lucene.Net.Test.csproj (original)
+++ incubator/lucene.net/trunk/test/core/Lucene.Net.Test.csproj Fri Mar 23 19:43:16 2012
@@ -493,6 +493,7 @@
     <Compile Include="Util\TestIndexableBinaryStringTools.cs" />
     <Compile Include="Util\TestNumericUtils.cs" />
     <Compile Include="Util\TestOpenBitSet.cs" />
+    <Compile Include="Util\TestParameter.cs" />
     <Compile Include="Util\TestPriorityQueue.cs">
       <SubType>Code</SubType>
     </Compile>

Added: incubator/lucene.net/trunk/test/core/Util/TestParameter.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/test/core/Util/TestParameter.cs?rev=1304573&view=auto
==============================================================================
--- incubator/lucene.net/trunk/test/core/Util/TestParameter.cs (added)
+++ incubator/lucene.net/trunk/test/core/Util/TestParameter.cs Fri Mar 23 19:43:16 2012
@@ -0,0 +1,54 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Lucene.Net.Search;
+using Lucene.Net.Util;
+using NUnit.Framework;
+
+namespace Lucene.Net.Test.Util
+{
+    [TestFixture]
+    public class TestParameter
+    {
+        internal class MockParameter : Parameter
+        {
+            public MockParameter(string name)
+                : base(name)
+            { }
+        }
+
+        [Test]
+        public void TestEquals()
+        {
+            var first = new MockParameter("FIRST");
+            var other = new MockParameter("OTHER");
+
+            // Make sure it's equal against itself
+            Assert.AreEqual(first, first);
+            // Not equal if it has a different name
+            Assert.AreNotEqual(first, other);
+            
+            // Test == operator
+            Assert.IsTrue(first == first);
+            Assert.IsFalse(first == other);
+
+            // Test != operator
+            Assert.IsFalse(first != first);
+            Assert.IsTrue(first != other);
+        }
+
+        
+        [Test]
+        public void TestLuceneNet472()
+        {
+            var thing = new MockParameter("THING");
+            var otherThing = new MockParameter("OTHERTHING");
+
+            // LUCENENET-472 - NRE on ==/!= parameter
+            Assert.IsTrue(thing != null);
+            Assert.IsFalse(thing == null);
+            Assert.IsTrue(otherThing != null);
+        }
+    }
+}



Mime
View raw message