diff --git a/engine/storage/src/org/apache/cloudstack/storage/BaseType.java b/engine/storage/src/org/apache/cloudstack/storage/BaseType.java index 6d27772b557..1df02baac31 100644 --- a/engine/storage/src/org/apache/cloudstack/storage/BaseType.java +++ b/engine/storage/src/org/apache/cloudstack/storage/BaseType.java @@ -25,19 +25,24 @@ public abstract class BaseType { return true; } else if (that instanceof BaseType) { BaseType th = (BaseType)that; - if (this.toString().equalsIgnoreCase(th.toString())) { + if (toString().equalsIgnoreCase(th.toString())) { return true; } } return false; } + @Override + public int hashCode() { + return toString().toLowerCase().hashCode(); + } + public boolean isSameTypeAs(Object that) { - if (this.equals(that)){ + if (equals(that)){ return true; } if (that instanceof String) { - if (this.toString().equalsIgnoreCase((String)that)) { + if (toString().equalsIgnoreCase((String)that)) { return true; } } diff --git a/engine/storage/test/org/apache/cloudstack/storage/BaseTypeTest.java b/engine/storage/test/org/apache/cloudstack/storage/BaseTypeTest.java new file mode 100644 index 00000000000..5cce0153157 --- /dev/null +++ b/engine/storage/test/org/apache/cloudstack/storage/BaseTypeTest.java @@ -0,0 +1,32 @@ +package org.apache.cloudstack.storage; + +import org.junit.Assert; +import org.junit.Test; + +import com.google.common.testing.EqualsTester; + +public class BaseTypeTest { + @Test + public void testEquals() { + new EqualsTester() + .addEqualityGroup(new TestType("a"), new TestType("A")) + .addEqualityGroup(new TestType("Bd"), new TestType("bD")) + .testEquals(); + } + + @Test + public void testIsSameTypeAs() { + Assert.assertTrue("'a' and 'A' should be considdered the same type", new TestType("a").isSameTypeAs("A")); + Assert.assertTrue("'B' and 'b' should be considdered the same address", new TestType("B").isSameTypeAs(new TestType("b"))); + } + class TestType extends BaseType { + String content; + public TestType(String t) { + content = t; + } + @Override + public String toString() { + return content; + } + } +} diff --git a/framework/config/test/org/apache/cloudstack/framework/config/ConfigKeyTest.java b/framework/config/test/org/apache/cloudstack/framework/config/ConfigKeyTest.java new file mode 100644 index 00000000000..b02ac1f53b1 --- /dev/null +++ b/framework/config/test/org/apache/cloudstack/framework/config/ConfigKeyTest.java @@ -0,0 +1,34 @@ +package org.apache.cloudstack.framework.config; + +import org.junit.Assert; +import org.junit.Test; + +import com.google.common.testing.EqualsTester; + +import org.apache.cloudstack.framework.config.ConfigKey.Scope; + +import com.cloud.utils.exception.CloudRuntimeException; + +public class ConfigKeyTest { + @Test + public void testEquals() { + new EqualsTester() + .addEqualityGroup(new ConfigKey("cat", String.class, "naam", "nick", "bijnaam", true, Scope.Cluster), + new ConfigKey("hond", Boolean.class, "naam", "truus", "thrown name", false), + new ConfigKey(Long.class, "naam", "vis", "goud", "zwemt", true, Scope.Account, 3L) + ) + .testEquals(); + } + + @Test + public void testIsSameKeyAs() { + ConfigKey key = new ConfigKey("cat", String.class, "naam", "nick", "bijnaam", true, Scope.Cluster); + Assert.assertTrue("1 and one should be considdered the same address", key.isSameKeyAs("naam")); + } + + @Test(expected = CloudRuntimeException.class) + public void testIsSameKeyAsThrowingCloudRuntimeException() { + ConfigKey key = new ConfigKey("hond", Boolean.class, "naam", "truus", "thrown name", false); + Assert.assertFalse("zero and 0L should be considdered the same address", key.isSameKeyAs(0L)); + } +} diff --git a/pom.xml b/pom.xml index 900f2e4a696..e76d74735c3 100644 --- a/pom.xml +++ b/pom.xml @@ -63,6 +63,7 @@ 1.0.0-build217 2.6.9 1.7.2 + 18.0 18.0 6.2.0-3.1 4.3.6 diff --git a/utils/pom.xml b/utils/pom.xml index c1a56104dfe..937dc08f7e0 100755 --- a/utils/pom.xml +++ b/utils/pom.xml @@ -162,6 +162,11 @@ com.google.code.gson gson + + com.google.guava + guava-testlib + ${cs.guava-testlib.version} + diff --git a/utils/src/com/cloud/utils/net/Ip4Address.java b/utils/src/com/cloud/utils/net/Ip4Address.java index 9712a1ff4a0..0bd814d605e 100644 --- a/utils/src/com/cloud/utils/net/Ip4Address.java +++ b/utils/src/com/cloud/utils/net/Ip4Address.java @@ -22,6 +22,7 @@ package com.cloud.utils.net; public class Ip4Address { String _addr; String _mac; + static final String s_empty_mac = "00:00:00:00:00:00"; public Ip4Address(String addr, String mac) { _addr = addr; @@ -34,11 +35,11 @@ public class Ip4Address { } public Ip4Address(String addr) { - this(addr, null); + this(addr, s_empty_mac); } public Ip4Address(long addr) { - this(NetUtils.long2Ip(addr), null); + this(NetUtils.long2Ip(addr), s_empty_mac); } public String ip4() { @@ -58,7 +59,7 @@ public class Ip4Address { if (that instanceof Ip4Address) { Ip4Address ip4 = (Ip4Address)that; - return this._addr.equals(ip4._addr) && (this._mac == ip4._mac || this._mac.equals(ip4._mac)); + return _addr.equals(ip4._addr) && (_mac == ip4._mac || _mac.equals(ip4._mac)); } else { return false; } @@ -68,12 +69,12 @@ public class Ip4Address { if (other instanceof String) { // Assume that is an ip4 address in String form return _addr.equals(other); } else { - return this.equals(other); + return equals(other); } } @Override public int hashCode(){ - return (int)(_mac.hashCode()*_addr.hashCode()); + return _mac.hashCode()*_addr.hashCode(); } } diff --git a/utils/test/com/cloud/utils/net/Ip4AddressTest.java b/utils/test/com/cloud/utils/net/Ip4AddressTest.java new file mode 100644 index 00000000000..d4e24d33c63 --- /dev/null +++ b/utils/test/com/cloud/utils/net/Ip4AddressTest.java @@ -0,0 +1,24 @@ +package com.cloud.utils.net; + +import org.junit.Assert; +import org.junit.Test; + +import com.google.common.testing.EqualsTester; + +public class Ip4AddressTest { + + @Test + public void testEquals() throws Exception { + new EqualsTester() + .addEqualityGroup(new Ip4Address("0.0.0.1", "00:00:00:00:00:02"), new Ip4Address(1L, 2L)) + .addEqualityGroup(new Ip4Address("0.0.0.1", "00:00:00:00:00:00"), new Ip4Address(1L, 0L), new Ip4Address(1L, 0L), new Ip4Address(1L), new Ip4Address("0.0.0.1")) + .testEquals(); + } + + @Test + public void testIsSameAddressAs() { + Assert.assertTrue("1 and one should be considdered the same address", new Ip4Address(1L, 5L).isSameAddressAs("0.0.0.1")); + Assert.assertFalse("zero and 0L should be considdered the same address but a Long won't be accepted", new Ip4Address("0.0.0.0", "00:00:00:00:00:08").isSameAddressAs(0L)); + } + +} diff --git a/utils/test/com/cloud/utils/net/IpTest.java b/utils/test/com/cloud/utils/net/IpTest.java index d11b779ad91..89608f1967f 100644 --- a/utils/test/com/cloud/utils/net/IpTest.java +++ b/utils/test/com/cloud/utils/net/IpTest.java @@ -21,8 +21,11 @@ package com.cloud.utils.net; import static org.junit.Assert.assertEquals; +import org.junit.Assert; import org.junit.Test; +import com.google.common.testing.EqualsTester; + public class IpTest { @Test @@ -43,4 +46,18 @@ public class IpTest { assertEquals("Minimal address not created", "0.0.0.0", min.addr()); } + @Test + public void testEquals() { + new EqualsTester() + .addEqualityGroup(new Ip("0.0.0.1"), new Ip(1L)) + .addEqualityGroup(new Ip("0.0.0.0"), new Ip(0L)) + .testEquals(); + } + + @Test + public void testIsSameAddressAs() { + Assert.assertTrue("1 and one should be considdered the same address", new Ip(1L).isSameAddressAs("0.0.0.1")); + Assert.assertTrue("zero and 0L should be considdered the same address", new Ip("0.0.0.0").isSameAddressAs(0L)); + } + }