You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2018/05/24 17:17:26 UTC
[camel] branch master updated: CAMEL-12491: Fixed dumping route
coverage to reset statistics between tests so each dump report does not
include numbers from other tests. Because spring can dirties context then
we need two ways of doing this with a big of magic code to handle that.
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
The following commit(s) were added to refs/heads/master by this push:
new e186d1c CAMEL-12491: Fixed dumping route coverage to reset statistics between tests so each dump report does not include numbers from other tests. Because spring can dirties context then we need two ways of doing this with a big of magic code to handle that.
e186d1c is described below
commit e186d1cb6c6d8e8b379a1fedd9c910530fdf2613
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu May 24 19:11:45 2018 +0200
CAMEL-12491: Fixed dumping route coverage to reset statistics between tests so each dump report does not include numbers from other tests. Because spring can dirties context then we need two ways of doing this with a big of magic code to handle that.
---
.../camel/test/spring/CamelAnnotationsHandler.java | 31 +++++++++++++
.../spring/CamelSpringBootExecutionListener.java | 17 +++++++
.../CamelSpringDelegatingTestContextLoader.java | 3 ++
...EventNotifier.java => RouteCoverageDumper.java} | 39 +++++-----------
.../test/spring/RouteCoverageEventNotifier.java | 52 +---------------------
5 files changed, 64 insertions(+), 78 deletions(-)
diff --git a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelAnnotationsHandler.java b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelAnnotationsHandler.java
index 4dacc8a..f4e1991 100644
--- a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelAnnotationsHandler.java
+++ b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelAnnotationsHandler.java
@@ -25,6 +25,7 @@ import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
+import org.apache.camel.api.management.mbean.ManagedCamelContextMBean;
import org.apache.camel.component.properties.PropertiesComponent;
import org.apache.camel.impl.DefaultDebugger;
import org.apache.camel.impl.InterceptSendToMockEndpointStrategy;
@@ -95,6 +96,36 @@ public final class CamelAnnotationsHandler {
}
}
+ public static void handleRouteCoverageDump(ConfigurableApplicationContext context, Class<?> testClass, Function testMethod) throws Exception {
+ if (testClass.isAnnotationPresent(EnableRouteCoverage.class)) {
+ CamelSpringTestHelper.doToSpringCamelContexts(context, new CamelSpringTestHelper.DoToSpringCamelContextsStrategy() {
+
+ @Override
+ public void execute(String contextName, SpringCamelContext camelContext) throws Exception {
+ LOGGER.debug("Dumping RouteCoverage");
+
+ String testMethodName = (String) testMethod.apply(this);
+ RouteCoverageDumper.dumpRouteCoverage(camelContext, testClass.getName(), testMethodName);
+
+ // reset JMX statistics
+ ManagedCamelContextMBean managedCamelContext = camelContext.getManagedCamelContext();
+ if (managedCamelContext != null) {
+ LOGGER.debug("Resetting JMX statistics for RouteCoverage");
+ managedCamelContext.reset(true);
+ }
+
+ // turn off dumping one more time by removing the event listener (which would dump as well when Camel is stopping)
+ // but this method was explict invoked to dump such as from afterTest callbacks from JUnit.
+ RouteCoverageEventNotifier eventNotifier = camelContext.hasService(RouteCoverageEventNotifier.class);
+ if (eventNotifier != null) {
+ camelContext.getManagementStrategy().removeEventNotifier(eventNotifier);
+ camelContext.removeService(eventNotifier);
+ }
+ }
+ });
+ }
+ }
+
public static void handleProvidesBreakpoint(ConfigurableApplicationContext context, Class<?> testClass) throws Exception {
Collection<Method> methods = getAllMethods(testClass);
final List<Breakpoint> breakpoints = new LinkedList<>();
diff --git a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringBootExecutionListener.java b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringBootExecutionListener.java
index 99240f3..3f7e71a 100644
--- a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringBootExecutionListener.java
+++ b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringBootExecutionListener.java
@@ -27,6 +27,8 @@ public class CamelSpringBootExecutionListener extends AbstractTestExecutionListe
private static final Logger LOG = LoggerFactory.getLogger(CamelSpringBootExecutionListener.class);
+ protected static ThreadLocal<ConfigurableApplicationContext> threadApplicationContext = new ThreadLocal<>();
+
@Override
public void prepareTestInstance(TestContext testContext) throws Exception {
LOG.info("@RunWith(CamelSpringBootRunner.class) preparing: {}", testContext.getTestClass());
@@ -60,6 +62,7 @@ public class CamelSpringBootExecutionListener extends AbstractTestExecutionListe
String testName = testContext.getTestMethod().getName();
ConfigurableApplicationContext context = (ConfigurableApplicationContext) testContext.getApplicationContext();
+ threadApplicationContext.set(context);
// mark Camel to be startable again and start Camel
System.clearProperty("skipStartingCamelContext");
@@ -71,4 +74,18 @@ public class CamelSpringBootExecutionListener extends AbstractTestExecutionListe
CamelAnnotationsHandler.handleCamelContextStartup(context, testClass);
}
+ @Override
+ public void afterTestMethod(TestContext testContext) throws Exception {
+ LOG.info("@RunWith(CamelSpringBootRunner.class) after: {}.{}", testContext.getTestClass(), testContext.getTestMethod().getName());
+
+ Class<?> testClass = testContext.getTestClass();
+ String testName = testContext.getTestMethod().getName();
+
+ ConfigurableApplicationContext context = threadApplicationContext.get();
+ if (context != null && context.isRunning()) {
+ // dump route coverage for each test method so its accurate statistics
+ // even if spring application context is running (i.e. its not dirtied per test method)
+ CamelAnnotationsHandler.handleRouteCoverageDump(context, testClass, s -> testName);
+ }
+ }
}
diff --git a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringDelegatingTestContextLoader.java b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringDelegatingTestContextLoader.java
index b01c2d2..e96561c 100644
--- a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringDelegatingTestContextLoader.java
+++ b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringDelegatingTestContextLoader.java
@@ -35,7 +35,10 @@ import org.springframework.test.context.support.DelegatingSmartContextLoader;
* <em>This loader can handle either classes or locations for configuring the context.</em>
* <br>
* NOTE: This TestContextLoader doesn't support the annotation of ExcludeRoutes now.
+ *
+ * @deprecated use {@link CamelSpringRunner} or {@link CamelSpringBootRunner} instead.
*/
+@Deprecated
public class CamelSpringDelegatingTestContextLoader extends DelegatingSmartContextLoader {
protected final Logger logger = LoggerFactory.getLogger(getClass());
diff --git a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/RouteCoverageEventNotifier.java b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/RouteCoverageDumper.java
similarity index 69%
copy from components/camel-test-spring/src/main/java/org/apache/camel/test/spring/RouteCoverageEventNotifier.java
copy to components/camel-test-spring/src/main/java/org/apache/camel/test/spring/RouteCoverageDumper.java
index 88b9de0..8aefbff 100644
--- a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/RouteCoverageEventNotifier.java
+++ b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/RouteCoverageDumper.java
@@ -21,51 +21,34 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
-import java.util.EventObject;
-import java.util.function.Function;
import org.apache.camel.CamelContext;
import org.apache.camel.api.management.mbean.ManagedCamelContextMBean;
-import org.apache.camel.management.event.CamelContextStoppingEvent;
-import org.apache.camel.support.EventNotifierSupport;
import org.apache.camel.util.IOHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class RouteCoverageEventNotifier extends EventNotifierSupport {
-
- private static final Logger LOG = LoggerFactory.getLogger(RouteCoverageEventNotifier.class);
+/**
+ * Helper to dump route coverage when using {@link EnableRouteCoverage}.
+ */
+public final class RouteCoverageDumper {
- private final String testClassName;
- private final Function testMethodName;
+ private static final Logger LOG = LoggerFactory.getLogger(RouteCoverageDumper.class);
- public RouteCoverageEventNotifier(String testClassName, Function testMethodName) {
- this.testClassName = testClassName;
- this.testMethodName = testMethodName;
- setIgnoreCamelContextEvents(false);
- setIgnoreExchangeEvents(true);
+ private RouteCoverageDumper() {
}
- @Override
- public boolean isEnabled(EventObject event) {
- return event instanceof CamelContextStoppingEvent;
- }
-
- @Override
- public void notify(EventObject event) throws Exception {
- CamelContext context = ((CamelContextStoppingEvent) event).getContext();
+ public static void dumpRouteCoverage(CamelContext context, String testClassName, String testName) {
try {
- String className = this.getClass().getSimpleName();
String dir = "target/camel-route-coverage";
- String testName = (String) testMethodName.apply(this);
- String name = className + "-" + testName + ".xml";
+ String name = testClassName + "-" + testName + ".xml";
ManagedCamelContextMBean managedCamelContext = context.getManagedCamelContext();
if (managedCamelContext == null) {
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(testName) + xml + "\n</camelRouteCoverage>";
+ String combined = "<camelRouteCoverage>\n" + gatherTestDetailsAsXml(testClassName, testName) + xml + "\n</camelRouteCoverage>";
File file = new File(dir);
// ensure dir exists
@@ -81,12 +64,13 @@ public class RouteCoverageEventNotifier extends EventNotifierSupport {
} catch (Exception e) {
LOG.warn("Error during dumping route coverage statistic. This exception is ignored.", e);
}
+
}
/**
* Gathers test details as xml
*/
- private String gatherTestDetailsAsXml(String testName) {
+ private static String gatherTestDetailsAsXml(String testClassName, String testName) {
StringBuilder sb = new StringBuilder();
sb.append("<test>\n");
sb.append(" <class>").append(testClassName).append("</class>\n");
@@ -94,5 +78,4 @@ public class RouteCoverageEventNotifier extends EventNotifierSupport {
sb.append("</test>\n");
return sb.toString();
}
-
}
diff --git a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/RouteCoverageEventNotifier.java b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/RouteCoverageEventNotifier.java
index 88b9de0..d8306af 100644
--- a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/RouteCoverageEventNotifier.java
+++ b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/RouteCoverageEventNotifier.java
@@ -16,26 +16,15 @@
*/
package org.apache.camel.test.spring;
-import java.io.ByteArrayInputStream;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.InputStream;
-import java.io.OutputStream;
import java.util.EventObject;
import java.util.function.Function;
import org.apache.camel.CamelContext;
-import org.apache.camel.api.management.mbean.ManagedCamelContextMBean;
import org.apache.camel.management.event.CamelContextStoppingEvent;
import org.apache.camel.support.EventNotifierSupport;
-import org.apache.camel.util.IOHelper;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
public class RouteCoverageEventNotifier extends EventNotifierSupport {
- private static final Logger LOG = LoggerFactory.getLogger(RouteCoverageEventNotifier.class);
-
private final String testClassName;
private final Function testMethodName;
@@ -54,45 +43,8 @@ public class RouteCoverageEventNotifier extends EventNotifierSupport {
@Override
public void notify(EventObject event) throws Exception {
CamelContext context = ((CamelContextStoppingEvent) event).getContext();
- try {
- String className = this.getClass().getSimpleName();
- String dir = "target/camel-route-coverage";
- String testName = (String) testMethodName.apply(this);
- String name = className + "-" + testName + ".xml";
-
- ManagedCamelContextMBean managedCamelContext = context.getManagedCamelContext();
- if (managedCamelContext == null) {
- 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(testName) + 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(combined.getBytes());
- OutputStream os = new FileOutputStream(file, false);
- IOHelper.copyAndCloseInput(is, os);
- IOHelper.close(os);
- }
- } catch (Exception e) {
- LOG.warn("Error during dumping route coverage statistic. This exception is ignored.", e);
- }
- }
-
- /**
- * Gathers test details as xml
- */
- private String gatherTestDetailsAsXml(String testName) {
- StringBuilder sb = new StringBuilder();
- sb.append("<test>\n");
- sb.append(" <class>").append(testClassName).append("</class>\n");
- sb.append(" <method>").append(testName).append("</method>\n");
- sb.append("</test>\n");
- return sb.toString();
+ String testName = (String) testMethodName.apply(this);
+ RouteCoverageDumper.dumpRouteCoverage(context, testClassName, testName);
}
}
--
To stop receiving notification emails like this one, please contact
davsclaus@apache.org.