camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1296769 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/builder/ camel-core/src/main/java/org/apache/camel/impl/ camel-core/src/test/java/org/apache/camel/processor/interceptor/ components/camel-test/src/main/java/org/apache/c...
Date Sun, 04 Mar 2012 08:48:25 GMT
Author: davsclaus
Date: Sun Mar  4 08:48:25 2012
New Revision: 1296769

URL: http://svn.apache.org/viewvc?rev=1296769&view=rev
Log:
CAMEL-5059: Added mockEndpointsAndSkip to advice with and test kit.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithMockEndpointsWithSkipTest.java
      - copied, changed from r1296761, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithMockEndpointsTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithMockMultipleEndpointsWithSkipTest.java
    camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/IsMockEndpointsAndSkipJUnit4Test.java
      - copied, changed from r1296761, camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/IsMockEndpointsJUnit4Test.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/InterceptSendToMockEndpointStrategy.java
    camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
    camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/CamelTestSupport.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java?rev=1296769&r1=1296768&r2=1296769&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java
Sun Mar  4 08:48:25 2012
@@ -80,12 +80,27 @@ public abstract class AdviceWithRouteBui
     /**
      * Mock all endpoints matching the given pattern.
      *
-     * @param pattern the pattern.
+     * @param pattern the pattern(s).
      * @throws Exception can be thrown if error occurred
      * @see org.apache.camel.util.EndpointHelper#matchEndpoint(String, String)
      */
-    public void mockEndpoints(String pattern) throws Exception {
-        getContext().addRegisterEndpointCallback(new InterceptSendToMockEndpointStrategy(pattern));
+    public void mockEndpoints(String... pattern) throws Exception {
+        for (String s : pattern) {
+            getContext().addRegisterEndpointCallback(new InterceptSendToMockEndpointStrategy(s));
+        }
+    }
+
+    /**
+     * Mock all endpoints matching the given pattern, and <b>skips</b> sending
to the original endpoint (detour messages).
+     *
+     * @param pattern the pattern(s).
+     * @throws Exception can be thrown if error occurred
+     * @see org.apache.camel.util.EndpointHelper#matchEndpoint(String, String)
+     */
+    public void mockEndpointsAndSkip(String... pattern) throws Exception {
+        for (String s : pattern) {
+            getContext().addRegisterEndpointCallback(new InterceptSendToMockEndpointStrategy(s,
true));
+        }
     }
 
     /**

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/InterceptSendToMockEndpointStrategy.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/InterceptSendToMockEndpointStrategy.java?rev=1296769&r1=1296768&r2=1296769&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/InterceptSendToMockEndpointStrategy.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/InterceptSendToMockEndpointStrategy.java
Sun Mar  4 08:48:25 2012
@@ -39,6 +39,7 @@ public class InterceptSendToMockEndpoint
 
     private static final Logger LOG = LoggerFactory.getLogger(InterceptSendToMockEndpointStrategy.class);
     private final String pattern;
+    private boolean skip;
 
     /**
      * Mock all endpoints.
@@ -54,7 +55,19 @@ public class InterceptSendToMockEndpoint
      * @see EndpointHelper#matchEndpoint(String, String)
      */
     public InterceptSendToMockEndpointStrategy(String pattern) {
+        this(pattern, false);
+    }
+
+    /**
+     * Mock endpoints based on the given pattern.
+     *
+     * @param pattern the pattern.
+     * @param skip <tt>true</tt> to skip sending after the detour to the original
endpoint
+     * @see EndpointHelper#matchEndpoint(String, String)
+     */
+    public InterceptSendToMockEndpointStrategy(String pattern, boolean skip) {
         this.pattern = pattern;
+        this.skip = skip;
     }
 
     public Endpoint registerEndpoint(String uri, Endpoint endpoint) {
@@ -69,7 +82,7 @@ public class InterceptSendToMockEndpoint
 
             // only proxy if the uri is matched decorate endpoint with our proxy
             // should be false by default
-            InterceptSendToEndpoint proxy = new InterceptSendToEndpoint(endpoint, false);
+            InterceptSendToEndpoint proxy = new InterceptSendToEndpoint(endpoint, skip);
 
             // create mock endpoint which we will use as interceptor
             // replace :// from scheme to make it easy to lookup the mock endpoint without
having double :// in uri

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithMockEndpointsWithSkipTest.java
(from r1296761, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithMockEndpointsTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithMockEndpointsWithSkipTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithMockEndpointsWithSkipTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithMockEndpointsTest.java&r1=1296761&r2=1296769&rev=1296769&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithMockEndpointsTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithMockEndpointsWithSkipTest.java
Sun Mar  4 08:48:25 2012
@@ -19,96 +19,47 @@ package org.apache.camel.processor.inter
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.builder.AdviceWithRouteBuilder;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.seda.SedaEndpoint;
 
 /**
  * @version 
  */
-public class AdviceWithMockEndpointsTest extends ContextTestSupport {
-
-    public void testNoAdvised() throws Exception {
-        getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
-
-        template.sendBody("direct:start", "Hello World");
-
-        assertMockEndpointsSatisfied();
-    }
+public class AdviceWithMockEndpointsWithSkipTest extends ContextTestSupport {
 
     // START SNIPPET: e1
-    public void testAdvisedMockEndpoints() throws Exception {
+    public void testAdvisedMockEndpointsWithSkip() throws Exception {
         // advice the first route using the inlined AdviceWith route builder
         // which has extended capabilities than the regular route builder
         context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder()
{
             @Override
             public void configure() throws Exception {
-                // mock all endpoints
-                mockEndpoints();
+                // mock sending to direct:foo and skip send to it
+                mockEndpointsAndSkip("direct:foo");
             }
         });
 
-        getMockEndpoint("mock:direct:start").expectedBodiesReceived("Hello World");
-        getMockEndpoint("mock:direct:foo").expectedBodiesReceived("Hello World");
-        getMockEndpoint("mock:log:foo").expectedBodiesReceived("Bye World");
-        getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
+        getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
+        getMockEndpoint("mock:direct:foo").expectedMessageCount(1);
 
         template.sendBody("direct:start", "Hello World");
 
         assertMockEndpointsSatisfied();
 
-        // additional test to ensure correct endpoints in registry
-        assertNotNull(context.hasEndpoint("direct:start"));
-        assertNotNull(context.hasEndpoint("direct:foo"));
-        assertNotNull(context.hasEndpoint("log:foo"));
-        assertNotNull(context.hasEndpoint("mock:result"));
-        // all the endpoints was mocked
-        assertNotNull(context.hasEndpoint("mock:direct:start"));
-        assertNotNull(context.hasEndpoint("mock:direct:foo"));
-        assertNotNull(context.hasEndpoint("mock:log:foo"));
+        // the message was not send to the direct:foo route and thus not sent to the seda
endpoint
+        SedaEndpoint seda = context.getEndpoint("seda:foo", SedaEndpoint.class);
+        assertEquals(0, seda.getCurrentQueueSize());
     }
     // END SNIPPET: e1
 
-    // START SNIPPET: e2
-    public void testAdvisedMockEndpointsWithPattern() throws Exception {
-        // advice the first route using the inlined AdviceWith route builder
-        // which has extended capabilities than the regular route builder
-        context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder()
{
-            @Override
-            public void configure() throws Exception {
-                // mock only log endpoints
-                mockEndpoints("log*");
-            }
-        });
-
-        // now we can refer to log:foo as a mock and set our expectations
-        getMockEndpoint("mock:log:foo").expectedBodiesReceived("Bye World");
-
-        getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
-
-        template.sendBody("direct:start", "Hello World");
-
-        assertMockEndpointsSatisfied();
-
-        // additional test to ensure correct endpoints in registry
-        assertNotNull(context.hasEndpoint("direct:start"));
-        assertNotNull(context.hasEndpoint("direct:foo"));
-        assertNotNull(context.hasEndpoint("log:foo"));
-        assertNotNull(context.hasEndpoint("mock:result"));
-        // only the log:foo endpoint was mocked
-        assertNotNull(context.hasEndpoint("mock:log:foo"));
-        assertNull(context.hasEndpoint("mock:direct:start"));
-        assertNull(context.hasEndpoint("mock:direct:foo"));
-    }
-    // END SNIPPET: e2
-
-
     // START SNIPPET: route
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("direct:start").to("direct:foo").to("log:foo").to("mock:result");
+                from("direct:start").to("direct:foo").to("mock:result");
 
-                from("direct:foo").transform(constant("Bye World"));
+                from("direct:foo").transform(constant("Bye World")).to("seda:foo");
             }
         };
     }

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithMockMultipleEndpointsWithSkipTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithMockMultipleEndpointsWithSkipTest.java?rev=1296769&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithMockMultipleEndpointsWithSkipTest.java
(added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithMockMultipleEndpointsWithSkipTest.java
Sun Mar  4 08:48:25 2012
@@ -0,0 +1,69 @@
+/**
+ * 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.processor.interceptor;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.AdviceWithRouteBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.seda.SedaEndpoint;
+
+/**
+ * @version 
+ */
+public class AdviceWithMockMultipleEndpointsWithSkipTest extends ContextTestSupport {
+
+    // START SNIPPET: e1
+    public void testAdvisedMockEndpointsWithSkip() throws Exception {
+        // advice the first route using the inlined AdviceWith route builder
+        // which has extended capabilities than the regular route builder
+        context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder()
{
+            @Override
+            public void configure() throws Exception {
+                // mock sending to direct:foo and direct:bar and skip send to it
+                mockEndpointsAndSkip("direct:foo", "direct:bar");
+            }
+        });
+
+        getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
+        getMockEndpoint("mock:direct:foo").expectedMessageCount(1);
+        getMockEndpoint("mock:direct:bar").expectedMessageCount(1);
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+
+        // the message was not send to the direct:foo route and thus not sent to the seda
endpoint
+        SedaEndpoint seda = context.getEndpoint("seda:foo", SedaEndpoint.class);
+        assertEquals(0, seda.getCurrentQueueSize());
+    }
+    // END SNIPPET: e1
+
+    // START SNIPPET: route
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").to("direct:foo").to("direct:bar").to("mock:result");
+
+                from("direct:foo").transform(constant("Bye World")).to("seda:foo");
+                from("direct:bar").transform(constant("Hi World")).to("seda:foo");
+            }
+        };
+    }
+    // END SNIPPET: route
+}

Modified: camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java?rev=1296769&r1=1296768&r2=1296769&view=diff
==============================================================================
--- camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
(original)
+++ camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
Sun Mar  4 08:48:25 2012
@@ -142,6 +142,18 @@ public abstract class CamelTestSupport e
     }
 
     /**
+     * Override to enable auto mocking endpoints based on the pattern, and <b>skip</b>
sending
+     * to original endpoint.
+     * <p/>
+     * Return <tt>*</tt> to mock all endpoints.
+     *
+     * @see org.apache.camel.util.EndpointHelper#matchEndpoint(String, String)
+     */
+    public String isMockEndpointsAndSkip() {
+        return null;
+    }
+
+    /**
      * Override to enable debugger
      * <p/>
      * Is default <tt>false</tt>
@@ -259,6 +271,10 @@ public abstract class CamelTestSupport e
         if (pattern != null) {
             context.addRegisterEndpointCallback(new InterceptSendToMockEndpointStrategy(pattern));
         }
+        pattern = isMockEndpointsAndSkip();
+        if (pattern != null) {
+            context.addRegisterEndpointCallback(new InterceptSendToMockEndpointStrategy(pattern,
true));
+        }
 
         postProcessTest();
         

Copied: camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/IsMockEndpointsAndSkipJUnit4Test.java
(from r1296761, camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/IsMockEndpointsJUnit4Test.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/IsMockEndpointsAndSkipJUnit4Test.java?p2=camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/IsMockEndpointsAndSkipJUnit4Test.java&p1=camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/IsMockEndpointsJUnit4Test.java&r1=1296761&r2=1296769&rev=1296769&view=diff
==============================================================================
--- camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/IsMockEndpointsJUnit4Test.java
(original)
+++ camel/trunk/components/camel-test/src/test/java/org/apache/camel/test/patterns/IsMockEndpointsAndSkipJUnit4Test.java
Sun Mar  4 08:48:25 2012
@@ -17,6 +17,7 @@
 package org.apache.camel.test.patterns;
 
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.seda.SedaEndpoint;
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.junit.Test;
 
@@ -24,36 +25,28 @@ import org.junit.Test;
  * @version 
  */
 // START SNIPPET: e1
-public class IsMockEndpointsJUnit4Test extends CamelTestSupport {
+public class IsMockEndpointsAndSkipJUnit4Test extends CamelTestSupport {
 
     @Override
-    public String isMockEndpoints() {
-        // override this method and return the pattern for which endpoints to mock.
-        // use * to indicate all
-        return "*";
+    public String isMockEndpointsAndSkip() {
+        // override this method and return the pattern for which endpoints to mock,
+        // and skip sending to the original endpoint.
+        return "direct:foo";
     }
 
     @Test
-    public void testMockAllEndpoints() throws Exception {
-        // notice we have automatic mocked all endpoints and the name of the endpoints is
"mock:uri"
-        getMockEndpoint("mock:direct:start").expectedBodiesReceived("Hello World");
-        getMockEndpoint("mock:direct:foo").expectedBodiesReceived("Hello World");
-        getMockEndpoint("mock:log:foo").expectedBodiesReceived("Bye World");
-        getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
+    public void testMockEndpointAndSkip() throws Exception {
+        // notice we have automatic mocked the direct:foo endpoints and the name of the endpoints
is "mock:uri"
+        getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
+        getMockEndpoint("mock:direct:foo").expectedMessageCount(1);
 
         template.sendBody("direct:start", "Hello World");
 
         assertMockEndpointsSatisfied();
 
-        // additional test to ensure correct endpoints in registry
-        assertNotNull(context.hasEndpoint("direct:start"));
-        assertNotNull(context.hasEndpoint("direct:foo"));
-        assertNotNull(context.hasEndpoint("log:foo"));
-        assertNotNull(context.hasEndpoint("mock:result"));
-        // all the endpoints was mocked
-        assertNotNull(context.hasEndpoint("mock:direct:start"));
-        assertNotNull(context.hasEndpoint("mock:direct:foo"));
-        assertNotNull(context.hasEndpoint("mock:log:foo"));
+        // the message was not send to the direct:foo route and thus not sent to the seda
endpoint
+        SedaEndpoint seda = context.getEndpoint("seda:foo", SedaEndpoint.class);
+        assertEquals(0, seda.getCurrentQueueSize());
     }
 
     @Override
@@ -61,9 +54,9 @@ public class IsMockEndpointsJUnit4Test e
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("direct:start").to("direct:foo").to("log:foo").to("mock:result");
+                from("direct:start").to("direct:foo").to("mock:result");
 
-                from("direct:foo").transform(constant("Bye World"));
+                from("direct:foo").transform(constant("Bye World")).to("seda:foo");
             }
         };
     }

Modified: camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/CamelTestSupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/CamelTestSupport.java?rev=1296769&r1=1296768&r2=1296769&view=diff
==============================================================================
--- camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/CamelTestSupport.java
(original)
+++ camel/trunk/components/camel-testng/src/main/java/org/apache/camel/testng/CamelTestSupport.java
Sun Mar  4 08:48:25 2012
@@ -140,6 +140,18 @@ public abstract class CamelTestSupport e
     }
 
     /**
+     * Override to enable auto mocking endpoints based on the pattern, and <b>skip</b>
sending
+     * to original endpoint.
+     * <p/>
+     * Return <tt>*</tt> to mock all endpoints.
+     *
+     * @see org.apache.camel.util.EndpointHelper#matchEndpoint(String, String)
+     */
+    public String isMockEndpointsAndSkip() {
+        return null;
+    }
+
+    /**
      * Override to enable debugger
      * <p/>
      * Is default <tt>false</tt>
@@ -262,6 +274,10 @@ public abstract class CamelTestSupport e
         if (pattern != null) {
             context.addRegisterEndpointCallback(new InterceptSendToMockEndpointStrategy(pattern));
         }
+        pattern = isMockEndpointsAndSkip();
+        if (pattern != null) {
+            context.addRegisterEndpointCallback(new InterceptSendToMockEndpointStrategy(pattern,
true));
+        }
 
         postProcessTest();
 



Mime
View raw message