Using Java to compare Coldfusion objects

Posted on Monday, November 22nd, 2010 at 8:56 pm

This past weekend I was building some unit tests when I came across a situation where I needed to compare two Coldfusion objects for equality. If I had a way to simply assert true the equality of two objects would make the test infinitely more simple than what it need be.

I have never really had a need to do this before and am not aware of any way in coldfusion to compare complex objects against one another. However, since CF is built on the very powerful Java libraries it didn’t take much digging to turn up the Java equals() function that accepts an object to be compared against. It essentially works with something like:

isequal = object1.equals(object2);

Pretty straight forward. Let’s look at an example illustrating how to grab this function and utilize it using Coldfusion on Coldfusion objects.

Java Equals() in Coldfusion

<!--- get the Java Comparator class --->
<cfset variables.comparator = createObject("java", "java.util.Comparator") />

<!--- build the first object --->
<cfset variables.objectOne = structNew() />
<cfset variables.objectOne.name = "matt" />
<cfset variables.objectOne.lastname = "cook" />

<!--- build the second object --->
<cfset variables.objectTwo = structNew() />
<cfset variables.objectTwo.name = "matt" />
<cfset variables.objectTwo.lastname = "cook" />

<!--- compare the objects --->
<cfset variables.isEqual = variables.objectTwo.Equals(variables.objectOne) />

<!--- output the boolean --->
<cfdump var="#variables.isEqual#" />

Once the class is loaded in, one of the object references can be appended with the equals() function containing the object to be compared with. It will return ‘true’ if the objects are equal, and ‘false’ if not. Simple enough. It actually compares values as well. So if the object structures are not identical it will return false. But even if the key structures are identical, if the values are not it will still return false. Both values and keys must be identical to get a returned boolean of ‘true’. So for example, changing the ‘name’ key of objectOne will cause variables.isEqual to contain ‘false’. Such as:

<!--- get the Java Comparator class --->
<cfset variables.comparator = createObject("java", "java.util.Comparator") />

<!--- build the first object --->
<cfset variables.objectOne = structNew() />
<cfset variables.objectOne.name = "matthew" />
<cfset variables.objectOne.lastname = "cook" />

<!--- build the second object --->
<cfset variables.objectTwo = structNew() />
<cfset variables.objectTwo.name = "matt" />
<cfset variables.objectTwo.lastname = "cook" />

<!--- compare the objects --->
<cfset variables.isEqual = variables.objectTwo.Equals(variables.objectOne) />

<!--- output the boolean --->
<cfdump var="#variables.isEqual#" />

Using this method of comparing objects means that in my unit test I can simply run something like the code above, and then write a simple assertTrue() as

<cfset assertTrue(variables.isEqual, "The object returned does not match the object set.") />

to complete the unit test.

CategoryJava
4 Responses to “Using Java to compare Coldfusion objects”
  1. john barnes says:

    Hi, have tried the example..and it appears to work even if you dont get the java comparator class in line 2.

  2. Is the compare case sensitive?

  3. Matthew Cook says:

    John, I just tried this myself and you’re exactly right. The ‘variables.comparator’ I set never even gets called. I guess I just looked this up in the Java docs and wrote the createObject() and forgot about it. I wish I could explain why we don’t need to use createObject() to load in the Comparator Java class, but I’m not sure how CF handles this. Thanks for pointing this out though!

  4. Matthew Cook says:

    Hi Tim, this is a really good point since we are using Java to do the comparison. When I ran a test on this a few moments ago I really expected it to be case sensitive, but it’s actually not. So whether it’s a good thing or not, it appears to only compare the object structure and case-insensitive keys and values.

Leave a Reply

*
(Won't be published) *