Skip to content

Commit

Permalink
Fixed javadoc and added date object as default object type.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Torson committed Feb 10, 2016
1 parent 5e91de9 commit 2520911
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/objectpartners/EverythingTransfer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.objectpartners;

import java.util.Date;

/**
* An example object with all primitives to verify code coverage.
*/
Expand All @@ -8,6 +10,7 @@ public class EverythingTransfer {
private Boolean booleanObject;
private Byte byteObject;
private Character characterObject;
private Date dateObject;
private Double doubleObject;
private Float floatObject;
private Integer intObject;
Expand Down Expand Up @@ -35,6 +38,10 @@ public Character getCharacterObject() {
return characterObject;
}

public Date getDateObject() {
return dateObject;
}

public Double getDoubleObject() {
return doubleObject;
}
Expand Down Expand Up @@ -103,6 +110,10 @@ public void setCharacterObject(Character characterObject) {
this.characterObject = characterObject;
}

public void setDateObject(Date dateObject) {
this.dateObject = dateObject;
}

public void setDoubleObject(Double doubleObject) {
this.doubleObject = doubleObject;
}
Expand Down
34 changes: 21 additions & 13 deletions src/test/java/com/objectpartners/DtoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand All @@ -25,13 +26,14 @@

/**
* A utility class which allows for testing entity and transfer object classes. This is mainly for code coverage since
* these types of objects are normally nothing more than setters and settings. If any logic exists in the method, then
* the get method should be sent in to be ignored and a custom test function should be written.
* these types of objects are normally nothing more than getters and setters. If any logic exists in the method, then
* the get method name should be sent in as an ignored field and a custom test function should be written.
*
* @param <T> The object type to test.
*/
public abstract class DtoTest<T> {

/** A map of default mappers for common objects. */
private static final ImmutableMap<Class<?>, Supplier<?>> DEFAULT_MAPPERS;

static {
Expand All @@ -57,6 +59,7 @@ public abstract class DtoTest<T> {
mapperBuilder.put(Character.class, () -> Character.valueOf((char) 0));

mapperBuilder.put(BigDecimal.class, () -> BigDecimal.ONE);
mapperBuilder.put(Date.class, () -> new Date());

/* Collection Types. */
mapperBuilder.put(Set.class, () -> Collections.emptySet());
Expand All @@ -77,16 +80,16 @@ public abstract class DtoTest<T> {
private final ImmutableMap<Class<?>, Supplier<?>> mappers;

/**
* Creates an instance of {@link TransferObjectTest} with the default ignore fields.
* Creates an instance of {@link DtoTest} with the default ignore fields.
*/
protected DtoTest() {
this(null, null);
}

/**
* Creates an instance of {@link TransferObjectTest} with the default ignore fields.
* Creates an instance of {@link DtoTest} with the default ignore fields.
*
* @param capitalizedVariables If the variables in the test class start with a capital letter.
* @param customMappers Any custom mappers for a given class type.
* @param ignoreFields The getters which should be ignored (e.g., "getId" or "isActive").
*/
protected DtoTest(Map<Class<?>, Supplier<?>> customMappers, Set<String> ignoreFields) {
Expand All @@ -112,19 +115,23 @@ protected DtoTest(Map<Class<?>, Supplier<?>> customMappers, Set<String> ignoreFi
* @param fieldName The field name (used for error messages).
* @param getter The get {@link Method}.
* @param instance The test instance.
* @param fieldType The type of the return type.
* @param expected The expected result.
*
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InvocationTargetException
* @throws IllegalAccessException if this Method object is enforcing Java language access control and the underlying
* method is inaccessible.
* @throws IllegalArgumentException if the method is an instance method and the specified object argument is not an
* instance of the class or interface declaring the underlying method (or of a subclass or implementor
* thereof); if the number of actual and formal parameters differ; if an unwrapping conversion for
* primitive arguments fails; or if, after possible unwrapping, a parameter value cannot be converted to
* the corresponding formal parameter type by a method invocation conversion.
* @throws InvocationTargetException if the underlying method throws an exception.
*/
private void callGetter(String fieldName, Method getter, T instance, Class<?> fieldType, Object expected)
private void callGetter(String fieldName, Method getter, T instance, Object expected)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {

final Object getResult = getter.invoke(instance);

if (fieldType.isPrimitive()) {
if (getter.getReturnType().isPrimitive()) {
/* Calling assetEquals() here due to autoboxing of primitive to object type. */
assertEquals(fieldName + " is different", expected, getResult);
} else {
Expand All @@ -136,6 +143,7 @@ private void callGetter(String fieldName, Method getter, T instance, Class<?> fi
/**
* Creates an object for the given {@link Class}.
*
* @param fieldName The name of the field.
* @param clazz The {@link Class} type to create.
*
* @return A new instance for the given {@link Class}.
Expand Down Expand Up @@ -244,7 +252,7 @@ public void testGettersAndSetters() throws Exception {

pair.getSetter().invoke(instance, newObject);

callGetter(fieldName, pair.getGetter(), instance, pair.getGetter().getReturnType(), newObject);
callGetter(fieldName, pair.getGetter(), instance, newObject);
} else if (pair.getGetter() != null) {
/*
* Object is immutable (no setter but Hibernate or something else sets it via reflection). Use
Expand All @@ -255,7 +263,7 @@ public void testGettersAndSetters() throws Exception {
field.setAccessible(true);
field.set(instance, newObject);

callGetter(fieldName, pair.getGetter(), instance, pair.getGetter().getReturnType(), newObject);
callGetter(fieldName, pair.getGetter(), instance, newObject);
}
}
}
Expand Down

0 comments on commit 2520911

Please sign in to comment.