Author: davsclaus Date: Thu May 13 07:07:55 2010 New Revision: 943820 URL: http://svn.apache.org/viewvc?rev=943820&view=rev Log: CAMEL-2253: Validate only one of handled or continued option is in use.
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RoutingSlipInOutAndInOnlyTest.java (with props) camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionHandledAndContinueTest.java - copied, changed from r943816, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionContinueTest.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/OnExceptionDefinition.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/OnExceptionDefinition.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/OnExceptionDefinition.java?rev=943820&r1=943819&r2=943820&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/model/OnExceptionDefinition.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/OnExceptionDefinition.java Thu May 13 07:07:55 2010 @@ -40,6 +40,7 @@ import org.apache.camel.builder.Expressi import org.apache.camel.processor.CatchProcessor; import org.apache.camel.processor.RedeliveryPolicy; import org.apache.camel.spi.RouteContext; +import org.apache.camel.util.CamelContextHelper; import org.apache.camel.util.CastUtils; import org.apache.camel.util.ObjectHelper; @@ -136,9 +137,17 @@ public class OnExceptionDefinition exten setHandledFromExpressionType(routeContext); setContinuedFromExpressionType(routeContext); setRetryUntilFromExpressionType(routeContext); + + // only one of handled or continued is allowed + if (getHandledPolicy() != null && getContinuedPolicy() != null) { + throw new IllegalArgumentException("Only one of handled or continued is allowed to be configured on: " + this); + } + // lookup onRedelivery if ref is provided if (ObjectHelper.isNotEmpty(onRedeliveryRef)) { - setOnRedelivery(routeContext.lookup(onRedeliveryRef, Processor.class)); + // if ref is provided then use mandatory lookup to fail if not found + Processor onRedelivery = CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), onRedeliveryRef, Processor.class); + setOnRedelivery(onRedelivery); } // lets attach this on exception to the route error handler Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RoutingSlipInOutAndInOnlyTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RoutingSlipInOutAndInOnlyTest.java?rev=943820&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RoutingSlipInOutAndInOnlyTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RoutingSlipInOutAndInOnlyTest.java Thu May 13 07:07:55 2010 @@ -0,0 +1,66 @@ +/** + * 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; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; + +/** + * @version $Revision$ + */ +public class RoutingSlipInOutAndInOnlyTest extends ContextTestSupport { + + private String slip = "direct:a,direct:b,direct:c"; + + public void testRoutingSlipInOut() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("HelloABC"); + + String out = template.requestBodyAndHeader("direct:start", "Hello", "slip", slip, String.class); + assertEquals("HelloABC", out); + + assertMockEndpointsSatisfied(); + } + + public void testRoutingSlipInOnly() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("HelloABC"); + + template.sendBodyAndHeader("direct:start", "Hello", "slip", slip); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .routingSlip("slip") + .to("mock:result"); + + from("direct:a") + .transform(body().append("A")); + + from("direct:b") + .transform(body().append("B")); + + from("direct:c") + .transform(body().append("C")); + } + }; + } +} Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RoutingSlipInOutAndInOnlyTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/RoutingSlipInOutAndInOnlyTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionHandledAndContinueTest.java (from r943816, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionContinueTest.java) URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionHandledAndContinueTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionHandledAndContinueTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionContinueTest.java&r1=943816&r2=943820&rev=943820&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionContinueTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionHandledAndContinueTest.java Thu May 13 07:07:55 2010 @@ -17,43 +17,39 @@ package org.apache.camel.processor.onexception; import org.apache.camel.ContextTestSupport; -import org.apache.camel.Exchange; +import org.apache.camel.FailedToCreateRouteException; import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.mock.MockEndpoint; /** * @version $Revision$ */ -public class OnExceptionContinueTest extends ContextTestSupport { +public class OnExceptionHandledAndContinueTest extends ContextTestSupport { - public void testContinued() throws Exception { - getMockEndpoint("mock:start").expectedMessageCount(1); - - MockEndpoint mock = getMockEndpoint("mock:result"); - mock.expectedBodiesReceived("Hello World"); - // and we should keep the exception so we know what caused the failure - mock.message(0).property(Exchange.EXCEPTION_CAUGHT).isInstanceOf(IllegalArgumentException.class); - - template.sendBody("direct:start", "Hello World"); - - assertMockEndpointsSatisfied(); + @Override + public boolean isUseRouteBuilder() { + return false; } - @Override - protected RouteBuilder createRouteBuilder() throws Exception { - return new RouteBuilder() { + public void testHandledAndContinued() throws Exception { + context.addRoutes(new RouteBuilder() { @Override - // START SNIPPET: e1 public void configure() throws Exception { - // tell Camel to handle and continue when this exception is thrown - onException(IllegalArgumentException.class).continued(true); + // should not be allowed + onException(IllegalArgumentException.class).continued(true).handled(true); from("direct:start") .to("mock:start") .throwException(new IllegalArgumentException("Forced")) .to("mock:result"); } - // END SNIPPET: e1 - }; + }); + try { + context.start(); + fail("Should thrown an exception"); + } catch (FailedToCreateRouteException e) { + assertIsInstanceOf(IllegalArgumentException.class, e.getCause()); + assertTrue(e.getCause().getMessage().startsWith("Only one of handled or continued is allowed to be configured")); + } } + } \ No newline at end of file