Class RecursiveComparisonDifferenceCalculator

    • Constructor Detail

      • RecursiveComparisonDifferenceCalculator

        public RecursiveComparisonDifferenceCalculator()
    • Method Detail

      • 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.
      • getFieldsNames

        private static Set<String> getFieldsNames​(Class<?> clazz)
      • isContainerType

        private static boolean isContainerType​(Object o)
      • differentTypeErrorMessage

        private static String differentTypeErrorMessage​(DualValue dualValue,
                                                        String actualTypeDescription)
      • hasCustomEquals

        static boolean hasCustomEquals​(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.
      • expectedTypeIsNotSubtypeOfActualType

        private static boolean expectedTypeIsNotSubtypeOfActualType​(DualValue dualField)
      • expectedTypeIsNotSubtypeOfActualType

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

        private static String describeOrderedCollectionTypes()