Author: davsclaus Date: Wed Aug 17 11:06:52 2011 New Revision: 1158627 URL: http://svn.apache.org/viewvc?rev=1158627&view=rev Log: CAMEL-4339: Added replaceFrom to adviceWith. To replace a route input to another endpoint uri, making unit testing easier.
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithReplaceFromTest.java - copied, changed from r1158534, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTasksSelectTest.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithTasks.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java?rev=1158627&r1=1158626&r2=1158627&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java Wed Aug 17 11:06:52 2011 @@ -19,6 +19,7 @@ package org.apache.camel.builder; import java.util.ArrayList; import java.util.List; +import org.apache.camel.Endpoint; import org.apache.camel.impl.InterceptSendToMockEndpointStrategy; import org.apache.camel.model.ProcessorDefinition; import org.apache.camel.model.RouteDefinition; @@ -88,6 +89,26 @@ public abstract class AdviceWithRouteBui } /** + * Replaces the route from endpoint with a new uri + * + * @param uri uri of the new endpoint + */ + public void replaceFrom(String uri) { + ObjectHelper.notNull(originalRoute, "originalRoute", this); + getAdviceWithTasks().add(AdviceWithTasks.replaceFrom(originalRoute, uri)); + } + + /** + * Replaces the route from endpoint with a new endpoint + * + * @param endpoint the new endpoint + */ + public void replaceFrom(Endpoint endpoint) { + ObjectHelper.notNull(originalRoute, "originalRoute", this); + getAdviceWithTasks().add(AdviceWithTasks.replaceFrom(originalRoute, endpoint)); + } + + /** * Weaves by matching id of the nodes in the route. * <p/> * Uses the {@link org.apache.camel.util.EndpointHelper#matchPattern(String, String)} matching algorithm. Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithTasks.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithTasks.java?rev=1158627&r1=1158626&r2=1158627&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithTasks.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/AdviceWithTasks.java Wed Aug 17 11:06:52 2011 @@ -20,6 +20,8 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import org.apache.camel.Endpoint; +import org.apache.camel.model.FromDefinition; import org.apache.camel.model.ProcessorDefinition; import org.apache.camel.model.ProcessorDefinitionHelper; import org.apache.camel.model.RouteDefinition; @@ -307,6 +309,30 @@ public final class AdviceWithTasks { }; } + public static AdviceWithTask replaceFrom(final RouteDefinition route, final String uri) { + return new AdviceWithTask() { + public void task() throws Exception { + FromDefinition from = route.getInputs().get(0); + LOG.info("AdviceWith replace input from [{}] --> [{}]", from.getUriOrRef(), uri); + from.setEndpoint(null); + from.setRef(null); + from.setUri(uri); + } + }; + } + + public static AdviceWithTask replaceFrom(final RouteDefinition route, final Endpoint endpoint) { + return new AdviceWithTask() { + public void task() throws Exception { + FromDefinition from = route.getInputs().get(0); + LOG.info("AdviceWith replace input from [{}] --> [{}]", from.getUriOrRef(), endpoint.getEndpointUri()); + from.setRef(null); + from.setUri(null); + from.setEndpoint(endpoint); + } + }; + } + /** * Create iterator which walks the route, and only returns nodes which matches the given set of criteria. * Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithReplaceFromTest.java (from r1158534, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTasksSelectTest.java) URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithReplaceFromTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithReplaceFromTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTasksSelectTest.java&r1=1158534&r2=1158627&rev=1158627&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithTasksSelectTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithReplaceFromTest.java Wed Aug 17 11:06:52 2011 @@ -17,188 +17,73 @@ package org.apache.camel.processor.interceptor; import org.apache.camel.ContextTestSupport; +import org.apache.camel.Endpoint; +import org.apache.camel.FailedToCreateRouteException; +import org.apache.camel.ResolveEndpointFailedException; import org.apache.camel.builder.AdviceWithRouteBuilder; import org.apache.camel.builder.RouteBuilder; /** * Advice with match multiple ids test */ -public class AdviceWithTasksSelectTest extends ContextTestSupport { +public class AdviceWithReplaceFromTest extends ContextTestSupport { - public void testSelectFirst() throws Exception { + public void testReplaceFromUri() throws Exception { context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { - // should only match the first - weaveById("gold*").selectFirst().replace().multicast().to("mock:a").to("mock:b"); + // replace the input in the route with a new endpoint uri + replaceFrom("seda:foo"); } }); - getMockEndpoint("mock:foo").expectedMessageCount(0); - getMockEndpoint("mock:bar").expectedMessageCount(1); - getMockEndpoint("mock:baz").expectedMessageCount(1); - getMockEndpoint("mock:a").expectedMessageCount(1); - getMockEndpoint("mock:b").expectedMessageCount(1); getMockEndpoint("mock:result").expectedMessageCount(1); - template.sendBody("direct:start", "Hello World"); + // has been replaced to a seda endpoint instead + template.sendBody("seda:foo", "Hello World"); assertMockEndpointsSatisfied(); } - public void testSelectLast() throws Exception { - context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { - @Override - public void configure() throws Exception { - // should only match the last - weaveById("gold*").selectLast().replace().multicast().to("mock:a").to("mock:b"); - } - }); - - getMockEndpoint("mock:foo").expectedMessageCount(1); - getMockEndpoint("mock:bar").expectedMessageCount(1); - getMockEndpoint("mock:baz").expectedMessageCount(0); - getMockEndpoint("mock:a").expectedMessageCount(1); - getMockEndpoint("mock:b").expectedMessageCount(1); - getMockEndpoint("mock:result").expectedMessageCount(1); - - template.sendBody("direct:start", "Hello World"); - - assertMockEndpointsSatisfied(); - } - - - public void testSelectIndexZero() throws Exception { - context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { - @Override - public void configure() throws Exception { - // should match the first index (0 based) - weaveById("gold*").selectIndex(0).replace().multicast().to("mock:a").to("mock:b"); - } - }); - - getMockEndpoint("mock:foo").expectedMessageCount(0); - getMockEndpoint("mock:bar").expectedMessageCount(1); - getMockEndpoint("mock:baz").expectedMessageCount(1); - getMockEndpoint("mock:a").expectedMessageCount(1); - getMockEndpoint("mock:b").expectedMessageCount(1); - getMockEndpoint("mock:result").expectedMessageCount(1); - - template.sendBody("direct:start", "Hello World"); - - assertMockEndpointsSatisfied(); - } - - public void testSelectIndexOne() throws Exception { - context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { - @Override - public void configure() throws Exception { - // should match the second index (0 based) - weaveById("gold*").selectIndex(1).replace().multicast().to("mock:a").to("mock:b"); - } - }); + public void testReplaceFromEndpoint() throws Exception { + final Endpoint endpoint = context.getEndpoint("seda:foo"); - getMockEndpoint("mock:foo").expectedMessageCount(1); - getMockEndpoint("mock:bar").expectedMessageCount(0); - getMockEndpoint("mock:baz").expectedMessageCount(1); - getMockEndpoint("mock:a").expectedMessageCount(1); - getMockEndpoint("mock:b").expectedMessageCount(1); - getMockEndpoint("mock:result").expectedMessageCount(1); - - template.sendBody("direct:start", "Hello World"); - - assertMockEndpointsSatisfied(); - } - - public void testSelectIndexTwo() throws Exception { context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { - // should match the third index (0 based) - weaveById("gold*").selectIndex(2).replace().multicast().to("mock:a").to("mock:b"); + // replace the input in the route with a new endpoint + replaceFrom(endpoint); } }); - getMockEndpoint("mock:foo").expectedMessageCount(1); - getMockEndpoint("mock:bar").expectedMessageCount(1); - getMockEndpoint("mock:baz").expectedMessageCount(0); - getMockEndpoint("mock:a").expectedMessageCount(1); - getMockEndpoint("mock:b").expectedMessageCount(1); getMockEndpoint("mock:result").expectedMessageCount(1); - template.sendBody("direct:start", "Hello World"); + // has been replaced to a seda endpoint instead + template.sendBody("seda:foo", "Hello World"); assertMockEndpointsSatisfied(); } - public void testSelectIndexOutOfBounds() throws Exception { + public void testReplaceFromInvalidUri() throws Exception { try { context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { - // should be out of bounds - weaveById("gold*").selectIndex(3).replace().multicast().to("mock:a").to("mock:b"); + replaceFrom("xxx:foo"); } }); - fail("Should hve thrown exception"); - } catch (IllegalArgumentException e) { - assertTrue(e.getMessage(), e.getMessage().startsWith("There are no outputs which matches: gold* in the route")); + fail("Should have thrown exception"); + } catch (FailedToCreateRouteException e) { + assertIsInstanceOf(ResolveEndpointFailedException.class, e.getCause()); } } - public void testSelectRangeZeroOne() throws Exception { - context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { - @Override - public void configure() throws Exception { - // should match the first two (0-based) - weaveById("gold*").selectRange(0, 1).replace().multicast().to("mock:a").to("mock:b"); - } - }); - - getMockEndpoint("mock:foo").expectedMessageCount(0); - getMockEndpoint("mock:bar").expectedMessageCount(0); - getMockEndpoint("mock:baz").expectedMessageCount(1); - getMockEndpoint("mock:a").expectedMessageCount(2); - getMockEndpoint("mock:b").expectedMessageCount(2); - getMockEndpoint("mock:result").expectedMessageCount(1); - - template.sendBody("direct:start", "Hello World"); - - assertMockEndpointsSatisfied(); - } - - public void testSelectRangeOneTwo() throws Exception { - context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { - @Override - public void configure() throws Exception { - // should match the 2nd and 3rd (0-based) - weaveById("gold*").selectRange(1, 2).replace().multicast().to("mock:a").to("mock:b"); - } - }); - - getMockEndpoint("mock:foo").expectedMessageCount(1); - getMockEndpoint("mock:bar").expectedMessageCount(0); - getMockEndpoint("mock:baz").expectedMessageCount(0); - getMockEndpoint("mock:a").expectedMessageCount(2); - getMockEndpoint("mock:b").expectedMessageCount(2); - getMockEndpoint("mock:result").expectedMessageCount(1); - - template.sendBody("direct:start", "Hello World"); - - assertMockEndpointsSatisfied(); - } - @Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { - from("direct:start") - .log("Got ${body}").id("foo") - .to("mock:foo").id("gold-1") - .to("mock:bar").id("gold-2") - .to("mock:baz").id("gold-3") - .to("mock:result").id("silver-1"); + from("direct:start").to("mock:result"); } }; }