org.unitils.reflectionassert
Class ReflectionAssert

java.lang.Object
  extended by org.unitils.reflectionassert.ReflectionAssert

public class ReflectionAssert
extends Object

A class for asserting that 2 objects/collections are equal by comparing properties and fields of the objects/collections using reflection.

The (combination of) comparator modes specify how strict the comparison must be:

There are 2 versions of each method: a lenient and a reflection version. With the ref versions you can set the comparator modes explicitly (note: no modes means strict comparison). The len versions are the same as the ref versions but have lenient order and ignore defaults set by default.

The name assert..ReflectionEquals is chosen instead of assert..Equals so it can be added as a static import without naming collisions.

Author:
Tim Ducheyne, Filip Neven
See Also:
ReflectionComparator, ReflectionComparatorMode

Nested Class Summary
protected static class ReflectionAssert.OgnlTransformer
          A commons collections transformer that takes an object and returns the value of the property that is specified by the given ognl expression.
 
Constructor Summary
ReflectionAssert()
           
 
Method Summary
static void assertLenientEquals(Object expected, Object actual)
          Asserts that two objects are equal.
static void assertLenientEquals(String message, Object expected, Object actual)
          Asserts that two objects are equal.
static void assertPropertiesNotNull(String message, Object object)
          All fields are checked for null values (except the static ones).
static void assertPropertyLenientEquals(String propertyName, Collection<?> expectedPropertyValues, Collection<?> actualObjects)
          Asserts that a property of all objects in the collection are equal to the given values.
static void assertPropertyLenientEquals(String propertyName, Object expectedPropertyValue, Object actualObject)
          Asserts that the value of a property of an object is equal to the given value.
static void assertPropertyLenientEquals(String message, String propertyName, Collection<?> expectedPropertyValues, Collection<?> actualObjects)
          Asserts that a property of all objects in the collection are equal to the given values.
static void assertPropertyLenientEquals(String message, String propertyName, Object expectedPropertyValue, Object actualObject)
          Asserts that the value of a property of an object is equal to the given value.
static void assertPropertyReflectionEquals(String propertyName, Collection<?> expectedPropertyValues, Collection<?> actualObjects, ReflectionComparatorMode... modes)
          Asserts that a property of all objects in the collection are equal to the given values.
static void assertPropertyReflectionEquals(String propertyName, Object expectedPropertyValue, Object actualObject, ReflectionComparatorMode... modes)
          Asserts that the value of a property of an object is equal to the given value.
static void assertPropertyReflectionEquals(String message, String propertyName, Collection<?> expectedPropertyValues, Collection<?> actualObjects, ReflectionComparatorMode... modes)
          Asserts that a property of all objects in the collection are equal to the given values.
static void assertPropertyReflectionEquals(String message, String propertyName, Object expectedPropertyValue, Object actualObject, ReflectionComparatorMode... modes)
          Asserts that the value of a property of an object is equal to the given value.
static void assertReflectionEquals(Object expected, Object actual, ReflectionComparatorMode... modes)
          Asserts that two objects are equal.
static void assertReflectionEquals(String message, Object expected, Object actual, ReflectionComparatorMode... modes)
          Asserts that two objects are equal.
protected static String formatMessage(String suppliedMessage, String specificMessage)
          Formats the exception message.
protected static String getFailureMessage(String message, Difference difference)
           
protected static Object getProperty(Object object, String ognlExpression)
          Evaluates the given OGNL expression, and returns the corresponding property value from the given object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectionAssert

public ReflectionAssert()
Method Detail

assertLenientEquals

public static void assertLenientEquals(Object expected,
                                       Object actual)
                                throws junit.framework.AssertionFailedError
Asserts that two objects are equal. Reflection is used to compare all fields of these values. If they are not equal an AssertionFailedError is thrown.

This is identical to assertReflectionEquals with lenient order and ignore defaults set as comparator modes.

Parameters:
expected - the expected object
actual - the given object
Throws:
junit.framework.AssertionFailedError - when both objects are not equals

assertReflectionEquals

public static void assertReflectionEquals(Object expected,
                                          Object actual,
                                          ReflectionComparatorMode... modes)
                                   throws junit.framework.AssertionFailedError
Asserts that two objects are equal. Reflection is used to compare all fields of these values. If they are not equal an AssertionFailedError is thrown.

The comparator modes determine how strict to compare the values.

Parameters:
expected - the expected object
actual - the given object
modes - the comparator modes
Throws:
junit.framework.AssertionFailedError - when both objects are not equals

assertLenientEquals

public static void assertLenientEquals(String message,
                                       Object expected,
                                       Object actual)
                                throws junit.framework.AssertionFailedError
Asserts that two objects are equal. Reflection is used to compare all fields of these values. If they are not equal an AssertionFailedError is thrown.

This is identical to assertReflectionEquals with lenient order and ignore defaults set as comparator modes.

Parameters:
message - a message for when the assertion fails
expected - the expected object
actual - the given object
Throws:
junit.framework.AssertionFailedError - when both objects are not equals

assertReflectionEquals

public static void assertReflectionEquals(String message,
                                          Object expected,
                                          Object actual,
                                          ReflectionComparatorMode... modes)
                                   throws junit.framework.AssertionFailedError
Asserts that two objects are equal. Reflection is used to compare all fields of these values. If they are not equal an AssertionFailedError is thrown.

The comparator modes determine how strict to compare the values.

Parameters:
message - a message for when the assertion fails
expected - the expected object
actual - the given object
modes - the comparator modes
Throws:
junit.framework.AssertionFailedError - when both objects are not equals

getFailureMessage

protected static String getFailureMessage(String message,
                                          Difference difference)
Parameters:
message - a custom user-provided message, null if the user didn't provide a message
difference - the difference, not null
Returns:
a failure message describing the difference found

assertPropertyLenientEquals

public static void assertPropertyLenientEquals(String propertyName,
                                               Object expectedPropertyValue,
                                               Object actualObject)
                                        throws junit.framework.AssertionFailedError
Asserts that the value of a property of an object is equal to the given value.

Bean notation can be used to specify inner properties. Eg myArray[2].innerValue. assertLenientEquals is used to check whether both values are equal.

This is identical to assertPropertyReflectionEquals with lenient order and ignore defaults set as comparator modes.

Parameters:
propertyName - the property, not null
expectedPropertyValue - the expected value
actualObject - the object that contains the property
Throws:
junit.framework.AssertionFailedError - when both objects are not equals

assertPropertyReflectionEquals

public static void assertPropertyReflectionEquals(String propertyName,
                                                  Object expectedPropertyValue,
                                                  Object actualObject,
                                                  ReflectionComparatorMode... modes)
                                           throws junit.framework.AssertionFailedError
Asserts that the value of a property of an object is equal to the given value.

Bean notation can be used to specify inner properties. Eg myArray[2].innerValue. assertReflectionEquals is used to check whether both values are equal.

The comparator modes determine how strict to compare the values.

Parameters:
propertyName - the property, not null
expectedPropertyValue - the expected value
actualObject - the object that contains the property
modes - the comparator modes
Throws:
junit.framework.AssertionFailedError - when both objects are not equals

assertPropertyLenientEquals

public static void assertPropertyLenientEquals(String message,
                                               String propertyName,
                                               Object expectedPropertyValue,
                                               Object actualObject)
                                        throws junit.framework.AssertionFailedError
Asserts that the value of a property of an object is equal to the given value.

Bean notation can be used to specify inner properties. Eg myArray[2].innerValue. assertReflectionEquals is used to check whether both values are equal.

This is identical to assertPropertyReflectionEquals with lenient order and ignore defaults set as comparator modes.

Parameters:
message - a message for when the assertion fails
propertyName - the property, not null
expectedPropertyValue - the expected value
actualObject - the object that contains the property
Throws:
junit.framework.AssertionFailedError - when both objects are not equals

assertPropertyReflectionEquals

public static void assertPropertyReflectionEquals(String message,
                                                  String propertyName,
                                                  Object expectedPropertyValue,
                                                  Object actualObject,
                                                  ReflectionComparatorMode... modes)
                                           throws junit.framework.AssertionFailedError
Asserts that the value of a property of an object is equal to the given value.

Bean notation can be used to specify inner properties. Eg myArray[2].innerValue. assertReflectionEquals is used to check whether both values are equal.

The comparator modes determine how strict to compare the values.

Parameters:
message - a message for when the assertion fails
propertyName - the property, not null
expectedPropertyValue - the expected value
actualObject - the object that contains the property
modes - the comparator modes
Throws:
junit.framework.AssertionFailedError - when both objects are not equals

assertPropertyLenientEquals

public static void assertPropertyLenientEquals(String propertyName,
                                               Collection<?> expectedPropertyValues,
                                               Collection<?> actualObjects)
                                        throws junit.framework.AssertionFailedError
Asserts that a property of all objects in the collection are equal to the given values.

Example: assertPropertyEquals("id", myIdCollection, myObjectCollection) checks whether all values of the id field of the myObjectCollection elements matches the values in the myIdCollection

Bean notation can be used to specify inner properties. Eg myArray[2].innerValue. assertReflectionEquals is used to check whether both values are equal.

This is identical to assertPropertyReflectionEquals with lenient order and ignore defaults set as comparator modes.

Parameters:
propertyName - the property, not null
expectedPropertyValues - the expected values
actualObjects - the objects that contain the property
Throws:
junit.framework.AssertionFailedError - when both objects are not equals

assertPropertyReflectionEquals

public static void assertPropertyReflectionEquals(String propertyName,
                                                  Collection<?> expectedPropertyValues,
                                                  Collection<?> actualObjects,
                                                  ReflectionComparatorMode... modes)
                                           throws junit.framework.AssertionFailedError
Asserts that a property of all objects in the collection are equal to the given values.

Example: assertPropertyEquals("id", myIdCollection, myObjectCollection) checks whether all values of the id field of the myObjectCollection elements matches the values in the myIdCollection

Bean notation can be used to specify inner properties. Eg myArray[2].innerValue. assertReflectionEquals is used to check whether both values are equal.

The comparator modes determine how strict to compare the values.

Parameters:
propertyName - the property, not null
expectedPropertyValues - the expected values
actualObjects - the objects that contain the property
modes - the comparator modes
Throws:
junit.framework.AssertionFailedError - when both objects are not equals

assertPropertyLenientEquals

public static void assertPropertyLenientEquals(String message,
                                               String propertyName,
                                               Collection<?> expectedPropertyValues,
                                               Collection<?> actualObjects)
                                        throws junit.framework.AssertionFailedError
Asserts that a property of all objects in the collection are equal to the given values.

Example: assertPropertyEquals("id", myIdCollection, myObjectCollection) checks whether all values of the id field of the myObjectCollection elements matches the values in the myIdCollection

Bean notation can be used to specify inner properties. Eg myArray[2].innerValue. assertReflectionEquals is used to check whether both values are equal.

This is identical to assertPropertyReflectionEquals with lenient order and ignore defaults set as comparator modes.

Parameters:
message - a message for when the assertion fails
propertyName - the property, not null
expectedPropertyValues - the expected values, not null
actualObjects - the objects that contain the property
Throws:
junit.framework.AssertionFailedError - when both objects are not equals

assertPropertiesNotNull

public static void assertPropertiesNotNull(String message,
                                           Object object)
All fields are checked for null values (except the static ones). Private fields are also checked. This is NOT recursive, only the values of the first object will be checked. An assertion error will be thrown when a property is null.

Parameters:
message - a message for when the assertion fails
object - the object that will be checked for null values.

assertPropertyReflectionEquals

public static void assertPropertyReflectionEquals(String message,
                                                  String propertyName,
                                                  Collection<?> expectedPropertyValues,
                                                  Collection<?> actualObjects,
                                                  ReflectionComparatorMode... modes)
                                           throws junit.framework.AssertionFailedError
Asserts that a property of all objects in the collection are equal to the given values.

Example: assertPropertyEquals("id", myIdCollection, myObjectCollection) checks whether all values of the id field of the myObjectCollection elements matches the values in the myIdCollection

Bean notation can be used to specify inner properties. Eg myArray[2].innerValue. assertReflectionEquals is used to check whether both values are equal.

The comparator modes determine how strict to compare the values.

Parameters:
message - a message for when the assertion fails
propertyName - the property, not null
expectedPropertyValues - the expected values, not null
actualObjects - the objects that contain the property
modes - the comparator modes
Throws:
junit.framework.AssertionFailedError - when both objects are not equals

formatMessage

protected static String formatMessage(String suppliedMessage,
                                      String specificMessage)
Formats the exception message.

Parameters:
suppliedMessage - the user supplied message
specificMessage - the reason
Returns:
the formatted message

getProperty

protected static Object getProperty(Object object,
                                    String ognlExpression)
Evaluates the given OGNL expression, and returns the corresponding property value from the given object.

Parameters:
object - The object on which the expression is evaluated
ognlExpression - The OGNL expression that is evaluated
Returns:
The value for the given OGNL expression


Copyright © 2011. All Rights Reserved.