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/12/08 09:15:55 UTC

svn commit: r1043330 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/component/mock/ main/java/org/apache/camel/processor/ main/java/org/apache/camel/spi/ test/java/org/apache/camel/processor/intercept/

Author: davsclaus
Date: Wed Dec  8 08:15:54 2010
New Revision: 1043330

URL: http://svn.apache.org/viewvc?rev=1043330&view=rev
Log:
CAMEL-3403: Fixed interceptSendToEndpoint not checking stop DSL to prevent sending to intercepted endpoint. Fixed MockEndpoint assertions when interceptSendToEndpoint being used.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSendToEndpointWithStopTest.java
      - copied, changed from r1043324, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSendToEndpointTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/PipelineHelper.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ManagementAware.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java?rev=1043330&r1=1043329&r2=1043330&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/mock/MockEndpoint.java Wed Dec  8 08:15:54 2010
@@ -44,6 +44,7 @@ import org.apache.camel.builder.Expressi
 import org.apache.camel.builder.ProcessorBuilder;
 import org.apache.camel.impl.DefaultEndpoint;
 import org.apache.camel.impl.DefaultProducer;
+import org.apache.camel.impl.InterceptSendToEndpoint;
 import org.apache.camel.spi.BrowsableEndpoint;
 import org.apache.camel.util.CamelContextHelper;
 import org.apache.camel.util.ExpressionComparator;
@@ -147,6 +148,10 @@ public class MockEndpoint extends Defaul
         ObjectHelper.notNull(context, "camelContext");
         Collection<Endpoint> endpoints = context.getEndpoints();
         for (Endpoint endpoint : endpoints) {
+            // if the endpoint was intercepted we should get the delegate
+            if (endpoint instanceof InterceptSendToEndpoint) {
+                endpoint = ((InterceptSendToEndpoint) endpoint).getDelegate();
+            }
             if (endpoint instanceof MockEndpoint) {
                 MockEndpoint mockEndpoint = (MockEndpoint) endpoint;
                 mockEndpoint.assertIsSatisfied();

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/PipelineHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/PipelineHelper.java?rev=1043330&r1=1043329&r2=1043330&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/PipelineHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/PipelineHelper.java Wed Dec  8 08:15:54 2010
@@ -33,30 +33,30 @@ public final class PipelineHelper {
     }
 
     /**
-     * Should we continue processing the next exchange?
+     * Should we continue processing the exchange?
      *
-     * @param nextExchange the next exchange
+     * @param exchange the next exchange
      * @param message a message to use when logging that we should not continue processing
      * @param log a logger
      * @return <tt>true</tt> to continue processing, <tt>false</tt> to break out, for example if an exception occurred.
      */
-    public static boolean continueProcessing(Exchange nextExchange, String message, Log log) {
+    public static boolean continueProcessing(Exchange exchange, String message, Log log) {
         // check for error if so we should break out
-        boolean exceptionHandled = hasExceptionBeenHandledByErrorHandler(nextExchange);
-        if (nextExchange.isFailed() || nextExchange.isRollbackOnly() || exceptionHandled) {
+        boolean exceptionHandled = hasExceptionBeenHandledByErrorHandler(exchange);
+        if (exchange.isFailed() || exchange.isRollbackOnly() || exceptionHandled) {
             // The Exchange.ERRORHANDLED_HANDLED property is only set if satisfactory handling was done
             // by the error handler. It's still an exception, the exchange still failed.
             if (log.isDebugEnabled()) {
                 StringBuilder sb = new StringBuilder();
-                sb.append("Message exchange has failed " + message + " for exchange: ").append(nextExchange);
-                if (nextExchange.isRollbackOnly()) {
+                sb.append("Message exchange has failed " + message + " for exchange: ").append(exchange);
+                if (exchange.isRollbackOnly()) {
                     sb.append(" Marked as rollback only.");
                 }
-                if (nextExchange.getException() != null) {
-                    sb.append(" Exception: ").append(nextExchange.getException());
+                if (exchange.getException() != null) {
+                    sb.append(" Exception: ").append(exchange.getException());
                 }
-                if (nextExchange.hasOut() && nextExchange.getOut().isFault()) {
-                    sb.append(" Fault: ").append(nextExchange.getOut());
+                if (exchange.hasOut() && exchange.getOut().isFault()) {
+                    sb.append(" Fault: ").append(exchange.getOut());
                 }
                 if (exceptionHandled) {
                     sb.append(" Handled by the error handler.");
@@ -67,6 +67,19 @@ public final class PipelineHelper {
             return false;
         }
 
+
+        // check for stop
+        Object stop = exchange.getProperty(Exchange.ROUTE_STOP);
+        if (stop != null) {
+            boolean doStop = exchange.getContext().getTypeConverter().convertTo(Boolean.class, exchange, stop);
+            if (doStop) {
+                if (log.isDebugEnabled()) {
+                    log.debug("ExchangeId: " + exchange.getExchangeId() + " is marked to stop routing: " + exchange);
+                }
+                return false;
+            }
+        }
+
         return true;
     }
 

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ManagementAware.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ManagementAware.java?rev=1043330&r1=1043329&r2=1043330&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ManagementAware.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ManagementAware.java Wed Dec  8 08:15:54 2010
@@ -29,7 +29,8 @@ package org.apache.camel.spi;
  * and therefore can return a {@link javax.management.MBeanInfo} object.
  *
  * @version $Revision$
- * @deprecated use Spring JMX annotations
+ * @deprecated use Spring JMX annotations,
+ *             see this <a href="http://camel.apache.org/why-is-my-processor-not-showing-up-in-jconsole.html">FAQ entry</a>
  */
 @Deprecated
 public interface ManagementAware<T> {

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSendToEndpointWithStopTest.java (from r1043324, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSendToEndpointTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSendToEndpointWithStopTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSendToEndpointWithStopTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSendToEndpointTest.java&r1=1043324&r2=1043330&rev=1043330&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSendToEndpointTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSendToEndpointWithStopTest.java Wed Dec  8 08:15:54 2010
@@ -24,185 +24,45 @@ import org.apache.camel.builder.RouteBui
  * 
  * @version $Revision$
  */
-public class InterceptSendToEndpointTest extends ContextTestSupport {
+public class InterceptSendToEndpointWithStopTest extends ContextTestSupport {
 
-    @Override
-    public boolean isUseRouteBuilder() {
-        return false;
-    }
-
-    public void testInterceptEndpoint() throws Exception {
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                context.setTracing(true);
-
-                // START SNIPPET: e1
-                // we intercept by endpoint, that means that whenever an exchange is about to be sent to
-                // this endpoint, its intercepted and routed with this detour route beforehand
-                // afterwards its send to the original intended destination. So this is kinda AOP before.
-                // That means mock:foo will receive the message (Bye World).
-                interceptSendToEndpoint("mock:foo")
-                    .to("mock:detour").transform(constant("Bye World"));
-
-                from("direct:first")
-                    .to("mock:bar")
-                    .to("mock:foo")
-                    .to("mock:result");
-                // END SNIPPET: e1
-
-            }
-        });
-        context.start();
-
-        getMockEndpoint("mock:bar").expectedBodiesReceived("Hello World");
-        getMockEndpoint("mock:detour").expectedBodiesReceived("Hello World");
-        getMockEndpoint("mock:foo").expectedBodiesReceived("Bye World");
-        getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
-
-        template.sendBody("direct:first", "Hello World");
-
-        assertMockEndpointsSatisfied();
-    }
-
-    public void testInterceptEndpointWithPredicate() throws Exception {
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                // START SNIPPET: e2
-                // we can also attach a predicate to the endpoint interceptor. So in this example the exchange is
-                // only intercepted if the body is Hello World
-                interceptSendToEndpoint("mock:foo").when(body().isEqualTo("Hello World"))
-                    .to("mock:detour").transform(constant("Bye World"));
-
-                from("direct:second")
-                    .to("mock:bar")
-                    .to("mock:foo")
-                    .to("mock:result");
-                // END SNIPPET: e2
-
-            }
-        });
-        context.start();
-
-        getMockEndpoint("mock:bar").expectedBodiesReceived("Hello World", "Hi");
-        // Hi is filtered out with the predicate on the intercept
-        getMockEndpoint("mock:detour").expectedBodiesReceived("Hello World");
-        getMockEndpoint("mock:foo").expectedBodiesReceived("Bye World", "Hi");
-        getMockEndpoint("mock:result").expectedBodiesReceived("Bye World", "Hi");
+    public void testInterceptSendToEndpointWithStop() throws Exception {
+        getMockEndpoint("mock:a").expectedMessageCount(1);
+        getMockEndpoint("mock:b").expectedMessageCount(0);
+        getMockEndpoint("mock:c").expectedMessageCount(0);
+        getMockEndpoint("mock:result").expectedMessageCount(0);
 
-        template.sendBody("direct:second", "Hello World");
-        template.sendBody("direct:second", "Hi");
+        template.sendBody("direct:start", "stop");
 
         assertMockEndpointsSatisfied();
     }
 
-    public void testInterceptEndpointStop() throws Exception {
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                context.setTracing(true);
-
-                // START SNIPPET: e3
-                // since we use the skipSendToOriginalEndpoint() we instruct Camel to skip
-                // sending the exchange to the original intended destination after the intercept
-                // route is complete.
-                // That means that mock:foo will NOT receive the message, but the message
-                // is skipped and continued in the original route, so mock:result will receive
-                // the message.
-                interceptSendToEndpoint("mock:foo").skipSendToOriginalEndpoint()
-                    .transform(constant("Bye World")).to("mock:detour");
-
-                from("direct:third")
-                    .to("mock:bar")
-                    .to("mock:foo")
-                    .to("mock:result");
-                // END SNIPPET: e3
-            }
-        });
-        context.start();
-
-        getMockEndpoint("mock:bar").expectedBodiesReceived("Hello World");
-        getMockEndpoint("mock:detour").expectedBodiesReceived("Bye World");
-        // as we stop the original destination do not receive the exchange
-        getMockEndpoint("mock:foo").expectedMessageCount(0);
-        // but we continue to route afterwards
+    public void testInterceptSendToEndpointWithNoStop() throws Exception {
+        getMockEndpoint("mock:a").expectedMessageCount(1);
+        getMockEndpoint("mock:b").expectedMessageCount(1);
+        getMockEndpoint("mock:c").expectedMessageCount(1);
         getMockEndpoint("mock:result").expectedMessageCount(1);
 
-        template.sendBody("direct:third", "Hello World");
-
-        assertMockEndpointsSatisfied();
-    }
-
-    public void testInterceptEndpointDirectly() throws Exception {
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                interceptSendToEndpoint("direct:start")
-                    .to("mock:detour").transform(constant("Bye World"));
-
-                from("direct:start")
-                    .to("mock:foo")
-                    .to("mock:result");
-            }
-        });
-        context.start();
-
-        getMockEndpoint("mock:detour").expectedBodiesReceived("Hello World");
-        getMockEndpoint("mock:foo").expectedBodiesReceived("Bye World");
-        getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
-
         template.sendBody("direct:start", "Hello World");
 
         assertMockEndpointsSatisfied();
     }
 
-    public void testInterceptEndpointWithStop() throws Exception {
-        context.addRoutes(new RouteBuilder() {
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                interceptSendToEndpoint("direct:start")
-                    .to("mock:detour").stop();
+                interceptSendToEndpoint("mock:b")
+                    .choice()
+                        .when(body().isEqualTo("stop")).stop()
+                        .otherwise().to("mock:c");
 
                 from("direct:start")
-                    .to("mock:foo")
+                    .to("mock:a")
+                    .to("mock:b")
                     .to("mock:result");
             }
-        });
-        context.start();
-
-        getMockEndpoint("mock:detour").expectedMessageCount(1);
-        getMockEndpoint("mock:foo").expectedMessageCount(0);
-        getMockEndpoint("mock:result").expectedMessageCount(0);
-
-        template.sendBody("direct:start", "Hello World");
-
-        assertMockEndpointsSatisfied();
-    }
-
-    public void testInterceptEndpointOnce() throws Exception {
-        context.addRoutes(new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                interceptSendToEndpoint("direct:intercept1").to("mock:detour1");
-                interceptSendToEndpoint("direct:intercept2").to("mock:detour2");
-                
-                from("direct:input1").to("direct:intercept1");
-                from("direct:input2").to("direct:intercept2");
-
-                from("direct:intercept1").to("log:1");
-                from("direct:intercept2").to("log:2");
-            }
-        });
-        context.start();
-
-        getMockEndpoint("mock:detour1").expectedBodiesReceived("Hello World");
-        getMockEndpoint("mock:detour2").expectedBodiesReceived("Hello World");
-
-        template.sendBody("direct:input1", "Hello World");
-        template.sendBody("direct:input2", "Hello World");
-
-        assertMockEndpointsSatisfied();
+        };
     }
-    
 }