This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit e580a7ff588677184e37b604897125e1f03cebb1 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Tue Jun 4 10:18:21 2019 +0200 CAMEL-13608: Add route filter to model camel context so you can filter out unwanted routes, such as from unit testing. --- .../apache/camel/test/junit4/CamelTestSupport.java | 34 ++++++++--- .../camel/test/RouteFilterPatternExcludeTest.java | 57 +++++++++++++++++++ .../test/RouteFilterPatternIncludeExcludeTest.java | 65 ++++++++++++++++++++++ .../camel/test/RouteFilterPatternIncludeTest.java | 57 +++++++++++++++++++ 4 files changed, 205 insertions(+), 8 deletions(-) diff --git a/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java b/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java index 6841f0f..02c167b 100644 --- a/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java +++ b/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java @@ -116,8 +116,6 @@ public abstract class CamelTestSupport extends TestSupport { protected volatile ConsumerTemplate consumer; protected volatile Service camelContextService; protected boolean dumpRouteStats; - private String routeFilterIncludePattern; - private String routeFilterExcludePattern; private boolean useRouteBuilder = true; private final DebugBreakpoint breakpoint = new DebugBreakpoint(); private final StopWatch watch = new StopWatch(); @@ -227,9 +225,27 @@ public abstract class CamelTestSupport extends TestSupport { * * Exclude takes precedence over include. */ - public void setRouteFilterPattern(String include, String exclude) { - this.routeFilterIncludePattern = include; - this.routeFilterExcludePattern = exclude; + public String getRouteFilterIncludePattern() { + return null; + } + + /** + * Used for filtering routes routes matching the given pattern, which follows the following rules: + * + * - Match by route id + * - Match by route input endpoint uri + * + * The matching is using exact match, by wildcard and regular expression. + * + * For example to only include routes which starts with foo in their route id's, use: include=foo* + * And to exclude routes which starts from JMS endpoints, use: exclude=jms:* + * + * Multiple patterns can be separated by comma, for example to exclude both foo and bar routes, use: exclude=foo*,bar* + * + * Exclude takes precedence over include. + */ + public String getRouteFilterExcludePattern() { + return null; } /** @@ -405,9 +421,11 @@ public abstract class CamelTestSupport extends TestSupport { pc.setIgnoreMissingLocation(ignore); } - if (routeFilterIncludePattern != null || routeFilterExcludePattern != null) { - log.info("Route filtering pattern: include={}, exclude={}", routeFilterIncludePattern, routeFilterExcludePattern); - context.getExtension(Model.class).setRouteFilterPattern(routeFilterIncludePattern, routeFilterExcludePattern); + String include = getRouteFilterIncludePattern(); + String exclude = getRouteFilterExcludePattern(); + if (include != null || exclude != null) { + log.info("Route filtering pattern: include={}, exclude={}", include, exclude); + context.getExtension(Model.class).setRouteFilterPattern(include, exclude); } // prepare for in-between tests diff --git a/components/camel-test/src/test/java/org/apache/camel/test/RouteFilterPatternExcludeTest.java b/components/camel-test/src/test/java/org/apache/camel/test/RouteFilterPatternExcludeTest.java new file mode 100644 index 0000000..f5bca0d --- /dev/null +++ b/components/camel-test/src/test/java/org/apache/camel/test/RouteFilterPatternExcludeTest.java @@ -0,0 +1,57 @@ +/* + * 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.test; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class RouteFilterPatternExcludeTest extends CamelTestSupport { + + @Override + public String getRouteFilterExcludePattern() { + return "bar*"; + } + + @Test + public void testRouteFilter() throws Exception { + assertEquals(1, context.getRoutes().size()); + assertEquals(1, context.getRouteDefinitions().size()); + assertEquals("foo", context.getRouteDefinitions().get(0).getId()); + + getMockEndpoint("mock:foo").expectedMessageCount(1); + + template.sendBody("direct:foo", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:foo").routeId("foo") + .to("mock:foo"); + + from("direct:bar").routeId("bar") + .to("mock:bar"); + } + }; + } + +} diff --git a/components/camel-test/src/test/java/org/apache/camel/test/RouteFilterPatternIncludeExcludeTest.java b/components/camel-test/src/test/java/org/apache/camel/test/RouteFilterPatternIncludeExcludeTest.java new file mode 100644 index 0000000..3c8261b --- /dev/null +++ b/components/camel-test/src/test/java/org/apache/camel/test/RouteFilterPatternIncludeExcludeTest.java @@ -0,0 +1,65 @@ +/* + * 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.test; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class RouteFilterPatternIncludeExcludeTest extends CamelTestSupport { + + @Override + public String getRouteFilterIncludePattern() { + return "foo*"; + } + + @Override + public String getRouteFilterExcludePattern() { + return "jms:*"; + } + + @Test + public void testRouteFilter() throws Exception { + assertEquals(1, context.getRoutes().size()); + assertEquals(1, context.getRouteDefinitions().size()); + assertEquals("foo", context.getRouteDefinitions().get(0).getId()); + + getMockEndpoint("mock:foo").expectedMessageCount(1); + + template.sendBody("direct:foo", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:foo").routeId("foo") + .to("mock:foo"); + + from("direct:bar").routeId("bar") + .to("mock:bar"); + + from("jms:beer").routeId("foolish") + .to("mock:beer"); + } + }; + } + +} diff --git a/components/camel-test/src/test/java/org/apache/camel/test/RouteFilterPatternIncludeTest.java b/components/camel-test/src/test/java/org/apache/camel/test/RouteFilterPatternIncludeTest.java new file mode 100644 index 0000000..ce6d262 --- /dev/null +++ b/components/camel-test/src/test/java/org/apache/camel/test/RouteFilterPatternIncludeTest.java @@ -0,0 +1,57 @@ +/* + * 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.test; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class RouteFilterPatternIncludeTest extends CamelTestSupport { + + @Override + public String getRouteFilterIncludePattern() { + return "foo*"; + } + + @Test + public void testRouteFilter() throws Exception { + assertEquals(1, context.getRoutes().size()); + assertEquals(1, context.getRouteDefinitions().size()); + assertEquals("foo", context.getRouteDefinitions().get(0).getId()); + + getMockEndpoint("mock:foo").expectedMessageCount(1); + + template.sendBody("direct:foo", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:foo").routeId("foo") + .to("mock:foo"); + + from("direct:bar").routeId("bar") + .to("mock:bar"); + } + }; + } + +}