Skip to content

Commit d8ab5f3

Browse files
committed
findbugs: equals unit tests
Signed-off-by: Daan Hoogland <daan@onecht.net>
1 parent 3462944 commit d8ab5f3

8 files changed

Lines changed: 127 additions & 8 deletions

File tree

engine/storage/src/org/apache/cloudstack/storage/BaseType.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,24 @@ public boolean equals(Object that) {
2525
return true;
2626
} else if (that instanceof BaseType) {
2727
BaseType th = (BaseType)that;
28-
if (this.toString().equalsIgnoreCase(th.toString())) {
28+
if (toString().equalsIgnoreCase(th.toString())) {
2929
return true;
3030
}
3131
}
3232
return false;
3333
}
3434

35+
@Override
36+
public int hashCode() {
37+
return toString().toLowerCase().hashCode();
38+
}
39+
3540
public boolean isSameTypeAs(Object that) {
36-
if (this.equals(that)){
41+
if (equals(that)){
3742
return true;
3843
}
3944
if (that instanceof String) {
40-
if (this.toString().equalsIgnoreCase((String)that)) {
45+
if (toString().equalsIgnoreCase((String)that)) {
4146
return true;
4247
}
4348
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.apache.cloudstack.storage;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import com.google.common.testing.EqualsTester;
7+
8+
public class BaseTypeTest {
9+
@Test
10+
public void testEquals() {
11+
new EqualsTester()
12+
.addEqualityGroup(new TestType("a"), new TestType("A"))
13+
.addEqualityGroup(new TestType("Bd"), new TestType("bD"))
14+
.testEquals();
15+
}
16+
17+
@Test
18+
public void testIsSameTypeAs() {
19+
Assert.assertTrue("'a' and 'A' should be considdered the same type", new TestType("a").isSameTypeAs("A"));
20+
Assert.assertTrue("'B' and 'b' should be considdered the same address", new TestType("B").isSameTypeAs(new TestType("b")));
21+
}
22+
class TestType extends BaseType {
23+
String content;
24+
public TestType(String t) {
25+
content = t;
26+
}
27+
@Override
28+
public String toString() {
29+
return content;
30+
}
31+
}
32+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.apache.cloudstack.framework.config;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import com.google.common.testing.EqualsTester;
7+
8+
import org.apache.cloudstack.framework.config.ConfigKey.Scope;
9+
10+
import com.cloud.utils.exception.CloudRuntimeException;
11+
12+
public class ConfigKeyTest {
13+
@Test
14+
public void testEquals() {
15+
new EqualsTester()
16+
.addEqualityGroup(new ConfigKey("cat", String.class, "naam", "nick", "bijnaam", true, Scope.Cluster),
17+
new ConfigKey("hond", Boolean.class, "naam", "truus", "thrown name", false),
18+
new ConfigKey(Long.class, "naam", "vis", "goud", "zwemt", true, Scope.Account, 3L)
19+
)
20+
.testEquals();
21+
}
22+
23+
@Test
24+
public void testIsSameKeyAs() {
25+
ConfigKey key = new ConfigKey("cat", String.class, "naam", "nick", "bijnaam", true, Scope.Cluster);
26+
Assert.assertTrue("1 and one should be considdered the same address", key.isSameKeyAs("naam"));
27+
}
28+
29+
@Test(expected = CloudRuntimeException.class)
30+
public void testIsSameKeyAsThrowingCloudRuntimeException() {
31+
ConfigKey key = new ConfigKey("hond", Boolean.class, "naam", "truus", "thrown name", false);
32+
Assert.assertFalse("zero and 0L should be considdered the same address", key.isSameKeyAs(0L));
33+
}
34+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<cs.trilead.version>1.0.0-build217</cs.trilead.version>
6464
<cs.ehcache.version>2.6.9</cs.ehcache.version>
6565
<cs.gson.version>1.7.2</cs.gson.version>
66+
<cs.guava-testlib.version>18.0</cs.guava-testlib.version>
6667
<cs.guava.version>18.0</cs.guava.version>
6768
<cs.xapi.version>6.2.0-3.1</cs.xapi.version>
6869
<cs.httpclient.version>4.3.6</cs.httpclient.version>

utils/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@
162162
<groupId>com.google.code.gson</groupId>
163163
<artifactId>gson</artifactId>
164164
</dependency>
165+
<dependency>
166+
<groupId>com.google.guava</groupId>
167+
<artifactId>guava-testlib</artifactId>
168+
<version>${cs.guava-testlib.version}</version>
169+
</dependency>
165170
</dependencies>
166171
<build>
167172
<plugins>

utils/src/com/cloud/utils/net/Ip4Address.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
public class Ip4Address {
2323
String _addr;
2424
String _mac;
25+
static final String s_empty_mac = "00:00:00:00:00:00";
2526

2627
public Ip4Address(String addr, String mac) {
2728
_addr = addr;
@@ -34,11 +35,11 @@ public Ip4Address(long addr, long mac) {
3435
}
3536

3637
public Ip4Address(String addr) {
37-
this(addr, null);
38+
this(addr, s_empty_mac);
3839
}
3940

4041
public Ip4Address(long addr) {
41-
this(NetUtils.long2Ip(addr), null);
42+
this(NetUtils.long2Ip(addr), s_empty_mac);
4243
}
4344

4445
public String ip4() {
@@ -58,7 +59,7 @@ public boolean equals(Object that) {
5859

5960
if (that instanceof Ip4Address) {
6061
Ip4Address ip4 = (Ip4Address)that;
61-
return this._addr.equals(ip4._addr) && (this._mac == ip4._mac || this._mac.equals(ip4._mac));
62+
return _addr.equals(ip4._addr) && (_mac == ip4._mac || _mac.equals(ip4._mac));
6263
} else {
6364
return false;
6465
}
@@ -68,12 +69,12 @@ public boolean isSameAddressAs(Object other) {
6869
if (other instanceof String) { // Assume that is an ip4 address in String form
6970
return _addr.equals(other);
7071
} else {
71-
return this.equals(other);
72+
return equals(other);
7273
}
7374
}
7475

7576
@Override
7677
public int hashCode(){
77-
return (int)(_mac.hashCode()*_addr.hashCode());
78+
return _mac.hashCode()*_addr.hashCode();
7879
}
7980
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.cloud.utils.net;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import com.google.common.testing.EqualsTester;
7+
8+
public class Ip4AddressTest {
9+
10+
@Test
11+
public void testEquals() throws Exception {
12+
new EqualsTester()
13+
.addEqualityGroup(new Ip4Address("0.0.0.1", "00:00:00:00:00:02"), new Ip4Address(1L, 2L))
14+
.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"))
15+
.testEquals();
16+
}
17+
18+
@Test
19+
public void testIsSameAddressAs() {
20+
Assert.assertTrue("1 and one should be considdered the same address", new Ip4Address(1L, 5L).isSameAddressAs("0.0.0.1"));
21+
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));
22+
}
23+
24+
}

utils/test/com/cloud/utils/net/IpTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121

2222
import static org.junit.Assert.assertEquals;
2323

24+
import org.junit.Assert;
2425
import org.junit.Test;
2526

27+
import com.google.common.testing.EqualsTester;
28+
2629
public class IpTest {
2730

2831
@Test
@@ -43,4 +46,18 @@ public void testStart() {
4346
assertEquals("Minimal address not created", "0.0.0.0", min.addr());
4447
}
4548

49+
@Test
50+
public void testEquals() {
51+
new EqualsTester()
52+
.addEqualityGroup(new Ip("0.0.0.1"), new Ip(1L))
53+
.addEqualityGroup(new Ip("0.0.0.0"), new Ip(0L))
54+
.testEquals();
55+
}
56+
57+
@Test
58+
public void testIsSameAddressAs() {
59+
Assert.assertTrue("1 and one should be considdered the same address", new Ip(1L).isSameAddressAs("0.0.0.1"));
60+
Assert.assertTrue("zero and 0L should be considdered the same address", new Ip("0.0.0.0").isSameAddressAs(0L));
61+
}
62+
4663
}

0 commit comments

Comments
 (0)