views:

869

answers:

7

I'm trying to write unit tests for a variety of clone() operations inside a large project and I'm wondering if there is an existing class somewhere that is capable of taking two objects of the same type, doing a deep comparison, and saying if they're identical or not?

A: 

Just found this article with example code.

Zed
+2  A: 

Unitils has this functionality:

Equality assertion through reflection, with different options like ignoring Java default/null values and ignoring order of collections

Wolfgang
+7  A: 

Apache commons-lang has a reflection based equals builder documented here: http://commons.apache.org/lang/api-release/org/apache/commons/lang/builder/EqualsBuilder.html

Jherico
+1  A: 

I am usin XStream:

/**
 * @see java.lang.Object#equals(java.lang.Object)
 */
@Override
public boolean equals(Object o) {
 XStream xstream = new XStream();
 String oxml = xstream.toXML(o);
 String myxml = xstream.toXML(this);

 return myxml.equals(oxml);
}

/**
 * @see java.lang.Object#hashCode()
 */
@Override
public int hashCode() {
 XStream xstream = new XStream();
 String myxml = xstream.toXML(this);
 return myxml.hashCode();
}
A: 

I guess you know this, but In theory, you're supposed to always override .equals to assert that two objects are truly equal. This would imply that they check the overridden .equals methods on their members.

This kind of thing is why .equals is defined in Object.

If this were done consistently you wouldn't have a problem.

Bill K
The problem is that I want to automate testing this for a large existing codebase that I didn't write... :)
Uri
A: 

I love this question! Mainly because it is hardly ever answered or answered badly. It's like nobody has figured it out yet. Virgin territory :)

First off, don't even think about using equals. The contract of equals, as defined in the javadoc, is an equivalence relation (reflexive, symmetric, and transitive), not an equality relation. For that, it would also have to be antisymmetric. The only implementation of equals that is (or ever could be) a true equality relation is the one in java.lang.Object. Even if you did use equals to compare everything in the graph, the risk of breaking the contract is quite high. As Josh Bloch pointed out in Effective Java, the contract of equals is very easy to break:

"There is simply no way to extend an instantiable class and add an aspect while preserving the equals contract"

Besides what good does a boolean method really do you anyway? It'd be nice to actually encapsulate all the differences between the original and the clone, don't you think? Also, I'll assume here that you don't want to be bothered with writing/maintaining comparison code for each object in the graph, but rather you're looking for something that will scale with the source as it changes over time.

Soooo, what you really want is some kind of state comparison tool. How that tool is implemented is really dependent on the nature of your domain model and your performance restrictions. In my experience, there is no generic magic bullet. And it will be slow over a large number of iterations. But for testing the completeness of a clone operation, it'll do the job pretty well. Your two best options are serialization and reflection.

Some issues you will encounter:

  • Collection order: Should two collections be considered similar if they hold the same objects, but in a different order?
  • Which fields to ignore: Transient? Static?
  • Type equivalence: Should field values be of exactly the same type? Or is it ok for one to extend the other?
  • There's more, but I forget...

XStream is pretty fast and combined with XMLUnit will do the job in just a few lines of code. XMLUnit is nice because it can report all the differences, or just stop at the first one it finds. And its output includes the xpath to the differing nodes, which is nice. By default it doesn't allow unordered collections, but it can be configured to do so. Injecting a special difference handler (Called a DifferenceListener) allows you to specify the way you want to deal with differences, including ignoring order. However, as soon as you want to do anything beyond the simplest customization, it becomes difficult to write and the details tend to be tied down to a specific domain object.

My personal preference is to use reflection to cycle through all the declared fields and drill down into each one, tracking differences as I go. Word of warning: Don't use recursion unless you like stack overflow exceptions. Keep things in scope with a stack (use a LinkedList or something). I usually ignore transient and static fields, and I skip object pairs that I've already compared, so I don't end up in infinite loops if someone decided to write self-referential code (However, I always compare primitive wrappers no matter what, since the same object refs are often reused). You can configure things up front to ignore collection ordering and to ignore special types or fields, but I like to define my state comparison policies on the fields themselves via annotations. This, IMHO, is exactly what annotations were meant for, to make meta data about the class available at runtime. Something like:


@StatePolicy(unordered=true, ignore=false, exactTypesOnly=true)
private List<StringyThing> _mylist;

I think this is actually a really hard problem, but totally solvable! And once you have something that works for you, it is really, really, handy :)

So, good luck. And if you come up with something that's just pure genius, don't forget to share!

Kevin C
A: 

(Using an answer instead of a comment to get a longer limit and better formatting.)

A halting guarantee for such a deep comparison might be a problem. What should the following do? (If you implement such a comparator, this would make a good unit test.)

LinkedListNode a = new LinkedListNode();
a.next = a;
LinkedListNode b = new LinkedListNode();
b.next = b;

System.out.println(DeepCompare(a, b));

Here's another:

LinkedListNode c = new LinkedListNode();
LinkedListNode d = new LinkedListNode();
c.next = d;
d.next = c;

System.out.println(DeepCompare(c, d));
Ben Voigt