activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r531854 - in /activemq/camel/trunk: camel-core/src/main/java/org/apache/camel/ camel-core/src/main/java/org/apache/camel/builder/ camel-core/src/main/java/org/apache/camel/builder/xpath/ camel-core/src/main/java/org/apache/camel/component/m...
Date Tue, 24 Apr 2007 09:54:44 GMT
Author: jstrachan
Date: Tue Apr 24 02:54:42 2007
New Revision: 531854

URL: http://svn.apache.org/viewvc?view=rev&rev=531854
Log:
added the ability to add assertions to predicates for easier testing of routing rules; such
as with the MockEndpoint

Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/BinaryPredicateSupport.java
  (with props)
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/PredicateSupport.java
  (with props)
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Predicate.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/xpath/XPathBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/AssertionClause.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/ObjectConverter.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
    activemq/camel/trunk/camel-josql/src/main/java/org/apache/camel/builder/sql/SqlBuilder.java
    activemq/camel/trunk/camel-saxon/src/main/java/org/apache/camel/builder/saxon/XQueryBuilder.java
    activemq/camel/trunk/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Predicate.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Predicate.java?view=diff&rev=531854&r1=531853&r2=531854
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Predicate.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Predicate.java Tue Apr
24 02:54:42 2007
@@ -31,4 +31,16 @@
      * @return true if the predicate matches
      */
     boolean matches(E exchange);
+
+    /**
+     * Allows this predicate to be used nicely in testing to generate
+     * a nicely formatted exception and message if this predicate does not
+     * match for the given exchange.
+     *
+     * @param text the description to use in the exception message
+     * @param exchange the exchange to evaluate the expression on
+     * @throws AssertionError if the predicate does not match
+     */
+    void assertMatches(String text, E exchange) throws AssertionError;
+
 }

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=531854&r1=531853&r2=531854
==============================================================================
--- 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 02:54:42 2007
@@ -16,9 +16,12 @@
  */
 package org.apache.camel.builder;
 
+import static org.apache.camel.util.ObjectHelper.compare;
 import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
 import org.apache.camel.Predicate;
+import org.apache.camel.impl.PredicateSupport;
+import org.apache.camel.impl.BinaryPredicateSupport;
 import org.apache.camel.util.ObjectHelper;
 import static org.apache.camel.util.ObjectHelper.notNull;
 
@@ -37,7 +40,7 @@
     public static <E extends Exchange> Predicate<E> and(final Predicate<E>
left, final Predicate<E> right) {
         notNull(left, "left");
         notNull(right, "right");
-        return new Predicate<E>() {
+        return new PredicateSupport<E>() {
             public boolean matches(E exchange) {
                 return left.matches(exchange) && right.matches(exchange);
             }
@@ -55,7 +58,7 @@
     public static <E extends Exchange> Predicate<E> or(final Predicate<E>
left, final Predicate<E> right) {
         notNull(left, "left");
         notNull(right, "right");
-        return new Predicate<E>() {
+        return new PredicateSupport<E>() {
             public boolean matches(E exchange) {
                 return left.matches(exchange) || right.matches(exchange);
             }
@@ -68,176 +71,122 @@
     }
 
     public static <E extends Exchange> Predicate<E> isEqualTo(final Expression<E>
left, final Expression<E> right) {
-        notNull(left, "left");
-        notNull(right, "right");
+        return new BinaryPredicateSupport<E>(left, right) {
 
-        return new Predicate<E>() {
-            public boolean matches(E exchange) {
-                Object value1 = left.evaluate(exchange);
-                Object value2 = right.evaluate(exchange);
-                return ObjectHelper.equals(value1, value2);
+            protected boolean matches(E exchange, Object leftValue, Object rightValue) {
+                return ObjectHelper.equals(leftValue, rightValue);
             }
 
-            @Override
-            public String toString() {
-                return left + " == " + right;
+            protected String getOperationText() {
+                return "==";
             }
         };
     }
 
     public static <E extends Exchange> Predicate<E> isNotEqualTo(final Expression<E>
left, final Expression<E> right) {
-        notNull(left, "left");
-        notNull(right, "right");
+        return new BinaryPredicateSupport<E>(left, right) {
 
-        return new Predicate<E>() {
-            public boolean matches(E exchange) {
-                Object value1 = left.evaluate(exchange);
-                Object value2 = right.evaluate(exchange);
-                return !ObjectHelper.equals(value1, value2);
+            protected boolean matches(E exchange, Object leftValue, Object rightValue) {
+                return !ObjectHelper.equals(leftValue, rightValue);
             }
 
-            @Override
-            public String toString() {
-                return left + " != " + right;
+            protected String getOperationText() {
+                return "==";
             }
         };
     }
 
     public static <E extends Exchange> Predicate<E> isLessThan(final Expression<E>
left, final Expression<E> right) {
-        notNull(left, "left");
-        notNull(right, "right");
+        return new BinaryPredicateSupport<E>(left, right) {
 
-        return new Predicate<E>() {
-            public boolean matches(E exchange) {
-                Object value1 = left.evaluate(exchange);
-                Object value2 = right.evaluate(exchange);
-                return ObjectHelper.compare(value1, value2) < 0;
+            protected boolean matches(E exchange, Object leftValue, Object rightValue) {
+                return compare(leftValue, rightValue) < 0;
             }
 
-            @Override
-            public String toString() {
-                return left + " < " + right;
+            protected String getOperationText() {
+                return "<";
             }
         };
     }
 
     public static <E extends Exchange> Predicate<E> isLessThanOrEqualTo(final
Expression<E> left, final Expression<E> right) {
-        notNull(left, "left");
-        notNull(right, "right");
+        return new BinaryPredicateSupport<E>(left, right) {
 
-        return new Predicate<E>() {
-            public boolean matches(E exchange) {
-                Object value1 = left.evaluate(exchange);
-                Object value2 = right.evaluate(exchange);
-                return ObjectHelper.compare(value1, value2) <= 0;
+            protected boolean matches(E exchange, Object leftValue, Object rightValue) {
+                return compare(leftValue, rightValue) <= 0;
             }
 
-            @Override
-            public String toString() {
-                return left + " <= " + right;
+            protected String getOperationText() {
+                return "<=";
             }
         };
     }
 
     public static <E extends Exchange> Predicate<E> isGreaterThan(final Expression<E>
left, final Expression<E> right) {
-        notNull(left, "left");
-        notNull(right, "right");
+        return new BinaryPredicateSupport<E>(left, right) {
 
-        return new Predicate<E>() {
-            public boolean matches(E exchange) {
-                Object value1 = left.evaluate(exchange);
-                Object value2 = right.evaluate(exchange);
-                return ObjectHelper.compare(value1, value2) > 0;
+            protected boolean matches(E exchange, Object leftValue, Object rightValue) {
+                return compare(leftValue, rightValue) > 0;
             }
 
-            @Override
-            public String toString() {
-                return left + " > " + right;
+            protected String getOperationText() {
+                return ">";
             }
         };
     }
 
     public static <E extends Exchange> Predicate<E> isGreaterThanOrEqualTo(final
Expression<E> left, final Expression<E> right) {
-        notNull(left, "left");
-        notNull(right, "right");
+        return new BinaryPredicateSupport<E>(left, right) {
 
-        return new Predicate<E>() {
-            public boolean matches(E exchange) {
-                Object value1 = left.evaluate(exchange);
-                Object value2 = right.evaluate(exchange);
-                return ObjectHelper.compare(value1, value2) >= 0;
+            protected boolean matches(E exchange, Object leftValue, Object rightValue) {
+                return compare(leftValue, rightValue) < 0;
             }
 
-            @Override
-            public String toString() {
-                return left + " >= " + right;
+            protected String getOperationText() {
+                return ">=";
             }
         };
     }
 
-    public static <E extends Exchange> Predicate<E> isInstanceOf(final Expression<E>
expression, final Class type) {
-        notNull(expression, "expression");
-        notNull(type, "type");
+    public static <E extends Exchange> Predicate<E> contains(final Expression<E>
left, final Expression<E> right) {
+        return new BinaryPredicateSupport<E>(left, right) {
 
-        return new Predicate<E>() {
-            public boolean matches(E exchange) {
-                Object value = expression.evaluate(exchange);
-                return type.isInstance(value);
+            protected boolean matches(E exchange, Object leftValue, Object rightValue) {
+                return ObjectHelper.contains(leftValue, rightValue);
             }
 
-            @Override
-            public String toString() {
-                return expression + " instanceof " + type.getName();
+            protected String getOperationText() {
+                return "contains";
             }
         };
     }
 
     public static <E extends Exchange> Predicate<E> isNull(final Expression<E>
expression) {
-        notNull(expression, "expression");
-
-        return new Predicate<E>() {
-            public boolean matches(E exchange) {
-                Object value = expression.evaluate(exchange);
-                return value == null;
-            }
-
-            @Override
-            public String toString() {
-                return expression + " == null";
-            }
-        };
+        return isEqualTo(expression, (Expression<E>) ExpressionBuilder.constantExpression(null));
     }
 
     public static <E extends Exchange> Predicate<E> isNotNull(final Expression<E>
expression) {
+        return isNotEqualTo(expression, (Expression<E>) ExpressionBuilder.constantExpression(null));
+    }
+
+    public static <E extends Exchange> Predicate<E> isInstanceOf(final Expression<E>
expression, final Class type) {
         notNull(expression, "expression");
+        notNull(type, "type");
 
-        return new Predicate<E>() {
+        return new PredicateSupport<E>() {
             public boolean matches(E exchange) {
                 Object value = expression.evaluate(exchange);
-                return value != null;
+                return type.isInstance(value);
             }
 
             @Override
             public String toString() {
-                return expression + " != null";
-            }
-        };
-    }
-
-    public static <E extends Exchange> Predicate<E> contains(final Expression<E>
left, final Expression<E> right) {
-        notNull(left, "left");
-        notNull(right, "right");
-
-        return new Predicate<E>() {
-            public boolean matches(E exchange) {
-                Object value1 = left.evaluate(exchange);
-                Object value2 = right.evaluate(exchange);
-                return ObjectHelper.contains(value1, value2);
+                return expression + " instanceof " + type.getName();
             }
 
             @Override
-            public String toString() {
-                return left + ".contains(" + right + ")";
+            protected String assertionFailureMessage(E exchange) {
+                return super.assertionFailureMessage(exchange) + " for <" + expression.evaluate(exchange)
+ ">";
             }
         };
     }
@@ -265,7 +214,7 @@
         notNull(expression, "expression");
         notNull(pattern, "pattern");
 
-        return new Predicate<E>() {
+        return new PredicateSupport<E>() {
             public boolean matches(E exchange) {
                 Object value = expression.evaluate(exchange);
                 if (value != null) {
@@ -279,6 +228,12 @@
             public String toString() {
                 return expression + ".matches(" + pattern + ")";
             }
+
+            @Override
+            protected String assertionFailureMessage(E exchange) {
+                return super.assertionFailureMessage(exchange) + " for <" + expression.evaluate(exchange)
+ ">";
+            }
+
         };
     }
 

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xpath/XPathBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xpath/XPathBuilder.java?view=diff&rev=531854&r1=531853&r2=531854
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xpath/XPathBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xpath/XPathBuilder.java
Tue Apr 24 02:54:42 2007
@@ -17,6 +17,7 @@
  */
 package org.apache.camel.builder.xpath;
 
+import static org.apache.camel.converter.ObjectConverter.toBoolean;
 import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
 import org.apache.camel.Predicate;
@@ -66,7 +67,15 @@
     }
 
     public boolean matches(E exchange) {
-        return ObjectConverter.toBoolean(evaluateAs(exchange, XPathConstants.BOOLEAN));
+        Object booleanResult = evaluateAs(exchange, XPathConstants.BOOLEAN);
+        return toBoolean(booleanResult);
+    }
+
+    public void assertMatches(String text, E exchange) throws AssertionError {
+        Object booleanResult = evaluateAs(exchange, XPathConstants.BOOLEAN);
+        if (!toBoolean(booleanResult)) {
+            throw new AssertionError(this + " failed on " + exchange + " as returned <"
+ booleanResult + ">");
+        }
     }
 
     public Object evaluate(E exchange) {

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/AssertionClause.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/AssertionClause.java?view=diff&rev=531854&r1=531853&r2=531854
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/AssertionClause.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/AssertionClause.java
Tue Apr 24 02:54:42 2007
@@ -117,8 +117,7 @@
 
 
         protected Predicate<E> onNewPredicate(Predicate<E> predicate) {
-            //addPredicate(predicate);
-            predicates.add(predicate);
+            addPredicate(predicate);
             return predicate;
         }
     }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/ObjectConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/ObjectConverter.java?view=diff&rev=531854&r1=531853&r2=531854
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/ObjectConverter.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/ObjectConverter.java
Tue Apr 24 02:54:42 2007
@@ -23,6 +23,7 @@
 import java.util.Collections;
 import java.util.Collection;
 import java.util.Arrays;
+import java.util.List;
 
 /**
  * Some core java.lang based 
@@ -66,4 +67,5 @@
         }
         return false;
     }
+ 
 }

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/BinaryPredicateSupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/BinaryPredicateSupport.java?view=auto&rev=531854
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/BinaryPredicateSupport.java
(added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/BinaryPredicateSupport.java
Tue Apr 24 02:54:42 2007
@@ -0,0 +1,70 @@
+/**
+ *
+ * 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.impl;
+
+import org.apache.camel.Predicate;
+import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
+import org.apache.camel.util.ObjectHelper;
+import static org.apache.camel.util.ObjectHelper.notNull;
+
+/**
+ * A useful base class for {@link Predicate} implementations
+ *
+ * @version $Revision: 1.1 $
+ */
+public abstract class BinaryPredicateSupport<E extends Exchange> implements Predicate<E>
{
+
+    private final Expression<E> left;
+    private final Expression<E> right;
+
+    protected BinaryPredicateSupport(Expression<E> left, Expression<E> right)
{
+        notNull(left, "left");
+        notNull(right, "right");
+
+        this.left = left;
+        this.right = right;
+    }
+
+    @Override
+    public String toString() {
+        return left + " " + getOperationText() + " " + right;
+    }
+
+    public boolean matches(E exchange) {
+        Object leftValue = left.evaluate(exchange);
+        Object rightValue = right.evaluate(exchange);
+        return matches(exchange, leftValue, rightValue);
+    }
+
+    public void assertMatches(String text, E exchange) {
+        Object leftValue = left.evaluate(exchange);
+        Object rightValue = right.evaluate(exchange);
+        if (!matches(exchange, leftValue, rightValue)) {
+            throw new AssertionError(assertionFailureMessage(exchange, leftValue, rightValue));
+        }
+    }
+
+    protected abstract boolean matches(E exchange, Object leftValue, Object rightValue);
+
+    protected abstract String getOperationText();
+
+    protected String assertionFailureMessage(E exchange, Object leftValue, Object rightValue)
{
+        return this + " failed on " + exchange + " with left value <" + leftValue + ">
right value <" + rightValue + ">";
+    }
+}

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

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/PredicateSupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/PredicateSupport.java?view=auto&rev=531854
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/PredicateSupport.java
(added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/PredicateSupport.java
Tue Apr 24 02:54:42 2007
@@ -0,0 +1,39 @@
+/**
+ *
+ * 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.impl;
+
+import org.apache.camel.Predicate;
+import org.apache.camel.Exchange;
+
+/**
+ * A useful base class for {@link Predicate} implementations
+ *
+ * @version $Revision: 1.1 $
+ */
+public abstract class PredicateSupport<E extends Exchange> implements Predicate<E>
{
+
+    public void assertMatches(String text, E exchange) {
+        if (! matches(exchange)) {
+            throw new AssertionError(assertionFailureMessage(exchange));
+        }
+    }
+
+    protected String assertionFailureMessage(E exchange) {
+        return this + " failed on " + exchange;
+    }
+}

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

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java?view=diff&rev=531854&r1=531853&r2=531854
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
Tue Apr 24 02:54:42 2007
@@ -133,6 +133,29 @@
         }
     }
 
+
+    /**
+     * Returns the predicate matching boolean on a {@link List} result set
+     * where if the first element is a boolean its value is used
+     * otherwise this method returns true if the collection is not empty
+     *
+     * @returns true if the first element is a boolean and its value is true or if the list
is non empty
+     */
+    public static boolean matches(List list) {
+        if (!list.isEmpty()) {
+            Object value = list.get(0);
+            if (value instanceof Boolean) {
+                Boolean flag = (Boolean) value;
+                return flag.booleanValue();
+            }
+            else {
+                // lets assume non-empty results are true
+                return true;
+            }
+        }
+        return false;
+    }
+
     public static boolean isNotNullOrBlank(String text) {
         return text != null && text.trim().length() > 0;
     }

Modified: activemq/camel/trunk/camel-josql/src/main/java/org/apache/camel/builder/sql/SqlBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-josql/src/main/java/org/apache/camel/builder/sql/SqlBuilder.java?view=diff&rev=531854&r1=531853&r2=531854
==============================================================================
--- activemq/camel/trunk/camel-josql/src/main/java/org/apache/camel/builder/sql/SqlBuilder.java
(original)
+++ activemq/camel/trunk/camel-josql/src/main/java/org/apache/camel/builder/sql/SqlBuilder.java
Tue Apr 24 02:54:42 2007
@@ -21,6 +21,7 @@
 import org.apache.camel.Message;
 import org.apache.camel.Predicate;
 import org.apache.camel.RuntimeExpressionException;
+import org.apache.camel.util.ObjectHelper;
 import org.josql.Query;
 import org.josql.QueryExecutionException;
 import org.josql.QueryParseException;
@@ -51,18 +52,14 @@
 
     public boolean matches(E exchange) {
         List list = evaluateQuery(exchange);
-        if (!list.isEmpty()) {
-            Object value = list.get(0);
-            if (value instanceof Boolean) {
-                Boolean flag = (Boolean) value;
-                return flag.booleanValue();
-            }
-            else {
-                // lets assume non-empty results are true
-                return true;
-            }
+        return matches(exchange, list);
+    }
+
+    public void assertMatches(String text, E exchange) throws AssertionError {
+        List list = evaluateQuery(exchange);
+        if (!matches(exchange, list)) {
+            throw new AssertionError(this + " failed on " + exchange + " as found " + list);
         }
-        return false;
     }
 
     // Builder API
@@ -103,6 +100,10 @@
 
     // Implementation methods
     //-----------------------------------------------------------------------
+    protected boolean matches(E exchange, List list) {
+        return ObjectHelper.matches(list);
+    }
+
     protected List evaluateQuery(E exchange) {
         configureQuery(exchange);
         Message in = exchange.getIn();

Modified: activemq/camel/trunk/camel-saxon/src/main/java/org/apache/camel/builder/saxon/XQueryBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-saxon/src/main/java/org/apache/camel/builder/saxon/XQueryBuilder.java?view=diff&rev=531854&r1=531853&r2=531854
==============================================================================
--- activemq/camel/trunk/camel-saxon/src/main/java/org/apache/camel/builder/saxon/XQueryBuilder.java
(original)
+++ activemq/camel/trunk/camel-saxon/src/main/java/org/apache/camel/builder/saxon/XQueryBuilder.java
Tue Apr 24 02:54:42 2007
@@ -27,6 +27,7 @@
 import org.apache.camel.Expression;
 import org.apache.camel.Predicate;
 import org.apache.camel.RuntimeExpressionException;
+import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.converter.jaxp.BytesSource;
 import org.apache.camel.converter.jaxp.XmlConverter;
 import org.apache.camel.converter.jaxp.StringSource;
@@ -61,6 +62,11 @@
     private ResultFormat resultsFormat = ResultFormat.DOM;
     private Properties properties = new Properties();
 
+    @Override
+    public String toString() {
+        return "XQuery[" + expression + "]";
+    }
+
     public Object evaluate(E exchange) {
         try {
             switch (resultsFormat) {
@@ -123,14 +129,25 @@
     public boolean matches(E exchange) {
         try {
             List list = evaluateAsList(exchange);
-            System.out.println("Found list: " + list);
-            return false;
+            return matches(exchange, list);
         }
         catch (Exception e) {
             throw new RuntimeExpressionException(e);
         }
     }
 
+    public void assertMatches(String text, E exchange) throws AssertionError {
+        try {
+            List list = evaluateAsList(exchange);
+            if (!matches(exchange, list)) {
+                throw new AssertionError(this + " failed on " + exchange + " as evaluated:
" + list);
+            }
+        }
+        catch (Exception e) {
+            throw new AssertionError(e);
+        }
+    }
+
     // Builder methods
     //-------------------------------------------------------------------------
     public static <E extends Exchange> XQueryBuilder<E> xquery(final String queryText)
{
@@ -307,5 +324,9 @@
     protected void clearBuilderReferences() {
         staticQueryContext = null;
         configuration = null;
+    }
+
+    protected boolean matches(E exchange, List results) {
+        return ObjectHelper.matches(results);
     }
 }

Modified: activemq/camel/trunk/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java?view=diff&rev=531854&r1=531853&r2=531854
==============================================================================
--- activemq/camel/trunk/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java
(original)
+++ activemq/camel/trunk/camel-script/src/main/java/org/apache/camel/builder/script/ScriptBuilder.java
Tue Apr 24 02:54:42 2007
@@ -70,7 +70,15 @@
     }
 
     public boolean matches(E exchange) {
-        return ObjectConverter.toBoolean(evaluateScript(exchange));
+        Object scriptValue = evaluateScript(exchange);
+        return matches(exchange, scriptValue);
+    }
+
+    public void assertMatches(String text, E exchange) throws AssertionError {
+        Object scriptValue = evaluateScript(exchange);
+        if (!matches(exchange, scriptValue)) {
+            throw new AssertionError(this + " failed on " + exchange + " as script returned
<" + scriptValue + ">");
+        }
     }
 
     public void process(E exchange) {
@@ -380,6 +388,11 @@
             }
         }
     }
+    
+    protected boolean matches(E exchange, Object scriptValue) {
+        return ObjectConverter.toBoolean(scriptValue);
+    }
+
     protected ScriptEngine createScriptEngine() {
         ScriptEngineManager manager = new ScriptEngineManager();
         return manager.getEngineByName(scriptEngineName);



Mime
View raw message