monksy monksy -3 years ago 135
Groovy Question

Comparing two objects with Groovy

Is there an easy utility with Groovy that will give me a difference of two different objects? I'm getting the message via the equals method that they aren't equal but where can I find what properties are not being matched?

Answer Source

I found that this was easy to do as that the .properties attribute of the object produced a map listing the property name as the key and the value as the value.

Once I had that then I could do a difference between the two maps and I've got the answer I was looking for.

Each of the objects has a .properties extension. This works like the Python .dict.

Example

object1.properties - object2.properties

So these will show you the difference between the object1 properties and object2 properties, but only where there exist an entity (key+value) that differs from within object2. If there are extra properties in object2 they will not be shown.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download