Return-Path: X-Original-To: apmail-cloudstack-commits-archive@www.apache.org Delivered-To: apmail-cloudstack-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id B58AB105C2 for ; Sat, 25 Jan 2014 19:26:13 +0000 (UTC) Received: (qmail 57837 invoked by uid 500); 25 Jan 2014 19:26:12 -0000 Delivered-To: apmail-cloudstack-commits-archive@cloudstack.apache.org Received: (qmail 57720 invoked by uid 500); 25 Jan 2014 19:26:12 -0000 Mailing-List: contact commits-help@cloudstack.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cloudstack.apache.org Delivered-To: mailing list commits@cloudstack.apache.org Received: (qmail 57701 invoked by uid 99); 25 Jan 2014 19:26:11 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 25 Jan 2014 19:26:11 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 0014A905A08; Sat, 25 Jan 2014 19:26:10 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: kocka@apache.org To: commits@cloudstack.apache.org Date: Sat, 25 Jan 2014 19:26:11 -0000 Message-Id: In-Reply-To: <46b96cad06234150a65c3adfbf765777@git.apache.org> References: <46b96cad06234150a65c3adfbf765777@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [2/2] git commit: updated refs/heads/master to e4da377 removed redundant Long, Short, Double, Float and Boolean instantiations - Added unit tests - Added javadoc Signed-off-by: Laszlo Hornyak 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 Authored: Sat Jan 25 19:47:48 2014 +0100 Committer: Laszlo Hornyak 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 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 getObject(Class type, ResultSet rs, int index) throws SQLException { + protected static M getObject(Class 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 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 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); + } + +}