Pascal Schumacher created CAMEL-12949:
-----------------------------------------

             Summary: onWhen predicate with onException can not bind to method 
with exception subclass parameter
                 Key: CAMEL-12949
                 URL: https://issues.apache.org/jira/browse/CAMEL-12949
             Project: Camel
          Issue Type: Bug
          Components: camel-core
    Affects Versions: 2.22.2
            Reporter: Pascal Schumacher


Inspired by this example for "Camel in Action, Second Edition" (page 510):
{code:java}
public final class MyHttpUtil {
    public static boolean isIllegalDataError(
        HttpOperationFailedException cause) {
        int code = cause.getStatusCode();
        if (code != 500) {
            return false;
            }
        return "ILLEGAL DATA".equals(cause.getResponseBody().toString());
    }
}

onException(HttpOperationFailedException.class)
    .onWhen(bean(MyHttpUtil.class, "isIllegalData"))
    .handled(true)
    .to("file:/rider/files/illegal");
{code}
I expected this (simplified test) to pass:
{code:java}
public class OnWhenBindMethodWithExceptionSubclassParameterTest extends 
CamelTestSupport {

    public static class IOExceptionMatcher {
        public static boolean matches(IOException e) {
            // real logic omited
            return true;
        }
    }

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {

            public void configure() {
                onException(IOException.class)
                    .onWhen(method(IOExceptionMatcher.class))
                    .to("mock:ioexception");
                from("seda:start")
                    .throwException(new IOException());
            }
        };
    }

    @Test
    public void test() throws Exception {
        getMockEndpoint("mock:ioexception").expectedMessageCount(1);

        template.sendBody("seda:start", "Hello World");

        assertMockEndpointsSatisfied();
    }
}
{code}
but it fails with with:
{noformat}
java.lang.AssertionError: mock://ioexception Received message count. Expected: 
<1> but was: <0>
{noformat}
The workaround is to replace the {{IOException}} parameter of 
{{IOExceptionMatcher#matches}} with an generic {{Exception}}.
{code:java}
public static class IOExceptionMatcher {
    public static boolean matches(Exception e) {
        if (e instanceof IOException) {
            return true;
        }
        return false;
    }
}
{code}

but this is not as elegant and more error prone.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to