cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wid...@apache.org
Subject [43/50] [abbrv] git commit: updated refs/heads/reporter to 28300b5
Date Wed, 08 Jul 2015 19:20:18 GMT
findbugs: equals unit tests

Signed-off-by: Daan Hoogland <daan@onecht.net>


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/d8ab5f3a
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/d8ab5f3a
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/d8ab5f3a

Branch: refs/heads/reporter
Commit: d8ab5f3ab815579c651150ad273facd0854ea51f
Parents: 3462944
Author: Daan Hoogland <daan.hoogland@gmail.com>
Authored: Mon Jul 6 16:07:02 2015 +0200
Committer: Daan Hoogland <daan@onecht.net>
Committed: Tue Jul 7 13:48:19 2015 +0200

----------------------------------------------------------------------
 .../org/apache/cloudstack/storage/BaseType.java | 11 +++++--
 .../apache/cloudstack/storage/BaseTypeTest.java | 32 ++++++++++++++++++
 .../framework/config/ConfigKeyTest.java         | 34 ++++++++++++++++++++
 pom.xml                                         |  1 +
 utils/pom.xml                                   |  5 +++
 utils/src/com/cloud/utils/net/Ip4Address.java   | 11 ++++---
 .../com/cloud/utils/net/Ip4AddressTest.java     | 24 ++++++++++++++
 utils/test/com/cloud/utils/net/IpTest.java      | 17 ++++++++++
 8 files changed, 127 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d8ab5f3a/engine/storage/src/org/apache/cloudstack/storage/BaseType.java
----------------------------------------------------------------------
diff --git a/engine/storage/src/org/apache/cloudstack/storage/BaseType.java b/engine/storage/src/org/apache/cloudstack/storage/BaseType.java
index 6d27772..1df02ba 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;
             }
         }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d8ab5f3a/engine/storage/test/org/apache/cloudstack/storage/BaseTypeTest.java
----------------------------------------------------------------------
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 0000000..5cce015
--- /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;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d8ab5f3a/framework/config/test/org/apache/cloudstack/framework/config/ConfigKeyTest.java
----------------------------------------------------------------------
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 0000000..b02ac1f
--- /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));
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d8ab5f3a/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 900f2e4..e76d747 100644
--- a/pom.xml
+++ b/pom.xml
@@ -63,6 +63,7 @@
     <cs.trilead.version>1.0.0-build217</cs.trilead.version>
     <cs.ehcache.version>2.6.9</cs.ehcache.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.xapi.version>6.2.0-3.1</cs.xapi.version>
     <cs.httpclient.version>4.3.6</cs.httpclient.version>

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d8ab5f3a/utils/pom.xml
----------------------------------------------------------------------
diff --git a/utils/pom.xml b/utils/pom.xml
index c1a5610..937dc08 100755
--- a/utils/pom.xml
+++ b/utils/pom.xml
@@ -162,6 +162,11 @@
       <groupId>com.google.code.gson</groupId>
       <artifactId>gson</artifactId>
     </dependency>
+    <dependency>
+      <groupId>com.google.guava</groupId>
+      <artifactId>guava-testlib</artifactId>
+      <version>${cs.guava-testlib.version}</version>
+    </dependency>
   </dependencies>
   <build>
     <plugins>

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d8ab5f3a/utils/src/com/cloud/utils/net/Ip4Address.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/net/Ip4Address.java b/utils/src/com/cloud/utils/net/Ip4Address.java
index 9712a1f..0bd814d 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();
     }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d8ab5f3a/utils/test/com/cloud/utils/net/Ip4AddressTest.java
----------------------------------------------------------------------
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 0000000..d4e24d3
--- /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));
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d8ab5f3a/utils/test/com/cloud/utils/net/IpTest.java
----------------------------------------------------------------------
diff --git a/utils/test/com/cloud/utils/net/IpTest.java b/utils/test/com/cloud/utils/net/IpTest.java
index d11b779..89608f1 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));
+    }
+
 }


Mime
View raw message