Author: davsclaus Date: Mon Feb 14 14:36:24 2011 New Revision: 1070507 URL: http://svn.apache.org/viewvc?rev=1070507&view=rev Log: CAMEL-3665: AdviceWith will now fail if using error handler, as its not supported. Fixed potential NPE in intercept from.
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/issues/AdviceWithIssueTest.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/InterceptFromDefinition.java camel/trunk/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/InterceptFromDefinition.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/InterceptFromDefinition.java?rev=1070507&r1=1070506&r2=1070507&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/model/InterceptFromDefinition.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/InterceptFromDefinition.java Mon Feb 14 14:36:24 2011 @@ -74,7 +74,11 @@ public class InterceptFromDefinition ext // this allows us to use the same header for both the interceptFrom and interceptSendToEndpoint SetHeaderDefinition headerDefinition = new SetHeaderDefinition(Exchange.INTERCEPTED_ENDPOINT, new ExpressionAdapter() { public Object evaluate(Exchange exchange, Class type) { - return exchange.getFromEndpoint().getEndpointUri(); + if (exchange.getFromEndpoint() != null) { + return exchange.getFromEndpoint().getEndpointUri(); + } else { + return null; + } } public String toString() { Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java?rev=1070507&r1=1070506&r2=1070507&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java Mon Feb 14 14:36:24 2011 @@ -203,6 +203,10 @@ public class RouteDefinition extends Pro throw new IllegalArgumentException("You can only advice from a RouteBuilder which has no existing routes." + " Remove all routes from the route builder."); } + // we can not advice with error handlers + if (routes.getErrorHandlerBuilder() != null) { + throw new IllegalArgumentException("You can not advice with error handlers. Remove the error handlers from the route builder."); + } // stop and remove this existing route camelContext.removeRouteDefinition(this); Added: camel/trunk/camel-core/src/test/java/org/apache/camel/issues/AdviceWithIssueTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/issues/AdviceWithIssueTest.java?rev=1070507&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/issues/AdviceWithIssueTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/issues/AdviceWithIssueTest.java Mon Feb 14 14:36:24 2011 @@ -0,0 +1,157 @@ +/** + * 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.issues; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.AdviceWithRouteBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.model.RouteDefinition; + +/** + * @version $Revision$ + */ +public class AdviceWithIssueTest extends ContextTestSupport { + + public void testNoAdvice() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("Hello World"); + + template.sendBody("direct:start", "World"); + try { + template.sendBody("direct:start", "Kaboom"); + fail("Should have thrown exception"); + } catch (Exception e) { + // expected + } + + assertMockEndpointsSatisfied(); + } + + public void testAdviceWithErrorHandler() throws Exception { + RouteDefinition route = context.getRouteDefinitions().get(0); + try { + route.adviceWith(context, new AdviceWithRouteBuilder() { + @Override + public void configure() throws Exception { + errorHandler(deadLetterChannel("mock:dead")); + } + }); + fail("Should have thrown exception"); + } catch (IllegalArgumentException e) { + assertEquals("You can not advice with error handlers. Remove the error handlers from the route builder.", e.getMessage()); + } + } + + public void testAdviceWithOnException() throws Exception { + RouteDefinition route = context.getRouteDefinitions().get(0); + route.adviceWith(context, new AdviceWithRouteBuilder() { + @Override + public void configure() throws Exception { + onException(IllegalArgumentException.class) + .handled(true) + .to("mock:error"); + } + }); + + getMockEndpoint("mock:result").expectedBodiesReceived("Hello World"); + getMockEndpoint("mock:error").expectedBodiesReceived("Kaboom"); + + template.sendBody("direct:start", "World"); + template.sendBody("direct:start", "Kaboom"); + + assertMockEndpointsSatisfied(); + } + + public void testAdviceWithInterceptFrom() throws Exception { + RouteDefinition route = context.getRouteDefinitions().get(0); + route.adviceWith(context, new AdviceWithRouteBuilder() { + @Override + public void configure() throws Exception { + interceptFrom().to("mock:from"); + } + }); + + getMockEndpoint("mock:result").expectedBodiesReceived("Hello World"); + getMockEndpoint("mock:from").expectedBodiesReceived("World"); + getMockEndpoint("mock:from").expectedHeaderReceived(Exchange.INTERCEPTED_ENDPOINT, "direct://start"); + + template.sendBody("direct:start", "World"); + + assertMockEndpointsSatisfied(); + } + + public void testAdviceWithInterceptSendToEndpoint() throws Exception { + RouteDefinition route = context.getRouteDefinitions().get(0); + route.adviceWith(context, new AdviceWithRouteBuilder() { + @Override + public void configure() throws Exception { + interceptSendToEndpoint("mock:result").to("mock:to"); + } + }); + + getMockEndpoint("mock:result").expectedBodiesReceived("Hello World"); + getMockEndpoint("mock:to").expectedBodiesReceived("Hello World"); + getMockEndpoint("mock:to").expectedHeaderReceived(Exchange.INTERCEPTED_ENDPOINT, "mock://result"); + + template.sendBody("direct:start", "World"); + + assertMockEndpointsSatisfied(); + } + + public void testAdviceWithOnCompletion() throws Exception { + RouteDefinition route = context.getRouteDefinitions().get(0); + route.adviceWith(context, new AdviceWithRouteBuilder() { + @Override + public void configure() throws Exception { + onCompletion().to("mock:done"); + } + }); + + getMockEndpoint("mock:result").expectedBodiesReceived("Hello World"); + getMockEndpoint("mock:done").expectedBodiesReceived("Hello World"); + + template.sendBody("direct:start", "World"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .process(new MyProcessor()) + .to("mock:result"); + } + }; + } + + private final class MyProcessor implements Processor { + + @Override + public void process(Exchange exchange) throws Exception { + String body = exchange.getIn().getBody(String.class); + if ("Kaboom".equals(body)) { + throw new IllegalArgumentException("Kaboom"); + } + exchange.getIn().setBody("Hello " + body); + } + } + +}