Author: davsclaus
Date: Sun Aug 14 11:51:04 2011
New Revision: 1157519

URL: http://svn.apache.org/viewvc?rev=1157519&view=rev
Log:
CAMEL-4333: Added wereSentTo to NotifyBuilder.

Modified:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/NotifyBuilder.java
    
camel/trunk/camel-core/src/test/java/org/apache/camel/builder/NotifyBuilderTest.java

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/NotifyBuilder.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/NotifyBuilder.java?rev=1157519&r1=1157518&r2=1157519&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/NotifyBuilder.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/NotifyBuilder.java
 Sun Aug 14 11:51:04 2011
@@ -37,6 +37,7 @@ import org.apache.camel.management.Event
 import org.apache.camel.management.event.ExchangeCompletedEvent;
 import org.apache.camel.management.event.ExchangeCreatedEvent;
 import org.apache.camel.management.event.ExchangeFailedEvent;
+import org.apache.camel.management.event.ExchangeSentEvent;
 import org.apache.camel.spi.EventNotifier;
 import org.apache.camel.util.EndpointHelper;
 import org.apache.camel.util.ObjectHelper;
@@ -157,8 +158,8 @@ public class NotifyBuilder {
             @Override
             public boolean onExchange(Exchange exchange) {
                 // always accept direct endpoints as they are a special case 
as it will create the UoW beforehand
-                // and just continue to route that on the consumer side, which 
causes the EventNotifer not to
-                // emit events when the consumer received the exchange, as its 
alreay done. For example by
+                // and just continue to route that on the consumer side, which 
causes the EventNotifier not to
+                // emit events when the consumer received the exchange, as its 
already done. For example by
                 // ProducerTemplate which creates the UoW before producing 
messages.
                 if (exchange.getFromEndpoint() != null && 
exchange.getFromEndpoint() instanceof DirectEndpoint) {
                     return true;
@@ -235,6 +236,47 @@ public class NotifyBuilder {
     }
 
     /**
+     * Optionally a <tt>sent to</tt> endpoint which means that this expression 
should only be based
+     * on {@link Exchange} which has been sent to the given endpoint uri.
+     * <p/>
+     * Notice the {@link Exchange} may have been sent to other endpoints as 
well. This condition will match
+     * if the {@link Exchange} has been sent at least once to the given 
endpoint.
+     *
+     * @param endpointUri uri of endpoint or pattern (see the EndpointHelper 
javadoc)
+     * @return the builder
+     * @see org.apache.camel.util.EndpointHelper#matchEndpoint(String, String)
+     */
+    public NotifyBuilder wereSentTo(final String endpointUri) {
+        stack.push(new EventPredicateSupport() {
+            private boolean matches;
+
+            @Override
+            public boolean onExchangeSent(Exchange exchange, Endpoint 
endpoint, long timeTaken) {
+                if (EndpointHelper.matchEndpoint(endpoint.getEndpointUri(), 
endpointUri)) {
+                    // we should match if matching once
+                    matches = true;
+                }
+                return true;
+            }
+
+            public boolean matches() {
+                return matches;
+            }
+
+            @Override
+            public void reset() {
+                matches = false;
+            }
+
+            @Override
+            public String toString() {
+                return "wereSentTo(" + endpointUri + ")";
+            }
+        });
+        return this;
+    }
+
+    /**
      * Sets a condition when <tt>number</tt> of {@link Exchange} has been 
received.
      * <p/>
      * The number matching is <i>at least</i> based which means that if more 
messages received
@@ -1195,6 +1237,8 @@ public class NotifyBuilder {
                 onExchangeCompleted((ExchangeCompletedEvent) event);
             } else if (event instanceof ExchangeFailedEvent) {
                 onExchangeFailed((ExchangeFailedEvent) event);
+            } else if (event instanceof ExchangeSentEvent) {
+                onExchangeSent((ExchangeSentEvent) event);
             }
 
             // now compute whether we matched
@@ -1223,6 +1267,12 @@ public class NotifyBuilder {
             }
         }
 
+        private void onExchangeSent(ExchangeSentEvent event) {
+            for (EventPredicateHolder predicate : predicates) {
+                predicate.getPredicate().onExchangeSent(event.getExchange(), 
event.getEndpoint(), event.getTimeTaken());
+            }
+        }
+
         private synchronized void computeMatches() {
             // use a temporary answer until we have computed the value to 
assign
             Boolean answer = null;
@@ -1316,6 +1366,16 @@ public class NotifyBuilder {
          * @return <tt>true</tt> to allow continue evaluating, <tt>false</tt> 
to stop immediately
          */
         boolean onExchangeFailed(Exchange exchange);
+
+        /**
+         * Callback for {@link Exchange} lifecycle
+         *
+         * @param exchange the exchange
+         * @param endpoint the endpoint sent to
+         * @param timeTaken time taken in millis to send the to endpoint
+         * @return <tt>true</tt> to allow continue evaluating, <tt>false</tt> 
to stop immediately
+         */
+        boolean onExchangeSent(Exchange exchange, Endpoint endpoint, long 
timeTaken);
     }
 
     private abstract class EventPredicateSupport implements EventPredicate {
@@ -1336,6 +1396,12 @@ public class NotifyBuilder {
             return onExchange(exchange);
         }
 
+        public boolean onExchangeSent(Exchange exchange, Endpoint endpoint, 
long timeTaken) {
+            // no need to invoke onExchange as this is a special case when the 
Exchange
+            // was sent to a specific endpoint
+            return true;
+        }
+
         public boolean onExchange(Exchange exchange) {
             return true;
         }
@@ -1425,6 +1491,16 @@ public class NotifyBuilder {
         }
 
         @Override
+        public boolean onExchangeSent(Exchange exchange, Endpoint endpoint, 
long timeTaken) {
+            for (EventPredicate predicate : predicates) {
+                if (!predicate.onExchangeSent(exchange, endpoint, timeTaken)) {
+                    return false;
+                }
+            }
+            return true;
+        }
+
+        @Override
         public String toString() {
             StringBuilder sb = new StringBuilder();
             for (Iterator<EventPredicate> it = predicates.iterator(); 
it.hasNext();) {

Modified: 
camel/trunk/camel-core/src/test/java/org/apache/camel/builder/NotifyBuilderTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/NotifyBuilderTest.java?rev=1157519&r1=1157518&r2=1157519&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/builder/NotifyBuilderTest.java
 (original)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/builder/NotifyBuilderTest.java
 Sun Aug 14 11:51:04 2011
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.builder;
 
+import org.apache.camel.CamelExecutionException;
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.component.mock.MockEndpoint;
 
@@ -860,6 +861,71 @@ public class NotifyBuilderTest extends C
         assertEquals(true, notify.matches());
     }
 
+    public void testWereSentTo() throws Exception {
+        NotifyBuilder notify = new NotifyBuilder(context)
+                .wereSentTo("mock:foo")
+                .create();
+
+        template.sendBody("direct:bar", "Hello World");
+        assertEquals(false, notify.matches());
+
+        template.sendBody("direct:foo", "Bye World");
+        assertEquals(true, notify.matches());
+    }
+
+    public void testTwoWereSentTo() throws Exception {
+        // sent to both endpoints
+        NotifyBuilder notify = new NotifyBuilder(context)
+                .wereSentTo("log:beer").wereSentTo("mock:beer")
+                .create();
+
+        template.sendBody("direct:bar", "Hello World");
+        assertEquals(false, notify.matches());
+
+        template.sendBody("direct:beer", "Bye World");
+        assertEquals(true, notify.matches());
+    }
+
+    public void testTwoWereSentToRegExp() throws Exception {
+        // send to any endpoint with beer in the uri
+        NotifyBuilder notify = new NotifyBuilder(context)
+                .wereSentTo(".*beer.*")
+                .create();
+
+        template.sendBody("direct:bar", "Hello World");
+        assertEquals(false, notify.matches());
+
+        template.sendBody("direct:beer", "Bye World");
+        assertEquals(true, notify.matches());
+    }
+
+    public void testTwoWereSentToDoneAndFailed() throws Exception {
+        // we expect 2+ done messages which were sent to mock:bar
+        // and 1+ failed message which were sent to mock:fail
+        NotifyBuilder notify = new NotifyBuilder(context)
+                .whenDone(2).wereSentTo("mock:bar")
+                .and()
+                .whenFailed(1).wereSentTo("mock:fail")
+                .create();
+
+        template.sendBody("direct:bar", "Hello World");
+        assertEquals(false, notify.matches());
+
+        template.sendBody("direct:bar", "Hello World");
+        assertEquals(false, notify.matches());
+
+        template.sendBody("direct:foo", "Hello World");
+        assertEquals(false, notify.matches());
+
+        try {
+            template.sendBody("direct:fail", "Bye World");
+            fail("Should have thrown exception");
+        } catch (CamelExecutionException e) {
+            // expected
+        }
+        assertEquals(true, notify.matches());
+    }
+
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
@@ -869,7 +935,7 @@ public class NotifyBuilderTest extends C
 
                 from("direct:bar").routeId("bar").to("mock:bar");
 
-                from("direct:fail").routeId("fail").throwException(new 
IllegalArgumentException("Damn"));
+                
from("direct:fail").routeId("fail").to("mock:fail").throwException(new 
IllegalArgumentException("Damn"));
 
                 
from("direct:cake").routeId("cake").transform(body().prepend("Bye 
")).to("log:cake");
 


Reply via email to