Repository: camel Updated Branches: refs/heads/camel-2.17.x 052807ad2 -> 0d4511a33 refs/heads/master 59c3420d8 -> ef0b438d4
CAMEL-10156: Improved bean method call to validate if method name with parameters has valid number of parenthesis in the syntax. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/ef0b438d Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/ef0b438d Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/ef0b438d Branch: refs/heads/master Commit: ef0b438d4a3ab1a5e6bd530479aa32f71fef857d Parents: 59c3420 Author: Claus Ibsen <davscl...@apache.org> Authored: Tue Jul 26 11:30:59 2016 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Tue Jul 26 13:03:23 2016 +0200 ---------------------------------------------------------------------- .../apache/camel/component/bean/BeanInfo.java | 5 ++ .../bean/BeanParameterInvalidSyntaxTest.java | 80 ++++++++++++++++++++ 2 files changed, 85 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/ef0b438d/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java b/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java index 5a3a0ef..752db1e 100644 --- a/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java +++ b/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java @@ -214,6 +214,11 @@ public class BeanInfo { if (!methodName.endsWith(")")) { throw new IllegalArgumentException("Method should end with parenthesis, was " + methodName); } + // and there must be an even number of parenthesis in the syntax + // (we can use betweenOuterPair as it return null if the syntax is invalid) + if (ObjectHelper.betweenOuterPair(methodName, '(', ')') == null) { + throw new IllegalArgumentException("Method should have even pair of parenthesis, was " + methodName); + } } boolean emptyParameters = methodName.endsWith("()"); http://git-wip-us.apache.org/repos/asf/camel/blob/ef0b438d/camel-core/src/test/java/org/apache/camel/component/bean/BeanParameterInvalidSyntaxTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/bean/BeanParameterInvalidSyntaxTest.java b/camel-core/src/test/java/org/apache/camel/component/bean/BeanParameterInvalidSyntaxTest.java new file mode 100644 index 0000000..024f326 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/bean/BeanParameterInvalidSyntaxTest.java @@ -0,0 +1,80 @@ +/** + * 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.CamelExecutionException; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.JndiRegistry; + +public class BeanParameterInvalidSyntaxTest extends ContextTestSupport { + + public void testBeanParameterInvalidSyntax() throws Exception { + getMockEndpoint("mock:result").expectedMessageCount(0); + + try { + template.sendBody("direct:a", "World"); + fail("Should have thrown exception"); + } catch (CamelExecutionException e) { + IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, e.getCause()); + assertEquals("Method should have even pair of parenthesis, was echo(${body}, 5))", iae.getMessage()); + } + + assertMockEndpointsSatisfied(); + } + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry jndi = super.createRegistry(); + jndi.bind("foo", new MyBean()); + return jndi; + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:a") + // invalid due extra parenthesis at the end + .to("bean:foo?method=echo(${body}, 5))") + .to("mock:result"); + } + }; + } + + public static class MyBean { + + public String echo(String body, int times) { + if (body == null) { + // use an empty string for no body + return ""; + } + + if (times > 0) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < times; i++) { + sb.append(body); + } + return sb.toString(); + } + + return body; + } + + } +}