camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1003398 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/builder/ main/java/org/apache/camel/component/mock/ test/java/org/apache/camel/builder/xml/
Date Fri, 01 Oct 2010 06:13:12 GMT
Author: davsclaus
Date: Fri Oct  1 06:13:12 2010
New Revision: 1003398

URL: http://svn.apache.org/viewvc?rev=1003398&view=rev
Log:
CAMEL-3182: Easier to set predicates on mock endpoints.

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathMockTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java?rev=1003398&r1=1003397&r2=1003398&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java Fri
Oct  1 06:13:12 2010
@@ -47,8 +47,12 @@ public final class PredicateBuilder {
     public static Predicate toPredicate(final Expression expression) {
         return new Predicate() {
             public boolean matches(Exchange exchange) {
-                Object value = expression.evaluate(exchange, Object.class);
-                return ObjectHelper.evaluateValuePredicate(value);
+                if (expression instanceof Predicate) {
+                    return ((Predicate) expression).matches(exchange);
+                } else {
+                    Object value = expression.evaluate(exchange, Object.class);
+                    return ObjectHelper.evaluateValuePredicate(value);
+                }
             }
 
             @Override

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java?rev=1003398&r1=1003397&r2=1003398&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java Fri Oct
 1 06:13:12 2010
@@ -56,10 +56,10 @@ public class ValueBuilder implements Exp
         return onNewPredicate(PredicateBuilder.toPredicate(expression));
     }
 
-    public Predicate matches() {
-        return onNewPredicate(PredicateBuilder.toPredicate(expression));
+    public ExpressionClause<Predicate> matches() {
+        return new ExpressionClause<Predicate>(onNewPredicate(PredicateBuilder.toPredicate(expression)));

     }
-    
+
     public Predicate isNotEqualTo(Object value) {
         Expression right = asExpression(value);
         return onNewPredicate(PredicateBuilder.isNotEqualTo(expression, right));

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java?rev=1003398&r1=1003397&r2=1003398&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
Fri Oct  1 06:13:12 2010
@@ -461,6 +461,23 @@ public class MockEndpoint extends Defaul
     }
 
     /**
+     * Sets an expectation that the given predicates matches the received messages by this
endpoint
+     */
+    public void expectedMessagesMatches(Predicate... predicates) {
+        for (int i = 0; i < predicates.length; i++) {
+            final int messageIndex = i;
+            final Predicate predicate = predicates[i];
+            final AssertionClause clause = new AssertionClause() {
+                public void run() {
+                    addPredicate(predicate);
+                    applyAssertionOn(MockEndpoint.this, messageIndex, assertExchangeReceived(messageIndex));
+                }
+            };
+            expects(clause);
+        }
+    }
+
+    /**
      * Sets an expectation that the given body values are received by this endpoint
      */
     public void expectedBodiesReceived(Object... bodies) {

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathMockTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathMockTest.java?rev=1003398&r1=1003397&r2=1003398&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathMockTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xml/XPathMockTest.java Fri
Oct  1 06:13:12 2010
@@ -20,6 +20,7 @@ import org.apache.camel.ContextTestSuppo
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 
+import static org.apache.camel.builder.PredicateBuilder.not;
 import static org.apache.camel.builder.xml.XPathBuilder.xpath;
 
 /**
@@ -36,6 +37,53 @@ public class XPathMockTest extends Conte
         assertMockEndpointsSatisfied();
     }
 
+    public void testXPathMock2() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.message(0).body().matches().xpath("/foo/text() = 'Hello World'");
+
+        template.sendBody("direct:start", "<foo>Hello World</foo>");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testXPathMock3() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.message(0).predicate().xpath("/foo/text() = 'Hello World'");
+
+        template.sendBody("direct:start", "<foo>Hello World</foo>");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testXPathMockMatches() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessagesMatches(xpath("/foo/text() = 'Hello World'"));
+
+        template.sendBody("direct:start", "<foo>Hello World</foo>");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testXPathMockMatchesTwo() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessagesMatches(xpath("/foo/text() = 'Hello World'"), xpath("/foo/text()
= 'Bye World'"));
+
+        template.sendBody("direct:start", "<foo>Hello World</foo>");
+        template.sendBody("direct:start", "<foo>Bye World</foo>");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testNonXPathMockMatches() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessagesMatches(not(body().contains("Bye")), body().contains("World"));
+
+        template.sendBody("direct:start", "<foo>Hello World</foo>");
+        template.sendBody("direct:start", "<foo>Bye World</foo>");
+
+        assertMockEndpointsSatisfied();
+    }
+
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {



Mime
View raw message