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 2014/01/28 12:38:04 UTC

[2/2] git commit: CAMEL-7154: Allow end users more easily to extend InterceptSendToMockEndpointStrategy.

CAMEL-7154: Allow end users more easily to extend InterceptSendToMockEndpointStrategy.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/0ab8d62e
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/0ab8d62e
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/0ab8d62e

Branch: refs/heads/camel-2.12.x
Commit: 0ab8d62e765c31af7f7c0d94cf1a69bddda9e44c
Parents: 78d7d05
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Jan 28 12:28:28 2014 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Jan 28 12:38:41 2014 +0100

----------------------------------------------------------------------
 .../InterceptSendToMockEndpointStrategy.java    | 34 ++++++++-
 ...eptSendToMockEndpointStrategyCustomTest.java | 79 ++++++++++++++++++++
 2 files changed, 110 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/0ab8d62e/camel-core/src/main/java/org/apache/camel/impl/InterceptSendToMockEndpointStrategy.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/impl/InterceptSendToMockEndpointStrategy.java b/camel-core/src/main/java/org/apache/camel/impl/InterceptSendToMockEndpointStrategy.java
index 2de9eff..cdd0cfd 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/InterceptSendToMockEndpointStrategy.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/InterceptSendToMockEndpointStrategy.java
@@ -17,7 +17,7 @@
 package org.apache.camel.impl;
 
 import org.apache.camel.Endpoint;
-import org.apache.camel.Processor;
+import org.apache.camel.Producer;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.spi.EndpointStrategy;
 import org.apache.camel.util.EndpointHelper;
@@ -77,7 +77,7 @@ public class InterceptSendToMockEndpointStrategy implements EndpointStrategy {
         } else if (endpoint instanceof MockEndpoint) {
             // we should not intercept mock endpoints
             return endpoint;
-        } else if (uri == null || pattern == null || EndpointHelper.matchEndpoint(endpoint.getCamelContext(), uri, pattern)) {
+        } else if (matchPattern(uri, endpoint, pattern)) {
             // if pattern is null then it mean to match all
 
             // only proxy if the uri is matched decorate endpoint with our proxy
@@ -94,14 +94,17 @@ public class InterceptSendToMockEndpointStrategy implements EndpointStrategy {
             LOG.info("Adviced endpoint [" + uri + "] with mock endpoint [" + key + "]");
 
             MockEndpoint mock = endpoint.getCamelContext().getEndpoint(key, MockEndpoint.class);
-            Processor producer;
+            Producer producer;
             try {
                 producer = mock.createProducer();
             } catch (Exception e) {
                 throw wrapRuntimeCamelException(e);
             }
 
+            // allow custom logic
+            producer = onInterceptEndpoint(uri, endpoint, mock, producer);
             proxy.setDetour(producer);
+
             return proxy;
         } else {
             // no proxy so return regular endpoint
@@ -109,6 +112,31 @@ public class InterceptSendToMockEndpointStrategy implements EndpointStrategy {
         }
     }
 
+    /**
+     * Does the pattern match the endpoint?
+     *
+     * @param uri          the uri
+     * @param endpoint     the endpoint
+     * @param pattern      the pattern
+     * @return <tt>true</tt> to match and therefore intercept, <tt>false</tt> if not matched and should not intercept
+     */
+    protected boolean matchPattern(String uri, Endpoint endpoint, String pattern) {
+        return uri == null || pattern == null || EndpointHelper.matchEndpoint(endpoint.getCamelContext(), uri, pattern);
+    }
+
+    /**
+     * Callback when an endpoint was intercepted with the given mock endpoint
+     *
+     * @param uri          the uri
+     * @param endpoint     the endpoint
+     * @param mockEndpoint the mocked endpoint
+     * @param mockProducer the mock producer
+     * @return the mock producer
+     */
+    protected Producer onInterceptEndpoint(String uri, Endpoint endpoint, MockEndpoint mockEndpoint, Producer mockProducer) {
+        return mockProducer;
+    }
+
     @Override
     public String toString() {
         return "InterceptSendToMockEndpointStrategy";

http://git-wip-us.apache.org/repos/asf/camel/blob/0ab8d62e/camel-core/src/test/java/org/apache/camel/impl/InterceptSendToMockEndpointStrategyCustomTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/impl/InterceptSendToMockEndpointStrategyCustomTest.java b/camel-core/src/test/java/org/apache/camel/impl/InterceptSendToMockEndpointStrategyCustomTest.java
new file mode 100644
index 0000000..9017314
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/impl/InterceptSendToMockEndpointStrategyCustomTest.java
@@ -0,0 +1,79 @@
+/**
+ * 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.impl;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Producer;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * @version 
+ */
+public class InterceptSendToMockEndpointStrategyCustomTest extends ContextTestSupport {
+
+    private static boolean called;
+
+    private class MyStrategy extends InterceptSendToMockEndpointStrategy {
+
+        @Override
+        protected Producer onInterceptEndpoint(String uri, Endpoint endpoint, MockEndpoint mockEndpoint, Producer mockProducer) {
+            called = true;
+            return mockProducer;
+        }
+    }
+
+    public void testAdvisedMockEndpoints() throws Exception {
+        context.addRegisterEndpointCallback(new MyStrategy());
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").to("direct:foo").to("log:foo").to("mock:result");
+
+                from("direct:foo").transform(constant("Bye World"));
+            }
+        });
+        context.start();
+
+        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");
+
+        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"));
+
+        assertTrue("Should be called", called);
+    }
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+}