CAMEL-7570 Added an unit test for it
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/d3aa7f8b Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/d3aa7f8b Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/d3aa7f8b Branch: refs/heads/camel-2.12.x Commit: d3aa7f8b7192ae633123b97dcd0b1612aaffb957 Parents: 2247cd9 Author: Willem Jiang <willem.ji...@gmail.com> Authored: Thu Jul 3 12:09:32 2014 +0800 Committer: Willem Jiang <willem.ji...@gmail.com> Committed: Thu Jul 3 16:08:22 2014 +0800 ---------------------------------------------------------------------- .../enricher/EnricherSendEventTest.java | 118 +++++++++++++++++++ 1 file changed, 118 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/d3aa7f8b/camel-core/src/test/java/org/apache/camel/processor/enricher/EnricherSendEventTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/processor/enricher/EnricherSendEventTest.java b/camel-core/src/test/java/org/apache/camel/processor/enricher/EnricherSendEventTest.java new file mode 100644 index 0000000..9072be6 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/processor/enricher/EnricherSendEventTest.java @@ -0,0 +1,118 @@ +/** + * 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.enricher; + +import java.util.EventObject; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.camel.CamelContext; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.management.DefaultManagementStrategy; +import org.apache.camel.management.event.ExchangeSendingEvent; +import org.apache.camel.management.event.ExchangeSentEvent; +import org.apache.camel.processor.async.MyAsyncComponent; +import org.apache.camel.spi.ManagementStrategy; +import org.apache.camel.spi.ShutdownStrategy; +import org.apache.camel.support.EventNotifierSupport; + + +public class EnricherSendEventTest extends ContextTestSupport { + private MyEventNotifier en = new MyEventNotifier(); + + public void testAsyncEnricher() throws Exception { + + template.sendBody("direct:start1", "test"); + assertEquals("Get a wrong sending event number", 3, en.exchangeSendingEvent.get()); + assertEquals("Get a wrong sent event number", 3, en.exchangeSentEvent.get()); + } + + + public void testSyncEnricher() throws Exception { + template.sendBody("direct:start2", "test"); + assertEquals("Get a wrong sending event number", 3, en.exchangeSendingEvent.get()); + assertEquals("Get a wrong sent event number", 3, en.exchangeSentEvent.get()); + } + + protected CamelContext createCamelContext() throws Exception { + CamelContext camelContext = super.createCamelContext(); + ShutdownStrategy shutdownStrategy = camelContext.getShutdownStrategy(); + camelContext.addComponent("async", new MyAsyncComponent()); + + shutdownStrategy.setTimeout(1000); + shutdownStrategy.setTimeUnit(TimeUnit.MILLISECONDS); + shutdownStrategy.setShutdownNowOnTimeout(true); + + + ManagementStrategy managementStrategy = new DefaultManagementStrategy(); + managementStrategy.addEventNotifier(en); + + camelContext.setManagementStrategy(managementStrategy); + return camelContext; + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() throws Exception { + from("direct:start1") + // using the async utility component to ensure that the async routing engine kicks in + .enrich("async:out?reply=Reply") + .to("mock:result"); + + from("direct:start2") + .enrich("direct:result") + .to("mock:result"); + + from("direct:result").setBody(constant("result")); + } + }; + } + + static class MyEventNotifier extends EventNotifierSupport { + + AtomicInteger exchangeSendingEvent = new AtomicInteger(); + AtomicInteger exchangeSentEvent = new AtomicInteger(); + + + @Override + public void notify(EventObject event) throws Exception { + + if (event instanceof ExchangeSendingEvent) { + exchangeSendingEvent.incrementAndGet(); + } else if (event instanceof ExchangeSentEvent) { + exchangeSentEvent.incrementAndGet(); + } + } + + @Override + public boolean isEnabled(EventObject event) { + return true; + } + + @Override + protected void doStart() throws Exception { + } + + @Override + protected void doStop() throws Exception { + } + } + + +}