This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-3.11.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.11.x by this push: new 0381d38 CAMEL-16821: camel-bean - Fixed calling bean that is a Processor which throws a java.lang.Error such as assertion error would not catch the exception and cause the exchange to fail. 0381d38 is described below commit 0381d3863c922e340a598b624fee8ebb7c3905b4 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Fri Jul 30 12:42:23 2021 +0200 CAMEL-16821: camel-bean - Fixed calling bean that is a Processor which throws a java.lang.Error such as assertion error would not catch the exception and cause the exchange to fail. --- .../component/bean/AbstractBeanProcessor.java | 2 +- .../bean/BeanThrowAssertionErrorTest.java | 86 ++++++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/components/camel-bean/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java b/components/camel-bean/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java index c2624b9..ce5bd9f 100644 --- a/components/camel-bean/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java +++ b/components/camel-bean/src/main/java/org/apache/camel/component/bean/AbstractBeanProcessor.java @@ -106,7 +106,7 @@ public abstract class AbstractBeanProcessor extends AsyncProcessorSupport { } try { target.process(exchange); - } catch (Exception e) { + } catch (Throwable e) { exchange.setException(e); } callback.done(true); diff --git a/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanThrowAssertionErrorTest.java b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanThrowAssertionErrorTest.java new file mode 100644 index 0000000..2c3dfc1 --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/component/bean/BeanThrowAssertionErrorTest.java @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.bean; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; + +public class BeanThrowAssertionErrorTest extends ContextTestSupport { + + @Test + public void testAssertion() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("Hello Camel"); + template.sendBody("direct:start", "Hello Camel"); + assertMockEndpointsSatisfied(); + + try { + template.sendBody("direct:start", "Hello World"); + fail("Should fail"); + } catch (Exception e) { + // ignore + } + } + + @Test + public void testAssertionProcessor() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("Hello World"); + template.sendBody("direct:start2", "Hello World"); + assertMockEndpointsSatisfied(); + + try { + template.sendBody("direct:start2", "Hello Camel"); + fail("Should fail"); + } catch (Exception e) { + // ignore + } + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .bean(BeanThrowAssertionErrorTest.this, "doSomething") + .to("mock:result"); + + from("direct:start2") + .bean(new MyProcessorBean()) + .to("mock:result"); + } + }; + } + + public void doSomething(String body) { + assertEquals("Hello Camel", body); + } + + private static class MyProcessorBean implements Processor { + + @Override + public void process(Exchange exchange) throws Exception { + assertEquals("Hello World", exchange.getMessage().getBody()); + } + } + +}