[jira] Created: (CAMEL-772) Predicates with two types should consider casting to desired type before evaluating

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

[jira] Created: (CAMEL-772) Predicates with two types should consider casting to desired type before evaluating

JIRA jira@apache.org
Predicates with two types should consider casting to desired type before evaluating
-----------------------------------------------------------------------------------

                 Key: CAMEL-772
                 URL: https://issues.apache.org/activemq/browse/CAMEL-772
             Project: Apache Camel
          Issue Type: Improvement
          Components: camel-core
    Affects Versions: 1.4.0
            Reporter: Claus Ibsen


I had this unit test where I want to assert that the message body contains this string part
{code}
        MockEndpoint error = getMockEndpoint("mock:errorQueue");
        error.expectedMessageCount(1);
        error.message(0).body().contains("<patientCpr>0101701234</patientCpr>");
{code}

But the expression fails because the body is byte array and is not casted to String as the contains type is.
If I change the code to:
{code}
        error.message(0).body().convertTo(String.class).contains("<patientCpr>0101701234</patientCpr>");
{code}

Then it of course works. But what if there already is a type convert in Camel that could have done the cast for me?

But is it dangerous to add such automatic type casting behind the end-users back? Any thoughts?

--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply | Threaded
Open this post in threaded view
|

[jira] Commented: (CAMEL-772) Predicates with two types should consider casting to desired type before evaluating

JIRA jira@apache.org

    [ https://issues.apache.org/activemq/browse/CAMEL-772?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=44634#action_44634 ]

Claus Ibsen commented on CAMEL-772:
-----------------------------------

Here is a snippet how I have prototyped the constains() predicate (a bit quick and dirty) but good for show and tell:

{code}
    public static <E extends Exchange> Predicate<E> contains(final Expression<E> left,
                                                             final Expression<E> right) {
        return new BinaryPredicateSupport<E>(left, right) {

            protected boolean matches(E exchange, Object leftValue, Object rightValue) {
                Object castedValue = exchange.getContext().getTypeConverter().convertTo(rightValue.getClass(), leftValue);
                if (castedValue != null) {
                    return ObjectHelper.contains(castedValue, rightValue);
                } else {
                    return ObjectHelper.contains(leftValue, rightValue);
                }
            }

            protected String getOperationText() {
                return "contains";
            }
        };
    }
{code}

> Predicates with two types should consider casting to desired type before evaluating
> -----------------------------------------------------------------------------------
>
>                 Key: CAMEL-772
>                 URL: https://issues.apache.org/activemq/browse/CAMEL-772
>             Project: Apache Camel
>          Issue Type: Improvement
>          Components: camel-core
>    Affects Versions: 1.4.0
>            Reporter: Claus Ibsen
>
> I had this unit test where I want to assert that the message body contains this string part
> {code}
>         MockEndpoint error = getMockEndpoint("mock:errorQueue");
>         error.expectedMessageCount(1);
>         error.message(0).body().contains("<patientCpr>0101701234</patientCpr>");
> {code}
> But the expression fails because the body is byte array and is not casted to String as the contains type is.
> If I change the code to:
> {code}
>         error.message(0).body().convertTo(String.class).contains("<patientCpr>0101701234</patientCpr>");
> {code}
> Then it of course works. But what if there already is a type convert in Camel that could have done the cast for me?
> But is it dangerous to add such automatic type casting behind the end-users back? Any thoughts?

--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply | Threaded
Open this post in threaded view
|

[jira] Updated: (CAMEL-772) Predicates with two types should consider casting to desired type before evaluating

JIRA jira@apache.org
In reply to this post by JIRA jira@apache.org

     [ https://issues.apache.org/activemq/browse/CAMEL-772?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Claus Ibsen updated CAMEL-772:
------------------------------

    Fix Version/s: 2.0.0

> Predicates with two types should consider casting to desired type before evaluating
> -----------------------------------------------------------------------------------
>
>                 Key: CAMEL-772
>                 URL: https://issues.apache.org/activemq/browse/CAMEL-772
>             Project: Apache Camel
>          Issue Type: Improvement
>          Components: camel-core
>    Affects Versions: 1.4.0
>            Reporter: Claus Ibsen
>             Fix For: 2.0.0
>
>
> I had this unit test where I want to assert that the message body contains this string part
> {code}
>         MockEndpoint error = getMockEndpoint("mock:errorQueue");
>         error.expectedMessageCount(1);
>         error.message(0).body().contains("<patientCpr>0101701234</patientCpr>");
> {code}
> But the expression fails because the body is byte array and is not casted to String as the contains type is.
> If I change the code to:
> {code}
>         error.message(0).body().convertTo(String.class).contains("<patientCpr>0101701234</patientCpr>");
> {code}
> Then it of course works. But what if there already is a type convert in Camel that could have done the cast for me?
> But is it dangerous to add such automatic type casting behind the end-users back? Any thoughts?

--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply | Threaded
Open this post in threaded view
|

[jira] Assigned: (CAMEL-772) Predicates with two types should consider casting to desired type before evaluating

JIRA jira@apache.org
In reply to this post by JIRA jira@apache.org

     [ https://issues.apache.org/activemq/browse/CAMEL-772?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Claus Ibsen reassigned CAMEL-772:
---------------------------------

    Assignee: Claus Ibsen

> Predicates with two types should consider casting to desired type before evaluating
> -----------------------------------------------------------------------------------
>
>                 Key: CAMEL-772
>                 URL: https://issues.apache.org/activemq/browse/CAMEL-772
>             Project: Apache Camel
>          Issue Type: Improvement
>          Components: camel-core
>    Affects Versions: 1.4.0
>            Reporter: Claus Ibsen
>            Assignee: Claus Ibsen
>             Fix For: 2.0.0
>
>
> I had this unit test where I want to assert that the message body contains this string part
> {code}
>         MockEndpoint error = getMockEndpoint("mock:errorQueue");
>         error.expectedMessageCount(1);
>         error.message(0).body().contains("<patientCpr>0101701234</patientCpr>");
> {code}
> But the expression fails because the body is byte array and is not casted to String as the contains type is.
> If I change the code to:
> {code}
>         error.message(0).body().convertTo(String.class).contains("<patientCpr>0101701234</patientCpr>");
> {code}
> Then it of course works. But what if there already is a type convert in Camel that could have done the cast for me?
> But is it dangerous to add such automatic type casting behind the end-users back? Any thoughts?

--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply | Threaded
Open this post in threaded view
|

[jira] Updated: (CAMEL-772) Predicates with two types should consider casting to desired type before evaluating

JIRA jira@apache.org
In reply to this post by JIRA jira@apache.org

     [ https://issues.apache.org/activemq/browse/CAMEL-772?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Claus Ibsen updated CAMEL-772:
------------------------------

    Attachment: CAMEL-772.patch

A patch

> Predicates with two types should consider casting to desired type before evaluating
> -----------------------------------------------------------------------------------
>
>                 Key: CAMEL-772
>                 URL: https://issues.apache.org/activemq/browse/CAMEL-772
>             Project: Apache Camel
>          Issue Type: Improvement
>          Components: camel-core
>    Affects Versions: 1.4.0
>            Reporter: Claus Ibsen
>            Assignee: Claus Ibsen
>             Fix For: 2.0.0
>
>         Attachments: CAMEL-772.patch
>
>
> I had this unit test where I want to assert that the message body contains this string part
> {code}
>         MockEndpoint error = getMockEndpoint("mock:errorQueue");
>         error.expectedMessageCount(1);
>         error.message(0).body().contains("<patientCpr>0101701234</patientCpr>");
> {code}
> But the expression fails because the body is byte array and is not casted to String as the contains type is.
> If I change the code to:
> {code}
>         error.message(0).body().convertTo(String.class).contains("<patientCpr>0101701234</patientCpr>");
> {code}
> Then it of course works. But what if there already is a type convert in Camel that could have done the cast for me?
> But is it dangerous to add such automatic type casting behind the end-users back? Any thoughts?

--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply | Threaded
Open this post in threaded view
|

[jira] Commented: (CAMEL-772) Predicates with two types should consider casting to desired type before evaluating

JIRA jira@apache.org
In reply to this post by JIRA jira@apache.org

    [ https://issues.apache.org/activemq/browse/CAMEL-772?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=48353#action_48353 ]

Claus Ibsen commented on CAMEL-772:
-----------------------------------

I have attached a patch with this change. However as it's a bit magical that Camel will type convert to the right value (usually the data type on the exchange/message) before performing the matching.

But you can do:
{code}
mock.message(0).body().contains("<patientCpr>0101701234</patientCpr>");
{code}
And it will convert the string to byte[] as the body() is a byte[]

You can still do the convertBodyTo as well:
{code}
mock.message(0).convertBodyTo(String.class).contains("<patientCpr>0101701234</patientCpr>");
{code}


> Predicates with two types should consider casting to desired type before evaluating
> -----------------------------------------------------------------------------------
>
>                 Key: CAMEL-772
>                 URL: https://issues.apache.org/activemq/browse/CAMEL-772
>             Project: Apache Camel
>          Issue Type: Improvement
>          Components: camel-core
>    Affects Versions: 1.4.0
>            Reporter: Claus Ibsen
>            Assignee: Claus Ibsen
>             Fix For: 2.0.0
>
>         Attachments: CAMEL-772.patch
>
>
> I had this unit test where I want to assert that the message body contains this string part
> {code}
>         MockEndpoint error = getMockEndpoint("mock:errorQueue");
>         error.expectedMessageCount(1);
>         error.message(0).body().contains("<patientCpr>0101701234</patientCpr>");
> {code}
> But the expression fails because the body is byte array and is not casted to String as the contains type is.
> If I change the code to:
> {code}
>         error.message(0).body().convertTo(String.class).contains("<patientCpr>0101701234</patientCpr>");
> {code}
> Then it of course works. But what if there already is a type convert in Camel that could have done the cast for me?
> But is it dangerous to add such automatic type casting behind the end-users back? Any thoughts?

--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply | Threaded
Open this post in threaded view
|

[jira] Resolved: (CAMEL-772) Predicates with two types should consider casting to desired type before evaluating

JIRA jira@apache.org
In reply to this post by JIRA jira@apache.org

     [ https://issues.apache.org/activemq/browse/CAMEL-772?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Claus Ibsen resolved CAMEL-772.
-------------------------------

    Resolution: Won't Fix

The problem is that both types (right vs. left) might need to be casted and you really dont know which one was the one the end user wanted it to be expected as.

> Predicates with two types should consider casting to desired type before evaluating
> -----------------------------------------------------------------------------------
>
>                 Key: CAMEL-772
>                 URL: https://issues.apache.org/activemq/browse/CAMEL-772
>             Project: Apache Camel
>          Issue Type: Improvement
>          Components: camel-core
>    Affects Versions: 1.4.0
>            Reporter: Claus Ibsen
>            Assignee: Claus Ibsen
>             Fix For: 2.0.0
>
>         Attachments: CAMEL-772.patch
>
>
> I had this unit test where I want to assert that the message body contains this string part
> {code}
>         MockEndpoint error = getMockEndpoint("mock:errorQueue");
>         error.expectedMessageCount(1);
>         error.message(0).body().contains("<patientCpr>0101701234</patientCpr>");
> {code}
> But the expression fails because the body is byte array and is not casted to String as the contains type is.
> If I change the code to:
> {code}
>         error.message(0).body().convertTo(String.class).contains("<patientCpr>0101701234</patientCpr>");
> {code}
> Then it of course works. But what if there already is a type convert in Camel that could have done the cast for me?
> But is it dangerous to add such automatic type casting behind the end-users back? Any thoughts?

--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.