camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r899226 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/builder/NotifierBuilder.java test/java/org/apache/camel/builder/NotifierBuilderTest.java
Date Thu, 14 Jan 2010 14:55:58 GMT
Author: davsclaus
Date: Thu Jan 14 14:55:57 2010
New Revision: 899226

URL: http://svn.apache.org/viewvc?rev=899226&view=rev
Log:
CAMEL-2357: Added additional builder methods.

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/builder/NotifierBuilder.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/builder/NotifierBuilderTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/NotifierBuilder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/NotifierBuilder.java?rev=899226&r1=899225&r2=899226&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/NotifierBuilder.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/NotifierBuilder.java Thu
Jan 14 14:55:57 2010
@@ -17,6 +17,7 @@
 package org.apache.camel.builder;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.EventObject;
 import java.util.Iterator;
 import java.util.List;
@@ -350,18 +351,48 @@
     }
 
     /**
-     * Sets a condition that <b>any</b> received {@link Exchange} should match
the {@link Predicate}
+     * Sets a condition that <b>any received</b> {@link Exchange} should match
the {@link Predicate}
      *
      * @param predicate the predicate
      * @return the builder
      */
     public NotifierBuilder whenAnyReceivedMatches(final Predicate predicate) {
+        return doWhenAnyMatches(predicate, true);
+    }
+
+    /**
+     * Sets a condition that <b>any done</b> {@link Exchange} should match the
{@link Predicate}
+     *
+     * @param predicate the predicate
+     * @return the builder
+     */
+    public NotifierBuilder whenAnyDoneMatches(final Predicate predicate) {
+        return doWhenAnyMatches(predicate, false);
+    }
+
+    private NotifierBuilder doWhenAnyMatches(final Predicate predicate, final boolean received)
{
         stack.push(new EventPredicateSupport() {
             private boolean matches;
 
             @Override
+            public boolean onExchangeCompleted(Exchange exchange) {
+                if (!received && !matches) {
+                    matches = predicate.matches(exchange);
+                }
+                return true;
+            }
+
+            @Override
+            public boolean onExchangeFailure(Exchange exchange) {
+                if (!received && !matches) {
+                    matches = predicate.matches(exchange);
+                }
+                return true;
+            }
+
+            @Override
             public boolean onExchangeCreated(Exchange exchange) {
-                if (!matches) {
+                if (received && !matches) {
                     matches = predicate.matches(exchange);
                 }
                 return true;
@@ -373,25 +404,59 @@
 
             @Override
             public String toString() {
-                return "whenAnyReceivedMatches(" + predicate + ")";
+                if (received) {
+                    return "whenAnyReceivedMatches(" + predicate + ")";
+                } else {
+                    return "whenAnyDoneMatches(" + predicate + ")";
+                }
             }
         });
         return this;
     }
 
     /**
-     * Sets a condition that <b>all</b> received {@link Exchange} should match
the {@link Predicate}
+     * Sets a condition that <b>all received</b> {@link Exchange} should match
the {@link Predicate}
      *
      * @param predicate the predicate
      * @return the builder
      */
     public NotifierBuilder whenAllReceivedMatches(final Predicate predicate) {
+        return doWhenAllMatches(predicate, true);
+    }
+
+    /**
+     * Sets a condition that <b>all done</b> {@link Exchange} should match the
{@link Predicate}
+     *
+     * @param predicate the predicate
+     * @return the builder
+     */
+    public NotifierBuilder whenAllDoneMatches(final Predicate predicate) {
+        return doWhenAllMatches(predicate, false);
+    }
+
+    private NotifierBuilder doWhenAllMatches(final Predicate predicate, final boolean received)
{
         stack.push(new EventPredicateSupport() {
             private boolean matches = true;
 
             @Override
+            public boolean onExchangeCompleted(Exchange exchange) {
+                if (!received && matches) {
+                    matches = predicate.matches(exchange);
+                }
+                return true;
+            }
+
+            @Override
+            public boolean onExchangeFailure(Exchange exchange) {
+                if (!received && matches) {
+                    matches = predicate.matches(exchange);
+                }
+                return true;
+            }
+
+            @Override
             public boolean onExchangeCreated(Exchange exchange) {
-                if (matches) {
+                if (received && matches) {
                     matches = predicate.matches(exchange);
                 }
                 return true;
@@ -403,7 +468,11 @@
 
             @Override
             public String toString() {
-                return "whenAllReceivedMatches(" + predicate + ")";
+                if (received) {
+                    return "whenAllReceivedMatches(" + predicate + ")";
+                } else {
+                    return "whenAllDoneMatches(" + predicate + ")";
+                }
             }
         });
         return this;
@@ -599,6 +668,130 @@
         return this;
     }
 
+    /**
+     * Sets a condition that the bodies is expected to be <b>received</b> in
the order as well.
+     * <p/>
+     * This condition will discard any additional messages. If you need a more strict condition
+     * then use {@link #whenExactBodiesReceived(Object...)}
+     *
+     * @param bodies the expected bodies
+     * @return the builder
+     * @see #whenExactBodiesReceived(Object...)
+     */
+    public NotifierBuilder whenBodiesReceived(Object... bodies) {
+        List<Object> bodyList = new ArrayList<Object>();
+        bodyList.addAll(Arrays.asList(bodies));
+        return doWhenBodies(bodyList, true, false);
+    }
+
+    /**
+     * Sets a condition that the bodies is expected to be <b>done</b> in the
order as well.
+     * <p/>
+     * This condition will discard any additional messages. If you need a more strict condition
+     * then use {@link #whenExactBodiesDone(Object...)}
+     *
+     * @param bodies the expected bodies
+     * @return the builder
+     * @see #whenExactBodiesDone(Object...)
+     */
+    public NotifierBuilder whenBodiesDone(Object... bodies) {
+        List<Object> bodyList = new ArrayList<Object>();
+        bodyList.addAll(Arrays.asList(bodies));
+        return doWhenBodies(bodyList, false, false);
+    }
+
+    /**
+     * Sets a condition that the bodies is expected to be <b>received</b> in
the order as well.
+     * <p/>
+     * This condition is strict which means that it only expect that exact number of bodies
+     *
+     * @param bodies the expected bodies
+     * @return the builder
+     * @see #whenBodiesReceived(Object...)
+     */
+    public NotifierBuilder whenExactBodiesReceived(Object... bodies) {
+        List<Object> bodyList = new ArrayList<Object>();
+        bodyList.addAll(Arrays.asList(bodies));
+        return doWhenBodies(bodyList, true, true);
+    }
+
+    /**
+     * Sets a condition that the bodies is expected to be <b>done</b> in the
order as well.
+     * <p/>
+     * This condition is strict which means that it only expect that exact number of bodies
+     *
+     * @param bodies the expected bodies
+     * @return the builder
+     * @see #whenExactBodiesDone(Object...)
+     */
+    public NotifierBuilder whenExactBodiesDone(Object... bodies) {
+        List<Object> bodyList = new ArrayList<Object>();
+        bodyList.addAll(Arrays.asList(bodies));
+        return doWhenBodies(bodyList, false, true);
+    }
+
+    private NotifierBuilder doWhenBodies(final List bodies, final boolean received, final
boolean exact) {
+        stack.push(new EventPredicateSupport() {
+            private boolean matches;
+            private int current;
+
+            @Override
+            public boolean onExchangeCreated(Exchange exchange) {
+                if (received) {
+                    matchBody(exchange);
+                }
+                return true;
+            }
+
+            @Override
+            public boolean onExchangeFailure(Exchange exchange) {
+                if (!received) {
+                    matchBody(exchange);
+                }
+                return true;
+            }
+
+            @Override
+            public boolean onExchangeCompleted(Exchange exchange) {
+                if (!received) {
+                    matchBody(exchange);
+                }
+                return true;
+            }
+
+            private void matchBody(Exchange exchange) {
+                current++;
+
+                if (current > bodies.size()) {
+                    // out of bounds
+                    return;
+                }
+
+                Object actual = exchange.getIn().getBody();
+                Object expected = bodies.get(current - 1);
+                matches = ObjectHelper.equal(expected, actual);
+            }
+
+            public boolean matches() {
+                if (exact) {
+                    return matches && current == bodies.size();
+                } else {
+                    return matches && current >= bodies.size();
+                }
+            }
+
+            @Override
+            public String toString() {
+                if (received) {
+                    return "" + (exact ? "whenExactBodiesReceived(" : "whenBodiesReceived(")
+ bodies + ")";
+                } else {
+                    return "" + (exact ? "whenExactBodiesDone(" : "whenBodiesDone(") + bodies
+ ")";
+                }
+            }
+        });
+        return this;
+    }
+
 
     /**
      * Prepares to append an additional expression using the <i>and</i> operator.

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/builder/NotifierBuilderTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/NotifierBuilderTest.java?rev=899226&r1=899225&r2=899226&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/builder/NotifierBuilderTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/builder/NotifierBuilderTest.java
Thu Jan 14 14:55:57 2010
@@ -401,6 +401,118 @@
         assertEquals(false, notifier.matches());
     }
 
+    public void testWhenAnyDoneMatches() throws Exception {
+        NotifierBuilder notifier = new NotifierBuilder(context)
+                .whenAnyDoneMatches(body().contains("Bye"))
+                .create();
+
+        assertEquals(false, notifier.matches());
+
+        template.sendBody("direct:foo", "Hi World");
+        assertEquals(false, notifier.matches());
+
+        template.sendBody("direct:cake", "Camel");
+        assertEquals(true, notifier.matches());
+
+        template.sendBody("direct:foo", "Damn World");
+        assertEquals(true, notifier.matches());
+    }
+
+    public void testWhenAllDoneMatches() throws Exception {
+        NotifierBuilder notifier = new NotifierBuilder(context)
+                .whenAllDoneMatches(body().contains("Bye"))
+                .create();
+
+        assertEquals(false, notifier.matches());
+
+        template.sendBody("direct:cake", "Camel");
+        assertEquals(true, notifier.matches());
+
+        template.sendBody("direct:cake", "World");
+        assertEquals(true, notifier.matches());
+
+        template.sendBody("direct:foo", "Hi World");
+        assertEquals(false, notifier.matches());
+    }
+
+    public void testWhenBodiesReceived() throws Exception {
+        NotifierBuilder notifier = new NotifierBuilder(context)
+                .whenBodiesReceived("Hi World", "Hello World")
+                .create();
+
+        assertEquals(false, notifier.matches());
+
+        template.sendBody("direct:foo", "Hi World");
+        assertEquals(false, notifier.matches());
+
+        template.sendBody("direct:foo", "Hello World");
+        assertEquals(true, notifier.matches());
+
+        // should keep being true
+        template.sendBody("direct:foo", "Bye World");
+        assertEquals(true, notifier.matches());
+
+        template.sendBody("direct:foo", "Damn World");
+        assertEquals(true, notifier.matches());
+    }
+
+    public void testWhenBodiesDone() throws Exception {
+        NotifierBuilder notifier = new NotifierBuilder(context)
+                .whenBodiesDone("Bye World", "Bye Camel")
+                .create();
+
+        assertEquals(false, notifier.matches());
+
+        template.requestBody("direct:cake", "World");
+        assertEquals(false, notifier.matches());
+
+        template.sendBody("direct:cake", "Camel");
+        assertEquals(true, notifier.matches());
+
+        // should keep being true
+        template.sendBody("direct:foo", "Damn World");
+        assertEquals(true, notifier.matches());
+    }
+
+    public void testWhenExactBodiesReceived() throws Exception {
+        NotifierBuilder notifier = new NotifierBuilder(context)
+                .whenExactBodiesReceived("Hi World", "Hello World")
+                .create();
+
+        assertEquals(false, notifier.matches());
+
+        template.sendBody("direct:foo", "Hi World");
+        assertEquals(false, notifier.matches());
+
+        template.sendBody("direct:foo", "Hello World");
+        assertEquals(true, notifier.matches());
+
+        // should not keep being true
+        template.sendBody("direct:foo", "Bye World");
+        assertEquals(false, notifier.matches());
+
+        template.sendBody("direct:foo", "Damn World");
+        assertEquals(false, notifier.matches());
+    }
+
+    public void testWhenExactBodiesDone() throws Exception {
+        NotifierBuilder notifier = new NotifierBuilder(context)
+                .whenExactBodiesDone("Bye World", "Bye Camel")
+                .create();
+
+        assertEquals(false, notifier.matches());
+
+        template.requestBody("direct:cake", "World");
+        assertEquals(false, notifier.matches());
+
+        template.sendBody("direct:cake", "Camel");
+        assertEquals(true, notifier.matches());
+
+        // should NOT keep being true
+        template.sendBody("direct:foo", "Damn World");
+        assertEquals(false, notifier.matches());
+    }
+
     public void testWhenReceivedSatisfied() throws Exception {
         // lets use a mock to set the expressions as it got many great assertions for that
         // notice we use mock:assert which does NOT exist in the route, its just a pseudo
name
@@ -562,7 +674,7 @@
 
                 from("seda:cheese").delay(3000).to("mock:cheese");
 
-                from("direct:cake").transform(body().prepend("Bye "));
+                from("direct:cake").transform(body().prepend("Bye ")).to("log:cake");
             }
         };
     }



Mime
View raw message