You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by to...@apache.org on 2011/05/31 20:41:33 UTC

svn commit: r1129840 - in /hadoop/common/trunk: CHANGES.txt src/java/org/apache/hadoop/net/StandardSocketFactory.java src/test/core/org/apache/hadoop/ipc/TestSocketFactory.java

Author: todd
Date: Tue May 31 18:41:33 2011
New Revision: 1129840

URL: http://svn.apache.org/viewvc?rev=1129840&view=rev
Log:
HADOOP-7208. Fix implementation of equals() and hashCode() in StandardSocketFactory. Contributed by Uma Maheswara Rao G.

Added:
    hadoop/common/trunk/src/test/core/org/apache/hadoop/ipc/TestSocketFactory.java
Modified:
    hadoop/common/trunk/CHANGES.txt
    hadoop/common/trunk/src/java/org/apache/hadoop/net/StandardSocketFactory.java

Modified: hadoop/common/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/CHANGES.txt?rev=1129840&r1=1129839&r2=1129840&view=diff
==============================================================================
--- hadoop/common/trunk/CHANGES.txt (original)
+++ hadoop/common/trunk/CHANGES.txt Tue May 31 18:41:33 2011
@@ -258,6 +258,9 @@ Trunk (unreleased changes)
     HADOOP-7282. ipc.Server.getRemoteIp() may return null.  (John George
     via szetszwo)
 
+    HADOOP-7208. Fix implementation of equals() and hashCode() in
+    StandardSocketFactory. (Uma Maheswara Rao G via todd)
+
 Release 0.22.0 - Unreleased
 
   INCOMPATIBLE CHANGES

Modified: hadoop/common/trunk/src/java/org/apache/hadoop/net/StandardSocketFactory.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/src/java/org/apache/hadoop/net/StandardSocketFactory.java?rev=1129840&r1=1129839&r2=1129840&view=diff
==============================================================================
--- hadoop/common/trunk/src/java/org/apache/hadoop/net/StandardSocketFactory.java (original)
+++ hadoop/common/trunk/src/java/org/apache/hadoop/net/StandardSocketFactory.java Tue May 31 18:41:33 2011
@@ -112,16 +112,13 @@ public class StandardSocketFactory exten
       return true;
     if (obj == null)
       return false;
-    if (!(obj instanceof StandardSocketFactory))
-      return false;
-    return true;
+    return obj.getClass().equals(this.getClass());
   }
 
   /* @inheritDoc */
   @Override
   public int hashCode() {
-    // Dummy hash code (to make find bugs happy)
-    return 47;
-  } 
-  
+    return this.getClass().hashCode();
+  }
+
 }

Added: hadoop/common/trunk/src/test/core/org/apache/hadoop/ipc/TestSocketFactory.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/src/test/core/org/apache/hadoop/ipc/TestSocketFactory.java?rev=1129840&view=auto
==============================================================================
--- hadoop/common/trunk/src/test/core/org/apache/hadoop/ipc/TestSocketFactory.java (added)
+++ hadoop/common/trunk/src/test/core/org/apache/hadoop/ipc/TestSocketFactory.java Tue May 31 18:41:33 2011
@@ -0,0 +1,71 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.ipc;
+
+import static org.junit.Assert.assertSame;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.net.SocketFactory;
+
+import junit.framework.Assert;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.net.NetUtils;
+import org.apache.hadoop.net.StandardSocketFactory;
+import org.junit.Test;
+
+public class TestSocketFactory {
+
+  @Test
+  public void testSocketFactoryAsKeyInMap() throws Exception {
+    Map<SocketFactory, Integer> dummyCache = new HashMap<SocketFactory, Integer>();
+    int toBeCached1 = 1;
+    int toBeCached2 = 2;
+    Configuration conf = new Configuration();
+    conf.set("hadoop.rpc.socket.factory.class.default",
+        "org.apache.hadoop.ipc.TestSocketFactory$DummySocketFactory");
+    final SocketFactory dummySocketFactory = NetUtils
+        .getDefaultSocketFactory(conf);
+    dummyCache.put(dummySocketFactory, toBeCached1);
+
+    conf.set("hadoop.rpc.socket.factory.class.default",
+        "org.apache.hadoop.net.StandardSocketFactory");
+    final SocketFactory defaultSocketFactory = NetUtils
+        .getDefaultSocketFactory(conf);
+    dummyCache.put(defaultSocketFactory, toBeCached2);
+
+    Assert
+        .assertEquals("The cache contains two elements", 2, dummyCache.size());
+    Assert.assertEquals("Equals of both socket factory shouldn't be same",
+        defaultSocketFactory.equals(dummySocketFactory), false);
+
+    assertSame(toBeCached2, dummyCache.remove(defaultSocketFactory));
+    dummyCache.put(defaultSocketFactory, toBeCached2);
+    assertSame(toBeCached1, dummyCache.remove(dummySocketFactory));
+
+  }
+
+  /**
+   * A dummy socket factory class that extends the StandardSocketFactory. 
+   */
+  static class DummySocketFactory extends StandardSocketFactory {
+    
+  }
+}