activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r531883 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/builder/ test/java/org/apache/camel/ test/java/org/apache/camel/builder/
Date Tue, 24 Apr 2007 11:34:32 GMT
Author: jstrachan
Date: Tue Apr 24 04:34:31 2007
New Revision: 531883

URL: http://svn.apache.org/viewvc?view=rev&rev=531883
Log:
some refactorings to make it easier to create expressions & predicates outside of the
routing DSL for testing

Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/Builder.java  
(with props)
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/TestSupport.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBuilderTest.java

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/Builder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/Builder.java?view=auto&rev=531883
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/Builder.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/Builder.java Tue
Apr 24 04:34:31 2007
@@ -0,0 +1,95 @@
+/**
+ *
+ * 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.builder;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
+
+/**
+ * A helper class for including portions of the
+ * <a href="http://activemq.apache.org/camel/expression.html">expression</a>
and
+ * <a href="http://activemq.apache.org/camel/predicate.html">predicate</a>
+ * <a href="http://activemq.apache.org/camel/dsl.html">Java DSL</a>
+ *
+ * @version $Revision: 1.1 $
+ */
+public class Builder {
+
+    /**
+     * Returns a constant expression
+     */
+    public static <E extends Exchange> ValueBuilder<E> constant(Object value)
{
+        Expression<E> expression = ExpressionBuilder.constantExpression(value);
+        return new ValueBuilder<E>(expression);
+    }
+
+    /**
+     * Returns a predicate and value builder for headers on an exchange
+     */
+    public static <E extends Exchange> ValueBuilder<E> header(@FluentArg("name")
String name) {
+        Expression<E> expression = ExpressionBuilder.headerExpression(name);
+        return new ValueBuilder<E>(expression);
+    }
+
+    /**
+     * Returns a predicate and value builder for the inbound body on an exchange
+     */
+    public static <E extends Exchange> ValueBuilder<E> body() {
+        Expression<E> expression = ExpressionBuilder.bodyExpression();
+        return new ValueBuilder<E>(expression);
+    }
+
+    /**
+     * Returns a predicate and value builder for the inbound message body as a specific type
+     */
+    public static <E extends Exchange, T> ValueBuilder<E> bodyAs( Class<T>
type) {
+        Expression<E> expression = ExpressionBuilder.<E, T>bodyExpression(type);
+        return new ValueBuilder<E>(expression);
+    }
+
+    /**
+     * Returns a predicate and value builder for the outbound body on an exchange
+     */
+    public static <E extends Exchange> ValueBuilder<E> outBody() {
+        Expression<E> expression = ExpressionBuilder.bodyExpression();
+        return new ValueBuilder<E>(expression);
+    }
+
+    /**
+     * Returns a predicate and value builder for the outbound message body as a specific
type
+     */
+    public static <E extends Exchange, T> ValueBuilder<E> outBody(Class<T>
type) {
+        Expression<E> expression = ExpressionBuilder.<E, T>bodyExpression(type);
+        return new ValueBuilder<E>(expression);
+    }
+
+
+    /**
+     * Returns an expression for the given system property
+     */
+    public static <E extends Exchange> ValueBuilder<E> systemProperty(final String
name) {
+        return systemProperty(name, null);
+    }
+
+    /**
+     * Returns an expression for the given system property
+     */
+    public static <E extends Exchange> ValueBuilder<E> systemProperty(final String
name, final String defaultValue) {
+        return new ValueBuilder<E>(ExpressionBuilder.<E>systemProperty(name,
defaultValue));
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/Builder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java?view=diff&rev=531883&r1=531882&r2=531883
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BuilderSupport.java
Tue Apr 24 04:34:31 2007
@@ -16,20 +16,20 @@
  */
 package org.apache.camel.builder;
 
-import java.util.ArrayList;
-import java.util.List;
-
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
-import org.apache.camel.Expression;
 import org.apache.camel.processor.LoggingLevel;
 import org.apache.camel.processor.SendProcessor;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+import java.util.ArrayList;
+import java.util.List;
+
 /**
- * Base class for implementation inheritance
+ * Base class for implementation inheritance for different clauses in the 
+ * <a href="http://activemq.apache.org/camel/dsl.html">Java DSL</a>
  *
  * @version $Revision: $
  */
@@ -50,87 +50,96 @@
         }
     }
 
-    // Helper methods
+    // Builder methods
     //-------------------------------------------------------------------------
 
     /**
-     * Resolves the given URI to an endpoint
+     * Returns a value builder for the given header
      */
     @Fluent
-    public Endpoint<E> endpoint(@FluentArg("uri") String uri) {
-        return getContext().resolveEndpoint(uri);
+    public ValueBuilder<E> header(@FluentArg("name")String name) {
+        return Builder.<E>header(name);
     }
 
     /**
-     * Resolves the list of URIs into a list of {@link Endpoint} instances
+     * Returns a predicate and value builder for the inbound body on an exchange
      */
     @Fluent
-    public List<Endpoint<E>> endpoints(@FluentArg("uris") String... uris) {
-        List<Endpoint<E>> endpoints = new ArrayList<Endpoint<E>>();
-        for (String uri : uris) {
-            endpoints.add(endpoint(uri));
-        }
-        return endpoints;
+    public ValueBuilder<E> body() {
+        return Builder.<E>body();
     }
 
     /**
-     * Helper method to create a list of {@link Endpoint} instances
+     * Returns a predicate and value builder for the inbound message body as a specific type
      */
     @Fluent
-    public List<Endpoint<E>> endpoints(@FluentArg("endpoints") Endpoint<E>...
endpoints) {
-        List<Endpoint<E>> answer = new ArrayList<Endpoint<E>>();
-        for (Endpoint<E> endpoint : endpoints) {
-            answer.add(endpoint);
-        }
-        return answer;
+    public <T> ValueBuilder<E> bodyAs(@FluentArg("class")Class<T> type)
{
+        return Builder.<E, T>bodyAs(type);
     }
 
-    // Builder methods
-    //-------------------------------------------------------------------------
+    /**
+     * Returns a predicate and value builder for the outbound body on an exchange
+     */
+    @Fluent
+    public ValueBuilder<E> outBody() {
+        return Builder.<E>outBody();
+    }
 
     /**
-     * Returns a predicate and value builder for headers on an exchange
+     * Returns a predicate and value builder for the outbound message body as a specific
type
      */
     @Fluent
-    public ValueBuilder<E> header(@FluentArg("name") String name) {
-        Expression<E> expression = ExpressionBuilder.headerExpression(name);
-        return new ValueBuilder<E>(expression);
+    public <T> ValueBuilder<E> outBody(@FluentArg("class")Class<T> type)
{
+        return Builder.<E, T>outBody(type);
     }
 
     /**
-     * Returns a predicate and value builder for the inbound body on an exchange
+     * Returns a value builder for the given system property
      */
     @Fluent
-    public ValueBuilder<E> body() {
-        Expression<E> expression = ExpressionBuilder.bodyExpression();
-        return new ValueBuilder<E>(expression);
+    public ValueBuilder<E> systemProperty(@FluentArg("name")String name) {
+        return Builder.<E>systemProperty(name);
     }
 
     /**
-     * Returns a predicate and value builder for the inbound message body as a specific type
+     * Returns a value builder for the given system property
      */
     @Fluent
-    public <T> ValueBuilder<E> bodyAs(@FluentArg("class") Class<T> type)
{
-        Expression<E> expression = ExpressionBuilder.bodyExpression(type);
-        return new ValueBuilder<E>(expression);
+    public ValueBuilder<E> systemProperty(
+            @FluentArg("name")String name, @FluentArg("defaultValue")String defaultValue)
{
+        return Builder.<E>systemProperty(name, defaultValue);
     }
 
     /**
-     * Returns a predicate and value builder for the outbound body on an exchange
+     * Resolves the given URI to an endpoint
      */
     @Fluent
-    public ValueBuilder<E> outBody() {
-        Expression<E> expression = ExpressionBuilder.bodyExpression();
-        return new ValueBuilder<E>(expression);
+    public Endpoint<E> endpoint(@FluentArg("uri")String uri) {
+        return getContext().resolveEndpoint(uri);
     }
 
     /**
-     * Returns a predicate and value builder for the outbound message body as a specific
type
+     * Resolves the list of URIs into a list of {@link Endpoint} instances
      */
     @Fluent
-    public <T> ValueBuilder<E> outBody(@FluentArg("class") Class<T> type)
{
-        Expression<E> expression = ExpressionBuilder.bodyExpression(type);
-        return new ValueBuilder<E>(expression);
+    public List<Endpoint<E>> endpoints(@FluentArg("uris")String... uris) {
+        List<Endpoint<E>> endpoints = new ArrayList<Endpoint<E>>();
+        for (String uri : uris) {
+            endpoints.add(endpoint(uri));
+        }
+        return endpoints;
+    }
+
+    /**
+     * Helper method to create a list of {@link Endpoint} instances
+     */
+    @Fluent
+    public List<Endpoint<E>> endpoints(@FluentArg("endpoints")Endpoint<E>...
endpoints) {
+        List<Endpoint<E>> answer = new ArrayList<Endpoint<E>>();
+        for (Endpoint<E> endpoint : endpoints) {
+            answer.add(endpoint);
+        }
+        return answer;
     }
 
     /**
@@ -153,7 +162,7 @@
      * Creates an error handler which just logs errors
      */
     @Fluent
-    public LoggingErrorHandlerBuilder<E> loggingErrorHandler(@FluentArg("log") String
log) {
+    public LoggingErrorHandlerBuilder<E> loggingErrorHandler(@FluentArg("log")String
log) {
         return loggingErrorHandler(LogFactory.getLog(log));
     }
 
@@ -161,7 +170,7 @@
      * Creates an error handler which just logs errors
      */
     @Fluent
-    public LoggingErrorHandlerBuilder<E> loggingErrorHandler(@FluentArg("log") Log
log) {
+    public LoggingErrorHandlerBuilder<E> loggingErrorHandler(@FluentArg("log")Log log)
{
         return new LoggingErrorHandlerBuilder<E>(log);
     }
 
@@ -169,7 +178,8 @@
      * Creates an error handler which just logs errors
      */
     @Fluent
-    public LoggingErrorHandlerBuilder<E> loggingErrorHandler(@FluentArg("log") Log
log, @FluentArg("level") LoggingLevel level) {
+    public LoggingErrorHandlerBuilder<E> loggingErrorHandler(
+            @FluentArg("log")Log log, @FluentArg("level")LoggingLevel level) {
         return new LoggingErrorHandlerBuilder<E>(log, level);
     }
 
@@ -179,12 +189,12 @@
     }
 
     @Fluent
-    public DeadLetterChannelBuilder<E> deadLetterChannel(@FluentArg("uri") String deadLetterUri)
{
+    public DeadLetterChannelBuilder<E> deadLetterChannel(@FluentArg("uri")String deadLetterUri)
{
         return deadLetterChannel(endpoint(deadLetterUri));
     }
 
     @Fluent
-    public DeadLetterChannelBuilder<E> deadLetterChannel(@FluentArg("endpoint") Endpoint<E>
deadLetterEndpoint) {
+    public DeadLetterChannelBuilder<E> deadLetterChannel(@FluentArg("endpoint")Endpoint<E>
deadLetterEndpoint) {
         return new DeadLetterChannelBuilder<E>(new SendProcessor<E>(deadLetterEndpoint));
     }
 

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java?view=diff&rev=531883&r1=531882&r2=531883
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
Tue Apr 24 04:34:31 2007
@@ -162,11 +162,11 @@
     }
 
     public static <E extends Exchange> Predicate<E> isNull(final Expression<E>
expression) {
-        return isEqualTo(expression, (Expression<E>) ExpressionBuilder.constantExpression(null));
+        return isEqualTo(expression, ExpressionBuilder.<E>constantExpression(null));
     }
 
     public static <E extends Exchange> Predicate<E> isNotNull(final Expression<E>
expression) {
-        return isNotEqualTo(expression, (Expression<E>) ExpressionBuilder.constantExpression(null));
+        return isNotEqualTo(expression, ExpressionBuilder.<E>constantExpression(null));
     }
 
     public static <E extends Exchange> Predicate<E> isInstanceOf(final Expression<E>
expression, final Class type) {

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java?view=diff&rev=531883&r1=531882&r2=531883
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
Tue Apr 24 04:34:31 2007
@@ -31,59 +31,61 @@
     public ValueBuilder(Expression<E> expression) {
         this.expression = expression;
     }
-    
+
     public Expression<E> getExpression() {
         return expression;
     }
 
-
     public Expression<E> createExpression() {
         return expression;
     }
 
+    // Predicate builders
+    //-------------------------------------------------------------------------
+
     @Fluent
-    public Predicate<E> isNotEqualTo(@FluentArg("value") Object value) {
-        Expression<E> right = ExpressionBuilder.constantExpression(value);
+    public Predicate<E> isNotEqualTo(@FluentArg("value")Object value) {
+        Expression<E> right = asExpression(value);
         return onNewPredicate(PredicateBuilder.isNotEqualTo(expression, right));
     }
 
     @Fluent
-    public Predicate<E> isEqualTo(@FluentArg("value") Object value) {
-        Expression<E> right = ExpressionBuilder.constantExpression(value);
+    public Predicate<E> isEqualTo(@FluentArg("value")Object value) {
+        Expression<E> right = asExpression(value);
         return onNewPredicate(PredicateBuilder.isEqualTo(expression, right));
     }
 
     @Fluent
-    public Predicate<E> isLessThan(@FluentArg("value") Object value) {
-        Expression<E> right = ExpressionBuilder.constantExpression(value);
+    public Predicate<E> isLessThan(@FluentArg("value")Object value) {
+        Expression<E> right = asExpression(value);
         return onNewPredicate(PredicateBuilder.isLessThan(expression, right));
     }
 
     @Fluent
-    public Predicate<E> isLessThanOrEqualTo(@FluentArg("value") Object value) {
-        Expression<E> right = ExpressionBuilder.constantExpression(value);
+    public Predicate<E> isLessThanOrEqualTo(@FluentArg("value")Object value) {
+        Expression<E> right = asExpression(value);
         return onNewPredicate(PredicateBuilder.isLessThanOrEqualTo(expression, right));
     }
 
     @Fluent
-    public Predicate<E> isGreaterThan(@FluentArg("value") Object value) {
-        Expression<E> right = ExpressionBuilder.constantExpression(value);
+    public Predicate<E> isGreaterThan(@FluentArg("value")Object value) {
+        Expression<E> right = asExpression(value);
         return onNewPredicate(PredicateBuilder.isGreaterThan(expression, right));
     }
 
     @Fluent
-    public Predicate<E> isGreaterThanOrEqualTo(@FluentArg("value") Object value) {
-        Expression<E> right = ExpressionBuilder.constantExpression(value);
+    public Predicate<E> isGreaterThanOrEqualTo(@FluentArg("value")Object value) {
+        Expression<E> right = asExpression(value);
         return onNewPredicate(PredicateBuilder.isGreaterThanOrEqualTo(expression, right));
     }
 
     @Fluent
-    public Predicate<E> isInstanceOf(@FluentArg("class") Class type) {
+    public Predicate<E> isInstanceOf(@FluentArg("class")Class type) {
         return onNewPredicate(PredicateBuilder.isInstanceOf(expression, type));
     }
 
     @Fluent
-    public Predicate<E> matchesRegex(@FluentArg("regex") String regex) {
+    public Predicate<E> matchesRegex(@FluentArg("regex")String regex) {
         return onNewPredicate(PredicateBuilder.regex(expression, regex));
     }
 
@@ -97,25 +99,68 @@
         return onNewPredicate(PredicateBuilder.isNotNull(expression));
     }
 
+    /**
+     * 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
+     */
+    @Fluent
+    public Predicate<E> regex(String regex) {
+        return onNewPredicate(PredicateBuilder.regex(expression, regex));
+    }
+
+
+    // Transformers
+    //-------------------------------------------------------------------------
+
     @Fluent
     public ValueBuilder<E> tokenize() {
         return tokenize("\n");
     }
 
     @Fluent
-    public ValueBuilder<E> tokenize(@FluentArg("token") String token) {
+    public ValueBuilder<E> tokenize(@FluentArg("token")String token) {
         Expression<E> newExp = ExpressionBuilder.tokenizeExpression(expression, token);
         return new ValueBuilder<E>(newExp);
     }
 
     /**
+     * Tokenizes the string conversion of this expression using the given regular expression
+     */
+    @Fluent
+    public ValueBuilder<E> regexTokenize(@FluentArg("regex")String regex) {
+        Expression<E> newExp = ExpressionBuilder.regexTokenize(expression, regex);
+        return new ValueBuilder<E>(newExp);
+    }
+
+    /**
+     * Replaces all occurrencies of the regular expression with the given replacement
+     */
+    @Fluent
+    public ValueBuilder<E> regexReplaceAll(@FluentArg("regex")String regex, @FluentArg("replacement")String
replacement) {
+        Expression<E> newExp = ExpressionBuilder.regexReplaceAll(expression, regex,
replacement);
+        return new ValueBuilder<E>(newExp);
+    }
+
+    /**
+     * Replaces all occurrencies of the regular expression with the given replacement
+     */
+    @Fluent
+    public ValueBuilder<E> regexReplaceAll(@FluentArg("regex")String regex, @FluentArg("replacement")Expression<E>
replacement) {
+        Expression<E> newExp = ExpressionBuilder.regexReplaceAll(expression, regex,
replacement);
+        return new ValueBuilder<E>(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
      */
     @Fluent
-    public ValueBuilder<E> convertTo(@FluentArg("type") Class type) {
+    public ValueBuilder<E> convertTo(@FluentArg("type")Class type) {
         Expression<E> newExp = ExpressionBuilder.convertTo(expression, type);
         return new ValueBuilder<E>(newExp);
     }
@@ -130,6 +175,10 @@
         return convertTo(String.class);
     }
 
+    
+    // Implementation methods
+    //-------------------------------------------------------------------------
+
     /**
      * A stategy method to allow derived classes to deal with the newly created predicate
      * in different ways
@@ -137,4 +186,18 @@
     protected Predicate<E> onNewPredicate(Predicate<E> predicate) {
         return predicate;
     }
+
+    protected Expression<E> asExpression(Object value) {
+        if (value instanceof Expression) {
+            return (Expression<E>) value;
+        }
+        else if (value instanceof ExpressionFactory) {
+            ExpressionFactory expressionFactory = (ExpressionFactory) value;
+            return expressionFactory.createExpression();
+        }
+        else {
+            return ExpressionBuilder.constantExpression(value);
+        }
+    }
+
 }

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/TestSupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/TestSupport.java?view=diff&rev=531883&r1=531882&r2=531883
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/TestSupport.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/TestSupport.java Tue Apr
24 04:34:31 2007
@@ -76,12 +76,35 @@
     /**
      * Asserts that the predicate returns the expected value on the exchange
      */
-    protected boolean assertPredicate(Predicate expression, Exchange exchange, boolean expected)
{
-        boolean value = expression.matches(exchange);
+    protected void assertPredicateMatches(Predicate predicate, Exchange exchange) {
+        assertPredicate(predicate, exchange, true);
+    }
 
-        log.debug("Evaluated predicate: " + expression + " on exchange: " + exchange + "
result: " + value);
+    /**
+     * Asserts that the predicate returns the expected value on the exchange
+     */
+    protected void assertPredicateDoesNotMatch(Predicate predicate, Exchange exchange) {
+        try {
+            predicate.assertMatches("Predicate should match", exchange);
+        }
+        catch (AssertionError e) {
+            log.debug("Caught expected assertion error: " + e);
+        }
+        assertPredicate(predicate, exchange, false);
+    }
 
-        assertEquals("Predicate: " + expression + " on Exchange: " + exchange, expected,
value);
+    /**
+     * Asserts that the predicate returns the expected value on the exchange
+     */
+    protected boolean assertPredicate(Predicate predicate, Exchange exchange, boolean expected)
{
+        if (expected) {
+            predicate.assertMatches("Predicate failed", exchange);
+        }
+        boolean value = predicate.matches(exchange);
+
+        log.debug("Evaluated predicate: " + predicate + " on exchange: " + exchange + " result:
" + value);
+
+        assertEquals("Predicate: " + predicate + " on Exchange: " + exchange, expected, value);
         return value;
     }
 

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBuilderTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBuilderTest.java?view=diff&rev=531883&r1=531882&r2=531883
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBuilderTest.java
(original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/PredicateBuilderTest.java
Tue Apr 24 04:34:31 2007
@@ -17,16 +17,13 @@
  */
 package org.apache.camel.builder;
 
-import org.apache.camel.TestSupport;
 import org.apache.camel.Exchange;
-import org.apache.camel.Expression;
-import org.apache.camel.Predicate;
 import org.apache.camel.Message;
-import static org.apache.camel.builder.PredicateBuilder.*;
-import org.apache.camel.impl.DefaultExchange;
+import org.apache.camel.Predicate;
+import org.apache.camel.TestSupport;
+import static org.apache.camel.builder.Builder.*;
 import org.apache.camel.impl.DefaultCamelContext;
-
-import java.util.Arrays;
+import org.apache.camel.impl.DefaultExchange;
 
 /**
  * @version $Revision$
@@ -34,14 +31,18 @@
 public class PredicateBuilderTest extends TestSupport {
     protected Exchange exchange = new DefaultExchange(new DefaultCamelContext());
 
-    public void testRegexTokenize() throws Exception {
-        Expression<Exchange> locationHeader = ExpressionBuilder.headerExpression("location");
+    public void testRegexPredicates() throws Exception {
+        assertMatches(header("location").regex("[a-zA-Z]+,London,UK"));
+        assertDoesNotMatch(header("location").regex("[a-zA-Z]+,Westminster,[a-zA-Z]+"));
+    }
 
-        Predicate<Exchange> predicate = regex(locationHeader, "[a-zA-Z]+,London,UK");
-        assertPredicate(predicate, exchange, true);
+    public void testPredicates() throws Exception {
+        assertMatches(header("name").isEqualTo(constant("James")));
+    }
 
-        predicate = regex(locationHeader, "[a-zA-Z]+,Westminster,[a-zA-Z]+");
-        assertPredicate(predicate, exchange, false);
+    public void testFailingPredicates() throws Exception {
+        assertDoesNotMatch(header("name").isEqualTo(constant("Hiram")));
+        assertDoesNotMatch(header("size").isGreaterThan(constant(100)));
     }
 
     @Override
@@ -51,5 +52,15 @@
         in.setBody("Hello there!");
         in.setHeader("name", "James");
         in.setHeader("location", "Islington,London,UK");
+        in.setHeader("size", 10);
+    }
+
+    protected void assertMatches(Predicate<Exchange> predicate) {
+        assertPredicateMatches(predicate, exchange);
     }
+
+    protected void assertDoesNotMatch(Predicate<Exchange> predicate) {
+        assertPredicateDoesNotMatch(predicate, exchange);
+    }
+
 }



Mime
View raw message