org.unitils.reflectionassert
Class ReflectionComparator

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

public class ReflectionComparator
extends Object

A comparator for comparing two values by reflection.

The actual comparison of the values is implemented as a comparator chain. The chain is passed as an argument during the construction. Each of the comparators in the chain is able to compare some types of values. E.g. a CollectionComparator is able to compare collections, simple values such as integers are compared by the SimpleCasesComparator... A number of 'leniency levels' can also be added to the chain: e.g. the LenientOrderCollectionComparator ignores the actual ordering of 2 collections.

The preferred way of creating new instances is by using the ReflectionComparatorFactory. This factory will make sure that a correct comparator chain is assembled.

A readable report differences can be created using the DifferenceReport.

Author:
Tim Ducheyne, Filip Neven

Field Summary
protected  Map<Object,Map<Object,Difference>> allDifferencesCachedResults
           
protected  List<Comparator> comparators
          The comparator chain.
protected  Map<Object,Map<Object,Difference>> firstDifferenceCachedResults
          A cache of results, so that comparisons are only performed once and infinite loops because of cycles are avoided A different cache is used dependent on whether only the first difference is required or whether we need all differences, since the resulting Difference objects differ.
 
Constructor Summary
ReflectionComparator(List<Comparator> comparators)
          Creates a comparator that will use the given chain.
 
Method Summary
protected  Map<Object,Difference> getCachedDifference(Object left, boolean onlyFirstDifference)
           
 Difference getDifference(Object left, Object right)
          Checks whether there is a difference between the left and right objects.
 Difference getDifference(Object left, Object right, boolean onlyFirstDifference)
          Checks whether there are differences between the left and right objects.
 boolean isEqual(Object left, Object right)
          Checks whether there is no difference between the left and right objects.
protected  void saveResultInCache(Object left, Map<Object,Difference> cachedResult, boolean onlyFirstDifference)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

comparators

protected List<Comparator> comparators
The comparator chain.


firstDifferenceCachedResults

protected Map<Object,Map<Object,Difference>> firstDifferenceCachedResults
A cache of results, so that comparisons are only performed once and infinite loops because of cycles are avoided A different cache is used dependent on whether only the first difference is required or whether we need all differences, since the resulting Difference objects differ.


allDifferencesCachedResults

protected Map<Object,Map<Object,Difference>> allDifferencesCachedResults
Constructor Detail

ReflectionComparator

public ReflectionComparator(List<Comparator> comparators)
Creates a comparator that will use the given chain.

Parameters:
comparators - The comparator chain, not null
Method Detail

isEqual

public boolean isEqual(Object left,
                       Object right)
Checks whether there is no difference between the left and right objects.

Parameters:
left - the left instance
right - the right instance
Returns:
true if there is no difference, false otherwise

getDifference

public Difference getDifference(Object left,
                                Object right)
Checks whether there is a difference between the left and right objects.

Parameters:
left - the left instance
right - the right instance
Returns:
the difference, null if there is no difference

getDifference

public Difference getDifference(Object left,
                                Object right,
                                boolean onlyFirstDifference)
Checks whether there are differences between the left and right objects. This will return the root difference of the whole difference tree containing all the differences between the objects.

Parameters:
left - the left instance
right - the right instance
onlyFirstDifference - True if the comparison should stop at the first differnece
Returns:
the root difference, null if there is no difference

saveResultInCache

protected void saveResultInCache(Object left,
                                 Map<Object,Difference> cachedResult,
                                 boolean onlyFirstDifference)

getCachedDifference

protected Map<Object,Difference> getCachedDifference(Object left,
                                                     boolean onlyFirstDifference)


Copyright © 2011. All Rights Reserved.