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/01/14 15:08:23 UTC

svn commit: r899206 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/builder/NotifierBuilder.java test/java/org/apache/camel/builder/NotifierBuilderTest.java

Author: davsclaus
Date: Thu Jan 14 14:08:22 2010
New Revision: 899206

URL: http://svn.apache.org/viewvc?rev=899206&view=rev
Log:
CAMEL-2357: when using mocks you can not decide whether its when received or done for more fine grainend control.

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=899206&r1=899205&r2=899206&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:08:22 2010
@@ -410,7 +410,8 @@
     }
 
     /**
-     * Sets a condition when the provided mock is satisfied.
+     * Sets a condition when the provided mock is satisfied based on {@link Exchange}
+     * being sent to it when they are <b>done</b>.
      * <p/>
      * The idea is that you can use Mock for setting fine grained expectations
      * and then use that together with this builder. The mock provided does <b>NOT</b>
@@ -421,22 +422,57 @@
      * @param mock the mock
      * @return the builder
      */
-    public NotifierBuilder whenSatisfied(final MockEndpoint mock) {
+    public NotifierBuilder whenDoneSatisfied(final MockEndpoint mock) {
+        return doWhenSatisfied(mock, false);
+    }
+
+    /**
+     * Sets a condition when the provided mock is satisfied based on {@link Exchange}
+     * being sent to it when they are <b>received</b>.
+     * <p/>
+     * The idea is that you can use Mock for setting fine grained expectations
+     * and then use that together with this builder. The mock provided does <b>NOT</b>
+     * have to already exist in the route. You can just create a new pseudo mock
+     * and this builder will send the done {@link Exchange} to it. So its like
+     * adding the mock to the end of your route(s).
+     *
+     * @param mock the mock
+     * @return the builder
+     */
+    public NotifierBuilder whenReceivedSatisfied(final MockEndpoint mock) {
+        return doWhenSatisfied(mock, true);
+    }
+
+    private NotifierBuilder doWhenSatisfied(final MockEndpoint mock, final boolean received) {
         stack.push(new EventPredicateSupport() {
 
             private Producer producer;
 
             @Override
+            public boolean onExchangeCreated(Exchange exchange) {
+                if (received) {
+                    sendToMock(exchange);
+                }
+                return true;
+            }
+
+            @Override
             public boolean onExchangeFailure(Exchange exchange) {
-                return sendToMock(exchange);
+                if (!received) {
+                    sendToMock(exchange);
+                }
+                return true;
             }
 
             @Override
             public boolean onExchangeCompleted(Exchange exchange) {
-                return sendToMock(exchange);
+                if (!received) {
+                    sendToMock(exchange);
+                }
+                return true;
             }
 
-            private boolean sendToMock(Exchange exchange) {
+            private void sendToMock(Exchange exchange) {
                 // send the exchange when its completed to the mock
                 try {
                     if (producer == null) {
@@ -446,7 +482,6 @@
                 } catch (Exception e) {
                     throw ObjectHelper.wrapRuntimeCamelException(e);
                 }
-                return true;
             }
 
             public boolean matches() {
@@ -459,14 +494,19 @@
 
             @Override
             public String toString() {
-                return "whenSatisfied(" + mock + ")";
+                if (received) {
+                    return "whenReceivedSatisfied(" + mock + ")";
+                } else {
+                    return "whenDoneSatisfied(" + mock + ")";
+                }
             }
         });
         return this;
     }
 
     /**
-     * Sets a condition when the provided mock is <b>not</b> satisfied.
+     * Sets a condition when the provided mock is <b>not</b> satisfied based on {@link Exchange}
+     * being sent to it when they are <b>received</b>.
      * <p/>
      * The idea is that you can use Mock for setting fine grained expectations
      * and then use that together with this builder. The mock provided does <b>NOT</b>
@@ -477,22 +517,57 @@
      * @param mock the mock
      * @return the builder
      */
-    public NotifierBuilder whenNotSatisfied(final MockEndpoint mock) {
+    public NotifierBuilder whenReceivedNotSatisfied(final MockEndpoint mock) {
+        return doWhenNotSatisfied(mock, true);
+    }
+
+    /**
+     * Sets a condition when the provided mock is <b>not</b> satisfied based on {@link Exchange}
+     * being sent to it when they are <b>done</b>.
+     * <p/>
+     * The idea is that you can use Mock for setting fine grained expectations
+     * and then use that together with this builder. The mock provided does <b>NOT</b>
+     * have to already exist in the route. You can just create a new pseudo mock
+     * and this builder will send the done {@link Exchange} to it. So its like
+     * adding the mock to the end of your route(s).
+     *
+     * @param mock the mock
+     * @return the builder
+     */
+    public NotifierBuilder whenDoneNotSatisfied(final MockEndpoint mock) {
+        return doWhenNotSatisfied(mock, false);
+    }
+
+    private NotifierBuilder doWhenNotSatisfied(final MockEndpoint mock, final boolean received) {
         stack.push(new EventPredicateSupport() {
 
             private Producer producer;
 
             @Override
+            public boolean onExchangeCreated(Exchange exchange) {
+                if (received) {
+                    sendToMock(exchange);
+                }
+                return true;
+            }
+
+            @Override
             public boolean onExchangeFailure(Exchange exchange) {
-                return sendToMock(exchange);
+                if (!received) {
+                    sendToMock(exchange);
+                }
+                return true;
             }
 
             @Override
             public boolean onExchangeCompleted(Exchange exchange) {
-                return sendToMock(exchange);
+                if (!received) {
+                    sendToMock(exchange);
+                }
+                return true;
             }
 
-            private boolean sendToMock(Exchange exchange) {
+            private void sendToMock(Exchange exchange) {
                 // send the exchange when its completed to the mock
                 try {
                     if (producer == null) {
@@ -502,7 +577,6 @@
                 } catch (Exception e) {
                     throw ObjectHelper.wrapRuntimeCamelException(e);
                 }
-                return true;
             }
 
             public boolean matches() {
@@ -515,7 +589,11 @@
 
             @Override
             public String toString() {
-                return "whenNotSatisfied(" + mock + ")";
+                if (received) {
+                    return "whenReceivedNotSatisfied(" + mock + ")";
+                } else {
+                    return "whenDoneNotSatisfied(" + mock + ")";
+                }
             }
         });
         return this;

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=899206&r1=899205&r2=899206&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:08:22 2010
@@ -401,14 +401,14 @@
         assertEquals(false, notifier.matches());
     }
 
-    public void testWhenSatisfied() throws Exception {
+    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
         MockEndpoint mock = getMockEndpoint("mock:assert");
         mock.expectedBodiesReceivedInAnyOrder("Hello World", "Bye World", "Hi World");
 
         NotifierBuilder notifier = new NotifierBuilder(context)
-                .from("direct:foo").whenSatisfied(mock)
+                .from("direct:foo").whenDoneSatisfied(mock)
                 .create();
 
         assertEquals(false, notifier.matches());
@@ -427,7 +427,7 @@
         assertEquals(true, notifier.matches());
     }
 
-    public void testWhenNotSatisfied() throws Exception {
+    public void testWhenReceivedNotSatisfied() 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
         MockEndpoint mock = getMockEndpoint("mock:assert");
@@ -435,7 +435,7 @@
         mock.message(1).body().contains("Camel");
 
         NotifierBuilder notifier = new NotifierBuilder(context)
-                .from("direct:foo").whenNotSatisfied(mock)
+                .from("direct:foo").whenReceivedNotSatisfied(mock)
                 .create();
 
         // is always false to start with
@@ -456,7 +456,7 @@
         mock.message(1).body().contains("Camel");
 
         NotifierBuilder notifier = new NotifierBuilder(context)
-                .from("direct:foo").whenSatisfied(mock)
+                .from("direct:foo").whenReceivedSatisfied(mock)
                 .create();
 
         assertEquals(false, notifier.matches());
@@ -473,7 +473,7 @@
         mock.expectedBodiesReceivedInAnyOrder("Hello World", "Bye World", "Hi World");
 
         NotifierBuilder notifier = new NotifierBuilder(context)
-                .from("direct:foo").whenSatisfied(mock)
+                .from("direct:foo").whenReceivedSatisfied(mock)
                 .and().from("direct:bar").whenExactlyDone(5).whenAnyReceivedMatches(body().contains("Camel"))
                 .create();
 
@@ -501,6 +501,54 @@
         assertEquals(true, notifier.matches());
     }
 
+    public void testWhenDoneSatisfied() 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
+        MockEndpoint mock = getMockEndpoint("mock:assert");
+        mock.expectedBodiesReceived("Bye World", "Bye Camel");
+
+        NotifierBuilder notifier = new NotifierBuilder(context)
+                .whenDoneSatisfied(mock)
+                .create();
+
+        // is always false to start with
+        assertEquals(false, notifier.matches());
+
+        template.requestBody("direct:cake", "World");
+        assertEquals(false, notifier.matches());
+
+        template.requestBody("direct:cake", "Camel");
+        assertEquals(true, notifier.matches());
+
+        template.requestBody("direct:cake", "Damn");
+        // will still be true as the mock has been completed
+        assertEquals(true, notifier.matches());
+    }
+
+    public void testWhenDoneNotSatisfied() 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
+        MockEndpoint mock = getMockEndpoint("mock:assert");
+        mock.expectedBodiesReceived("Bye World", "Bye Camel");
+
+        NotifierBuilder notifier = new NotifierBuilder(context)
+                .whenDoneNotSatisfied(mock)
+                .create();
+
+        // is always false to start with
+        assertEquals(false, notifier.matches());
+
+        template.requestBody("direct:cake", "World");
+        assertEquals(true, notifier.matches());
+
+        template.requestBody("direct:cake", "Camel");
+        assertEquals(false, notifier.matches());
+
+        template.requestBody("direct:cake", "Damn");
+        // will still be false as the mock has been completed
+        assertEquals(false, notifier.matches());
+    }
+
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
@@ -513,6 +561,8 @@
                 from("direct:fail").throwException(new IllegalArgumentException("Damn"));
 
                 from("seda:cheese").delay(3000).to("mock:cheese");
+
+                from("direct:cake").transform(body().prepend("Bye "));
             }
         };
     }