kafka-jira mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ASF GitHub Bot (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (KAFKA-6727) org.apache.kafka.clients.admin.Config has broken equals and hashCode method.
Date Mon, 16 Apr 2018 19:33:00 GMT

    [ https://issues.apache.org/jira/browse/KAFKA-6727?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16439922#comment-16439922
] 

ASF GitHub Bot commented on KAFKA-6727:
---------------------------------------

hachikuji closed pull request #4796: KAFKA-6727 fix broken Config hashCode() and equals()
URL: https://github.com/apache/kafka/pull/4796
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/clients/src/main/java/org/apache/kafka/clients/admin/Config.java b/clients/src/main/java/org/apache/kafka/clients/admin/Config.java
index 56f9c270792..c81c0b60274 100644
--- a/clients/src/main/java/org/apache/kafka/clients/admin/Config.java
+++ b/clients/src/main/java/org/apache/kafka/clients/admin/Config.java
@@ -21,39 +21,40 @@
 
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * A configuration object containing the configuration entries for a resource.
- *
+ * <p>
  * The API of this class is evolving, see {@link AdminClient} for details.
  */
 @InterfaceStability.Evolving
 public class Config {
 
-    private final Collection<ConfigEntry> entries;
+    private final Map<String, ConfigEntry> entries = new HashMap<>();
 
     /**
      * Create a configuration instance with the provided entries.
      */
     public Config(Collection<ConfigEntry> entries) {
-        this.entries = Collections.unmodifiableCollection(entries);
+        for (ConfigEntry entry : entries) {
+            this.entries.put(entry.name(), entry);
+        }
     }
 
     /**
      * Configuration entries for a resource.
      */
     public Collection<ConfigEntry> entries() {
-        return entries;
+        return Collections.unmodifiableCollection(entries.values());
     }
 
     /**
      * Get the configuration entry with the provided name or null if there isn't one.
      */
     public ConfigEntry get(String name) {
-        for (ConfigEntry entry : entries)
-            if (entry.name().equals(name))
-                return entry;
-        return null;
+        return entries.get(name);
     }
 
     @Override
@@ -75,6 +76,6 @@ public int hashCode() {
 
     @Override
     public String toString() {
-        return "Config(entries=" + entries + ")";
+        return "Config(entries=" + entries.values() + ")";
     }
 }
diff --git a/clients/src/test/java/org/apache/kafka/clients/admin/ConfigTest.java b/clients/src/test/java/org/apache/kafka/clients/admin/ConfigTest.java
new file mode 100644
index 00000000000..45b174bec19
--- /dev/null
+++ b/clients/src/test/java/org/apache/kafka/clients/admin/ConfigTest.java
@@ -0,0 +1,91 @@
+/*
+ * 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.kafka.clients.admin;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.hasItems;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+public class ConfigTest {
+    private static final ConfigEntry E1 = new ConfigEntry("a", "b");
+    private static final ConfigEntry E2 = new ConfigEntry("c", "d");
+    private Config config;
+
+    @Before
+    public void setUp() {
+        final Collection<ConfigEntry> entries = new ArrayList<>();
+        entries.add(E1);
+        entries.add(E2);
+
+        config = new Config(entries);
+    }
+
+    @Test
+    public void shouldGetEntry() {
+        assertThat(config.get("a"), is(E1));
+        assertThat(config.get("c"), is(E2));
+    }
+
+    @Test
+    public void shouldReturnNullOnGetUnknownEntry() {
+        assertThat(config.get("unknown"), is(nullValue()));
+    }
+
+    @Test
+    public void shouldGetAllEntries() {
+        assertThat(config.entries().size(), is(2));
+        assertThat(config.entries(), hasItems(E1, E2));
+    }
+
+    @Test
+    public void shouldImplementEqualsProperly() {
+        final Collection<ConfigEntry> entries = new ArrayList<>();
+        entries.add(E1);
+
+        assertThat(config, is(equalTo(config)));
+        assertThat(config, is(equalTo(new Config(config.entries()))));
+        assertThat(config, is(not(equalTo(new Config(entries)))));
+        assertThat(config, is(not(equalTo((Object) "this"))));
+    }
+
+    @Test
+    public void shouldImplementHashCodeProperly() {
+        final Collection<ConfigEntry> entries = new ArrayList<>();
+        entries.add(E1);
+
+        assertThat(config.hashCode(), is(config.hashCode()));
+        assertThat(config.hashCode(), is(new Config(config.entries()).hashCode()));
+        assertThat(config.hashCode(), is(not(new Config(entries).hashCode())));
+    }
+
+    @Test
+    public void shouldImplementToStringProperly() {
+        assertThat(config.toString(), containsString(E1.toString()));
+        assertThat(config.toString(), containsString(E2.toString()));
+    }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


> org.apache.kafka.clients.admin.Config has broken equals and hashCode method.
> ----------------------------------------------------------------------------
>
>                 Key: KAFKA-6727
>                 URL: https://issues.apache.org/jira/browse/KAFKA-6727
>             Project: Kafka
>          Issue Type: Improvement
>          Components: clients, tools
>    Affects Versions: 1.1.0
>            Reporter: Andy Coates
>            Assignee: Andy Coates
>            Priority: Minor
>             Fix For: 1.2.0
>
>
> `Config` makes use of `Collections.unmodifiableCollection` to wrap the supplied entries
to make it immutable. Unfortunately, this breaks `hashCode` and `equals`.
> From Java docs:
> > The returned collection does _not_ pass the hashCode and equals operations through
to the backing collection, but relies on {{Object}}'s {{equals}} and {{hashCode}} methods.
> See: https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#unmodifiableCollection(java.util.Collection)
>  
>  



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Mime
View raw message