You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2010/04/16 15:49:25 UTC

svn commit: r934869 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/ main/java/org/apache/camel/builder/ main/java/org/apache/camel/impl/ main/java/org/apache/camel/util/ test/java/org/apache/camel/component/mock/

Author: davsclaus
Date: Fri Apr 16 13:49:25 2010
New Revision: 934869

URL: http://svn.apache.org/viewvc?rev=934869&view=rev
Log:
CAMEL-2651: Mock endpoints now report better assertion exception messages for binary based predicates.

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/BinaryEvaluablePredicate.java   (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BinaryPredicateSupport.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/PredicateAssertHelper.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/BinaryEvaluablePredicate.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/BinaryEvaluablePredicate.java?rev=934869&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/BinaryEvaluablePredicate.java (added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/BinaryEvaluablePredicate.java Fri Apr 16 13:49:25 2010
@@ -0,0 +1,68 @@
+/**
+ * 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;
+
+/**
+ * A predicate which is evaluating a binary expression.
+ * <p/>
+ * The predicate has a left and right hand side expressions which
+ * is matched based on an operator.
+ * <p/>
+ * This predicate can return information about the evaluated expressions
+ * which allows you to get detailed information, so you better understand
+ * why the predicate did not match.
+ *
+ * @version $Revision$
+ */
+public interface BinaryEvaluablePredicate extends Predicate {
+
+    /**
+     * Gets the operator
+     *
+     * @return the operator text
+     */
+    String getOperator();
+
+    /**
+     * Gets the left hand side expression
+     *
+     * @return the left expression
+     */
+    Expression getLeft();
+
+    /**
+     * Gets the right hand side expression
+     *
+     * @return the right expression
+     */
+    Expression getRight();
+
+    /**
+     * Gets the evaluated left hand side value
+     *
+     * @return the left value, may be <tt>null</tt> if predicate has not been matched yet.
+     */
+    Object getLeftValue();
+
+    /**
+     * Gets the evaluated right hand side value
+     *
+     * @return the right value, may be <tt>null</tt> if predicate has not been matched yet.
+     */
+    Object getRightValue();
+
+}

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

Propchange: camel/trunk/camel-core/src/main/java/org/apache/camel/BinaryEvaluablePredicate.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BinaryPredicateSupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BinaryPredicateSupport.java?rev=934869&r1=934868&r2=934869&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BinaryPredicateSupport.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/BinaryPredicateSupport.java Fri Apr 16 13:49:25 2010
@@ -16,20 +16,24 @@
  */
 package org.apache.camel.builder;
 
+import org.apache.camel.BinaryEvaluablePredicate;
 import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
 import org.apache.camel.Predicate;
+
 import static org.apache.camel.util.ObjectHelper.notNull;
 
 /**
  * A useful base class for {@link Predicate} implementations
- * 
+ *
  * @version $Revision$
  */
-public abstract class BinaryPredicateSupport implements Predicate {
+public abstract class BinaryPredicateSupport implements BinaryEvaluablePredicate {
 
     private final Expression left;
     private final Expression right;
+    private Object leftValue;
+    private Object rightValue;
 
     protected BinaryPredicateSupport(Expression left, Expression right) {
         notNull(left, "left");
@@ -45,8 +49,8 @@ public abstract class BinaryPredicateSup
     }
 
     public boolean matches(Exchange exchange) {
-        Object leftValue = left.evaluate(exchange, Object.class);
-        Object rightValue = right.evaluate(exchange, Object.class);
+        leftValue = left.evaluate(exchange, Object.class);
+        rightValue = right.evaluate(exchange, Object.class);
         return matches(exchange, leftValue, rightValue);
     }
 
@@ -54,4 +58,23 @@ public abstract class BinaryPredicateSup
 
     protected abstract String getOperationText();
 
+    public Expression getRight() {
+        return right;
+    }
+
+    public Expression getLeft() {
+        return left;
+    }
+
+    public String getOperator() {
+        return getOperationText();
+    }
+
+    public Object getRightValue() {
+        return rightValue;
+    }
+
+    public Object getLeftValue() {
+        return leftValue;
+    }
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java?rev=934869&r1=934868&r2=934869&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExchange.java Fri Apr 16 13:49:25 2010
@@ -76,7 +76,7 @@ public final class DefaultExchange imple
 
     @Override
     public String toString() {
-        return "Exchange[" + in + "]";
+        return "Exchange[" + (out == null ? in : out) + "]";
     }
 
     public Exchange copy() {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/PredicateAssertHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/PredicateAssertHelper.java?rev=934869&r1=934868&r2=934869&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/PredicateAssertHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/PredicateAssertHelper.java Fri Apr 16 13:49:25 2010
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.util;
 
+import org.apache.camel.BinaryEvaluablePredicate;
 import org.apache.camel.Exchange;
 import org.apache.camel.Predicate;
 
@@ -31,6 +32,23 @@ public final class PredicateAssertHelper
     }
 
     public static void assertMatches(Predicate predicate, String text, Exchange exchange) {
+        if (predicate instanceof BinaryEvaluablePredicate) {
+            // special for binary evaluable as we can get more detailed information
+            BinaryEvaluablePredicate eval = (BinaryEvaluablePredicate) predicate;
+            if (!eval.matches(exchange)) {
+                String evalText = eval.getLeftValue() + " " + eval.getOperator() + " " + eval.getRightValue();
+                if (text == null) {
+                    throw new AssertionError(predicate + " evaluated as " + evalText + "  on " + exchange);
+                } else {
+                    throw new AssertionError(text + predicate + " evaluated as: " + evalText + " on " + exchange);
+                }
+            }
+        } else {
+            doAssertMatches(predicate, text, exchange);
+        }
+    }
+
+    private static void doAssertMatches(Predicate predicate, String text, Exchange exchange) {
         if (!predicate.matches(exchange)) {
             if (text == null) {
                 throw new AssertionError(predicate + " on " + exchange);
@@ -38,7 +56,6 @@ public final class PredicateAssertHelper
                 throw new AssertionError(text + predicate + " on " + exchange);
             }
         }
-
     }
 
 }
\ No newline at end of file

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java?rev=934869&r1=934868&r2=934869&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/mock/MockEndpointTest.java Fri Apr 16 13:49:25 2010
@@ -483,7 +483,7 @@ public class MockEndpointTest extends Co
         }
     }
 
-    public void testMessageIndex() throws Exception {
+    public void testMessageIndexIsEqualTo() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.expectedMessageCount(2);
         mock.message(0).header("foo").isEqualTo(123);
@@ -496,7 +496,44 @@ public class MockEndpointTest extends Co
             assertMockEndpointsSatisfied();
             fail("Should have thrown exception");
         } catch (AssertionError e) {
-            assertEquals("Assertion error at index 1 on mock mock://result with predicate: header(bar) == 444 on Exchange[Message: Hello World]", e.getMessage());
+            assertEquals("Assertion error at index 1 on mock mock://result with predicate: header(bar) == 444"
+                    + " evaluated as: 234 == 444 on Exchange[Message: Hello World]", e.getMessage());
+        }
+    }
+
+    public void testPredicateEvaluationIsNull() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(2);
+        mock.message(0).header("foo").isNotNull();
+        mock.message(1).header("bar").isNull();
+
+        template.sendBodyAndHeader("direct:a", "Hello World", "foo", 123);
+        template.sendBodyAndHeader("direct:a", "Hello World", "bar", 234);
+
+        try {
+            assertMockEndpointsSatisfied();
+            fail("Should have thrown exception");
+        } catch (AssertionError e) {
+            assertEquals("Assertion error at index 1 on mock mock://result with predicate: header(bar) is null"
+                    + " evaluated as: 234 is null on Exchange[Message: Hello World]", e.getMessage());
+        }
+    }
+
+    public void testPredicateEvaluationIsInstanceOf() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(2);
+        mock.message(0).header("foo").isNotNull();
+        mock.message(1).header("bar").isInstanceOf(String.class);
+
+        template.sendBodyAndHeader("direct:a", "Hello World", "foo", 123);
+        template.sendBodyAndHeader("direct:a", "Hello World", "bar", 234);
+
+        try {
+            assertMockEndpointsSatisfied();
+            fail("Should have thrown exception");
+        } catch (AssertionError e) {
+            assertEquals("Assertion error at index 1 on mock mock://result with predicate: header(bar) instanceof"
+                    + " java.lang.String on Exchange[Message: Hello World]", e.getMessage());
         }
     }