From b0c968e2b9a8d1dd8b62677df497ce20b48b848a Mon Sep 17 00:00:00 2001 From: Todd Lipcon Date: Tue, 31 May 2011 18:41:33 +0000 Subject: [PATCH] HADOOP-7208. Fix implementation of equals() and hashCode() in StandardSocketFactory. Contributed by Uma Maheswara Rao G. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1129840 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 3 + .../hadoop/net/StandardSocketFactory.java | 11 ++- .../apache/hadoop/ipc/TestSocketFactory.java | 71 +++++++++++++++++++ 3 files changed, 78 insertions(+), 7 deletions(-) create mode 100644 src/test/core/org/apache/hadoop/ipc/TestSocketFactory.java diff --git a/CHANGES.txt b/CHANGES.txt index 7a0609c665..d75b23d1e8 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -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 diff --git a/src/java/org/apache/hadoop/net/StandardSocketFactory.java b/src/java/org/apache/hadoop/net/StandardSocketFactory.java index 359b8963c8..f4942cef26 100644 --- a/src/java/org/apache/hadoop/net/StandardSocketFactory.java +++ b/src/java/org/apache/hadoop/net/StandardSocketFactory.java @@ -112,16 +112,13 @@ public boolean equals(Object obj) { 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(); + } + } diff --git a/src/test/core/org/apache/hadoop/ipc/TestSocketFactory.java b/src/test/core/org/apache/hadoop/ipc/TestSocketFactory.java new file mode 100644 index 0000000000..3298f1e605 --- /dev/null +++ b/src/test/core/org/apache/hadoop/ipc/TestSocketFactory.java @@ -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 dummyCache = new HashMap(); + 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 { + + } +}