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 28bb7dc7acc46dde39297930134ce496ce0115d4
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Tue Apr 16 15:20:25 2019 +0200

    Move mock component out of camel-core. Work in progress.
---
 .../camel/component/mock/AssertionClause.java      |  13 +-
 .../camel/component/mock/MockValueBuilder.java     | 378 +++++++++++++++++++++
 2 files changed, 383 insertions(+), 8 deletions(-)

diff --git 
a/core/camel-core/src/main/java/org/apache/camel/component/mock/AssertionClause.java
 
b/core/camel-core/src/main/java/org/apache/camel/component/mock/AssertionClause.java
index 53e5d40..beb0d18 100644
--- 
a/core/camel-core/src/main/java/org/apache/camel/component/mock/AssertionClause.java
+++ 
b/core/camel-core/src/main/java/org/apache/camel/component/mock/AssertionClause.java
@@ -25,16 +25,13 @@ import org.apache.camel.Expression;
 import org.apache.camel.Predicate;
 import org.apache.camel.StreamCache;
 import org.apache.camel.builder.ExpressionClause;
-import org.apache.camel.builder.ValueBuilder;
 import org.apache.camel.support.PredicateAssertHelper;
 import org.apache.camel.support.language.ExpressionModel;
 
 /**
  * A builder of assertions on message exchanges
  */
-public abstract class AssertionClause extends 
MockExpressionClauseSupport<ValueBuilder> implements Runnable {
-
-    // TODO: MockValueBuilder
+public abstract class AssertionClause extends 
MockExpressionClauseSupport<MockValueBuilder> implements Runnable {
 
     protected final MockEndpoint mock;
     protected volatile int currentIndex;
@@ -50,13 +47,13 @@ public abstract class AssertionClause extends 
MockExpressionClauseSupport<ValueB
     // Builder methods
     // 
-------------------------------------------------------------------------
 
-    public ValueBuilder expression(Expression expression) {
+    public MockValueBuilder expression(Expression expression) {
         // must override this method as we provide null in the constructor
         super.expression(expression);
         return new PredicateValueBuilder(expression);
     }
 
-    public ValueBuilder language(ExpressionModel expression) {
+    public MockValueBuilder language(ExpressionModel expression) {
         // must override this method as we provide null in the constructor
         super.expression(expression.createExpression(mock.getCamelContext()));
         return new PredicateValueBuilder(getExpressionValue());
@@ -144,7 +141,7 @@ public abstract class AssertionClause extends 
MockExpressionClauseSupport<ValueB
     /**
      * Public class needed for fluent builders
      */
-    public final class PredicateValueBuilder extends ValueBuilder {
+    public final class PredicateValueBuilder extends MockValueBuilder {
 
         public PredicateValueBuilder(Expression expression) {
             super(expression);
@@ -158,7 +155,7 @@ public abstract class AssertionClause extends 
MockExpressionClauseSupport<ValueB
         }
 
         @Override
-        protected ValueBuilder onNewValueBuilder(Expression exp) {
+        protected MockValueBuilder onNewValueBuilder(Expression exp) {
             return new PredicateValueBuilder(exp);
         }
     }
diff --git 
a/core/camel-core/src/main/java/org/apache/camel/component/mock/MockValueBuilder.java
 
b/core/camel-core/src/main/java/org/apache/camel/component/mock/MockValueBuilder.java
new file mode 100644
index 0000000..188bbc6
--- /dev/null
+++ 
b/core/camel-core/src/main/java/org/apache/camel/component/mock/MockValueBuilder.java
@@ -0,0 +1,378 @@
+/*
+ * 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.mock;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
+import org.apache.camel.Predicate;
+import org.apache.camel.builder.ExpressionBuilder;
+import org.apache.camel.builder.ExpressionClause;
+import org.apache.camel.builder.PredicateBuilder;
+import org.apache.camel.spi.NamespaceAware;
+import org.apache.camel.support.ExpressionAdapter;
+import org.apache.camel.support.ExpressionToPredicateAdapter;
+import org.apache.camel.support.builder.xml.Namespaces;
+
+/**
+ * A builder of expressions or predicates based on values.
+ */
+public class MockValueBuilder implements Expression, Predicate {
+    private Expression expression;
+    private boolean not;
+
+    public MockValueBuilder(Expression expression) {
+        this.expression = expression;
+    }
+
+    @Override
+    public <T> T evaluate(Exchange exchange, Class<T> type) {
+        return expression.evaluate(exchange, type);
+    }
+
+    @Override
+    public boolean matches(Exchange exchange) {
+        return PredicateBuilder.toPredicate(getExpression()).matches(exchange);
+    }
+
+    public Expression getExpression() {
+        return expression;
+    }
+
+    @Override
+    public String toString() {
+        return expression.toString();
+    }
+
+    // Predicate builders
+    // 
-------------------------------------------------------------------------
+
+    public Predicate matches(Expression expression) {
+        return 
onNewPredicate(ExpressionToPredicateAdapter.toPredicate(expression));
+    }
+
+    public ExpressionClause<Predicate> matches() {
+        // chicken-and-egg situation as we need to return an ExpressionClause
+        // which needs a right-hand side that is being built via the fluent
+        // builder that is returned, and therefore we need to use a ref
+        // to the expression (right hand side) that will be used below
+        // in the onNewPredicate where the actual matching is executed
+        final AtomicReference<Expression> ref = new AtomicReference<>();
+
+        final ExpressionClause<Predicate> answer = new ExpressionClause<>(
+            onNewPredicate(new Predicate() {
+                @Override
+                public boolean matches(Exchange exchange) {
+                    Expression left = expression;
+                    Expression right = ref.get();
+                    return PredicateBuilder.isEqualTo(left, 
right).matches(exchange);
+                }
+
+                @Override
+                public String toString() {
+                    return expression + " == " + ref.get();
+                }
+            }));
+
+        final Expression right = new ExpressionAdapter() {
+            @Override
+            public Object evaluate(Exchange exchange) {
+                return answer.evaluate(exchange, Object.class);
+                /*if (answer.getExpressionValue() != null) {
+                    return answer.getExpressionValue().evaluate(exchange, 
Object.class);
+                } else {
+                    return answer.getExpressionType().evaluate(exchange);
+                }*/
+            }
+        };
+        // okay now we can set the reference to the right-hand-side
+        ref.set(right);
+
+        return answer;
+    }
+
+    public Predicate isNotEqualTo(Object value) {
+        Expression right = asExpression(value);
+        return onNewPredicate(PredicateBuilder.isNotEqualTo(expression, 
right));
+    }
+
+    public Predicate isEqualTo(Object value) {
+        Expression right = asExpression(value);
+        return onNewPredicate(PredicateBuilder.isEqualTo(expression, right));
+    }
+
+    public Predicate isEqualToIgnoreCase(Object value) {
+        Expression right = asExpression(value);
+        return onNewPredicate(PredicateBuilder.isEqualToIgnoreCase(expression, 
right));
+    }
+
+    public Predicate isLessThan(Object value) {
+        Expression right = asExpression(value);
+        return onNewPredicate(PredicateBuilder.isLessThan(expression, right));
+    }
+
+    public Predicate isLessThanOrEqualTo(Object value) {
+        Expression right = asExpression(value);
+        return onNewPredicate(PredicateBuilder.isLessThanOrEqualTo(expression, 
right));
+    }
+
+    public Predicate isGreaterThan(Object value) {
+        Expression right = asExpression(value);
+        return onNewPredicate(PredicateBuilder.isGreaterThan(expression, 
right));
+    }
+
+    public Predicate isGreaterThanOrEqualTo(Object value) {
+        Expression right = asExpression(value);
+        return 
onNewPredicate(PredicateBuilder.isGreaterThanOrEqualTo(expression, right));
+    }
+
+    public Predicate isInstanceOf(Class<?> type) {
+        return onNewPredicate(PredicateBuilder.isInstanceOf(expression, type));
+    }
+
+    public Predicate isNull() {
+        return onNewPredicate(PredicateBuilder.isNull(expression));
+    }
+
+    public Predicate isNotNull() {
+        return onNewPredicate(PredicateBuilder.isNotNull(expression));
+    }
+   
+    public Predicate not(Predicate predicate) {
+        return onNewPredicate(PredicateBuilder.not(predicate));
+    }
+
+    public Predicate in(Object... values) {
+        List<Predicate> predicates = new ArrayList<>();
+        for (Object value : values) {
+            Expression right = asExpression(value);
+            right = ExpressionBuilder.convertToExpression(right, expression);
+            Predicate predicate = 
onNewPredicate(PredicateBuilder.isEqualTo(expression, right));
+            predicates.add(predicate);
+        }
+        return in(predicates.toArray(new Predicate[predicates.size()]));
+    }
+
+    public Predicate in(Predicate... predicates) {
+        return onNewPredicate(PredicateBuilder.in(predicates));
+    }
+
+    public Predicate startsWith(Object value) {
+        Expression right = asExpression(value);
+        return onNewPredicate(PredicateBuilder.startsWith(expression, right));
+    }
+
+    public Predicate endsWith(Object value) {
+        Expression right = asExpression(value);
+        return onNewPredicate(PredicateBuilder.endsWith(expression, right));
+    }
+
+    /**
+     * Create a predicate that the left hand expression contains the value of
+     * the right hand expression
+     * 
+     * @param value the element which is compared to be contained within this
+     *                expression
+     * @return a predicate which evaluates to true if the given value 
expression
+     *         is contained within this expression value
+     */
+    public Predicate contains(Object value) {
+        Expression right = asExpression(value);
+        return onNewPredicate(PredicateBuilder.contains(expression, right));
+    }
+
+    /**
+     * Creates a predicate which is true if this expression matches the given
+     * regular expression
+     * 
+     * @param regex the regular expression to match
+     * @return a predicate which evaluates to true if the expression matches 
the
+     *         regex
+     */
+    public Predicate regex(String regex) {
+        return onNewPredicate(PredicateBuilder.regex(expression, regex));
+    }
+
+    // Expression builders
+    // 
-------------------------------------------------------------------------
+
+    public MockValueBuilder tokenize() {
+        return tokenize("\n");
+    }
+
+    public MockValueBuilder tokenize(String token) {
+        Expression newExp = ExpressionBuilder.tokenizeExpression(expression, 
token);
+        return onNewValueBuilder(newExp);
+    }
+
+    public MockValueBuilder tokenize(String token, int group, boolean 
skipFirst) {
+        return tokenize(token, "" + group, skipFirst);
+    }
+
+    public MockValueBuilder tokenize(String token, String group, boolean 
skipFirst) {
+        Expression newExp = ExpressionBuilder.tokenizeExpression(expression, 
token);
+        if (group == null && skipFirst) {
+            // wrap in skip first (if group then it has its own skip first 
logic)
+            newExp = ExpressionBuilder.skipFirstExpression(newExp);
+        }
+        newExp = ExpressionBuilder.groupIteratorExpression(newExp, token, 
group, skipFirst);
+        return onNewValueBuilder(newExp);
+    }
+
+    public MockValueBuilder tokenizeXML(String tagName, String 
inheritNamespaceTagName) {
+        Expression newExp = ExpressionBuilder.tokenizeXMLExpression(tagName, 
inheritNamespaceTagName);
+        return onNewValueBuilder(newExp);
+    }
+
+    public MockValueBuilder xtokenize(String path, Namespaces namespaces) {
+        return xtokenize(path, 'i', namespaces);
+    }
+
+    public MockValueBuilder xtokenize(String path, char mode, Namespaces 
namespaces) {
+        Expression newExp = ExpressionBuilder.tokenizeXMLAwareExpression(path, 
mode);
+        ((NamespaceAware)newExp).setNamespaces(namespaces.getNamespaces());
+        return onNewValueBuilder(newExp);
+    }
+
+    public MockValueBuilder tokenizePair(String startToken, String endToken, 
boolean includeTokens) {
+        Expression newExp = 
ExpressionBuilder.tokenizePairExpression(startToken, endToken, includeTokens);
+        return onNewValueBuilder(newExp);
+    }
+
+    /**
+     * Tokenizes the string conversion of this expression using the given
+     * regular expression
+     */
+    public MockValueBuilder regexTokenize(String regex) {
+        Expression newExp = 
ExpressionBuilder.regexTokenizeExpression(expression, regex);
+        return onNewValueBuilder(newExp);
+    }
+
+    /**
+     * Replaces all occurrences of the regular expression with the given
+     * replacement
+     */
+    public MockValueBuilder regexReplaceAll(String regex, String replacement) {
+        Expression newExp = ExpressionBuilder.regexReplaceAll(expression, 
regex, replacement);
+        return onNewValueBuilder(newExp);
+    }
+
+    /**
+     * Replaces all occurrences of the regular expression with the given
+     * replacement
+     */
+    public MockValueBuilder regexReplaceAll(String regex, Expression 
replacement) {
+        Expression newExp = ExpressionBuilder.regexReplaceAll(expression, 
regex, replacement);
+        return onNewValueBuilder(newExp);
+    }
+
+    /**
+     * Converts the current value to the given type using the registered type
+     * converters
+     * 
+     * @param type the type to convert the value to
+     * @return the current builder
+     */
+    public MockValueBuilder convertTo(Class<?> type) {
+        Expression newExp = ExpressionBuilder.convertToExpression(expression, 
type);
+        return onNewValueBuilder(newExp);
+    }
+
+    /**
+     * Converts the current value to a String using the registered type 
converters
+     * 
+     * @return the current builder
+     */
+    public MockValueBuilder convertToString() {
+        return convertTo(String.class);
+    }
+
+    /**
+     * Appends the string evaluation of this expression with the given value
+     *
+     * @param value the value or expression to append
+     * @return the current builder
+     */
+    public MockValueBuilder append(Object value) {
+        Expression newExp = ExpressionBuilder.append(expression, 
asExpression(value));
+        return onNewValueBuilder(newExp);
+    }
+
+    /**
+     * Prepends the string evaluation of this expression with the given value
+     *
+     * @param value the value or expression to prepend
+     * @return the current builder
+     */
+    public MockValueBuilder prepend(Object value) {
+        Expression newExp = ExpressionBuilder.prepend(expression, 
asExpression(value));
+        return onNewValueBuilder(newExp);
+    }
+
+    /**
+     * Sorts the current value using the given comparator. The current value 
must be convertable
+     * to a {@link List} to allow sorting using the comparator.
+     *
+     * @param comparator  the comparator used by sorting
+     * @return the current builder
+     */
+    public MockValueBuilder sort(Comparator<?> comparator) {
+        Expression newExp = ExpressionBuilder.sortExpression(expression, 
comparator);
+        return onNewValueBuilder(newExp);
+    }
+
+    /**
+     * Negates the built expression.
+     *
+     * @return the current builder
+     */
+    public MockValueBuilder not() {
+        not = true;
+        return this;
+    }
+
+    // Implementation methods
+    // 
-------------------------------------------------------------------------
+
+    /**
+     * A strategy method to allow derived classes to deal with the newly 
created
+     * predicate in different ways
+     */
+    protected Predicate onNewPredicate(Predicate predicate) {
+        if (not) {
+            return PredicateBuilder.not(predicate);
+        } else {
+            return predicate;
+        }
+    }
+
+    protected Expression asExpression(Object value) {
+        if (value instanceof Expression) {
+            return (Expression)value;
+        } else {
+            return ExpressionBuilder.constantExpression(value);
+        }
+    }
+
+    protected MockValueBuilder onNewValueBuilder(Expression exp) {
+        return new MockValueBuilder(exp);
+    }
+}

Reply via email to