This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch mock in repository https://gitbox.apache.org/repos/asf/camel.git
commit adea00e8360f36b495cfe2da5d20c0fd38a2034f Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Mon Apr 15 16:26:50 2019 +0200 Move mock component out of camel-core. Work in progress. --- .../org/apache/camel/reifier/ChoiceReifier.java | 32 ++++++-- .../properties/XPathPropertyPlaceholderTest.java | 91 ++++++++++++++++++++++ 2 files changed, 118 insertions(+), 5 deletions(-) diff --git a/core/camel-core/src/main/java/org/apache/camel/reifier/ChoiceReifier.java b/core/camel-core/src/main/java/org/apache/camel/reifier/ChoiceReifier.java index 27cf327..1944472 100644 --- a/core/camel-core/src/main/java/org/apache/camel/reifier/ChoiceReifier.java +++ b/core/camel-core/src/main/java/org/apache/camel/reifier/ChoiceReifier.java @@ -19,7 +19,10 @@ package org.apache.camel.reifier; import java.util.ArrayList; import java.util.List; +import org.apache.camel.Expression; +import org.apache.camel.Predicate; import org.apache.camel.Processor; +import org.apache.camel.builder.ExpressionClause; import org.apache.camel.model.ChoiceDefinition; import org.apache.camel.model.ExpressionNode; import org.apache.camel.model.ProcessorDefinition; @@ -29,6 +32,7 @@ import org.apache.camel.model.language.ExpressionDefinition; import org.apache.camel.processor.ChoiceProcessor; import org.apache.camel.processor.FilterProcessor; import org.apache.camel.spi.RouteContext; +import org.apache.camel.support.language.ExpressionModel; class ChoiceReifier extends ProcessorReifier<ChoiceDefinition> { @@ -40,15 +44,33 @@ class ChoiceReifier extends ProcessorReifier<ChoiceDefinition> { public Processor createProcessor(RouteContext routeContext) throws Exception { List<FilterProcessor> filters = new ArrayList<>(); for (WhenDefinition whenClause : definition.getWhenClauses()) { + ExpressionDefinition exp = whenClause.getExpression(); + if (exp.getExpressionType() != null) { + exp = exp.getExpressionType(); + } + Predicate pre = exp.getPredicate(); + if (pre instanceof ExpressionClause) { + ExpressionClause<?> clause = (ExpressionClause<?>) pre; + if (clause.getExpressionType() != null) { + // if using the Java DSL then the expression may have been set using the + // ExpressionClause which is a fancy builder to define expressions and predicates + // using fluent builders in the DSL. However we need afterwards a callback to + // reset the expression to the expression type the ExpressionClause did build for us + ExpressionModel model = clause.getExpressionType(); + if (model instanceof ExpressionDefinition) { + whenClause.setExpression((ExpressionDefinition) model); + } + } + exp = whenClause.getExpression(); + } + // also resolve properties and constant fields on embedded expressions in the when clauses - ExpressionNode exp = whenClause; - ExpressionDefinition expressionDefinition = exp.getExpression(); - if (expressionDefinition != null) { + if (exp != null) { // resolve properties before we create the processor - ProcessorDefinitionHelper.resolvePropertyPlaceholders(routeContext.getCamelContext(), expressionDefinition); + ProcessorDefinitionHelper.resolvePropertyPlaceholders(routeContext.getCamelContext(), exp); // resolve constant fields (eg Exchange.FILE_NAME) - ProcessorDefinitionHelper.resolveKnownConstantFields(expressionDefinition); + ProcessorDefinitionHelper.resolveKnownConstantFields(exp); } FilterProcessor filter = (FilterProcessor) createProcessor(routeContext, whenClause); diff --git a/core/camel-core/src/test/java/org/apache/camel/component/properties/XPathPropertyPlaceholderTest.java b/core/camel-core/src/test/java/org/apache/camel/component/properties/XPathPropertyPlaceholderTest.java new file mode 100644 index 0000000..c09f9e8 --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/component/properties/XPathPropertyPlaceholderTest.java @@ -0,0 +1,91 @@ +/* + * 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.properties; + +import java.util.Properties; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.spi.PropertiesComponent; +import org.junit.Test; + +public class XPathPropertyPlaceholderTest extends ContextTestSupport { + + @Test + public void testFilter() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:output-filter"); + mock.expectedMessageCount(1); + + template.sendBody("direct:filter", "<greeting><text>Hello, world!</text></greeting>"); + + assertMockEndpointsSatisfied(); + } + + @Test + public void testChoice() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:output-choice"); + mock.expectedMessageCount(1); + + template.sendBody("direct:choice", "<greeting><text>Bye, world!</text></greeting>"); + + assertMockEndpointsSatisfied(); + } + + @Test + public void testChoice2() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:output-choice2"); + mock.expectedMessageCount(1); + + template.sendBody("direct:choice2", "<greeting><text>Bye, world!</text></greeting>"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + Properties prop = new Properties(); + prop.put("foo", "//greeting/text = 'Hello, world!'"); + prop.put("bar", "//greeting/text = 'Bye, world!'"); + + PropertiesComponent pc = context.getPropertiesComponent(true); + pc.setInitialProperties(prop); + + from("direct:filter") + .filter().xpath("{{foo}}") + .log("Passed filter!") + .to("mock:output-filter"); + + from("direct:choice") + .choice() + .when(xpath("{{bar}}")) + .log("Passed choice!") + .to("mock:output-choice"); + + from("direct:choice2") + .choice() + .when().xpath("{{bar}}") + .log("Passed choice2!") + .to("mock:output-choice2"); + } + }; + } + +}