Skip to content

Commit e4da377

Browse files
committed
removed redundant Long, Short, Double, Float and Boolean instantiations
- Added unit tests - Added javadoc Signed-off-by: Laszlo Hornyak <laszlo.hornyak@gmail.com>
1 parent 884e8c6 commit e4da377

2 files changed

Lines changed: 143 additions & 13 deletions

File tree

framework/db/src/com/cloud/utils/db/GenericDaoBase.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -658,9 +658,21 @@ protected void setField(Object entity, Field field, ResultSet rs, int index) thr
658658
}
659659
}
660660

661+
/**
662+
* Get a value from a result set.
663+
*
664+
* @param type
665+
* the expected type of the result
666+
* @param rs
667+
* the result set
668+
* @param index
669+
* the index of the column
670+
* @return the result in the requested type
671+
* @throws SQLException
672+
*/
661673
@DB()
662674
@SuppressWarnings("unchecked")
663-
protected <M> M getObject(Class<M> type, ResultSet rs, int index) throws SQLException {
675+
protected static <M> M getObject(Class<M> type, ResultSet rs, int index) throws SQLException {
664676
if (type == String.class) {
665677
byte[] bytes = rs.getBytes(index);
666678
if (bytes != null) {
@@ -681,12 +693,12 @@ protected <M> M getObject(Class<M> type, ResultSet rs, int index) throws SQLExce
681693
return (M)new Integer(rs.getInt(index));
682694
}
683695
} else if (type == long.class) {
684-
return (M)new Long(rs.getLong(index));
696+
return (M) (Long) rs.getLong(index);
685697
} else if (type == Long.class) {
686698
if (rs.getObject(index) == null) {
687699
return null;
688700
} else {
689-
return (M)new Long(rs.getLong(index));
701+
return (M) (Long) rs.getLong(index);
690702
}
691703
} else if (type == Date.class) {
692704
final Object data = rs.getDate(index);
@@ -696,44 +708,44 @@ protected <M> M getObject(Class<M> type, ResultSet rs, int index) throws SQLExce
696708
return (M)DateUtil.parseDateString(s_gmtTimeZone, rs.getString(index));
697709
}
698710
} else if (type == short.class) {
699-
return (M)new Short(rs.getShort(index));
711+
return (M) (Short) rs.getShort(index);
700712
} else if (type == Short.class) {
701713
if (rs.getObject(index) == null) {
702714
return null;
703715
} else {
704-
return (M)new Short(rs.getShort(index));
716+
return (M) (Short) rs.getShort(index);
705717
}
706718
} else if (type == boolean.class) {
707-
return (M)new Boolean(rs.getBoolean(index));
719+
return (M) (Boolean) rs.getBoolean(index);
708720
} else if (type == Boolean.class) {
709721
if (rs.getObject(index) == null) {
710722
return null;
711723
} else {
712-
return (M)new Boolean(rs.getBoolean(index));
724+
return (M) (Boolean) rs.getBoolean(index);
713725
}
714726
} else if (type == float.class) {
715-
return (M)new Float(rs.getFloat(index));
727+
return (M) (Float) rs.getFloat(index);
716728
} else if (type == Float.class) {
717729
if (rs.getObject(index) == null) {
718730
return null;
719731
} else {
720-
return (M)new Float(rs.getFloat(index));
732+
return (M) (Float) rs.getFloat(index);
721733
}
722734
} else if (type == double.class) {
723-
return (M)new Double(rs.getDouble(index));
735+
return (M) (Double) rs.getDouble(index);
724736
} else if (type == Double.class) {
725737
if (rs.getObject(index) == null) {
726738
return null;
727739
} else {
728-
return (M)new Double(rs.getDouble(index));
740+
return (M) (Double) rs.getDouble(index);
729741
}
730742
} else if (type == byte.class) {
731-
return (M)new Byte(rs.getByte(index));
743+
return (M) (Byte) rs.getByte(index);
732744
} else if (type == Byte.class) {
733745
if (rs.getObject(index) == null) {
734746
return null;
735747
} else {
736-
return (M)new Byte(rs.getByte(index));
748+
return (M) (Byte) rs.getByte(index);
737749
}
738750
} else if (type == Calendar.class) {
739751
final Object data = rs.getDate(index);
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package com.cloud.utils.db;
2+
3+
import java.sql.ResultSet;
4+
import java.sql.SQLException;
5+
6+
import junit.framework.Assert;
7+
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.mockito.Mock;
11+
import org.mockito.Mockito;
12+
import org.mockito.runners.MockitoJUnitRunner;
13+
14+
@RunWith(MockitoJUnitRunner.class)
15+
public class GenericDaoBaseTest {
16+
@Mock
17+
ResultSet resultSet;
18+
19+
@Test
20+
public void getObjectBoolean() throws SQLException {
21+
Mockito.when(resultSet.getObject(1)).thenReturn(false);
22+
Mockito.when(resultSet.getBoolean(1)).thenReturn(false);
23+
Assert.assertFalse(GenericDaoBase
24+
.getObject(Boolean.class, resultSet, 1));
25+
Mockito.verify(resultSet).getBoolean(1);
26+
}
27+
28+
@Test
29+
public void getObjectPrimitiveBoolean() throws SQLException {
30+
Mockito.when(resultSet.getObject(1)).thenReturn(false);
31+
Mockito.when(resultSet.getBoolean(1)).thenReturn(false);
32+
Assert.assertFalse(GenericDaoBase
33+
.getObject(boolean.class, resultSet, 1));
34+
Mockito.verify(resultSet).getBoolean(1);
35+
}
36+
37+
@Test
38+
public void getObjectPrimitiveShort() throws SQLException {
39+
Mockito.when(resultSet.getObject(1)).thenReturn((short) 1);
40+
Mockito.when(resultSet.getShort(1)).thenReturn((short) 1);
41+
Assert.assertEquals(Short.valueOf((short) 1),
42+
GenericDaoBase.getObject(short.class, resultSet, 1));
43+
Mockito.verify(resultSet).getShort(1);
44+
}
45+
46+
@Test
47+
public void getObjectShort() throws SQLException {
48+
Mockito.when(resultSet.getObject(1)).thenReturn((short) 1);
49+
Mockito.when(resultSet.getShort(1)).thenReturn((short) 1);
50+
Assert.assertEquals(Short.valueOf((short) 1),
51+
GenericDaoBase.getObject(Short.class, resultSet, 1));
52+
Mockito.verify(resultSet).getShort(1);
53+
}
54+
55+
@Test
56+
public void getObjectFloat() throws SQLException {
57+
Mockito.when(resultSet.getObject(1)).thenReturn(0.1f);
58+
Mockito.when(resultSet.getFloat(1)).thenReturn(0.1f);
59+
Assert.assertEquals(0.1f,
60+
GenericDaoBase.getObject(Float.class, resultSet, 1));
61+
Mockito.verify(resultSet).getFloat(1);
62+
}
63+
64+
@Test
65+
public void getObjectPrimitiveFloat() throws SQLException {
66+
Mockito.when(resultSet.getObject(1)).thenReturn(0.1f);
67+
Mockito.when(resultSet.getFloat(1)).thenReturn(0.1f);
68+
Assert.assertEquals(0.1f,
69+
GenericDaoBase.getObject(float.class, resultSet, 1));
70+
Mockito.verify(resultSet).getFloat(1);
71+
}
72+
73+
@Test
74+
public void getObjectPrimitiveDouble() throws SQLException {
75+
Mockito.when(resultSet.getObject(1)).thenReturn(0.1d);
76+
Mockito.when(resultSet.getDouble(1)).thenReturn(0.1d);
77+
Assert.assertEquals(0.1d,
78+
GenericDaoBase.getObject(double.class, resultSet, 1));
79+
Mockito.verify(resultSet).getDouble(1);
80+
}
81+
82+
@Test
83+
public void getObjectDouble() throws SQLException {
84+
Mockito.when(resultSet.getObject(1)).thenReturn(0.1d);
85+
Mockito.when(resultSet.getDouble(1)).thenReturn(0.1d);
86+
Assert.assertEquals(0.1d,
87+
GenericDaoBase.getObject(Double.class, resultSet, 1));
88+
Mockito.verify(resultSet).getDouble(1);
89+
}
90+
91+
@Test
92+
public void getObjectLong() throws SQLException {
93+
Mockito.when(resultSet.getObject(1)).thenReturn(1l);
94+
Mockito.when(resultSet.getLong(1)).thenReturn(1l);
95+
Assert.assertEquals((Long) 1l,
96+
GenericDaoBase.getObject(Long.class, resultSet, 1));
97+
Mockito.verify(resultSet).getLong(1);
98+
}
99+
100+
@Test
101+
public void getObjectPrimitiveLong() throws SQLException {
102+
Mockito.when(resultSet.getObject(1)).thenReturn(1l);
103+
Mockito.when(resultSet.getLong(1)).thenReturn(1l);
104+
Assert.assertEquals((Long) 1l,
105+
GenericDaoBase.getObject(long.class, resultSet, 1));
106+
Mockito.verify(resultSet).getLong(1);
107+
}
108+
109+
@Test
110+
public void getObjectPrimitiveByte() throws SQLException {
111+
Mockito.when(resultSet.getObject(1)).thenReturn((byte) 1);
112+
Mockito.when(resultSet.getByte(1)).thenReturn((byte) 1);
113+
Assert.assertTrue((byte) 1 == GenericDaoBase.getObject(byte.class,
114+
resultSet, 1));
115+
Mockito.verify(resultSet).getByte(1);
116+
}
117+
118+
}

0 commit comments

Comments
 (0)