You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@camel.apache.org by vetalik <ve...@gmail.com> on 2012/07/24 13:37:03 UTC

MockEndpoint.assertIsSatisfied(Object, Object)

Hello!
If i have a MockEndpoint which gets an POJO object in Message Body it try to
compare it with expected result using:
camel/util/ObjectHelper.java
ObjectHelper.equal(method)

Which contains following lines:
/**
     * A helper method for comparing objects for equality while handling
nulls
     */
    public static boolean equal(Object a, Object b) {
        if (a == b) {
            return true;
        }

        if (a instanceof byte[] && b instanceof byte[]) {
            return equalByteArray((byte[])a, (byte[])b);
        }

        return a != null && b != null && a.equals(b);
    }

This is sort of very strange method to compare Objects. It will work for
Strings and primitives, but won't work for POJO, because it compare with
"==" operator, and do not compare with .equals(obj1) method.
So i put
resultEndpoint.expectedBodiesReceived(expectedMT);
where expectedMT is instance of MT class
and then tested method will send to mocked Endpoint new instance of MT, with
the same data.
But
resultEndpoint.assertIsSatisfied();
will give error, because are not the same. like:
Expected: <co...@2674f9fb> but was:
<co...@53ddcd5f>

If assertIsSatisfied would work correctly and would use equals instead of
"==" then there would be correct assertion. 

Is it a bug, or i missuse MockEndpoint somehow?



--
View this message in context: http://camel.465427.n5.nabble.com/MockEndpoint-assertIsSatisfied-Object-Object-tp5716388.html
Sent from the Camel Development mailing list archive at Nabble.com.

Re: MockEndpoint.assertIsSatisfied(Object, Object)

Posted by Babak Vahdat <ba...@swissonline.ch>.
Hi

No! Don't let it baffle you. The usage of the "==" operator as the first
step is just because of the optimization & performance. A proper usage of
the equals() method is at the bottom of the method you mentioned.

So just override the equals() & hasCode() methods properly and then you're
on the right track. And also make sure you don't break the expected contract
below (see javadoc):

http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#equals(java.lang.Object)
http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#hashCode()

Babak



--
View this message in context: http://camel.465427.n5.nabble.com/MockEndpoint-assertIsSatisfied-Object-Object-tp5716388p5716398.html
Sent from the Camel Development mailing list archive at Nabble.com.

Re: MockEndpoint.assertIsSatisfied(Object, Object)

Posted by Babak Vahdat <ba...@swissonline.ch>.
And also please make use of the Camel user-forum @:

http://camel.465427.n5.nabble.com/Camel-Users-f465428.html

As this forum is used internally ONLY for the Apache Camel committers.

Babak


vetalik wrote
> 
> Hello!
> If i have a MockEndpoint which gets an POJO object in Message Body it try
> to compare it with expected result using:
> camel/util/ObjectHelper.java
> ObjectHelper.equal(method)
> 
> Which contains following lines:
> /**
>      * A helper method for comparing objects for equality while handling
> nulls
>      */
>     public static boolean equal(Object a, Object b) {
>         if (a == b) {
>             return true;
>         }
> 
>         if (a instanceof byte[] && b instanceof byte[]) {
>             return equalByteArray((byte[])a, (byte[])b);
>         }
> 
>         return a != null && b != null && a.equals(b);
>     }
> 
> This is sort of very strange method to compare Objects. It will work for
> Strings and primitives, but won't work for POJO, because it compare with
> "==" operator, and do not compare with .equals(obj1) method.
> So i put
> resultEndpoint.expectedBodiesReceived(expectedMT);
> where expectedMT is instance of MT class
> and then tested method will send to mocked Endpoint new instance of MT,
> with the same data.
> But
> resultEndpoint.assertIsSatisfied();
> will give error, because are not the same. like:
> Expected: &lt;com.somepackage.MT@2674f9fb&gt; but was:
> &lt;com.somepackage.MT@53ddcd5f&gt;
> 
> If assertIsSatisfied would work correctly and would use equals instead of
> "==" then there would be correct assertion. 
> 
> Is it a bug, or i missuse MockEndpoint somehow?
> 




--
View this message in context: http://camel.465427.n5.nabble.com/MockEndpoint-assertIsSatisfied-Object-Object-tp5716388p5716401.html
Sent from the Camel Development mailing list archive at Nabble.com.