ARTEMIS-151 Considering name as part of equals and hashCode

This commit is contained in:
Clebert Suconic 2015-10-07 12:38:31 -04:00
parent c21bee63cb
commit 33188bb4d9
2 changed files with 81 additions and 32 deletions

View File

@ -152,47 +152,32 @@ public class TransportConfiguration implements Serializable {
return params;
}
@Override
public int hashCode() {
return factoryClassName.hashCode();
int result = name != null ? name.hashCode() : 0;
result = 31 * result + factoryClassName.hashCode();
result = 31 * result + (params != null ? params.hashCode() : 0);
return result;
}
@Override
public boolean equals(final Object other) {
if (other instanceof TransportConfiguration == false) {
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
}
TransportConfiguration kother = (TransportConfiguration) other;
TransportConfiguration that = (TransportConfiguration) o;
if (factoryClassName.equals(kother.factoryClassName)) {
if (params == null || params.isEmpty()) {
return kother.params == null || kother.params.isEmpty();
}
else {
if (kother.params == null || kother.params.isEmpty()) {
return false;
}
else if (params.size() == kother.params.size()) {
for (Map.Entry<String, Object> entry : params.entrySet()) {
Object thisVal = entry.getValue();
Object otherVal = kother.params.get(entry.getKey());
if (otherVal == null || !otherVal.equals(thisVal)) {
return false;
}
}
return true;
}
else {
return false;
}
}
}
else {
if (!factoryClassName.equals(that.factoryClassName))
return false;
}
if (name != null ? !name.equals(that.name) : that.name != null)
return false;
if (params != null ? !params.equals(that.params) : that.params != null)
return false;
return true;
}
/**

View File

@ -0,0 +1,64 @@
/**
* 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.activemq.artemis.api.core;
import java.util.HashMap;
import org.junit.Assert;
import org.junit.Test;
public class TransportConfigurationTest {
@Test
public void testEquals() {
TransportConfiguration configuration = new TransportConfiguration("SomeClass", new HashMap<String, Object>(), null);
TransportConfiguration configuration2 = new TransportConfiguration("SomeClass", new HashMap<String, Object>(), null);
Assert.assertEquals(configuration, configuration2);
Assert.assertEquals(configuration.hashCode(), configuration2.hashCode());
HashMap<String, Object> configMap1 = new HashMap<>();
configMap1.put("host", "localhost");
HashMap<String, Object> configMap2 = new HashMap<>();
configMap2.put("host", "localhost");
System.out.println("Equals::" + configMap1.equals(configMap2));
configuration = new TransportConfiguration("SomeClass", configMap1, null);
configuration2 = new TransportConfiguration("SomeClass", configMap2, null);
Assert.assertEquals(configuration, configuration2);
Assert.assertEquals(configuration.hashCode(), configuration2.hashCode());
System.out.println("Equals::" + configMap1.equals(configMap2));
configuration = new TransportConfiguration("SomeClass", configMap1, "name1");
configuration2 = new TransportConfiguration("SomeClass", configMap2, "name2");
Assert.assertNotEquals(configuration, configuration2);
Assert.assertNotEquals(configuration.hashCode(), configuration2.hashCode());
Assert.assertTrue(configuration.isEquivalent(configuration2));
configMap1 = new HashMap<>();
configMap1.put("host", "localhost");
configMap2 = new HashMap<>();
configMap2.put("host", "localhost3");
configuration = new TransportConfiguration("SomeClass", configMap1, null);
configuration2 = new TransportConfiguration("SomeClass", configMap2, null);
Assert.assertNotEquals(configuration, configuration2);
Assert.assertNotEquals(configuration.hashCode(), configuration2.hashCode());
}
}