001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.component.mock;
018
019 import java.util.ArrayList;
020 import java.util.List;
021
022 import org.apache.camel.Exchange;
023 import org.apache.camel.Expression;
024 import org.apache.camel.Predicate;
025 import org.apache.camel.builder.ExpressionClause;
026 import org.apache.camel.builder.ExpressionClauseSupport;
027 import org.apache.camel.builder.ValueBuilder;
028 import org.apache.camel.util.PredicateAssertHelper;
029
030 /**
031 * A builder of assertions on message exchanges
032 *
033 * @version $Revision: 906378 $
034 */
035 public abstract class AssertionClause extends ExpressionClauseSupport<ValueBuilder> implements Runnable {
036
037 private List<Predicate> predicates = new ArrayList<Predicate>();
038
039 public AssertionClause() {
040 super(null);
041 }
042
043 // Builder methods
044 // -------------------------------------------------------------------------
045 public ValueBuilder expression(Expression expression) {
046 super.expression(expression);
047 return new PredicateValueBuilder(getExpressionValue());
048 }
049
050 /**
051 * Adds the given predicate to this assertion clause
052 */
053 public AssertionClause predicate(Predicate predicate) {
054 addPredicate(predicate);
055 return this;
056 }
057
058 public ExpressionClause<AssertionClause> predicate() {
059 ExpressionClause<AssertionClause> clause = new ExpressionClause<AssertionClause>(this);
060 addPredicate(clause);
061 return clause;
062 }
063
064 /**
065 * Performs any assertions on the given exchange
066 */
067 protected void applyAssertionOn(MockEndpoint endpoint, int index, Exchange exchange) {
068 for (Predicate predicate : predicates) {
069 PredicateAssertHelper.assertMatches(predicate, endpoint.getEndpointUri() + " ", exchange);
070 }
071 }
072
073 protected void addPredicate(Predicate predicate) {
074 predicates.add(predicate);
075 }
076
077 /**
078 * Public class needed for fluent builders
079 */
080 public final class PredicateValueBuilder extends ValueBuilder {
081
082 public PredicateValueBuilder(Expression expression) {
083 super(expression);
084 }
085
086 protected Predicate onNewPredicate(Predicate predicate) {
087 addPredicate(predicate);
088 return predicate;
089 }
090 }
091 }