CAMEL-6792: Added getMockEndpoint method to not auto create. Thanks to James 
Carman for the patch.


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

Branch: refs/heads/master
Commit: 41f5c83e6238e1621ddb557071db4f7bf708b965
Parents: 0cd5463
Author: Claus Ibsen <[email protected]>
Authored: Sun Sep 29 10:25:27 2013 +0200
Committer: Claus Ibsen <[email protected]>
Committed: Sun Sep 29 10:25:27 2013 +0200

----------------------------------------------------------------------
 .../camel/test/junit4/CamelTestSupport.java     | 24 +++++++++++++++++++-
 .../apache/camel/test/CamelTestSupportTest.java | 18 +++++++++++++++
 .../apache/camel/testng/CamelTestSupport.java   | 24 +++++++++++++++++++-
 3 files changed, 64 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/41f5c83e/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
----------------------------------------------------------------------
diff --git 
a/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
 
b/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
index 24e6deb..22774d9 100644
--- 
a/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
+++ 
b/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java
@@ -31,6 +31,7 @@ import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
 import org.apache.camel.Message;
+import org.apache.camel.NoSuchEndpointException;
 import org.apache.camel.Predicate;
 import org.apache.camel.Processor;
 import org.apache.camel.ProducerTemplate;
@@ -555,7 +556,28 @@ public abstract class CamelTestSupport extends TestSupport 
{
      * @return the mandatory mock endpoint or an exception is thrown if it 
could not be resolved
      */
     protected MockEndpoint getMockEndpoint(String uri) {
-        return resolveMandatoryEndpoint(uri, MockEndpoint.class);
+        return getMockEndpoint(uri, true);
+    }
+
+    /**
+     * Resolves the {@link MockEndpoint} using a URI of the form 
<code>mock:someName</code>, optionally
+     * creating it if it does not exist.
+     *
+     * @param uri      the URI which typically starts with "mock:" and has 
some name
+     * @param create   whether or not to allow the endpoint to be created if 
it doesn't exist
+     * @return the mock endpoint or an {@link NoSuchEndpointException} is 
thrown if it could not be resolved
+     * @throws NoSuchEndpointException is the mock endpoint does not exists
+     */
+    protected MockEndpoint getMockEndpoint(String uri, boolean create) throws 
NoSuchEndpointException {
+        if (create) {
+            return resolveMandatoryEndpoint(uri, MockEndpoint.class);
+        } else {
+            Endpoint endpoint = context.hasEndpoint(uri);
+            if (endpoint instanceof MockEndpoint) {
+                return (MockEndpoint) endpoint;
+            }
+            throw new NoSuchEndpointException(String.format("MockEndpoint %s 
does not exist.", uri));
+        }
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/camel/blob/41f5c83e/components/camel-test/src/test/java/org/apache/camel/test/CamelTestSupportTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-test/src/test/java/org/apache/camel/test/CamelTestSupportTest.java
 
b/components/camel-test/src/test/java/org/apache/camel/test/CamelTestSupportTest.java
index f046e90..cfa0cc5 100644
--- 
a/components/camel-test/src/test/java/org/apache/camel/test/CamelTestSupportTest.java
+++ 
b/components/camel-test/src/test/java/org/apache/camel/test/CamelTestSupportTest.java
@@ -17,7 +17,9 @@
 
 package org.apache.camel.test;
 
+import org.apache.camel.NoSuchEndpointException;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.junit.Before;
 import org.junit.Test;
@@ -40,6 +42,22 @@ public class CamelTestSupportTest extends CamelTestSupport {
         assertMockEndpointsSatisfied();
     }
 
+    @Test(expected = NoSuchEndpointException.class)
+    public void exceptionThrownWhenEndpointNotFoundAndNoCreate() {
+        getMockEndpoint("mock:bogus", false);
+    }
+
+    @Test(expected = NoSuchEndpointException.class)
+    public void exceptionThrownWhenEndpointNotAMockEndpoint() {
+        getMockEndpoint("direct:something", false);
+    }
+
+    @Test
+    public void autoCreateNoneExisting() {
+        MockEndpoint mock = getMockEndpoint("mock:bogus2", true);
+        assertNotNull(mock);
+    }
+
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {

http://git-wip-us.apache.org/repos/asf/camel/blob/41f5c83e/components/camel-testng/src/main/java/org/apache/camel/testng/CamelTestSupport.java
----------------------------------------------------------------------
diff --git 
a/components/camel-testng/src/main/java/org/apache/camel/testng/CamelTestSupport.java
 
b/components/camel-testng/src/main/java/org/apache/camel/testng/CamelTestSupport.java
index a4b2e46..33d42e2 100644
--- 
a/components/camel-testng/src/main/java/org/apache/camel/testng/CamelTestSupport.java
+++ 
b/components/camel-testng/src/main/java/org/apache/camel/testng/CamelTestSupport.java
@@ -30,6 +30,7 @@ import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
 import org.apache.camel.Message;
+import org.apache.camel.NoSuchEndpointException;
 import org.apache.camel.Predicate;
 import org.apache.camel.Processor;
 import org.apache.camel.ProducerTemplate;
@@ -512,7 +513,28 @@ public abstract class CamelTestSupport extends TestSupport 
{
      * @return the mandatory mock endpoint or an exception is thrown if it 
could not be resolved
      */
     protected MockEndpoint getMockEndpoint(String uri) {
-        return resolveMandatoryEndpoint(uri, MockEndpoint.class);
+        return getMockEndpoint(uri, true);
+    }
+
+    /**
+     * Resolves the {@link MockEndpoint} using a URI of the form 
<code>mock:someName</code>, optionally
+     * creating it if it does not exist.
+     *
+     * @param uri      the URI which typically starts with "mock:" and has 
some name
+     * @param create   whether or not to allow the endpoint to be created if 
it doesn't exist
+     * @return the mock endpoint or an {@link NoSuchEndpointException} is 
thrown if it could not be resolved
+     * @throws NoSuchEndpointException is the mock endpoint does not exists
+     */
+    protected MockEndpoint getMockEndpoint(String uri, boolean create) throws 
NoSuchEndpointException {
+        if (create) {
+            return resolveMandatoryEndpoint(uri, MockEndpoint.class);
+        } else {
+            Endpoint endpoint = context.hasEndpoint(uri);
+            if (endpoint instanceof MockEndpoint) {
+                return (MockEndpoint) endpoint;
+            }
+            throw new NoSuchEndpointException(String.format("MockEndpoint %s 
does not exist.", uri));
+        }
     }
 
     /**

Reply via email to