Author: davsclaus Date: Wed Oct 27 11:37:21 2010 New Revision: 1027919 URL: http://svn.apache.org/viewvc?rev=1027919&view=rev Log: CAMEL-3281: Java DSL now fails fast if onException and the likes are not configured before routes, which they must.
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionAfterRouteTest.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java camel/trunk/camel-core/src/test/java/org/apache/camel/builder/RouteBuilderAddRoutesTest.java camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaTcpWithIoOutProcessorExceptionTest.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java?rev=1027919&r1=1027918&r2=1027919&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/RouteBuilder.java Wed Oct 27 11:37:21 2010 @@ -136,6 +136,9 @@ public abstract class RouteBuilder exten * @return the current builder with the error handler configured */ public RouteBuilder errorHandler(ErrorHandlerBuilder errorHandlerBuilder) { + if (!routeCollection.getRoutes().isEmpty()) { + throw new IllegalArgumentException("errorHandler must be defined before any routes in the RouteBuilder"); + } routeCollection.setCamelContext(getContext()); setErrorHandlerBuilder(errorHandlerBuilder); return this; @@ -147,6 +150,9 @@ public abstract class RouteBuilder exten * @return the builder */ public InterceptDefinition intercept() { + if (!routeCollection.getRoutes().isEmpty()) { + throw new IllegalArgumentException("intercept must be defined before any routes in the RouteBuilder"); + } routeCollection.setCamelContext(getContext()); return routeCollection.intercept(); } @@ -157,6 +163,9 @@ public abstract class RouteBuilder exten * @return the builder */ public InterceptFromDefinition interceptFrom() { + if (!routeCollection.getRoutes().isEmpty()) { + throw new IllegalArgumentException("interceptFrom must be defined before any routes in the RouteBuilder"); + } routeCollection.setCamelContext(getContext()); return routeCollection.interceptFrom(); } @@ -168,6 +177,9 @@ public abstract class RouteBuilder exten * @return the builder */ public InterceptFromDefinition interceptFrom(String uri) { + if (!routeCollection.getRoutes().isEmpty()) { + throw new IllegalArgumentException("interceptFrom must be defined before any routes in the RouteBuilder"); + } routeCollection.setCamelContext(getContext()); return routeCollection.interceptFrom(uri); } @@ -179,6 +191,9 @@ public abstract class RouteBuilder exten * @return the builder */ public InterceptSendToEndpointDefinition interceptSendToEndpoint(String uri) { + if (!routeCollection.getRoutes().isEmpty()) { + throw new IllegalArgumentException("interceptSendToEndpoint must be defined before any routes in the RouteBuilder"); + } routeCollection.setCamelContext(getContext()); return routeCollection.interceptSendToEndpoint(uri); } @@ -191,6 +206,10 @@ public abstract class RouteBuilder exten * @return the builder */ public OnExceptionDefinition onException(Class exception) { + // is only allowed at the top currently + if (!routeCollection.getRoutes().isEmpty()) { + throw new IllegalArgumentException("onException must be defined before any routes in the RouteBuilder"); + } routeCollection.setCamelContext(getContext()); return routeCollection.onException(exception); } @@ -217,6 +236,10 @@ public abstract class RouteBuilder exten * @return the builder */ public OnCompletionDefinition onCompletion() { + // is only allowed at the top currently + if (!routeCollection.getRoutes().isEmpty()) { + throw new IllegalArgumentException("onCompletion must be defined before any routes in the RouteBuilder"); + } routeCollection.setCamelContext(getContext()); return routeCollection.onCompletion(); } Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/builder/RouteBuilderAddRoutesTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/RouteBuilderAddRoutesTest.java?rev=1027919&r1=1027918&r2=1027919&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/builder/RouteBuilderAddRoutesTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/builder/RouteBuilderAddRoutesTest.java Wed Oct 27 11:37:21 2010 @@ -28,9 +28,9 @@ public class RouteBuilderAddRoutesTest e return new RouteBuilder() { @Override public void configure() throws Exception { - from("direct:start").to("mock:result"); - includeRoutes(new MyExtraRoute()); + + from("direct:start").to("mock:result"); } }; } Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionAfterRouteTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionAfterRouteTest.java?rev=1027919&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionAfterRouteTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/onexception/OnExceptionAfterRouteTest.java Wed Oct 27 11:37:21 2010 @@ -0,0 +1,51 @@ +/** + * 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.onexception; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; + +/** + * @version $Revision$ + */ +public class OnExceptionAfterRouteTest extends ContextTestSupport { + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + public void testOnExceptionAfterRoute() throws Exception { + try { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .throwException(new IllegalArgumentException("Damn")); + + onException(IllegalArgumentException.class) + .handled(true) + .to("mock:damn"); + } + }); + fail("Should have thrown an exception"); + } catch (IllegalArgumentException e) { + assertEquals("onException must be defined before any routes in the RouteBuilder", e.getMessage()); + } + } + +} Modified: camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaTcpWithIoOutProcessorExceptionTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaTcpWithIoOutProcessorExceptionTest.java?rev=1027919&r1=1027918&r2=1027919&view=diff ============================================================================== --- camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaTcpWithIoOutProcessorExceptionTest.java (original) +++ camel/trunk/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaTcpWithIoOutProcessorExceptionTest.java Wed Oct 27 11:37:21 2010 @@ -32,24 +32,22 @@ public class MinaTcpWithIoOutProcessorEx // use parameter sync=true to force InOut pattern of the MinaExchange protected String uri = "mina:tcp://localhost:" + PORT + "?textline=true&sync=true"; - public void testExceptionThrownInProcessor() { String body = "Hello World"; Object result = template.requestBody(uri, body); // The exception should be passed to the client assertNotNull("the result should not be null", result); assertEquals("result is IllegalArgumentException", result, "java.lang.IllegalArgumentException: Forced exception"); - } protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { public void configure() { + // use no delay for fast unit testing + errorHandler(defaultErrorHandler().maximumRedeliveries(2)); + from(uri).process(new Processor() { public void process(Exchange e) { - // use no delay for fast unit testing - errorHandler(defaultErrorHandler().maximumRedeliveries(2)); - assertEquals("Hello World", e.getIn().getBody(String.class)); // simulate a problem processing the input to see if we can handle it properly throw new IllegalArgumentException("Forced exception");