Repository: camel Updated Branches: refs/heads/master 4b9701362 -> 0df6995a5
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/0df6995a Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/0df6995a Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/0df6995a Branch: refs/heads/master Commit: 0df6995a533be3012e9a753794a05ddb55864795 Parents: 4b97013 Author: Claus Ibsen <[email protected]> Authored: Wed Apr 29 19:02:14 2015 +0200 Committer: Claus Ibsen <[email protected]> Committed: Wed Apr 29 19:02:51 2015 +0200 ---------------------------------------------------------------------- .../camel/test/junit4/CamelTestSupport.java | 26 +++++++++++- .../camel/test/junit4/CamelTestWatcher.java | 44 ++++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/0df6995a/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 e716da6..d0f472f 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 @@ -62,6 +62,7 @@ import org.apache.camel.util.TimeUtils; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; +import org.junit.Rule; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -87,7 +88,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>(); - + private CamelTestWatcher camelTestWatcher = new CamelTestWatcher(); /** * Use the RouteBuilder or not @@ -373,12 +374,15 @@ public abstract class CamelTestSupport extends TestSupport { log.warn("Cannot dump route coverage to file as JMX is not enabled. Override useJmx() method to enable JMX in the unit test classes."); } else { String xml = managedCamelContext.dumpRoutesCoverageAsXml(); + String combined = "<camelRouteCoverage>\n" + gatherTestDetailsAsXml() + xml + "\n</camelRouteCoverage>"; + File file = new File(dir); // ensure dir exists file.mkdirs(); file = new File(dir, name); + log.info("Dumping route coverage to file: " + file); - InputStream is = new ByteArrayInputStream(xml.getBytes()); + InputStream is = new ByteArrayInputStream(combined.getBytes()); OutputStream os = new FileOutputStream(file, false); IOHelper.copyAndCloseInput(is, os); IOHelper.close(os); @@ -405,6 +409,19 @@ public abstract class CamelTestSupport extends TestSupport { } /** + * Gathers test details as xml + */ + private String gatherTestDetailsAsXml() { + StringBuilder sb = new StringBuilder(); + sb.append("<test>\n"); + sb.append(" <class>").append(getClass().getName()).append("</class>\n"); + sb.append(" <method>").append(getTestMethodName()).append("</method>\n"); + sb.append(" <time>").append(getCamelTestWatcher().timeTaken()).append("</time>\n"); + sb.append("</test>\n"); + return sb.toString(); + } + + /** * Returns the timeout to use when shutting down (unit in seconds). * <p/> * Will default use 10 seconds. @@ -444,6 +461,11 @@ public abstract class CamelTestSupport extends TestSupport { return null; } + @Rule + public CamelTestWatcher getCamelTestWatcher() { + return camelTestWatcher; + } + /** * Whether to ignore missing locations with the {@link PropertiesComponent}. * For example when unit testing you may want to ignore locations that are http://git-wip-us.apache.org/repos/asf/camel/blob/0df6995a/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestWatcher.java ---------------------------------------------------------------------- diff --git a/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestWatcher.java b/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestWatcher.java new file mode 100644 index 0000000..5108eb6 --- /dev/null +++ b/components/camel-test/src/main/java/org/apache/camel/test/junit4/CamelTestWatcher.java @@ -0,0 +1,44 @@ +/** + * 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.junit4; + +import org.apache.camel.util.StopWatch; +import org.junit.rules.TestWatcher; +import org.junit.runner.Description; + +/** + * A JUnit {@link org.junit.rules.TestWatcher} that is used to time how long the test takes. + */ +public class CamelTestWatcher extends TestWatcher { + + private final StopWatch watch = new StopWatch(); + + @Override + protected void starting(Description description) { + watch.restart(); + } + + @Override + protected void finished(Description description) { + watch.stop(); + } + + public long timeTaken() { + return watch.taken(); + } + +}
