Class RecursiveComparisonDifferenceCalculator

java.lang.Object
org.assertj.core.api.recursive.comparison.RecursiveComparisonDifferenceCalculator

public class RecursiveComparisonDifferenceCalculator extends Object
Based on DeepDifference but takes a RecursiveComparisonConfiguration, DeepDifference being itself based on the deep equals implementation of https://github.com/jdereg/java-util
  • Field Details

  • Constructor Details

    • RecursiveComparisonDifferenceCalculator

      public RecursiveComparisonDifferenceCalculator()
  • Method Details

    • determineDifferences

      public List<ComparisonDifference> determineDifferences(Object actual, Object expected, RecursiveComparisonConfiguration recursiveComparisonConfiguration)
      Compare two objects for differences by doing a 'deep' comparison. This will traverse the Object graph and perform either a field-by-field comparison on each object (if not .equals() method has been overridden from Object), or it will call the customized .equals() method if it exists.

      This method handles cycles correctly, for example A->B->C->A. Suppose a and a' are two separate instances of the A with the same values for all fields on A, B, and C. Then a.deepEquals(a') will return an empty list. It uses cycle detection storing visited objects in a Set to prevent endless loops.

      Parameters:
      actual - Object one to compare
      expected - Object two to compare
      recursiveComparisonConfiguration - the recursive comparison configuration
      Returns:
      the list of differences found or an empty list if objects are equivalent. Equivalent means that all field values of both subgraphs are the same, either at the field level or via the respectively encountered overridden .equals() methods during traversal.
    • determineDifferences

      private static List<ComparisonDifference> determineDifferences(Object actual, Object expected, FieldLocation fieldLocation, boolean isRootObject, List<DualValue> visited, RecursiveComparisonConfiguration recursiveComparisonConfiguration)
    • shouldCompareDualValue

      private static boolean shouldCompareDualValue(RecursiveComparisonConfiguration recursiveComparisonConfiguration, DualValue dualValue)
    • compareAsEnums

      private static void compareAsEnums(DualValue dualValue, RecursiveComparisonDifferenceCalculator.ComparisonState comparisonState, RecursiveComparisonConfiguration recursiveComparisonConfiguration)
    • shouldHonorOverriddenEquals

      private static boolean shouldHonorOverriddenEquals(DualValue dualValue, RecursiveComparisonConfiguration recursiveComparisonConfiguration)
    • compareArrays

      private static void compareArrays(DualValue dualValue, RecursiveComparisonDifferenceCalculator.ComparisonState comparisonState)
    • compareOrderedCollections

      private static void compareOrderedCollections(DualValue dualValue, RecursiveComparisonDifferenceCalculator.ComparisonState comparisonState)
    • differentTypeErrorMessage

      private static String differentTypeErrorMessage(DualValue dualValue, String actualTypeDescription)
    • compareUnorderedIterables

      private static void compareUnorderedIterables(DualValue dualValue, RecursiveComparisonDifferenceCalculator.ComparisonState comparisonState)
    • compareSortedMap

      private static <K, V> void compareSortedMap(DualValue dualValue, RecursiveComparisonDifferenceCalculator.ComparisonState comparisonState)
    • compareUnorderedMap

      private static void compareUnorderedMap(DualValue dualValue, RecursiveComparisonDifferenceCalculator.ComparisonState comparisonState)
    • compareOptional

      private static void compareOptional(DualValue dualValue, RecursiveComparisonDifferenceCalculator.ComparisonState comparisonState)
    • hasOverriddenEquals

      static boolean hasOverriddenEquals(Class<?> c)
      Determine if the passed in class has a non-Object.equals() method. This method caches its results in static ConcurrentHashMap to benefit execution performance.
      Parameters:
      c - Class to check.
      Returns:
      true, if the passed in Class has a .equals() method somewhere between itself and just below Object in it's inheritance.
    • deepHashCode

      static int deepHashCode(Object obj)
      Get a deterministic hashCode (int) value for an Object, regardless of when it was created or where it was loaded into memory. The problem with java.lang.Object.hashCode() is that it essentially relies on memory location of an object (what identity it was assigned), whereas this method will produce the same hashCode for any object graph, regardless of how many times it is created.

      This method will handle cycles correctly (A->B->C->A). In this case, Starting with object A, B, or C would yield the same hashCode. If an object encountered (root, subobject, etc.) has a hashCode() method on it (that is not Object.hashCode()), that hashCode() method will be called and it will stop traversal on that branch.
      Parameters:
      obj - Object who hashCode is desired.
      Returns:
      the 'deep' hashCode value for the passed in object.
    • hasCustomHashCode

      static boolean hasCustomHashCode(Class<?> c)
      Determine if the passed in class has a non-Object.hashCode() method. This method caches its results in static ConcurrentHashMap to benefit execution performance.
      Parameters:
      c - Class to check.
      Returns:
      true, if the passed in Class has a .hashCode() method somewhere between itself and just below Object in it's inheritance.
    • propertyOrFieldValuesAreEqual

      private static boolean propertyOrFieldValuesAreEqual(DualValue dualValue, RecursiveComparisonConfiguration recursiveComparisonConfiguration)
    • expectedAndActualTypeDifference

      private static ComparisonDifference expectedAndActualTypeDifference(Object actual, Object expected)
    • expectedTypeIsNotSubtypeOfActualType

      private static boolean expectedTypeIsNotSubtypeOfActualType(DualValue dualField)
    • expectedTypeIsNotSubtypeOfActualType

      private static boolean expectedTypeIsNotSubtypeOfActualType(Object actual, Object expected)
    • describeOrderedCollectionTypes

      private static String describeOrderedCollectionTypes()