cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From muralire...@apache.org
Subject [10/17] git commit: updated refs/heads/regionvpc to cf18fe1
Date Tue, 28 Jan 2014 09:26:41 GMT
removed redundant Long, Short, Double, Float and Boolean instantiations

- Added unit tests
- Added javadoc

Signed-off-by: Laszlo Hornyak <laszlo.hornyak@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/regionvpc
Commit: e4da3775c99847deee22073a662b95777b2ac285
Parents: 884e8c6
Author: Laszlo Hornyak <laszlo.hornyak@gmail.com>
Authored: Sat Jan 25 19:47:48 2014 +0100
Committer: Laszlo Hornyak <laszlo.hornyak@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);
+    }
+
+}


Mime
View raw message