You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by ko...@apache.org on 2014/01/25 20:26:10 UTC

[1/2] git commit: updated refs/heads/master to e4da377

Updated Branches:
  refs/heads/master ebf57654e -> e4da3775c


replaced Long instantiation with parseLong

Signed-off-by: Laszlo Hornyak <la...@gmail.com>


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

Branch: refs/heads/master
Commit: 884e8c65107ca4f36f7328637f54797b7e9b775b
Parents: ebf5765
Author: Laszlo Hornyak <la...@gmail.com>
Authored: Sat Jan 25 16:11:51 2014 +0100
Committer: Laszlo Hornyak <la...@gmail.com>
Committed: Sat Jan 25 16:11:51 2014 +0100

----------------------------------------------------------------------
 .../hypervisor/kvm/resource/LibvirtComputingResource.java    | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/884e8c65/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
----------------------------------------------------------------------
diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
index ab37446..70975fa 100755
--- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
+++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
@@ -3098,8 +3098,8 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
             String[] splitResult = result.split(":");
             int i = 0;
             while (i < splitResult.length - 1) {
-                stats[0] += (new Long(splitResult[i++])).longValue();
-                stats[1] += (new Long(splitResult[i++])).longValue();
+                stats[0] += Long.parseLong(splitResult[i++]);
+                stats[1] += Long.parseLong(splitResult[i++]);
             }
         }
         return stats;
@@ -3140,8 +3140,8 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
             String[] splitResult = result.split(":");
             int i = 0;
             while (i < splitResult.length - 1) {
-                stats[0] += (new Long(splitResult[i++])).longValue();
-                stats[1] += (new Long(splitResult[i++])).longValue();
+                stats[0] += Long.parseLong(splitResult[i++]);
+                stats[1] += Long.parseLong(splitResult[i++]);
             }
         }
         return stats;


[2/2] git commit: updated refs/heads/master to e4da377

Posted by ko...@apache.org.
removed redundant Long, Short, Double, Float and Boolean instantiations

- Added unit tests
- Added javadoc

Signed-off-by: Laszlo Hornyak <la...@gmail.com>


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

Branch: refs/heads/master
Commit: e4da3775c99847deee22073a662b95777b2ac285
Parents: 884e8c6
Author: Laszlo Hornyak <la...@gmail.com>
Authored: Sat Jan 25 19:47:48 2014 +0100
Committer: Laszlo Hornyak <la...@gmail.com>
Committed: Sat Jan 25 20:09:45 2014 +0100

----------------------------------------------------------------------
 .../src/com/cloud/utils/db/GenericDaoBase.java  |  38 ++++--
 .../com/cloud/utils/db/GenericDaoBaseTest.java  | 118 +++++++++++++++++++
 2 files changed, 143 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e4da3775/framework/db/src/com/cloud/utils/db/GenericDaoBase.java
----------------------------------------------------------------------
diff --git a/framework/db/src/com/cloud/utils/db/GenericDaoBase.java b/framework/db/src/com/cloud/utils/db/GenericDaoBase.java
index 503d759..f282428 100755
--- a/framework/db/src/com/cloud/utils/db/GenericDaoBase.java
+++ b/framework/db/src/com/cloud/utils/db/GenericDaoBase.java
@@ -658,9 +658,21 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
         }
     }
 
+    /**
+     * Get a value from a result set.
+     *
+     * @param type
+     *            the expected type of the result
+     * @param rs
+     *            the result set
+     * @param index
+     *            the index of the column
+     * @return the result in the requested type
+     * @throws SQLException
+     */
     @DB()
     @SuppressWarnings("unchecked")
-    protected <M> M getObject(Class<M> type, ResultSet rs, int index) throws SQLException {
+    protected static <M> M getObject(Class<M> type, ResultSet rs, int index) throws SQLException {
         if (type == String.class) {
             byte[] bytes = rs.getBytes(index);
             if (bytes != null) {
@@ -681,12 +693,12 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
                 return (M)new Integer(rs.getInt(index));
             }
         } else if (type == long.class) {
-            return (M)new Long(rs.getLong(index));
+            return (M) (Long) rs.getLong(index);
         } else if (type == Long.class) {
             if (rs.getObject(index) == null) {
                 return null;
             } else {
-                return (M)new Long(rs.getLong(index));
+                return (M) (Long) rs.getLong(index);
             }
         } else if (type == Date.class) {
             final Object data = rs.getDate(index);
@@ -696,44 +708,44 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
                 return (M)DateUtil.parseDateString(s_gmtTimeZone, rs.getString(index));
             }
         } else if (type == short.class) {
-            return (M)new Short(rs.getShort(index));
+            return (M) (Short) rs.getShort(index);
         } else if (type == Short.class) {
             if (rs.getObject(index) == null) {
                 return null;
             } else {
-                return (M)new Short(rs.getShort(index));
+                return (M) (Short) rs.getShort(index);
             }
         } else if (type == boolean.class) {
-            return (M)new Boolean(rs.getBoolean(index));
+            return (M) (Boolean) rs.getBoolean(index);
         } else if (type == Boolean.class) {
             if (rs.getObject(index) == null) {
                 return null;
             } else {
-                return (M)new Boolean(rs.getBoolean(index));
+                return (M) (Boolean) rs.getBoolean(index);
             }
         } else if (type == float.class) {
-            return (M)new Float(rs.getFloat(index));
+            return (M) (Float) rs.getFloat(index);
         } else if (type == Float.class) {
             if (rs.getObject(index) == null) {
                 return null;
             } else {
-                return (M)new Float(rs.getFloat(index));
+                return (M) (Float) rs.getFloat(index);
             }
         } else if (type == double.class) {
-            return (M)new Double(rs.getDouble(index));
+            return (M) (Double) rs.getDouble(index);
         } else if (type == Double.class) {
             if (rs.getObject(index) == null) {
                 return null;
             } else {
-                return (M)new Double(rs.getDouble(index));
+                return (M) (Double) rs.getDouble(index);
             }
         } else if (type == byte.class) {
-            return (M)new Byte(rs.getByte(index));
+            return (M) (Byte) rs.getByte(index);
         } else if (type == Byte.class) {
             if (rs.getObject(index) == null) {
                 return null;
             } else {
-                return (M)new Byte(rs.getByte(index));
+                return (M) (Byte) rs.getByte(index);
             }
         } else if (type == Calendar.class) {
             final Object data = rs.getDate(index);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/e4da3775/framework/db/test/com/cloud/utils/db/GenericDaoBaseTest.java
----------------------------------------------------------------------
diff --git a/framework/db/test/com/cloud/utils/db/GenericDaoBaseTest.java b/framework/db/test/com/cloud/utils/db/GenericDaoBaseTest.java
new file mode 100644
index 0000000..7363d43
--- /dev/null
+++ b/framework/db/test/com/cloud/utils/db/GenericDaoBaseTest.java
@@ -0,0 +1,118 @@
+package com.cloud.utils.db;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import junit.framework.Assert;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+
+@RunWith(MockitoJUnitRunner.class)
+public class GenericDaoBaseTest {
+    @Mock
+    ResultSet resultSet;
+
+    @Test
+    public void getObjectBoolean() throws SQLException {
+        Mockito.when(resultSet.getObject(1)).thenReturn(false);
+        Mockito.when(resultSet.getBoolean(1)).thenReturn(false);
+        Assert.assertFalse(GenericDaoBase
+                .getObject(Boolean.class, resultSet, 1));
+        Mockito.verify(resultSet).getBoolean(1);
+    }
+
+    @Test
+    public void getObjectPrimitiveBoolean() throws SQLException {
+        Mockito.when(resultSet.getObject(1)).thenReturn(false);
+        Mockito.when(resultSet.getBoolean(1)).thenReturn(false);
+        Assert.assertFalse(GenericDaoBase
+                .getObject(boolean.class, resultSet, 1));
+        Mockito.verify(resultSet).getBoolean(1);
+    }
+
+    @Test
+    public void getObjectPrimitiveShort() throws SQLException {
+        Mockito.when(resultSet.getObject(1)).thenReturn((short) 1);
+        Mockito.when(resultSet.getShort(1)).thenReturn((short) 1);
+        Assert.assertEquals(Short.valueOf((short) 1),
+                GenericDaoBase.getObject(short.class, resultSet, 1));
+        Mockito.verify(resultSet).getShort(1);
+    }
+
+    @Test
+    public void getObjectShort() throws SQLException {
+        Mockito.when(resultSet.getObject(1)).thenReturn((short) 1);
+        Mockito.when(resultSet.getShort(1)).thenReturn((short) 1);
+        Assert.assertEquals(Short.valueOf((short) 1),
+                GenericDaoBase.getObject(Short.class, resultSet, 1));
+        Mockito.verify(resultSet).getShort(1);
+    }
+
+    @Test
+    public void getObjectFloat() throws SQLException {
+        Mockito.when(resultSet.getObject(1)).thenReturn(0.1f);
+        Mockito.when(resultSet.getFloat(1)).thenReturn(0.1f);
+        Assert.assertEquals(0.1f,
+                GenericDaoBase.getObject(Float.class, resultSet, 1));
+        Mockito.verify(resultSet).getFloat(1);
+    }
+
+    @Test
+    public void getObjectPrimitiveFloat() throws SQLException {
+        Mockito.when(resultSet.getObject(1)).thenReturn(0.1f);
+        Mockito.when(resultSet.getFloat(1)).thenReturn(0.1f);
+        Assert.assertEquals(0.1f,
+                GenericDaoBase.getObject(float.class, resultSet, 1));
+        Mockito.verify(resultSet).getFloat(1);
+    }
+
+    @Test
+    public void getObjectPrimitiveDouble() throws SQLException {
+        Mockito.when(resultSet.getObject(1)).thenReturn(0.1d);
+        Mockito.when(resultSet.getDouble(1)).thenReturn(0.1d);
+        Assert.assertEquals(0.1d,
+                GenericDaoBase.getObject(double.class, resultSet, 1));
+        Mockito.verify(resultSet).getDouble(1);
+    }
+
+    @Test
+    public void getObjectDouble() throws SQLException {
+        Mockito.when(resultSet.getObject(1)).thenReturn(0.1d);
+        Mockito.when(resultSet.getDouble(1)).thenReturn(0.1d);
+        Assert.assertEquals(0.1d,
+                GenericDaoBase.getObject(Double.class, resultSet, 1));
+        Mockito.verify(resultSet).getDouble(1);
+    }
+
+    @Test
+    public void getObjectLong() throws SQLException {
+        Mockito.when(resultSet.getObject(1)).thenReturn(1l);
+        Mockito.when(resultSet.getLong(1)).thenReturn(1l);
+        Assert.assertEquals((Long) 1l,
+                GenericDaoBase.getObject(Long.class, resultSet, 1));
+        Mockito.verify(resultSet).getLong(1);
+    }
+
+    @Test
+    public void getObjectPrimitiveLong() throws SQLException {
+        Mockito.when(resultSet.getObject(1)).thenReturn(1l);
+        Mockito.when(resultSet.getLong(1)).thenReturn(1l);
+        Assert.assertEquals((Long) 1l,
+                GenericDaoBase.getObject(long.class, resultSet, 1));
+        Mockito.verify(resultSet).getLong(1);
+    }
+
+    @Test
+    public void getObjectPrimitiveByte() throws SQLException {
+        Mockito.when(resultSet.getObject(1)).thenReturn((byte) 1);
+        Mockito.when(resultSet.getByte(1)).thenReturn((byte) 1);
+        Assert.assertTrue((byte) 1 == GenericDaoBase.getObject(byte.class,
+                resultSet, 1));
+        Mockito.verify(resultSet).getByte(1);
+    }
+
+}