Repository: camel Updated Branches: refs/heads/master c65b632dd -> 7903add4b
CAMEL-8656: camel-test - Add option to dump route stats to files. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/7903add4 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/7903add4 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/7903add4 Branch: refs/heads/master Commit: 7903add4bb62a799ce8fa0f34b790d48433a7c9e Parents: 8314bfe Author: Claus Ibsen <davscl...@apache.org> Authored: Fri Apr 17 11:58:50 2015 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Fri Apr 17 11:59:06 2015 +0200 ---------------------------------------------------------------------- .../camel/test/junit4/CamelTestSupport.java | 50 ++++++++++++++++++-- .../apache/camel/test/patterns/FilterTest.java | 5 ++ 2 files changed, 52 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/7903add4/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestSupport.java ---------------------------------------------------------------------- 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 22774d9..9154ac9 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 @@ -16,7 +16,11 @@ */ package org.apache.camel.test.junit4; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileOutputStream; import java.io.InputStream; +import java.io.OutputStream; import java.util.HashMap; import java.util.Hashtable; import java.util.Map; @@ -37,6 +41,7 @@ import org.apache.camel.Processor; import org.apache.camel.ProducerTemplate; import org.apache.camel.Service; import org.apache.camel.ServiceStatus; +import org.apache.camel.api.management.mbean.ManagedCamelContextMBean; import org.apache.camel.builder.AdviceWithRouteBuilder; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; @@ -51,6 +56,7 @@ import org.apache.camel.management.JmxSystemPropertyKeys; import org.apache.camel.model.ModelCamelContext; import org.apache.camel.model.ProcessorDefinition; import org.apache.camel.spi.Language; +import org.apache.camel.util.IOHelper; import org.apache.camel.util.StopWatch; import org.apache.camel.util.TimeUtils; import org.junit.After; @@ -80,6 +86,7 @@ public abstract class CamelTestSupport extends TestSupport { private final DebugBreakpoint breakpoint = new DebugBreakpoint(); private final StopWatch watch = new StopWatch(); private final Map<String, String> fromEndpoints = new HashMap<String, String>(); + protected boolean dumpRouteStats; /** * Use the RouteBuilder or not @@ -95,6 +102,18 @@ public abstract class CamelTestSupport extends TestSupport { } /** + * Whether to dump route utilization stats at the end of the test. + * <p/> + * This allows tooling or manual inspection of the stats, so you can generate a route trace diagram of which EIPs + * have been in use and which have not. Similar concepts as a code coverage report. + * + * @return <tt>true</tt> to write route stats in an xml file in the <tt>target</tt> directory after the test has finished. + */ + public boolean isDumpRouteStats() { + return false; + } + + /** * Override when using <a href="http://camel.apache.org/advicewith.html">advice with</a> and return <tt>true</tt>. * This helps knowing advice with is to be used, and {@link CamelContext} will not be started before * the advice with takes place. This helps by ensuring the advice with has been property setup before the @@ -238,10 +257,12 @@ public abstract class CamelTestSupport extends TestSupport { private void doSetUp() throws Exception { log.debug("setUp test"); - if (!useJmx()) { - disableJMX(); - } else { + // jmx is enabled if we have configured to use it, or if dump route stats is enabled (it requires JMX) + boolean jmx = useJmx() || isDumpRouteStats(); + if (jmx) { enableJMX(); + } else { + disableJMX(); } context = (ModelCamelContext)createCamelContext(); @@ -339,6 +360,29 @@ public abstract class CamelTestSupport extends TestSupport { log.info("********************************************************************************"); log.info("Testing done: " + getTestMethodName() + "(" + getClass().getName() + ")"); log.info("Took: " + TimeUtils.printDuration(time) + " (" + time + " millis)"); + + // if we should dump route stats, then write that to a file + if (isDumpRouteStats()) { + String className = this.getClass().getSimpleName(); + String dir = "target/camel-route-stats"; + String name = className + "-" + getTestMethodName() + ".xml"; + + ManagedCamelContextMBean managedCamelContext = context.getManagedCamelContext(); + if (managedCamelContext == null) { + log.warn("Cannot dump route stats to file as JMX is not enabled. Override useJmx() method to enable JMX in the unit test classes."); + } else { + String xml = managedCamelContext.dumpRoutesStatsAsXml(false, true); + File file = new File(dir); + // ensure dir exists + file.mkdirs(); + file = new File(dir, name); + log.info("Dumping route stats to file: " + file); + InputStream is = new ByteArrayInputStream(xml.getBytes()); + OutputStream os = new FileOutputStream(file, false); + IOHelper.copyAndCloseInput(is, os); + IOHelper.close(os); + } + } log.info("********************************************************************************"); if (isCreateCamelContextPerClass()) { http://git-wip-us.apache.org/repos/asf/camel/blob/7903add4/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterTest.java ---------------------------------------------------------------------- diff --git a/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterTest.java b/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterTest.java index 36d4320..ce56986 100644 --- a/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterTest.java +++ b/components/camel-test/src/test/java/org/apache/camel/test/patterns/FilterTest.java @@ -38,6 +38,11 @@ public class FilterTest extends CamelTestSupport { @Produce(uri = "direct:start") protected ProducerTemplate template; + @Override + public boolean isDumpRouteStats() { + return true; + } + @Test public void testSendMatchingMessage() throws Exception { String expectedBody = "<matched/>";