Skip to content

Commit

Permalink
Moved over camel route coverage maven plugin to master branch.
Browse files Browse the repository at this point in the history
  • Loading branch information
davsclaus committed Oct 12, 2017
1 parent 9ffb254 commit f015f7b
Show file tree
Hide file tree
Showing 43 changed files with 2,764 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,29 @@ public void startElement(final String uri, final String localName, final String
el.setAttribute("totalProcessingTime", "" + totalTime);
}
} else if ("from".equals(qName)) {
// TODO: include the stats from the route mbean as that would be the same
// grab statistics from the parent route as from would be the same
Element parent = elementStack.peek();
if (parent != null) {
String routeId = parent.getAttribute("id");
ManagedRouteMBean route = camelContext.getManagedRoute(routeId, ManagedRouteMBean.class);
if (route != null) {
long total = route.getExchangesTotal();
el.setAttribute("exchangesTotal", "" + total);
long totalTime = route.getTotalProcessingTime();
el.setAttribute("totalProcessingTime", "" + totalTime);
// from is index-0
el.setAttribute("index", "0");
}
}
} else {
ManagedProcessorMBean processor = camelContext.getManagedProcessor(id, ManagedProcessorMBean.class);
if (processor != null) {
long total = processor.getExchangesTotal();
el.setAttribute("exchangesTotal", "" + total);
long totalTime = processor.getTotalProcessingTime();
el.setAttribute("totalProcessingTime", "" + totalTime);
int index = processor.getIndex();
el.setAttribute("index", "" + index);
}
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import org.apache.camel.component.properties.PropertiesComponent;
import org.apache.camel.impl.DefaultDebugger;
import org.apache.camel.impl.InterceptSendToMockEndpointStrategy;
import org.apache.camel.management.JmxSystemPropertyKeys;
import org.apache.camel.spi.Breakpoint;
import org.apache.camel.spi.Debugger;
import org.apache.camel.spi.EventNotifier;
import org.apache.camel.spring.SpringCamelContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -69,6 +71,29 @@ public static void handleDisableJmx(ConfigurableApplicationContext context, Clas
}
}

/**
* Handles disabling of JMX on Camel contexts based on {@link DisableJmx}.
*
* @param context the initialized Spring context
* @param testClass the test class being executed
*/
public static void handleRouteCoverage(ConfigurableApplicationContext context, Class<?> testClass, Function testMethod) throws Exception {
if (testClass.isAnnotationPresent(EnableRouteCoverage.class)) {
System.setProperty("CamelTestRouteCoverage", "true");

CamelSpringTestHelper.doToSpringCamelContexts(context, new CamelSpringTestHelper.DoToSpringCamelContextsStrategy() {

@Override
public void execute(String contextName, SpringCamelContext camelContext) throws Exception {
LOGGER.info("Enabling RouteCoverage");
EventNotifier notifier = new RouteCoverageEventNotifier(testClass.getName(), testMethod);
camelContext.addService(notifier, true);
camelContext.getManagementStrategy().addEventNotifier(notifier);
}
});
}
}

public static void handleProvidesBreakpoint(ConfigurableApplicationContext context, Class<?> testClass) throws Exception {
Collection<Method> methods = getAllMethods(testClass);
final List<Breakpoint> breakpoints = new LinkedList<Breakpoint>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class CamelSpringBootExecutionListener extends AbstractTestExecutionListe

@Override
public void prepareTestInstance(TestContext testContext) throws Exception {
LOG.info("@RunWith(CamelSpringBootJUnit4ClassRunner.class) preparing: {}", testContext.getTestClass());
LOG.info("@RunWith(CamelSpringBootRunner.class) preparing: {}", testContext.getTestClass());

Class<?> testClass = testContext.getTestClass();
// we are customizing the Camel context with
Expand All @@ -56,15 +56,20 @@ public void prepareTestInstance(TestContext testContext) throws Exception {

@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
LOG.info("@RunWith(CamelSpringBootJUnit4ClassRunner.class) before: {}.{}", testContext.getTestClass(), testContext.getTestMethod().getName());
LOG.info("@RunWith(CamelSpringBootRunner.class) before: {}.{}", testContext.getTestClass(), testContext.getTestMethod().getName());

Class<?> testClass = testContext.getTestClass();
String testName = testContext.getTestMethod().getName();

ConfigurableApplicationContext context = (ConfigurableApplicationContext) testContext.getApplicationContext();

// mark Camel to be startable again and start Camel
System.clearProperty("skipStartingCamelContext");

LOG.info("Initialized CamelSpringBootJUnit4ClassRunner now ready to start CamelContext");
// route coverage need to know the test method
CamelAnnotationsHandler.handleRouteCoverage(context, testClass, (String) -> testName);

LOG.info("Initialized CamelSpringBootRunner now ready to start CamelContext");
CamelAnnotationsHandler.handleCamelContextStartup(context, testClass);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.camel.test.spring;

import java.lang.reflect.Method;

import org.apache.camel.management.JmxSystemPropertyKeys;
import org.apache.camel.spring.SpringCamelContext;
import org.slf4j.Logger;
Expand Down Expand Up @@ -77,6 +79,7 @@ public ApplicationContext loadContext(ConfigurableApplicationContext context, Cl
AnnotationConfigUtils.registerAnnotationConfigProcessors((BeanDefinitionRegistry) context);

// Post CamelContext(s) instantiation but pre CamelContext(s) start setup
CamelAnnotationsHandler.handleRouteCoverage(context, testClass, (String) -> getTestMethod().getName());
CamelAnnotationsHandler.handleProvidesBreakpoint(context, testClass);
CamelAnnotationsHandler.handleShutdownTimeout(context, testClass);
CamelAnnotationsHandler.handleMockEndpoints(context, testClass);
Expand Down Expand Up @@ -119,4 +122,14 @@ protected Class<?> getTestClass() {
return CamelSpringTestHelper.getTestClass();
}

/**
* Returns the test method under test.
*
* @return the method that is being executed
* @see CamelSpringTestHelper
*/
protected Method getTestMethod() {
return CamelSpringTestHelper.getTestMethod();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.camel.management.JmxSystemPropertyKeys;
import org.apache.camel.spi.Breakpoint;
import org.apache.camel.spi.Debugger;
import org.apache.camel.spi.EventNotifier;
import org.apache.camel.spring.SpringCamelContext;
import org.apache.camel.test.ExcludingPackageScanClassResolver;
import org.apache.camel.test.spring.CamelSpringTestHelper.DoToSpringCamelContextsStrategy;
Expand Down Expand Up @@ -107,7 +108,7 @@ public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) t
public ApplicationContext loadContext(String... locations) throws Exception {

Class<?> testClass = getTestClass();

if (LOG.isDebugEnabled()) {
LOG.debug("Loading ApplicationContext for locations [" + StringUtils.arrayToCommaDelimitedString(locations) + "].");
}
Expand Down Expand Up @@ -153,6 +154,7 @@ protected ApplicationContext loadContext(GenericApplicationContext context, Clas
SpringCamelContext.setNoStart(false);

// Post CamelContext(s) instantiation but pre CamelContext(s) start setup
handleRouteCoverage(context, testClass);
handleProvidesBreakpoint(context, testClass);
handleShutdownTimeout(context, testClass);
handleMockEndpoints(context, testClass);
Expand Down Expand Up @@ -204,7 +206,6 @@ protected GenericApplicationContext createContext(Class<?> testClass, MergedCont

if (mergedConfig != null) {
parentContext = mergedConfig.getParentApplicationContext();

}

if (testClass.isAnnotationPresent(ExcludeRoutes.class)) {
Expand Down Expand Up @@ -258,7 +259,7 @@ protected GenericApplicationContext createContext(Class<?> testClass, MergedCont
*/
protected void handleDisableJmx(GenericApplicationContext context, Class<?> testClass) {
CamelSpringTestHelper.setOriginalJmxDisabledValue(System.getProperty(JmxSystemPropertyKeys.DISABLED));

if (testClass.isAnnotationPresent(DisableJmx.class)) {
if (testClass.getAnnotation(DisableJmx.class).value()) {
LOG.info("Disabling Camel JMX globally as DisableJmx annotation was found and disableJmx is set to true.");
Expand All @@ -267,12 +268,36 @@ protected void handleDisableJmx(GenericApplicationContext context, Class<?> test
LOG.info("Enabling Camel JMX as DisableJmx annotation was found and disableJmx is set to false.");
System.clearProperty(JmxSystemPropertyKeys.DISABLED);
}
} else {
} else if (!testClass.isAnnotationPresent(EnableRouteCoverage.class)) {
// route coverage need JMX so do not disable it by default
LOG.info("Disabling Camel JMX globally for tests by default. Use the DisableJMX annotation to override the default setting.");
System.setProperty(JmxSystemPropertyKeys.DISABLED, "true");
}
}


/**
* Handles disabling of JMX on Camel contexts based on {@link DisableJmx}.
*
* @param context the initialized Spring context
* @param testClass the test class being executed
*/
private void handleRouteCoverage(GenericApplicationContext context, Class<?> testClass) throws Exception {
if (testClass.isAnnotationPresent(EnableRouteCoverage.class)) {
System.setProperty("CamelTestRouteCoverage", "true");

CamelSpringTestHelper.doToSpringCamelContexts(context, new DoToSpringCamelContextsStrategy() {

@Override
public void execute(String contextName, SpringCamelContext camelContext) throws Exception {
LOG.info("Enabling RouteCoverage");
EventNotifier notifier = new RouteCoverageEventNotifier(testClass.getName(), (String) -> getTestMethod().getName());
camelContext.addService(notifier, true);
camelContext.getManagementStrategy().addEventNotifier(notifier);
}
});
}
}

/**
* Handles the processing of the {@link ProvidesBreakpoint} annotation on a test class. Exists here
* as it is needed in
Expand Down Expand Up @@ -504,4 +529,14 @@ public void execute(String contextName,
protected Class<?> getTestClass() {
return CamelSpringTestHelper.getTestClass();
}

/**
* Returns the test method under test.
*
* @return the method that is being executed
* @see CamelSpringTestHelper
*/
protected Method getTestMethod() {
return CamelSpringTestHelper.getTestMethod();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ public int getOrder() {
@Override
public void prepareTestInstance(TestContext testContext) throws Exception {
CamelSpringTestHelper.setTestClass(testContext.getTestClass());
}
CamelSpringTestHelper.setTestContext(testContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.camel.spring.SpringCamelContext;

import org.springframework.context.ApplicationContext;
import org.springframework.test.context.TestContext;

/**
* Helper that provides state information across the levels of Spring Test that do not expose the
Expand All @@ -41,7 +42,8 @@ public final class CamelSpringTestHelper {

private static ThreadLocal<String> originalJmxDisabledValue = new ThreadLocal<String>();
private static ThreadLocal<Class<?>> testClazz = new ThreadLocal<Class<?>>();

private static ThreadLocal<TestContext> testContext = new ThreadLocal<TestContext>();

private CamelSpringTestHelper() {
}

Expand All @@ -60,7 +62,15 @@ public static Class<?> getTestClass() {
public static void setTestClass(Class<?> testClass) {
testClazz.set(testClass);
}


public static Method getTestMethod() {
return testContext.get().getTestMethod();
}

public static void setTestContext(TestContext context) {
testContext.set(context);
}

/**
* Returns all methods defined in {@code clazz} and its superclasses/interfaces.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* 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.spring;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Enables dumping route coverage statistic.
* The route coverage status is written as xml files in the <tt>target/camel-route-coverage</tt> directory after the test has finished.
* <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.
*/
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface EnableRouteCoverage {

}
Loading

0 comments on commit f015f7b

Please sign in to comment.