removed redundant Long, Short, Double, Float and Boolean instantiations

- Added unit tests
- Added javadoc

Signed-off-by: Laszlo Hornyak <laszlo.hornyak@gmail.com>
This commit is contained in:
Laszlo Hornyak 2014-01-25 19:47:48 +01:00
parent 884e8c6510
commit e4da3775c9
2 changed files with 143 additions and 13 deletions

View File

@ -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);

View File

@ -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);
}
}