mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
findbugs: equals unit tests
Signed-off-by: Daan Hoogland <daan@onecht.net>
This commit is contained in:
parent
34629446b6
commit
d8ab5f3ab8
@ -25,19 +25,24 @@ public abstract class BaseType {
|
|||||||
return true;
|
return true;
|
||||||
} else if (that instanceof BaseType) {
|
} else if (that instanceof BaseType) {
|
||||||
BaseType th = (BaseType)that;
|
BaseType th = (BaseType)that;
|
||||||
if (this.toString().equalsIgnoreCase(th.toString())) {
|
if (toString().equalsIgnoreCase(th.toString())) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return toString().toLowerCase().hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isSameTypeAs(Object that) {
|
public boolean isSameTypeAs(Object that) {
|
||||||
if (this.equals(that)){
|
if (equals(that)){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (that instanceof String) {
|
if (that instanceof String) {
|
||||||
if (this.toString().equalsIgnoreCase((String)that)) {
|
if (toString().equalsIgnoreCase((String)that)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
1
pom.xml
1
pom.xml
@ -63,6 +63,7 @@
|
|||||||
<cs.trilead.version>1.0.0-build217</cs.trilead.version>
|
<cs.trilead.version>1.0.0-build217</cs.trilead.version>
|
||||||
<cs.ehcache.version>2.6.9</cs.ehcache.version>
|
<cs.ehcache.version>2.6.9</cs.ehcache.version>
|
||||||
<cs.gson.version>1.7.2</cs.gson.version>
|
<cs.gson.version>1.7.2</cs.gson.version>
|
||||||
|
<cs.guava-testlib.version>18.0</cs.guava-testlib.version>
|
||||||
<cs.guava.version>18.0</cs.guava.version>
|
<cs.guava.version>18.0</cs.guava.version>
|
||||||
<cs.xapi.version>6.2.0-3.1</cs.xapi.version>
|
<cs.xapi.version>6.2.0-3.1</cs.xapi.version>
|
||||||
<cs.httpclient.version>4.3.6</cs.httpclient.version>
|
<cs.httpclient.version>4.3.6</cs.httpclient.version>
|
||||||
|
|||||||
@ -162,6 +162,11 @@
|
|||||||
<groupId>com.google.code.gson</groupId>
|
<groupId>com.google.code.gson</groupId>
|
||||||
<artifactId>gson</artifactId>
|
<artifactId>gson</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.guava</groupId>
|
||||||
|
<artifactId>guava-testlib</artifactId>
|
||||||
|
<version>${cs.guava-testlib.version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
|||||||
@ -22,6 +22,7 @@ package com.cloud.utils.net;
|
|||||||
public class Ip4Address {
|
public class Ip4Address {
|
||||||
String _addr;
|
String _addr;
|
||||||
String _mac;
|
String _mac;
|
||||||
|
static final String s_empty_mac = "00:00:00:00:00:00";
|
||||||
|
|
||||||
public Ip4Address(String addr, String mac) {
|
public Ip4Address(String addr, String mac) {
|
||||||
_addr = addr;
|
_addr = addr;
|
||||||
@ -34,11 +35,11 @@ public class Ip4Address {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Ip4Address(String addr) {
|
public Ip4Address(String addr) {
|
||||||
this(addr, null);
|
this(addr, s_empty_mac);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Ip4Address(long addr) {
|
public Ip4Address(long addr) {
|
||||||
this(NetUtils.long2Ip(addr), null);
|
this(NetUtils.long2Ip(addr), s_empty_mac);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String ip4() {
|
public String ip4() {
|
||||||
@ -58,7 +59,7 @@ public class Ip4Address {
|
|||||||
|
|
||||||
if (that instanceof Ip4Address) {
|
if (that instanceof Ip4Address) {
|
||||||
Ip4Address ip4 = (Ip4Address)that;
|
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 {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -68,12 +69,12 @@ public class Ip4Address {
|
|||||||
if (other instanceof String) { // Assume that is an ip4 address in String form
|
if (other instanceof String) { // Assume that is an ip4 address in String form
|
||||||
return _addr.equals(other);
|
return _addr.equals(other);
|
||||||
} else {
|
} else {
|
||||||
return this.equals(other);
|
return equals(other);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode(){
|
public int hashCode(){
|
||||||
return (int)(_mac.hashCode()*_addr.hashCode());
|
return _mac.hashCode()*_addr.hashCode();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
24
utils/test/com/cloud/utils/net/Ip4AddressTest.java
Normal file
24
utils/test/com/cloud/utils/net/Ip4AddressTest.java
Normal file
@ -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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -21,8 +21,11 @@ package com.cloud.utils.net;
|
|||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.google.common.testing.EqualsTester;
|
||||||
|
|
||||||
public class IpTest {
|
public class IpTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -43,4 +46,18 @@ public class IpTest {
|
|||||||
assertEquals("Minimal address not created", "0.0.0.0", min.addr());
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user