Skip to content

Commit

Permalink
Update callGetter(). No need to check primitive class types.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Torson committed Feb 10, 2016
1 parent 89a1f8c commit 5e91de9
Showing 1 changed file with 3 additions and 25 deletions.
28 changes: 3 additions & 25 deletions src/test/java/com/objectpartners/DtoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -126,31 +125,10 @@ private void callGetter(String fieldName, Method getter, T instance, Class<?> fi
final Object getResult = getter.invoke(instance);

if (fieldType.isPrimitive()) {
/*
* Have to handle primitives explicitly. This is because this is a method and Java will perform autoboxing
* and convert our primitives into objects.
*/
if (Integer.class.equals(expected.getClass())) {
assertEquals(fieldName + " is different", ((Integer) expected).intValue(), getResult);
} else if (Double.class.equals(expected.getClass())) {
assertEquals(fieldName + " is different", ((Double) expected).doubleValue(), getResult);
} else if (Float.class.equals(expected.getClass())) {
assertEquals(fieldName + " is different", ((Float) expected).floatValue(), getResult);
} else if (Long.class.equals(expected.getClass())) {
assertEquals(fieldName + " is different", ((Long) expected).longValue(), getResult);
} else if (Boolean.class.equals(expected.getClass())) {
assertEquals(fieldName + " is different", ((Boolean) expected).booleanValue(), getResult);
} else if (Short.class.equals(expected.getClass())) {
assertEquals(fieldName + " is different", ((Short) expected).shortValue(), getResult);
} else if (Byte.class.equals(expected.getClass())) {
assertEquals(fieldName + " is different", ((Byte) expected).byteValue(), getResult);
} else if (Character.class.equals(expected.getClass())) {
assertEquals(fieldName + " is different", ((Character) expected).charValue(), getResult);
} else {
fail("Unknown primitive type: " + fieldType);
}

/* Calling assetEquals() here due to autoboxing of primitive to object type. */
assertEquals(fieldName + " is different", expected, getResult);
} else {
/* This is a normal object. The object passed in should be the exactly same object we get back. */
assertSame(fieldName + " is different", expected, getResult);
}
}
Expand Down

0 comments on commit 5e91de9

Please sign in to comment.