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 2017/10/08 12:02:44 UTC
[01/13] camel git commit: Camel java dsl parser prototype
Repository: camel
Updated Branches:
refs/heads/parser2 [created] 32e0a7162
Camel java dsl parser prototype
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/d8844048
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/d8844048
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/d8844048
Branch: refs/heads/parser2
Commit: d8844048b8f7def0b5eb25286de3802446de3a14
Parents: 3c14611
Author: Claus Ibsen <da...@apache.org>
Authored: Fri Dec 23 22:10:09 2016 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat Oct 7 16:33:25 2017 +0200
----------------------------------------------------------------------
tooling/camel-route-parser/pom.xml | 7 +
.../parser/AdvancedRouteBuilderParser.java | 56 +++
.../apache/camel/parser/graph/RenderRoute.java | 66 ++++
.../helper/AdvancedCamelJavaParserHelper.java | 392 +++++++++++++++++++
.../camel/parser/model/CamelNodeDetails.java | 86 ++++
.../parser/java/MyJavaDslRouteBuilder.java | 38 ++
.../camel/parser/java/RoasterJavaDslTest.java | 44 +++
7 files changed, 689 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/d8844048/tooling/camel-route-parser/pom.xml
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/pom.xml b/tooling/camel-route-parser/pom.xml
index 5d3aa77..4d67bd3 100644
--- a/tooling/camel-route-parser/pom.xml
+++ b/tooling/camel-route-parser/pom.xml
@@ -42,6 +42,13 @@
<scope>provided</scope>
</dependency>
+ <!-- the catalog has details the parser needs to parse the Camel Java DSL -->
+ <dependency>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-catalog</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
<!-- only test scopes for camel as we have no runtime dependency on camel -->
<dependency>
<groupId>org.apache.camel</groupId>
http://git-wip-us.apache.org/repos/asf/camel/blob/d8844048/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/AdvancedRouteBuilderParser.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/AdvancedRouteBuilderParser.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/AdvancedRouteBuilderParser.java
new file mode 100644
index 0000000..e3fb6d7
--- /dev/null
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/AdvancedRouteBuilderParser.java
@@ -0,0 +1,56 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.parser;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.camel.parser.helper.AdvancedCamelJavaParserHelper;
+import org.apache.camel.parser.helper.CamelJavaParserHelper;
+import org.apache.camel.parser.model.CamelNodeDetails;
+import org.jboss.forge.roaster.model.source.JavaClassSource;
+import org.jboss.forge.roaster.model.source.MethodSource;
+
+/**
+ * TODO: Merge this to {@link RouteBuilderParser}
+ */
+public class AdvancedRouteBuilderParser {
+
+ // TODO: list of details, on per route
+ public static CamelNodeDetails parseRouteBuilder(JavaClassSource clazz, boolean includeInlinedRouteBuilders) {
+ AdvancedCamelJavaParserHelper parser = new AdvancedCamelJavaParserHelper();
+
+ List<MethodSource<JavaClassSource>> methods = new ArrayList<>();
+ MethodSource<JavaClassSource> method = CamelJavaParserHelper.findConfigureMethod(clazz);
+ if (method != null) {
+ methods.add(method);
+ }
+ if (includeInlinedRouteBuilders) {
+ List<MethodSource<JavaClassSource>> inlinedMethods = CamelJavaParserHelper.findInlinedConfigureMethods(clazz);
+ if (!inlinedMethods.isEmpty()) {
+ methods.addAll(inlinedMethods);
+ }
+ }
+
+ for (MethodSource<JavaClassSource> configureMethod : methods) {
+ CamelNodeDetails details = parser.parseCamelRoute(clazz, configureMethod);
+ return details;
+ }
+
+ return null;
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/d8844048/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/graph/RenderRoute.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/graph/RenderRoute.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/graph/RenderRoute.java
new file mode 100644
index 0000000..2b1cd1c
--- /dev/null
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/graph/RenderRoute.java
@@ -0,0 +1,66 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.parser.graph;
+
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import javax.imageio.ImageIO;
+
+import org.apache.camel.parser.model.CamelNodeDetails;
+
+public class RenderRoute {
+
+ public static void main(String[] args) {
+ RenderRoute render = new RenderRoute();
+ render(null);
+ }
+
+ public static void render(CamelNodeDetails root) {
+ // TODO:
+ try {
+ int width = 200, height = 200;
+
+ // TYPE_INT_ARGB specifies the image format: 8-bit RGBA packed
+ // into integer pixels
+// BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
+ BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
+
+ Graphics2D ig2 = bi.createGraphics();
+
+ ig2.drawRect(10, 10, 80, 40);
+ ig2.drawLine(45, 50, 45, 80);
+ ig2.drawRect(10, 80, 80, 40);
+
+ Font font = new Font("Arial", Font.BOLD, 20);
+ ig2.setFont(font);
+ String message = "Apache Camel";
+ FontMetrics fontMetrics = ig2.getFontMetrics();
+ int stringWidth = fontMetrics.stringWidth(message);
+ int stringHeight = fontMetrics.getAscent();
+ ig2.setPaint(Color.black);
+ ig2.drawString(message, (width - stringWidth) / 2, height / 2 + stringHeight / 4);
+
+ ImageIO.write(bi, "PNG", new File("target/route.png"));
+
+ } catch (IOException ie) {
+ ie.printStackTrace();
+ }
+
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/d8844048/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java
new file mode 100644
index 0000000..1a5fa0b
--- /dev/null
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java
@@ -0,0 +1,392 @@
+/**
+ * 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.parser.helper;
+
+import java.util.List;
+import java.util.Map;
+
+import org.apache.camel.catalog.CamelCatalog;
+import org.apache.camel.catalog.DefaultCamelCatalog;
+import org.apache.camel.catalog.JSonSchemaHelper;
+import org.apache.camel.parser.RouteBuilderParser;
+import org.apache.camel.parser.model.CamelNodeDetails;
+import org.apache.camel.parser.roaster.StatementFieldSource;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ASTNode;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Block;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.BooleanLiteral;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Expression;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ExpressionStatement;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.FieldDeclaration;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.InfixExpression;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MemberValuePair;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodDeclaration;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodInvocation;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.NormalAnnotation;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.NumberLiteral;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ParenthesizedExpression;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.QualifiedName;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SimpleName;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SingleMemberAnnotation;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.StringLiteral;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Type;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.VariableDeclarationFragment;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.VariableDeclarationStatement;
+import org.jboss.forge.roaster.model.Annotation;
+import org.jboss.forge.roaster.model.source.FieldSource;
+import org.jboss.forge.roaster.model.source.JavaClassSource;
+import org.jboss.forge.roaster.model.source.MethodSource;
+
+/**
+ * A Camel Java parser that only depends on the Roaster API.
+ * <p/>
+ * This implementation is lower level details. For a higher level parser see {@link RouteBuilderParser}.
+ */
+public final class AdvancedCamelJavaParserHelper {
+
+ private CamelCatalog camelCatalog = new DefaultCamelCatalog(true);
+
+ public CamelNodeDetails parseCamelRoute(JavaClassSource clazz, MethodSource<JavaClassSource> method) {
+
+ // find any from which is the start of the route
+
+ CamelNodeDetails route = new CamelNodeDetails(null, "route");
+
+ if (method != null) {
+ MethodDeclaration md = (MethodDeclaration) method.getInternal();
+ Block block = md.getBody();
+ if (block != null) {
+ for (Object statement : md.getBody().statements()) {
+ // must be a method call expression
+ if (statement instanceof ExpressionStatement) {
+ ExpressionStatement es = (ExpressionStatement) statement;
+ Expression exp = es.getExpression();
+
+ parseExpression(clazz, block, exp, route);
+ }
+ }
+ }
+ }
+
+ // now parse the route node and build a tree structure of the EIPs
+ String root = route.getOutputs().get(0).getName();
+
+ CamelNodeDetails answer = new CamelNodeDetails(null, root);
+ CamelNodeDetails parent = answer;
+
+ // TODO: use camel catalog to know about these types and when to do what
+
+ for (int i = 1; i < route.getOutputs().size(); i++) {
+ CamelNodeDetails node = route.getOutputs().get(i);
+ String name = node.getName();
+
+ // special for some EIPs
+ if ("choice".equals(name)) {
+ CamelNodeDetails output = new CamelNodeDetails(parent, name);
+ parent.addOutput(output);
+ parent = output;
+ } else if ("when".equals(name)) {
+ parent = grandParent(parent, "choice");
+ CamelNodeDetails output = new CamelNodeDetails(parent, name);
+ parent.addOutput(output);
+ parent = output;
+ } else if ("otherwise".equals(name)) {
+ parent = grandParent(parent, "choice");
+ CamelNodeDetails output = new CamelNodeDetails(parent, name);
+ parent.addOutput(output);
+ parent = output;
+ } else if ("end".equals(name) || "endChoice".equals(name) || "endDoTry".equals(name)) {
+ // special for ending otherwise, as we end it automatic in Java DSL so do a double end then
+ if ("otherwise".equals(parent.getName())) {
+ parent = parent.getParent();
+ }
+ // parent should be grand parent
+ parent = parent.getParent();
+ } else {
+ boolean hasOutput = hasOutput(name);
+ if (hasOutput) {
+ // has output so add as new child node
+ CamelNodeDetails output = new CamelNodeDetails(parent, name);
+ parent.addOutput(output);
+ parent = output;
+ } else {
+ // add straight to itself
+ CamelNodeDetails output = new CamelNodeDetails(parent, name);
+ parent.addOutput(output);
+ }
+ }
+ }
+
+ return answer;
+ }
+
+ private boolean hasOutput(String name) {
+ String json = camelCatalog.modelJSonSchema(name);
+ List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("model", json, false);
+ return isModelOutput(rows);
+ }
+
+ private static boolean isModelOutput(List<Map<String, String>> rows) {
+ for (Map<String, String> row : rows) {
+ if (row.containsKey("output")) {
+ return "true".equals(row.get("output"));
+ }
+ }
+ return false;
+ }
+
+ private static CamelNodeDetails grandParent(CamelNodeDetails node, String parentName) {
+ if (node == null) {
+ return null;
+ }
+ if (parentName.equals(node.getName())) {
+ return node;
+ } else {
+ return grandParent(node.getParent(), parentName);
+ }
+ }
+
+ private void parseExpression(JavaClassSource clazz, Block block, Expression exp, CamelNodeDetails node) {
+ if (exp == null) {
+ return;
+ }
+ if (exp instanceof MethodInvocation) {
+ MethodInvocation mi = (MethodInvocation) exp;
+ node = doParseCamelModels(clazz, block, mi, node);
+ // if the method was called on another method, then recursive
+ exp = mi.getExpression();
+ parseExpression(clazz, block, exp, node);
+ }
+ }
+
+ private CamelNodeDetails doParseCamelModels(JavaClassSource clazz, Block block, MethodInvocation mi, CamelNodeDetails node) {
+ String name = mi.getName().getIdentifier();
+
+ // special for Java DSL having some endXXX
+ boolean isEnd = "end".equals(name) || "endChoice".equals(name) || "endDoTry".equals(name);
+
+ // only include if its a known Camel model
+ if (isEnd || camelCatalog.findModelNames().contains(name)) {
+ CamelNodeDetails newNode = new CamelNodeDetails(node, name);
+ node.addPreliminaryOutput(newNode);
+ return node;
+ }
+
+ return node;
+ }
+
+ @SuppressWarnings("unchecked")
+ private static FieldSource<JavaClassSource> getField(JavaClassSource clazz, Block block, SimpleName ref) {
+ String fieldName = ref.getIdentifier();
+ if (fieldName != null) {
+ // find field in class
+ FieldSource field = clazz != null ? clazz.getField(fieldName) : null;
+ if (field == null) {
+ field = findFieldInBlock(clazz, block, fieldName);
+ }
+ return field;
+ }
+ return null;
+ }
+
+ @SuppressWarnings("unchecked")
+ private static FieldSource<JavaClassSource> findFieldInBlock(JavaClassSource clazz, Block block, String fieldName) {
+ for (Object statement : block.statements()) {
+ // try local statements first in the block
+ if (statement instanceof VariableDeclarationStatement) {
+ final Type type = ((VariableDeclarationStatement) statement).getType();
+ for (Object obj : ((VariableDeclarationStatement) statement).fragments()) {
+ if (obj instanceof VariableDeclarationFragment) {
+ VariableDeclarationFragment fragment = (VariableDeclarationFragment) obj;
+ SimpleName name = fragment.getName();
+ if (name != null && fieldName.equals(name.getIdentifier())) {
+ return new StatementFieldSource(clazz, fragment, type);
+ }
+ }
+ }
+ }
+
+ // okay the field may be burried inside an anonymous inner class as a field declaration
+ // outside the configure method, so lets go back to the parent and see what we can find
+ ASTNode node = block.getParent();
+ if (node instanceof MethodDeclaration) {
+ node = node.getParent();
+ }
+ if (node instanceof AnonymousClassDeclaration) {
+ List declarations = ((AnonymousClassDeclaration) node).bodyDeclarations();
+ for (Object dec : declarations) {
+ if (dec instanceof FieldDeclaration) {
+ FieldDeclaration fd = (FieldDeclaration) dec;
+ final Type type = fd.getType();
+ for (Object obj : fd.fragments()) {
+ if (obj instanceof VariableDeclarationFragment) {
+ VariableDeclarationFragment fragment = (VariableDeclarationFragment) obj;
+ SimpleName name = fragment.getName();
+ if (name != null && fieldName.equals(name.getIdentifier())) {
+ return new StatementFieldSource(clazz, fragment, type);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ return null;
+ }
+
+ public static String getLiteralValue(JavaClassSource clazz, Block block, Expression expression) {
+ // unwrap parenthesis
+ if (expression instanceof ParenthesizedExpression) {
+ expression = ((ParenthesizedExpression) expression).getExpression();
+ }
+
+ if (expression instanceof StringLiteral) {
+ return ((StringLiteral) expression).getLiteralValue();
+ } else if (expression instanceof BooleanLiteral) {
+ return "" + ((BooleanLiteral) expression).booleanValue();
+ } else if (expression instanceof NumberLiteral) {
+ return ((NumberLiteral) expression).getToken();
+ }
+
+ // if it a method invocation then add a dummy value assuming the method invocation will return a valid response
+ if (expression instanceof MethodInvocation) {
+ String name = ((MethodInvocation) expression).getName().getIdentifier();
+ return "{{" + name + "}}";
+ }
+
+ // if its a qualified name (usually a constant field in another class)
+ // then add a dummy value as we cannot find the field value in other classes and maybe even outside the
+ // source code we have access to
+ if (expression instanceof QualifiedName) {
+ QualifiedName qn = (QualifiedName) expression;
+ String name = qn.getFullyQualifiedName();
+ return "{{" + name + "}}";
+ }
+
+ if (expression instanceof SimpleName) {
+ FieldSource<JavaClassSource> field = getField(clazz, block, (SimpleName) expression);
+ if (field != null) {
+ // is the field annotated with a Camel endpoint
+ if (field.getAnnotations() != null) {
+ for (Annotation ann : field.getAnnotations()) {
+ boolean valid = "org.apache.camel.EndpointInject".equals(ann.getQualifiedName()) || "org.apache.camel.cdi.Uri".equals(ann.getQualifiedName());
+ if (valid) {
+ Expression exp = (Expression) ann.getInternal();
+ if (exp instanceof SingleMemberAnnotation) {
+ exp = ((SingleMemberAnnotation) exp).getValue();
+ } else if (exp instanceof NormalAnnotation) {
+ List values = ((NormalAnnotation) exp).values();
+ for (Object value : values) {
+ MemberValuePair pair = (MemberValuePair) value;
+ if ("uri".equals(pair.getName().toString())) {
+ exp = pair.getValue();
+ break;
+ }
+ }
+ }
+ if (exp != null) {
+ return getLiteralValue(clazz, block, exp);
+ }
+ }
+ }
+ }
+ // is the field an org.apache.camel.Endpoint type?
+ if ("Endpoint".equals(field.getType().getSimpleName())) {
+ // then grab the uri from the first argument
+ VariableDeclarationFragment vdf = (VariableDeclarationFragment) field.getInternal();
+ expression = vdf.getInitializer();
+ if (expression instanceof MethodInvocation) {
+ MethodInvocation mi = (MethodInvocation) expression;
+ List args = mi.arguments();
+ if (args != null && args.size() > 0) {
+ // the first argument has the endpoint uri
+ expression = (Expression) args.get(0);
+ return getLiteralValue(clazz, block, expression);
+ }
+ }
+ } else {
+ // no annotations so try its initializer
+ VariableDeclarationFragment vdf = (VariableDeclarationFragment) field.getInternal();
+ expression = vdf.getInitializer();
+ if (expression == null) {
+ // its a field which has no initializer, then add a dummy value assuming the field will be initialized at runtime
+ return "{{" + field.getName() + "}}";
+ } else {
+ return getLiteralValue(clazz, block, expression);
+ }
+ }
+ } else {
+ // we could not find the field in this class/method, so its maybe from some other super class, so insert a dummy value
+ final String fieldName = ((SimpleName) expression).getIdentifier();
+ return "{{" + fieldName + "}}";
+ }
+ } else if (expression instanceof InfixExpression) {
+ String answer = null;
+ // is it a string that is concat together?
+ InfixExpression ie = (InfixExpression) expression;
+ if (InfixExpression.Operator.PLUS.equals(ie.getOperator())) {
+
+ String val1 = getLiteralValue(clazz, block, ie.getLeftOperand());
+ String val2 = getLiteralValue(clazz, block, ie.getRightOperand());
+
+ // if numeric then we plus the values, otherwise we string concat
+ boolean numeric = isNumericOperator(clazz, block, ie.getLeftOperand()) && isNumericOperator(clazz, block, ie.getRightOperand());
+ if (numeric) {
+ Long num1 = val1 != null ? Long.valueOf(val1) : 0;
+ Long num2 = val2 != null ? Long.valueOf(val2) : 0;
+ answer = "" + (num1 + num2);
+ } else {
+ answer = (val1 != null ? val1 : "") + (val2 != null ? val2 : "");
+ }
+
+ if (!answer.isEmpty()) {
+ // include extended when we concat on 2 or more lines
+ List extended = ie.extendedOperands();
+ if (extended != null) {
+ for (Object ext : extended) {
+ String val3 = getLiteralValue(clazz, block, (Expression) ext);
+ if (numeric) {
+ Long num3 = val3 != null ? Long.valueOf(val3) : 0;
+ Long num = Long.valueOf(answer);
+ answer = "" + (num + num3);
+ } else {
+ answer += val3 != null ? val3 : "";
+ }
+ }
+ }
+ }
+ }
+ return answer;
+ }
+
+ return null;
+ }
+
+ private static boolean isNumericOperator(JavaClassSource clazz, Block block, Expression expression) {
+ if (expression instanceof NumberLiteral) {
+ return true;
+ } else if (expression instanceof SimpleName) {
+ FieldSource field = getField(clazz, block, (SimpleName) expression);
+ if (field != null) {
+ return field.getType().isType("int") || field.getType().isType("long")
+ || field.getType().isType("Integer") || field.getType().isType("Long");
+ }
+ }
+ return false;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/d8844048/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetails.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetails.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetails.java
new file mode 100644
index 0000000..76c3bba
--- /dev/null
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetails.java
@@ -0,0 +1,86 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.parser.model;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class CamelNodeDetails {
+
+ private final String name;
+ private List<CamelNodeDetails> outputs;
+ private CamelNodeDetails parent;
+
+ public CamelNodeDetails(CamelNodeDetails parent, String name) {
+ this.parent = parent;
+ this.name = name;
+ }
+
+ public void addPreliminaryOutput(CamelNodeDetails output) {
+ if (outputs == null) {
+ outputs = new ArrayList<>();
+ }
+ // the parser walks the EIPs backwards so add from the top
+ outputs.add(0, output);
+ }
+
+ public void addOutput(CamelNodeDetails output) {
+ if (outputs == null) {
+ outputs = new ArrayList<>();
+ }
+ outputs.add(output);
+ }
+
+ public CamelNodeDetails getParent() {
+ return parent;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public List<CamelNodeDetails> getOutputs() {
+ return outputs;
+ }
+
+ public String toString() {
+ return name;
+ }
+
+ public String dump(int level) {
+ StringBuilder sb = new StringBuilder();
+ sb.append(padString(level));
+ sb.append(name);
+ if (outputs != null) {
+ level++;
+ for (CamelNodeDetails child : outputs) {
+ String text = child.dump(level);
+ sb.append("\n");
+ sb.append(text);
+ }
+ }
+ return sb.toString();
+ }
+
+ private static String padString(int level) {
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < level; i++) {
+ sb.append(" ");
+ }
+ return sb.toString();
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/d8844048/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/MyJavaDslRouteBuilder.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/MyJavaDslRouteBuilder.java b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/MyJavaDslRouteBuilder.java
new file mode 100644
index 0000000..91ff71f
--- /dev/null
+++ b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/MyJavaDslRouteBuilder.java
@@ -0,0 +1,38 @@
+/**
+ * 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.parser.java;
+
+import org.apache.camel.builder.RouteBuilder;
+
+public class MyJavaDslRouteBuilder extends RouteBuilder {
+
+ @Override
+ public void configure() throws Exception {
+ from("timer:foo").routeId("bar")
+ .log("I was here")
+ .setHeader("foo", constant("123"))
+ .choice()
+ .when(header("foo"))
+ .toD("log:a")
+ .when(header("bar"))
+ .toD("log:b")
+ .otherwise()
+ .wireTap("mock:tap")
+ .end()
+ .to("log:b");
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/d8844048/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
new file mode 100644
index 0000000..841c608
--- /dev/null
+++ b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.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.parser.java;
+
+import java.io.File;
+
+import org.apache.camel.parser.AdvancedRouteBuilderParser;
+import org.apache.camel.parser.model.CamelNodeDetails;
+import org.jboss.forge.roaster.Roaster;
+import org.jboss.forge.roaster.model.source.JavaClassSource;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class RoasterJavaDslTest {
+
+ private static final Logger LOG = LoggerFactory.getLogger(RoasterJavaDslTest.class);
+
+ @Test
+ public void parse() throws Exception {
+ JavaClassSource clazz = (JavaClassSource) Roaster.parse(new File("src/test/java/org/apache/camel/parser/java/MyJavaDslRouteBuilder.java"));
+
+ CamelNodeDetails details = AdvancedRouteBuilderParser.parseRouteBuilder(clazz, true);
+ String tree = details.dump(0);
+ LOG.info("\n" + tree);
+
+ System.out.println(tree);
+ }
+
+}
[13/13] camel git commit: Rename and polish a bit
Posted by da...@apache.org.
Rename and polish a bit
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/32e0a716
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/32e0a716
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/32e0a716
Branch: refs/heads/parser2
Commit: 32e0a71623ee8d6dad8100a24f855c41db124f5f
Parents: 797b9f0
Author: Claus Ibsen <da...@apache.org>
Authored: Sun Oct 8 13:43:25 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun Oct 8 13:43:25 2017 +0200
----------------------------------------------------------------------
examples/camel-example-spring-boot/pom.xml | 5 +-
.../java/sample/camel/FooApplicationTest.java | 2 +-
.../org/apache/camel/maven/CoverageMojo.java | 411 -------------------
.../apache/camel/maven/RouteCoverageMojo.java | 411 +++++++++++++++++++
.../camel/maven/helper/CoverageHelper.java | 114 -----
.../camel/maven/helper/RouteCoverageHelper.java | 114 +++++
.../apache/camel/maven/model/CoverageNode.java | 58 ---
.../camel/maven/model/RouteCoverageNode.java | 58 +++
8 files changed, 587 insertions(+), 586 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/32e0a716/examples/camel-example-spring-boot/pom.xml
----------------------------------------------------------------------
diff --git a/examples/camel-example-spring-boot/pom.xml b/examples/camel-example-spring-boot/pom.xml
index 205cfb7..c1332a9 100644
--- a/examples/camel-example-spring-boot/pom.xml
+++ b/examples/camel-example-spring-boot/pom.xml
@@ -120,11 +120,12 @@
<groupId>org.apache.camel</groupId>
<artifactId>camel-maven-plugin</artifactId>
<version>${project.version}</version>
- <!-- allows to fail if not all routes are fully covered during testing
+ <!-- allows to fail if not all routes are fully covered during testing -->
+<!--
<configuration>
<failOnError>true</failOnError>
</configuration>
- -->
+-->
</plugin>
</plugins>
</build>
http://git-wip-us.apache.org/repos/asf/camel/blob/32e0a716/examples/camel-example-spring-boot/src/test/java/sample/camel/FooApplicationTest.java
----------------------------------------------------------------------
diff --git a/examples/camel-example-spring-boot/src/test/java/sample/camel/FooApplicationTest.java b/examples/camel-example-spring-boot/src/test/java/sample/camel/FooApplicationTest.java
index 48aaf29..e8f5897 100644
--- a/examples/camel-example-spring-boot/src/test/java/sample/camel/FooApplicationTest.java
+++ b/examples/camel-example-spring-boot/src/test/java/sample/camel/FooApplicationTest.java
@@ -34,7 +34,7 @@ import static org.junit.Assert.assertTrue;
@SpringBootTest(classes = SampleCamelApplication.class,
properties = "greeting = Hell foo")
@EnableRouteCoverage
-@Ignore
+@Ignore // enable me to run this test as well so we can cover testing the route completely
public class FooApplicationTest {
@Autowired
http://git-wip-us.apache.org/repos/asf/camel/blob/32e0a716/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/CoverageMojo.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/CoverageMojo.java b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/CoverageMojo.java
deleted file mode 100644
index 82392f6..0000000
--- a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/CoverageMojo.java
+++ /dev/null
@@ -1,411 +0,0 @@
-/**
- * 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.maven;
-
-import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.PrintStream;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.LinkedHashSet;
-import java.util.List;
-import java.util.Set;
-import java.util.concurrent.atomic.AtomicInteger;
-import java.util.stream.Collectors;
-
-import org.apache.camel.maven.helper.CoverageHelper;
-import org.apache.camel.maven.helper.EndpointHelper;
-import org.apache.camel.maven.model.CoverageNode;
-import org.apache.camel.parser.RouteBuilderParser;
-import org.apache.camel.parser.model.CamelEndpointDetails;
-import org.apache.camel.parser.model.CamelNodeDetails;
-import org.apache.camel.parser.model.CamelRouteDetails;
-import org.apache.camel.parser.model.CamelSimpleExpressionDetails;
-import org.apache.camel.util.KeyValueHolder;
-import org.apache.maven.model.Resource;
-import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.plugin.MojoFailureException;
-import org.apache.maven.project.MavenProject;
-import org.codehaus.mojo.exec.AbstractExecMojo;
-import org.jboss.forge.roaster.Roaster;
-import org.jboss.forge.roaster.model.JavaType;
-import org.jboss.forge.roaster.model.source.JavaClassSource;
-
-/**
- * Performs route coverage reports after running Camel unit tests with camel-test modules
- *
- * @goal coverage
- * @threadSafe
- */
-public class CoverageMojo extends AbstractExecMojo {
-
- /**
- * The maven project.
- *
- * @parameter property="project"
- * @required
- * @readonly
- */
- protected MavenProject project;
-
- /**
- * Whether to fail if a route was not fully covered
- *
- * @parameter property="camel.failOnError"
- * default-value="false"
- */
- private boolean failOnError;
-
- /**
- * Whether to include test source code
- *
- * @parameter property="camel.includeTest"
- * default-value="false"
- */
- private boolean includeTest;
-
- /**
- * To filter the names of java and xml files to only include files matching any of the given list of patterns (wildcard and regular expression).
- * Multiple values can be separated by comma.
- *
- * @parameter property="camel.includes"
- */
- private String includes;
-
- /**
- * To filter the names of java and xml files to exclude files matching any of the given list of patterns (wildcard and regular expression).
- * Multiple values can be separated by comma.
- *
- * @parameter property="camel.excludes"
- */
- private String excludes;
-
- // CHECKSTYLE:OFF
- @Override
- public void execute() throws MojoExecutionException, MojoFailureException {
-
- List<CamelEndpointDetails> endpoints = new ArrayList<>();
- List<CamelSimpleExpressionDetails> simpleExpressions = new ArrayList<>();
- List<CamelRouteDetails> routeIds = new ArrayList<>();
- Set<File> javaFiles = new LinkedHashSet<File>();
- Set<File> xmlFiles = new LinkedHashSet<File>();
-
- // find all java route builder classes
- List list = project.getCompileSourceRoots();
- for (Object obj : list) {
- String dir = (String) obj;
- findJavaFiles(new File(dir), javaFiles);
- }
- // find all xml routes
- list = project.getResources();
- for (Object obj : list) {
- Resource dir = (Resource) obj;
- findXmlFiles(new File(dir.getDirectory()), xmlFiles);
- }
-
- if (includeTest) {
- list = project.getTestCompileSourceRoots();
- for (Object obj : list) {
- String dir = (String) obj;
- findJavaFiles(new File(dir), javaFiles);
- }
- list = project.getTestResources();
- for (Object obj : list) {
- Resource dir = (Resource) obj;
- findXmlFiles(new File(dir.getDirectory()), xmlFiles);
- }
- }
-
- List<CamelNodeDetails> routeTrees = new ArrayList<>();
-
- for (File file : javaFiles) {
- if (matchFile(file)) {
- try {
-
- // parse the java source code and find Camel RouteBuilder classes
- String fqn = file.getPath();
- String baseDir = ".";
- JavaType out = Roaster.parse(file);
- // we should only parse java classes (not interfaces and enums etc)
- if (out != null && out instanceof JavaClassSource) {
- JavaClassSource clazz = (JavaClassSource) out;
- List<CamelNodeDetails> result = RouteBuilderParser.parseRouteBuilderTree(clazz, baseDir, fqn, true);
- routeTrees.addAll(result);
- }
- } catch (Exception e) {
- getLog().warn("Error parsing java file " + file + " code due " + e.getMessage(), e);
- }
- }
- }
- for (File file : xmlFiles) {
- if (matchFile(file)) {
- try {
- // TODO: implement me
- } catch (Exception e) {
- getLog().warn("Error parsing xml file " + file + " code due " + e.getMessage(), e);
- }
- }
- }
-
- getLog().info("Discovered " + routeTrees.size() + " routes");
-
- // skip any routes which has no route id assigned
-
- long anonymous = routeTrees.stream().filter(t -> t.getRouteId() == null).count();
- if (anonymous > 0) {
- getLog().warn("Discovered " + anonymous + " anonymous routes. Add route ids to these routes for route coverage support");
- }
-
- final AtomicInteger notCovered = new AtomicInteger();
-
- routeTrees = routeTrees.stream().filter(t -> t.getRouteId() != null).collect(Collectors.toList());
- for (CamelNodeDetails t : routeTrees) {
- String routeId = t.getRouteId();
- String fileName = asRelativeFile(t.getFileName());
-
- // grab dump data for the route
- try {
- List<KeyValueHolder<String, Integer>> coverageData = CoverageHelper.parseDumpRouteCoverageByRouteId("target/camel-route-coverage", routeId);
- if (coverageData.isEmpty()) {
- getLog().warn("No route coverage data found for route: " + routeId
- + ". Make sure to enable route coverage in your unit tests and assign unique route ids to your routes. Also remember to run unit tests first.");
- } else {
- List<CoverageNode> coverage = gatherRouteCoverageSummary(t, coverageData);
- String out = templateCoverageData(fileName, routeId, coverage, notCovered);
- getLog().info("Route coverage summary:\n\n" + out);
- getLog().info("");
- }
-
- } catch (Exception e) {
- throw new MojoExecutionException("Error during gathering route coverage data for route: " + routeId, e);
- }
- }
-
- if (failOnError && notCovered.get() > 0) {
- throw new MojoExecutionException("There are " + notCovered.get() + " route(s) not fully covered!");
- }
- }
- // CHECKSTYLE:ON
-
- @SuppressWarnings("unchecked")
- private String templateCoverageData(String fileName, String routeId, List<CoverageNode> model, AtomicInteger notCovered) throws MojoExecutionException {
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- PrintStream sw = new PrintStream(bos);
-
- sw.println("File: " + fileName);
- sw.println("Route: " + routeId);
- sw.println();
- sw.println(String.format("%8s %8s %s", "Line #", "Count", "Route"));
- sw.println(String.format("%8s %8s %s", "------", "-----", "-----"));
-
- int covered = 0;
- for (CoverageNode node : model) {
- if (node.getCount() > 0) {
- covered++;
- }
- String pad = padString(node.getLevel());
- sw.println(String.format("%8s %8s %s", node.getLineNumber(), node.getCount(), pad + node.getName()));
- }
-
- if (covered != model.size()) {
- // okay here is a route that was not fully covered
- notCovered.incrementAndGet();
- }
-
- // calculate percentage of route coverage (must use double to have decimals)
- double percentage = ((double) covered / (double) model.size()) * 100;
- sw.println();
- sw.println("Coverage: " + covered + " out of " + model.size() + " (" + String.format("%.1f", percentage) + "%)");
- sw.println();
-
- return bos.toString();
- }
-
- private static List<CoverageNode> gatherRouteCoverageSummary(CamelNodeDetails route, List<KeyValueHolder<String, Integer>> coverageData) {
- List<CoverageNode> answer = new ArrayList<>();
-
- Iterator<KeyValueHolder<String, Integer>> it = coverageData.iterator();
- AtomicInteger level = new AtomicInteger();
- gatherRouteCoverageSummary(route, it, level, answer);
- return answer;
- }
-
- private static void gatherRouteCoverageSummary(CamelNodeDetails node, Iterator<KeyValueHolder<String, Integer>> it, AtomicInteger level, List<CoverageNode> answer) {
- CoverageNode data = new CoverageNode();
- data.setName(node.getName());
- data.setLineNumber(Integer.valueOf(node.getLineNumber()));
- data.setLevel(level.get());
-
- // add data
- answer.add(data);
-
- // find count
- boolean found = false;
- while (!found && it.hasNext()) {
- KeyValueHolder<String, Integer> holder = it.next();
- found = holder.getKey().equals(node.getName());
- if (found) {
- data.setCount(holder.getValue());
- }
- }
-
- if (node.getOutputs() != null) {
- level.addAndGet(1);
- for (CamelNodeDetails child : node.getOutputs()) {
- gatherRouteCoverageSummary(child, it, level, answer);
- }
- level.addAndGet(-1);
- }
- }
-
- private static String padString(int level) {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < level; i++) {
- sb.append(" ");
- }
- return sb.toString();
- }
-
- private void findJavaFiles(File dir, Set<File> javaFiles) {
- File[] files = dir.isDirectory() ? dir.listFiles() : null;
- if (files != null) {
- for (File file : files) {
- if (file.getName().endsWith(".java")) {
- javaFiles.add(file);
- } else if (file.isDirectory()) {
- findJavaFiles(file, javaFiles);
- }
- }
- }
- }
-
- private void findXmlFiles(File dir, Set<File> xmlFiles) {
- File[] files = dir.isDirectory() ? dir.listFiles() : null;
- if (files != null) {
- for (File file : files) {
- if (file.getName().endsWith(".xml")) {
- xmlFiles.add(file);
- } else if (file.isDirectory()) {
- findXmlFiles(file, xmlFiles);
- }
- }
- }
- }
-
- private boolean matchFile(File file) {
- if (excludes == null && includes == null) {
- return true;
- }
-
- // exclude take precedence
- if (excludes != null) {
- for (String exclude : excludes.split(",")) {
- exclude = exclude.trim();
- // try both with and without directory in the name
- String fqn = stripRootPath(asRelativeFile(file.getAbsolutePath()));
- boolean match = EndpointHelper.matchPattern(fqn, exclude) || EndpointHelper.matchPattern(file.getName(), exclude);
- if (match) {
- return false;
- }
- }
- }
-
- // include
- if (includes != null) {
- for (String include : includes.split(",")) {
- include = include.trim();
- // try both with and without directory in the name
- String fqn = stripRootPath(asRelativeFile(file.getAbsolutePath()));
- boolean match = EndpointHelper.matchPattern(fqn, include) || EndpointHelper.matchPattern(file.getName(), include);
- if (match) {
- return true;
- }
- }
- // did not match any includes
- return false;
- }
-
- // was not excluded nor failed include so its accepted
- return true;
- }
-
- private String asRelativeFile(String name) {
- String answer = name;
-
- String base = project.getBasedir().getAbsolutePath();
- if (name.startsWith(base)) {
- answer = name.substring(base.length());
- // skip leading slash for relative path
- if (answer.startsWith(File.separator)) {
- answer = answer.substring(1);
- }
- }
- return answer;
- }
-
- private String stripRootPath(String name) {
- // strip out any leading source / resource directory
-
- List list = project.getCompileSourceRoots();
- for (Object obj : list) {
- String dir = (String) obj;
- dir = asRelativeFile(dir);
- if (name.startsWith(dir)) {
- return name.substring(dir.length() + 1);
- }
- }
- list = project.getTestCompileSourceRoots();
- for (Object obj : list) {
- String dir = (String) obj;
- dir = asRelativeFile(dir);
- if (name.startsWith(dir)) {
- return name.substring(dir.length() + 1);
- }
- }
- List resources = project.getResources();
- for (Object obj : resources) {
- Resource resource = (Resource) obj;
- String dir = asRelativeFile(resource.getDirectory());
- if (name.startsWith(dir)) {
- return name.substring(dir.length() + 1);
- }
- }
- resources = project.getTestResources();
- for (Object obj : resources) {
- Resource resource = (Resource) obj;
- String dir = asRelativeFile(resource.getDirectory());
- if (name.startsWith(dir)) {
- return name.substring(dir.length() + 1);
- }
- }
-
- return name;
- }
-
- private static String asPackageName(String name) {
- return name.replace(File.separator, ".");
- }
-
- private static String asSimpleClassName(String className) {
- int dot = className.lastIndexOf('.');
- if (dot > 0) {
- return className.substring(dot + 1);
- } else {
- return className;
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/camel/blob/32e0a716/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/RouteCoverageMojo.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/RouteCoverageMojo.java b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/RouteCoverageMojo.java
new file mode 100644
index 0000000..63688fd
--- /dev/null
+++ b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/RouteCoverageMojo.java
@@ -0,0 +1,411 @@
+/**
+ * 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.maven;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.Collectors;
+
+import org.apache.camel.maven.helper.RouteCoverageHelper;
+import org.apache.camel.maven.helper.EndpointHelper;
+import org.apache.camel.maven.model.RouteCoverageNode;
+import org.apache.camel.parser.RouteBuilderParser;
+import org.apache.camel.parser.model.CamelEndpointDetails;
+import org.apache.camel.parser.model.CamelNodeDetails;
+import org.apache.camel.parser.model.CamelRouteDetails;
+import org.apache.camel.parser.model.CamelSimpleExpressionDetails;
+import org.apache.camel.util.KeyValueHolder;
+import org.apache.maven.model.Resource;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.mojo.exec.AbstractExecMojo;
+import org.jboss.forge.roaster.Roaster;
+import org.jboss.forge.roaster.model.JavaType;
+import org.jboss.forge.roaster.model.source.JavaClassSource;
+
+/**
+ * Performs route coverage reports after running Camel unit tests with camel-test modules
+ *
+ * @goal route-coverage
+ * @threadSafe
+ */
+public class RouteCoverageMojo extends AbstractExecMojo {
+
+ /**
+ * The maven project.
+ *
+ * @parameter property="project"
+ * @required
+ * @readonly
+ */
+ protected MavenProject project;
+
+ /**
+ * Whether to fail if a route was not fully covered
+ *
+ * @parameter property="camel.failOnError"
+ * default-value="false"
+ */
+ private boolean failOnError;
+
+ /**
+ * Whether to include test source code
+ *
+ * @parameter property="camel.includeTest"
+ * default-value="false"
+ */
+ private boolean includeTest;
+
+ /**
+ * To filter the names of java and xml files to only include files matching any of the given list of patterns (wildcard and regular expression).
+ * Multiple values can be separated by comma.
+ *
+ * @parameter property="camel.includes"
+ */
+ private String includes;
+
+ /**
+ * To filter the names of java and xml files to exclude files matching any of the given list of patterns (wildcard and regular expression).
+ * Multiple values can be separated by comma.
+ *
+ * @parameter property="camel.excludes"
+ */
+ private String excludes;
+
+ // CHECKSTYLE:OFF
+ @Override
+ public void execute() throws MojoExecutionException, MojoFailureException {
+
+ List<CamelEndpointDetails> endpoints = new ArrayList<>();
+ List<CamelSimpleExpressionDetails> simpleExpressions = new ArrayList<>();
+ List<CamelRouteDetails> routeIds = new ArrayList<>();
+ Set<File> javaFiles = new LinkedHashSet<File>();
+ Set<File> xmlFiles = new LinkedHashSet<File>();
+
+ // find all java route builder classes
+ List list = project.getCompileSourceRoots();
+ for (Object obj : list) {
+ String dir = (String) obj;
+ findJavaFiles(new File(dir), javaFiles);
+ }
+ // find all xml routes
+ list = project.getResources();
+ for (Object obj : list) {
+ Resource dir = (Resource) obj;
+ findXmlFiles(new File(dir.getDirectory()), xmlFiles);
+ }
+
+ if (includeTest) {
+ list = project.getTestCompileSourceRoots();
+ for (Object obj : list) {
+ String dir = (String) obj;
+ findJavaFiles(new File(dir), javaFiles);
+ }
+ list = project.getTestResources();
+ for (Object obj : list) {
+ Resource dir = (Resource) obj;
+ findXmlFiles(new File(dir.getDirectory()), xmlFiles);
+ }
+ }
+
+ List<CamelNodeDetails> routeTrees = new ArrayList<>();
+
+ for (File file : javaFiles) {
+ if (matchFile(file)) {
+ try {
+
+ // parse the java source code and find Camel RouteBuilder classes
+ String fqn = file.getPath();
+ String baseDir = ".";
+ JavaType out = Roaster.parse(file);
+ // we should only parse java classes (not interfaces and enums etc)
+ if (out != null && out instanceof JavaClassSource) {
+ JavaClassSource clazz = (JavaClassSource) out;
+ List<CamelNodeDetails> result = RouteBuilderParser.parseRouteBuilderTree(clazz, baseDir, fqn, true);
+ routeTrees.addAll(result);
+ }
+ } catch (Exception e) {
+ getLog().warn("Error parsing java file " + file + " code due " + e.getMessage(), e);
+ }
+ }
+ }
+ for (File file : xmlFiles) {
+ if (matchFile(file)) {
+ try {
+ // TODO: implement me
+ } catch (Exception e) {
+ getLog().warn("Error parsing xml file " + file + " code due " + e.getMessage(), e);
+ }
+ }
+ }
+
+ getLog().info("Discovered " + routeTrees.size() + " routes");
+
+ // skip any routes which has no route id assigned
+
+ long anonymous = routeTrees.stream().filter(t -> t.getRouteId() == null).count();
+ if (anonymous > 0) {
+ getLog().warn("Discovered " + anonymous + " anonymous routes. Add route ids to these routes for route coverage support");
+ }
+
+ final AtomicInteger notCovered = new AtomicInteger();
+
+ routeTrees = routeTrees.stream().filter(t -> t.getRouteId() != null).collect(Collectors.toList());
+ for (CamelNodeDetails t : routeTrees) {
+ String routeId = t.getRouteId();
+ String fileName = asRelativeFile(t.getFileName());
+
+ // grab dump data for the route
+ try {
+ List<KeyValueHolder<String, Integer>> coverageData = RouteCoverageHelper.parseDumpRouteCoverageByRouteId("target/camel-route-coverage", routeId);
+ if (coverageData.isEmpty()) {
+ getLog().warn("No route coverage data found for route: " + routeId
+ + ". Make sure to enable route coverage in your unit tests and assign unique route ids to your routes. Also remember to run unit tests first.");
+ } else {
+ List<RouteCoverageNode> coverage = gatherRouteCoverageSummary(t, coverageData);
+ String out = templateCoverageData(fileName, routeId, coverage, notCovered);
+ getLog().info("Route coverage summary:\n\n" + out);
+ getLog().info("");
+ }
+
+ } catch (Exception e) {
+ throw new MojoExecutionException("Error during gathering route coverage data for route: " + routeId, e);
+ }
+ }
+
+ if (failOnError && notCovered.get() > 0) {
+ throw new MojoExecutionException("There are " + notCovered.get() + " route(s) not fully covered!");
+ }
+ }
+ // CHECKSTYLE:ON
+
+ @SuppressWarnings("unchecked")
+ private String templateCoverageData(String fileName, String routeId, List<RouteCoverageNode> model, AtomicInteger notCovered) throws MojoExecutionException {
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ PrintStream sw = new PrintStream(bos);
+
+ sw.println("File: " + fileName);
+ sw.println("Route: " + routeId);
+ sw.println();
+ sw.println(String.format("%8s %8s %s", "Line #", "Count", "Route"));
+ sw.println(String.format("%8s %8s %s", "------", "-----", "-----"));
+
+ int covered = 0;
+ for (RouteCoverageNode node : model) {
+ if (node.getCount() > 0) {
+ covered++;
+ }
+ String pad = padString(node.getLevel());
+ sw.println(String.format("%8s %8s %s", node.getLineNumber(), node.getCount(), pad + node.getName()));
+ }
+
+ if (covered != model.size()) {
+ // okay here is a route that was not fully covered
+ notCovered.incrementAndGet();
+ }
+
+ // calculate percentage of route coverage (must use double to have decimals)
+ double percentage = ((double) covered / (double) model.size()) * 100;
+ sw.println();
+ sw.println("Coverage: " + covered + " out of " + model.size() + " (" + String.format("%.1f", percentage) + "%)");
+ sw.println();
+
+ return bos.toString();
+ }
+
+ private static List<RouteCoverageNode> gatherRouteCoverageSummary(CamelNodeDetails route, List<KeyValueHolder<String, Integer>> coverageData) {
+ List<RouteCoverageNode> answer = new ArrayList<>();
+
+ Iterator<KeyValueHolder<String, Integer>> it = coverageData.iterator();
+ AtomicInteger level = new AtomicInteger();
+ gatherRouteCoverageSummary(route, it, level, answer);
+ return answer;
+ }
+
+ private static void gatherRouteCoverageSummary(CamelNodeDetails node, Iterator<KeyValueHolder<String, Integer>> it, AtomicInteger level, List<RouteCoverageNode> answer) {
+ RouteCoverageNode data = new RouteCoverageNode();
+ data.setName(node.getName());
+ data.setLineNumber(Integer.valueOf(node.getLineNumber()));
+ data.setLevel(level.get());
+
+ // add data
+ answer.add(data);
+
+ // find count
+ boolean found = false;
+ while (!found && it.hasNext()) {
+ KeyValueHolder<String, Integer> holder = it.next();
+ found = holder.getKey().equals(node.getName());
+ if (found) {
+ data.setCount(holder.getValue());
+ }
+ }
+
+ if (node.getOutputs() != null) {
+ level.addAndGet(1);
+ for (CamelNodeDetails child : node.getOutputs()) {
+ gatherRouteCoverageSummary(child, it, level, answer);
+ }
+ level.addAndGet(-1);
+ }
+ }
+
+ private static String padString(int level) {
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < level; i++) {
+ sb.append(" ");
+ }
+ return sb.toString();
+ }
+
+ private void findJavaFiles(File dir, Set<File> javaFiles) {
+ File[] files = dir.isDirectory() ? dir.listFiles() : null;
+ if (files != null) {
+ for (File file : files) {
+ if (file.getName().endsWith(".java")) {
+ javaFiles.add(file);
+ } else if (file.isDirectory()) {
+ findJavaFiles(file, javaFiles);
+ }
+ }
+ }
+ }
+
+ private void findXmlFiles(File dir, Set<File> xmlFiles) {
+ File[] files = dir.isDirectory() ? dir.listFiles() : null;
+ if (files != null) {
+ for (File file : files) {
+ if (file.getName().endsWith(".xml")) {
+ xmlFiles.add(file);
+ } else if (file.isDirectory()) {
+ findXmlFiles(file, xmlFiles);
+ }
+ }
+ }
+ }
+
+ private boolean matchFile(File file) {
+ if (excludes == null && includes == null) {
+ return true;
+ }
+
+ // exclude take precedence
+ if (excludes != null) {
+ for (String exclude : excludes.split(",")) {
+ exclude = exclude.trim();
+ // try both with and without directory in the name
+ String fqn = stripRootPath(asRelativeFile(file.getAbsolutePath()));
+ boolean match = EndpointHelper.matchPattern(fqn, exclude) || EndpointHelper.matchPattern(file.getName(), exclude);
+ if (match) {
+ return false;
+ }
+ }
+ }
+
+ // include
+ if (includes != null) {
+ for (String include : includes.split(",")) {
+ include = include.trim();
+ // try both with and without directory in the name
+ String fqn = stripRootPath(asRelativeFile(file.getAbsolutePath()));
+ boolean match = EndpointHelper.matchPattern(fqn, include) || EndpointHelper.matchPattern(file.getName(), include);
+ if (match) {
+ return true;
+ }
+ }
+ // did not match any includes
+ return false;
+ }
+
+ // was not excluded nor failed include so its accepted
+ return true;
+ }
+
+ private String asRelativeFile(String name) {
+ String answer = name;
+
+ String base = project.getBasedir().getAbsolutePath();
+ if (name.startsWith(base)) {
+ answer = name.substring(base.length());
+ // skip leading slash for relative path
+ if (answer.startsWith(File.separator)) {
+ answer = answer.substring(1);
+ }
+ }
+ return answer;
+ }
+
+ private String stripRootPath(String name) {
+ // strip out any leading source / resource directory
+
+ List list = project.getCompileSourceRoots();
+ for (Object obj : list) {
+ String dir = (String) obj;
+ dir = asRelativeFile(dir);
+ if (name.startsWith(dir)) {
+ return name.substring(dir.length() + 1);
+ }
+ }
+ list = project.getTestCompileSourceRoots();
+ for (Object obj : list) {
+ String dir = (String) obj;
+ dir = asRelativeFile(dir);
+ if (name.startsWith(dir)) {
+ return name.substring(dir.length() + 1);
+ }
+ }
+ List resources = project.getResources();
+ for (Object obj : resources) {
+ Resource resource = (Resource) obj;
+ String dir = asRelativeFile(resource.getDirectory());
+ if (name.startsWith(dir)) {
+ return name.substring(dir.length() + 1);
+ }
+ }
+ resources = project.getTestResources();
+ for (Object obj : resources) {
+ Resource resource = (Resource) obj;
+ String dir = asRelativeFile(resource.getDirectory());
+ if (name.startsWith(dir)) {
+ return name.substring(dir.length() + 1);
+ }
+ }
+
+ return name;
+ }
+
+ private static String asPackageName(String name) {
+ return name.replace(File.separator, ".");
+ }
+
+ private static String asSimpleClassName(String className) {
+ int dot = className.lastIndexOf('.');
+ if (dot > 0) {
+ return className.substring(dot + 1);
+ } else {
+ return className;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/32e0a716/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/helper/CoverageHelper.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/helper/CoverageHelper.java b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/helper/CoverageHelper.java
deleted file mode 100644
index 33913d4..0000000
--- a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/helper/CoverageHelper.java
+++ /dev/null
@@ -1,114 +0,0 @@
-/**
- * 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.maven.helper;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.concurrent.atomic.AtomicInteger;
-
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-
-import org.apache.camel.catalog.CamelCatalog;
-import org.apache.camel.catalog.DefaultCamelCatalog;
-import org.apache.camel.util.IOHelper;
-import org.apache.camel.util.KeyValueHolder;
-import org.apache.camel.util.XmlLineNumberParser;
-
-public final class CoverageHelper {
-
- private CoverageHelper() {
- }
-
- public static List<KeyValueHolder<String, Integer>> parseDumpRouteCoverageByRouteId(String directory, String routeId) throws Exception {
- List<KeyValueHolder<String, Integer>> answer = new ArrayList<>();
-
- File[] files = new File(directory).listFiles(f -> f.getName().endsWith(".xml"));
- if (files == null) {
- return answer;
- }
-
- CamelCatalog catalog = new DefaultCamelCatalog(true);
-
- for (File file : files) {
- FileInputStream fis = new FileInputStream(file);
- Document dom = XmlLineNumberParser.parseXml(fis);
- IOHelper.close(fis);
- NodeList routes = dom.getElementsByTagName("route");
- for (int i = 0; i < routes.getLength(); i++) {
- Node route = routes.item(i);
- String id = route.getAttributes().getNamedItem("id").getNodeValue();
- // must be the target route
- if (routeId.equals(id)) {
- // parse each route and build a Map<String, Integer> with the no of messages processed
- // where String is the EIP name
- AtomicInteger counter = new AtomicInteger();
- parseRouteData(catalog, route, answer, counter);
- }
- }
- }
-
- return answer;
- }
-
- private static void parseRouteData(CamelCatalog catalog, Node node, List<KeyValueHolder<String, Integer>> data, AtomicInteger counter) {
- // must be a known EIP model
- String key = node.getNodeName();
- boolean valid = catalog.findModelNames().contains(key); // skip route as we use from instead
- if (!valid) {
- return;
- }
-
- // only calculate for elements within the route
- if (!"route".equals(key)) {
- Integer count = 0;
- Node total = node.getAttributes().getNamedItem("exchangesTotal");
- if (total != null) {
- count = Integer.valueOf(total.getNodeValue());
- }
- KeyValueHolder<String, Integer> holder = data.size() > counter.get() ? data.get(counter.get()) : null;
- if (holder != null && holder.getKey().equals(key)) {
- count += holder.getValue();
- }
- if (holder == null) {
- // add new
- data.add(counter.get(), new KeyValueHolder<>(key, count));
- } else {
- // replace existing
- data.set(counter.get(), new KeyValueHolder<>(key, count));
- }
- // advance counter
- counter.incrementAndGet();
- }
-
- // any children
- NodeList children = node.getChildNodes();
- if (children != null) {
- for (int i = 0; i < children.getLength(); i++) {
- Node child = children.item(i);
- if (child instanceof Element) {
- parseRouteData(catalog, child, data, counter);
- }
- }
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/camel/blob/32e0a716/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/helper/RouteCoverageHelper.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/helper/RouteCoverageHelper.java b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/helper/RouteCoverageHelper.java
new file mode 100644
index 0000000..d5a2971
--- /dev/null
+++ b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/helper/RouteCoverageHelper.java
@@ -0,0 +1,114 @@
+/**
+ * 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.maven.helper;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import org.apache.camel.catalog.CamelCatalog;
+import org.apache.camel.catalog.DefaultCamelCatalog;
+import org.apache.camel.util.IOHelper;
+import org.apache.camel.util.KeyValueHolder;
+import org.apache.camel.util.XmlLineNumberParser;
+
+public final class RouteCoverageHelper {
+
+ private RouteCoverageHelper() {
+ }
+
+ public static List<KeyValueHolder<String, Integer>> parseDumpRouteCoverageByRouteId(String directory, String routeId) throws Exception {
+ List<KeyValueHolder<String, Integer>> answer = new ArrayList<>();
+
+ File[] files = new File(directory).listFiles(f -> f.getName().endsWith(".xml"));
+ if (files == null) {
+ return answer;
+ }
+
+ CamelCatalog catalog = new DefaultCamelCatalog(true);
+
+ for (File file : files) {
+ FileInputStream fis = new FileInputStream(file);
+ Document dom = XmlLineNumberParser.parseXml(fis);
+ IOHelper.close(fis);
+ NodeList routes = dom.getElementsByTagName("route");
+ for (int i = 0; i < routes.getLength(); i++) {
+ Node route = routes.item(i);
+ String id = route.getAttributes().getNamedItem("id").getNodeValue();
+ // must be the target route
+ if (routeId.equals(id)) {
+ // parse each route and build a Map<String, Integer> with the no of messages processed
+ // where String is the EIP name
+ AtomicInteger counter = new AtomicInteger();
+ parseRouteData(catalog, route, answer, counter);
+ }
+ }
+ }
+
+ return answer;
+ }
+
+ private static void parseRouteData(CamelCatalog catalog, Node node, List<KeyValueHolder<String, Integer>> data, AtomicInteger counter) {
+ // must be a known EIP model
+ String key = node.getNodeName();
+ boolean valid = catalog.findModelNames().contains(key); // skip route as we use from instead
+ if (!valid) {
+ return;
+ }
+
+ // only calculate for elements within the route
+ if (!"route".equals(key)) {
+ Integer count = 0;
+ Node total = node.getAttributes().getNamedItem("exchangesTotal");
+ if (total != null) {
+ count = Integer.valueOf(total.getNodeValue());
+ }
+ KeyValueHolder<String, Integer> holder = data.size() > counter.get() ? data.get(counter.get()) : null;
+ if (holder != null && holder.getKey().equals(key)) {
+ count += holder.getValue();
+ }
+ if (holder == null) {
+ // add new
+ data.add(counter.get(), new KeyValueHolder<>(key, count));
+ } else {
+ // replace existing
+ data.set(counter.get(), new KeyValueHolder<>(key, count));
+ }
+ // advance counter
+ counter.incrementAndGet();
+ }
+
+ // any children
+ NodeList children = node.getChildNodes();
+ if (children != null) {
+ for (int i = 0; i < children.getLength(); i++) {
+ Node child = children.item(i);
+ if (child instanceof Element) {
+ parseRouteData(catalog, child, data, counter);
+ }
+ }
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/32e0a716/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/model/CoverageNode.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/model/CoverageNode.java b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/model/CoverageNode.java
deleted file mode 100644
index 9b26dd7..0000000
--- a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/model/CoverageNode.java
+++ /dev/null
@@ -1,58 +0,0 @@
-/**
- * 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.maven.model;
-
-public final class CoverageNode {
-
- private String name;
- private int lineNumber;
- private int count;
- private int level;
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getLineNumber() {
- return lineNumber;
- }
-
- public void setLineNumber(int lineNumber) {
- this.lineNumber = lineNumber;
- }
-
- public int getCount() {
- return count;
- }
-
- public void setCount(int count) {
- this.count = count;
- }
-
- public int getLevel() {
- return level;
- }
-
- public void setLevel(int level) {
- this.level = level;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/camel/blob/32e0a716/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/model/RouteCoverageNode.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/model/RouteCoverageNode.java b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/model/RouteCoverageNode.java
new file mode 100644
index 0000000..0f711b2
--- /dev/null
+++ b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/model/RouteCoverageNode.java
@@ -0,0 +1,58 @@
+/**
+ * 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.maven.model;
+
+public final class RouteCoverageNode {
+
+ private String name;
+ private int lineNumber;
+ private int count;
+ private int level;
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public int getLineNumber() {
+ return lineNumber;
+ }
+
+ public void setLineNumber(int lineNumber) {
+ this.lineNumber = lineNumber;
+ }
+
+ public int getCount() {
+ return count;
+ }
+
+ public void setCount(int count) {
+ this.count = count;
+ }
+
+ public int getLevel() {
+ return level;
+ }
+
+ public void setLevel(int level) {
+ this.level = level;
+ }
+
+}
[04/13] camel git commit: Camel java dsl parser prototype
Posted by da...@apache.org.
Camel java dsl parser prototype
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/84cb66bf
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/84cb66bf
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/84cb66bf
Branch: refs/heads/parser2
Commit: 84cb66bfdd450f142632ebfe0ca7c2a2df4c53d9
Parents: 7aef656
Author: Claus Ibsen <da...@apache.org>
Authored: Sat Oct 7 18:05:59 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat Oct 7 18:05:59 2017 +0200
----------------------------------------------------------------------
.../management/mbean/RouteCoverageXmlParser.java | 15 ++++++++++++++-
.../parser/helper/AdvancedCamelJavaParserHelper.java | 6 +++---
2 files changed, 17 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/84cb66bf/camel-core/src/main/java/org/apache/camel/management/mbean/RouteCoverageXmlParser.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/management/mbean/RouteCoverageXmlParser.java b/camel-core/src/main/java/org/apache/camel/management/mbean/RouteCoverageXmlParser.java
index c260e67..a28ff71 100644
--- a/camel-core/src/main/java/org/apache/camel/management/mbean/RouteCoverageXmlParser.java
+++ b/camel-core/src/main/java/org/apache/camel/management/mbean/RouteCoverageXmlParser.java
@@ -96,7 +96,20 @@ public final class RouteCoverageXmlParser {
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) {
http://git-wip-us.apache.org/repos/asf/camel/blob/84cb66bf/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java
index 322997b..e57a09b 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java
@@ -88,13 +88,13 @@ public final class AdvancedCamelJavaParserHelper {
}
}
- // now parse the route node and build a tree structure of the EIPs
- String root = route.getOutputs().get(0).getName();
+ // now parse the route node and build the correct model/tree structure of the EIPs
// re-create factory as we rebuild the tree
nodeFactory = CamelNodeDetailsFactory.newInstance();
- CamelNodeDetails answer = nodeFactory.newNode(null, root);
+ CamelNodeDetails from = route.getOutputs().get(0);
+ CamelNodeDetails answer = nodeFactory.copyNode(null, "from", from);
answer.setFileName(fullyQualifiedFileName);
CamelNodeDetails parent = answer;
[02/13] camel git commit: Camel java dsl parser prototype
Posted by da...@apache.org.
Camel java dsl parser prototype
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/3d3bc4bf
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/3d3bc4bf
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/3d3bc4bf
Branch: refs/heads/parser2
Commit: 3d3bc4bf35fa222ff78eb3711734caf12430577d
Parents: d884404
Author: Claus Ibsen <da...@apache.org>
Authored: Sat Oct 7 16:59:27 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat Oct 7 16:59:27 2017 +0200
----------------------------------------------------------------------
.../mbean/RouteCoverageXmlParser.java | 2 ++
.../helper/AdvancedCamelJavaParserHelper.java | 31 +++++++++---------
.../camel/parser/model/CamelNodeDetails.java | 12 +++++--
.../parser/model/CamelNodeDetailsFactory.java | 33 ++++++++++++++++++++
.../parser/java/MyJavaDslRouteBuilder.java | 6 ++--
.../camel/parser/java/RoasterJavaDslTest.java | 19 ++++++++++-
6 files changed, 83 insertions(+), 20 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/3d3bc4bf/camel-core/src/main/java/org/apache/camel/management/mbean/RouteCoverageXmlParser.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/management/mbean/RouteCoverageXmlParser.java b/camel-core/src/main/java/org/apache/camel/management/mbean/RouteCoverageXmlParser.java
index 4633ece..c260e67 100644
--- a/camel-core/src/main/java/org/apache/camel/management/mbean/RouteCoverageXmlParser.java
+++ b/camel-core/src/main/java/org/apache/camel/management/mbean/RouteCoverageXmlParser.java
@@ -104,6 +104,8 @@ public final class RouteCoverageXmlParser {
el.setAttribute("exchangesTotal", "" + total);
long totalTime = processor.getTotalProcessingTime();
el.setAttribute("totalProcessingTime", "" + totalTime);
+ int index = processor.getIndex();
+ el.setAttribute("index", "" + index);
}
}
} catch (Exception e) {
http://git-wip-us.apache.org/repos/asf/camel/blob/3d3bc4bf/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java
index 1a5fa0b..c264930 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java
@@ -24,6 +24,7 @@ import org.apache.camel.catalog.DefaultCamelCatalog;
import org.apache.camel.catalog.JSonSchemaHelper;
import org.apache.camel.parser.RouteBuilderParser;
import org.apache.camel.parser.model.CamelNodeDetails;
+import org.apache.camel.parser.model.CamelNodeDetailsFactory;
import org.apache.camel.parser.roaster.StatementFieldSource;
import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ASTNode;
import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
@@ -63,8 +64,9 @@ public final class AdvancedCamelJavaParserHelper {
public CamelNodeDetails parseCamelRoute(JavaClassSource clazz, MethodSource<JavaClassSource> method) {
// find any from which is the start of the route
+ CamelNodeDetailsFactory nodeFactory = CamelNodeDetailsFactory.newInstance();
- CamelNodeDetails route = new CamelNodeDetails(null, "route");
+ CamelNodeDetails route = nodeFactory.newNode(null, "route");
if (method != null) {
MethodDeclaration md = (MethodDeclaration) method.getInternal();
@@ -75,8 +77,7 @@ public final class AdvancedCamelJavaParserHelper {
if (statement instanceof ExpressionStatement) {
ExpressionStatement es = (ExpressionStatement) statement;
Expression exp = es.getExpression();
-
- parseExpression(clazz, block, exp, route);
+ parseExpression(nodeFactory, clazz, block, exp, route);
}
}
}
@@ -85,7 +86,7 @@ public final class AdvancedCamelJavaParserHelper {
// now parse the route node and build a tree structure of the EIPs
String root = route.getOutputs().get(0).getName();
- CamelNodeDetails answer = new CamelNodeDetails(null, root);
+ CamelNodeDetails answer = nodeFactory.newNode(null, root);
CamelNodeDetails parent = answer;
// TODO: use camel catalog to know about these types and when to do what
@@ -96,17 +97,17 @@ public final class AdvancedCamelJavaParserHelper {
// special for some EIPs
if ("choice".equals(name)) {
- CamelNodeDetails output = new CamelNodeDetails(parent, name);
+ CamelNodeDetails output = nodeFactory.newNode(parent, name);
parent.addOutput(output);
parent = output;
} else if ("when".equals(name)) {
parent = grandParent(parent, "choice");
- CamelNodeDetails output = new CamelNodeDetails(parent, name);
+ CamelNodeDetails output = nodeFactory.newNode(parent, name);
parent.addOutput(output);
parent = output;
} else if ("otherwise".equals(name)) {
parent = grandParent(parent, "choice");
- CamelNodeDetails output = new CamelNodeDetails(parent, name);
+ CamelNodeDetails output = nodeFactory.newNode(parent, name);
parent.addOutput(output);
parent = output;
} else if ("end".equals(name) || "endChoice".equals(name) || "endDoTry".equals(name)) {
@@ -120,12 +121,12 @@ public final class AdvancedCamelJavaParserHelper {
boolean hasOutput = hasOutput(name);
if (hasOutput) {
// has output so add as new child node
- CamelNodeDetails output = new CamelNodeDetails(parent, name);
+ CamelNodeDetails output = nodeFactory.newNode(parent, name);
parent.addOutput(output);
parent = output;
} else {
// add straight to itself
- CamelNodeDetails output = new CamelNodeDetails(parent, name);
+ CamelNodeDetails output = nodeFactory.newNode(parent, name);
parent.addOutput(output);
}
}
@@ -160,20 +161,22 @@ public final class AdvancedCamelJavaParserHelper {
}
}
- private void parseExpression(JavaClassSource clazz, Block block, Expression exp, CamelNodeDetails node) {
+ private void parseExpression(CamelNodeDetailsFactory nodeFactory,
+ JavaClassSource clazz, Block block, Expression exp, CamelNodeDetails node) {
if (exp == null) {
return;
}
if (exp instanceof MethodInvocation) {
MethodInvocation mi = (MethodInvocation) exp;
- node = doParseCamelModels(clazz, block, mi, node);
+ node = doParseCamelModels(nodeFactory, clazz, block, mi, node);
// if the method was called on another method, then recursive
exp = mi.getExpression();
- parseExpression(clazz, block, exp, node);
+ parseExpression(nodeFactory, clazz, block, exp, node);
}
}
- private CamelNodeDetails doParseCamelModels(JavaClassSource clazz, Block block, MethodInvocation mi, CamelNodeDetails node) {
+ private CamelNodeDetails doParseCamelModels(CamelNodeDetailsFactory nodeFactory,
+ JavaClassSource clazz, Block block, MethodInvocation mi, CamelNodeDetails node) {
String name = mi.getName().getIdentifier();
// special for Java DSL having some endXXX
@@ -181,7 +184,7 @@ public final class AdvancedCamelJavaParserHelper {
// only include if its a known Camel model
if (isEnd || camelCatalog.findModelNames().contains(name)) {
- CamelNodeDetails newNode = new CamelNodeDetails(node, name);
+ CamelNodeDetails newNode = nodeFactory.newNode(node, name);
node.addPreliminaryOutput(newNode);
return node;
}
http://git-wip-us.apache.org/repos/asf/camel/blob/3d3bc4bf/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetails.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetails.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetails.java
index 76c3bba..9712347 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetails.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetails.java
@@ -21,13 +21,15 @@ import java.util.List;
public class CamelNodeDetails {
+ private final CamelNodeDetails parent;
private final String name;
+ private final int order;
private List<CamelNodeDetails> outputs;
- private CamelNodeDetails parent;
- public CamelNodeDetails(CamelNodeDetails parent, String name) {
+ public CamelNodeDetails(CamelNodeDetails parent, String name, int order) {
this.parent = parent;
this.name = name;
+ this.order = order;
}
public void addPreliminaryOutput(CamelNodeDetails output) {
@@ -53,6 +55,10 @@ public class CamelNodeDetails {
return name;
}
+ public int getOrder() {
+ return order;
+ }
+
public List<CamelNodeDetails> getOutputs() {
return outputs;
}
@@ -63,6 +69,8 @@ public class CamelNodeDetails {
public String dump(int level) {
StringBuilder sb = new StringBuilder();
+ sb.append(order);
+ sb.append("\t");
sb.append(padString(level));
sb.append(name);
if (outputs != null) {
http://git-wip-us.apache.org/repos/asf/camel/blob/3d3bc4bf/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetailsFactory.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetailsFactory.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetailsFactory.java
new file mode 100644
index 0000000..502c2c7
--- /dev/null
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetailsFactory.java
@@ -0,0 +1,33 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.parser.model;
+
+public class CamelNodeDetailsFactory {
+
+ private int order;
+
+ private CamelNodeDetailsFactory() {
+ }
+
+ public static CamelNodeDetailsFactory newInstance() {
+ return new CamelNodeDetailsFactory();
+ }
+
+ public CamelNodeDetails newNode(CamelNodeDetails parent, String name) {
+ return new CamelNodeDetails(parent, name, ++order);
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/3d3bc4bf/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/MyJavaDslRouteBuilder.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/MyJavaDslRouteBuilder.java b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/MyJavaDslRouteBuilder.java
index 91ff71f..1a98ab8 100644
--- a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/MyJavaDslRouteBuilder.java
+++ b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/MyJavaDslRouteBuilder.java
@@ -22,7 +22,7 @@ public class MyJavaDslRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
- from("timer:foo").routeId("bar")
+ from("direct:start").routeId("bar")
.log("I was here")
.setHeader("foo", constant("123"))
.choice()
@@ -31,8 +31,8 @@ public class MyJavaDslRouteBuilder extends RouteBuilder {
.when(header("bar"))
.toD("log:b")
.otherwise()
- .wireTap("mock:tap")
+ .log("none")
.end()
- .to("log:b");
+ .to("mock:result");
}
}
http://git-wip-us.apache.org/repos/asf/camel/blob/3d3bc4bf/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
index 841c608..48e3a3c 100644
--- a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
+++ b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
@@ -20,16 +20,22 @@ import java.io.File;
import org.apache.camel.parser.AdvancedRouteBuilderParser;
import org.apache.camel.parser.model.CamelNodeDetails;
+import org.apache.camel.test.junit4.CamelTestSupport;
import org.jboss.forge.roaster.Roaster;
import org.jboss.forge.roaster.model.source.JavaClassSource;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class RoasterJavaDslTest {
+public class RoasterJavaDslTest extends CamelTestSupport {
private static final Logger LOG = LoggerFactory.getLogger(RoasterJavaDslTest.class);
+ @Override
+ public boolean isDumpRouteCoverage() {
+ return true;
+ }
+
@Test
public void parse() throws Exception {
JavaClassSource clazz = (JavaClassSource) Roaster.parse(new File("src/test/java/org/apache/camel/parser/java/MyJavaDslRouteBuilder.java"));
@@ -41,4 +47,15 @@ public class RoasterJavaDslTest {
System.out.println(tree);
}
+ @Test
+ public void testRouteCoverage() throws Exception {
+ context.addRoutes(new MyJavaDslRouteBuilder());
+
+ getMockEndpoint("mock:result").expectedMessageCount(1);
+
+ template.sendBody("direct:start", "Hello World");
+
+ assertMockEndpointsSatisfied();
+ }
+
}
[03/13] camel git commit: Camel java dsl parser prototype
Posted by da...@apache.org.
Camel java dsl parser prototype
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/7aef6567
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/7aef6567
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/7aef6567
Branch: refs/heads/parser2
Commit: 7aef65670715aa28c838440c5ed8e39cbb8a9488
Parents: 3d3bc4b
Author: Claus Ibsen <da...@apache.org>
Authored: Sat Oct 7 17:55:08 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat Oct 7 17:55:08 2017 +0200
----------------------------------------------------------------------
.../parser/AdvancedRouteBuilderParser.java | 5 +-
.../helper/AdvancedCamelJavaParserHelper.java | 73 ++++++++++++++++----
.../camel/parser/model/CamelNodeDetails.java | 41 ++++++++++-
.../parser/model/CamelNodeDetailsFactory.java | 4 ++
.../camel/parser/java/RoasterJavaDslTest.java | 5 +-
5 files changed, 110 insertions(+), 18 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/7aef6567/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/AdvancedRouteBuilderParser.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/AdvancedRouteBuilderParser.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/AdvancedRouteBuilderParser.java
index e3fb6d7..9159203 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/AdvancedRouteBuilderParser.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/AdvancedRouteBuilderParser.java
@@ -31,7 +31,8 @@ import org.jboss.forge.roaster.model.source.MethodSource;
public class AdvancedRouteBuilderParser {
// TODO: list of details, on per route
- public static CamelNodeDetails parseRouteBuilder(JavaClassSource clazz, boolean includeInlinedRouteBuilders) {
+ public static CamelNodeDetails parseRouteBuilder(JavaClassSource clazz, String baseDir, String fullyQualifiedFileName,
+ boolean includeInlinedRouteBuilders) {
AdvancedCamelJavaParserHelper parser = new AdvancedCamelJavaParserHelper();
List<MethodSource<JavaClassSource>> methods = new ArrayList<>();
@@ -47,7 +48,7 @@ public class AdvancedRouteBuilderParser {
}
for (MethodSource<JavaClassSource> configureMethod : methods) {
- CamelNodeDetails details = parser.parseCamelRoute(clazz, configureMethod);
+ CamelNodeDetails details = parser.parseCamelRoute(clazz, baseDir, fullyQualifiedFileName, configureMethod);
return details;
}
http://git-wip-us.apache.org/repos/asf/camel/blob/7aef6567/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java
index c264930..322997b 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java
@@ -16,6 +16,9 @@
*/
package org.apache.camel.parser.helper;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
import java.util.List;
import java.util.Map;
@@ -57,11 +60,13 @@ import org.jboss.forge.roaster.model.source.MethodSource;
* <p/>
* This implementation is lower level details. For a higher level parser see {@link RouteBuilderParser}.
*/
+// TODO: rename this class
public final class AdvancedCamelJavaParserHelper {
private CamelCatalog camelCatalog = new DefaultCamelCatalog(true);
- public CamelNodeDetails parseCamelRoute(JavaClassSource clazz, MethodSource<JavaClassSource> method) {
+ public CamelNodeDetails parseCamelRoute(JavaClassSource clazz, String baseDir, String fullyQualifiedFileName,
+ MethodSource<JavaClassSource> method) {
// find any from which is the start of the route
CamelNodeDetailsFactory nodeFactory = CamelNodeDetailsFactory.newInstance();
@@ -77,7 +82,7 @@ public final class AdvancedCamelJavaParserHelper {
if (statement instanceof ExpressionStatement) {
ExpressionStatement es = (ExpressionStatement) statement;
Expression exp = es.getExpression();
- parseExpression(nodeFactory, clazz, block, exp, route);
+ parseExpression(nodeFactory, fullyQualifiedFileName, clazz, block, exp, route);
}
}
}
@@ -86,28 +91,32 @@ public final class AdvancedCamelJavaParserHelper {
// now parse the route node and build a tree structure of the EIPs
String root = route.getOutputs().get(0).getName();
- CamelNodeDetails answer = nodeFactory.newNode(null, root);
- CamelNodeDetails parent = answer;
+ // re-create factory as we rebuild the tree
+ nodeFactory = CamelNodeDetailsFactory.newInstance();
- // TODO: use camel catalog to know about these types and when to do what
+ CamelNodeDetails answer = nodeFactory.newNode(null, root);
+ answer.setFileName(fullyQualifiedFileName);
+ CamelNodeDetails parent = answer;
for (int i = 1; i < route.getOutputs().size(); i++) {
+
CamelNodeDetails node = route.getOutputs().get(i);
String name = node.getName();
+ // TODO: use camel catalog to know about these types and when to do what
// special for some EIPs
if ("choice".equals(name)) {
- CamelNodeDetails output = nodeFactory.newNode(parent, name);
+ CamelNodeDetails output = nodeFactory.copyNode(parent, name, node);
parent.addOutput(output);
parent = output;
} else if ("when".equals(name)) {
parent = grandParent(parent, "choice");
- CamelNodeDetails output = nodeFactory.newNode(parent, name);
+ CamelNodeDetails output = nodeFactory.copyNode(parent, name, node);
parent.addOutput(output);
parent = output;
} else if ("otherwise".equals(name)) {
parent = grandParent(parent, "choice");
- CamelNodeDetails output = nodeFactory.newNode(parent, name);
+ CamelNodeDetails output = nodeFactory.copyNode(parent, name, node);
parent.addOutput(output);
parent = output;
} else if ("end".equals(name) || "endChoice".equals(name) || "endDoTry".equals(name)) {
@@ -121,12 +130,12 @@ public final class AdvancedCamelJavaParserHelper {
boolean hasOutput = hasOutput(name);
if (hasOutput) {
// has output so add as new child node
- CamelNodeDetails output = nodeFactory.newNode(parent, name);
+ CamelNodeDetails output = nodeFactory.copyNode(parent, name, node);
parent.addOutput(output);
parent = output;
} else {
// add straight to itself
- CamelNodeDetails output = nodeFactory.newNode(parent, name);
+ CamelNodeDetails output = nodeFactory.copyNode(parent, name, node);
parent.addOutput(output);
}
}
@@ -161,21 +170,21 @@ public final class AdvancedCamelJavaParserHelper {
}
}
- private void parseExpression(CamelNodeDetailsFactory nodeFactory,
+ private void parseExpression(CamelNodeDetailsFactory nodeFactory, String fullyQualifiedFileName,
JavaClassSource clazz, Block block, Expression exp, CamelNodeDetails node) {
if (exp == null) {
return;
}
if (exp instanceof MethodInvocation) {
MethodInvocation mi = (MethodInvocation) exp;
- node = doParseCamelModels(nodeFactory, clazz, block, mi, node);
+ node = doParseCamelModels(nodeFactory, fullyQualifiedFileName, clazz, block, mi, node);
// if the method was called on another method, then recursive
exp = mi.getExpression();
- parseExpression(nodeFactory, clazz, block, exp, node);
+ parseExpression(nodeFactory, fullyQualifiedFileName, clazz, block, exp, node);
}
}
- private CamelNodeDetails doParseCamelModels(CamelNodeDetailsFactory nodeFactory,
+ private CamelNodeDetails doParseCamelModels(CamelNodeDetailsFactory nodeFactory, String fullyQualifiedFileName,
JavaClassSource clazz, Block block, MethodInvocation mi, CamelNodeDetails node) {
String name = mi.getName().getIdentifier();
@@ -185,6 +194,15 @@ public final class AdvancedCamelJavaParserHelper {
// only include if its a known Camel model
if (isEnd || camelCatalog.findModelNames().contains(name)) {
CamelNodeDetails newNode = nodeFactory.newNode(node, name);
+
+ // include source code details
+ int pos = mi.getName().getStartPosition();
+ int line = findLineNumber(fullyQualifiedFileName, pos);
+ if (line > -1) {
+ newNode.setLineNumber("" + line);
+ }
+ newNode.setFileName(fullyQualifiedFileName);
+
node.addPreliminaryOutput(newNode);
return node;
}
@@ -251,6 +269,10 @@ public final class AdvancedCamelJavaParserHelper {
return null;
}
+ /**
+ * @deprecated currently not in use
+ */
+ @Deprecated
public static String getLiteralValue(JavaClassSource clazz, Block block, Expression expression) {
// unwrap parenthesis
if (expression instanceof ParenthesizedExpression) {
@@ -392,4 +414,27 @@ public final class AdvancedCamelJavaParserHelper {
return false;
}
+ private static int findLineNumber(String fullyQualifiedFileName, int position) {
+ int lines = 0;
+
+ try {
+ int current = 0;
+ try (BufferedReader br = new BufferedReader(new FileReader(new File(fullyQualifiedFileName)))) {
+ String line;
+ while ((line = br.readLine()) != null) {
+ lines++;
+ current += line.length() + 1; // add 1 for line feed
+ if (current >= position) {
+ return lines;
+ }
+ }
+ }
+ } catch (Exception e) {
+ // ignore
+ return -1;
+ }
+
+ return lines;
+ }
+
}
http://git-wip-us.apache.org/repos/asf/camel/blob/7aef6567/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetails.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetails.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetails.java
index 9712347..7167eb8 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetails.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetails.java
@@ -21,11 +21,26 @@ import java.util.List;
public class CamelNodeDetails {
+ // source code details
+ private String fileName;
+ private String lineNumber;
+ private String lineNumberEnd;
+
+ // camel node details
private final CamelNodeDetails parent;
private final String name;
private final int order;
private List<CamelNodeDetails> outputs;
+ public CamelNodeDetails(CamelNodeDetails parent, String name, int order, CamelNodeDetails copy) {
+ this.parent = parent;
+ this.name = name;
+ this.order = order;
+ this.fileName = copy.getFileName();
+ this.lineNumber = copy.getLineNumber();
+ this.lineNumberEnd = copy.getLineNumberEnd();
+ }
+
public CamelNodeDetails(CamelNodeDetails parent, String name, int order) {
this.parent = parent;
this.name = name;
@@ -63,13 +78,37 @@ public class CamelNodeDetails {
return outputs;
}
+ public String getFileName() {
+ return fileName;
+ }
+
+ public void setFileName(String fileName) {
+ this.fileName = fileName;
+ }
+
+ public String getLineNumber() {
+ return lineNumber;
+ }
+
+ public void setLineNumber(String lineNumber) {
+ this.lineNumber = lineNumber;
+ }
+
+ public String getLineNumberEnd() {
+ return lineNumberEnd;
+ }
+
+ public void setLineNumberEnd(String lineNumberEnd) {
+ this.lineNumberEnd = lineNumberEnd;
+ }
+
public String toString() {
return name;
}
public String dump(int level) {
StringBuilder sb = new StringBuilder();
- sb.append(order);
+ sb.append(lineNumber);
sb.append("\t");
sb.append(padString(level));
sb.append(name);
http://git-wip-us.apache.org/repos/asf/camel/blob/7aef6567/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetailsFactory.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetailsFactory.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetailsFactory.java
index 502c2c7..33b15c8 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetailsFactory.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetailsFactory.java
@@ -30,4 +30,8 @@ public class CamelNodeDetailsFactory {
public CamelNodeDetails newNode(CamelNodeDetails parent, String name) {
return new CamelNodeDetails(parent, name, ++order);
}
+
+ public CamelNodeDetails copyNode(CamelNodeDetails parent, String name, CamelNodeDetails copoy) {
+ return new CamelNodeDetails(parent, name, ++order, copoy);
+ }
}
http://git-wip-us.apache.org/repos/asf/camel/blob/7aef6567/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
index 48e3a3c..928ac87 100644
--- a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
+++ b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
@@ -40,10 +40,13 @@ public class RoasterJavaDslTest extends CamelTestSupport {
public void parse() throws Exception {
JavaClassSource clazz = (JavaClassSource) Roaster.parse(new File("src/test/java/org/apache/camel/parser/java/MyJavaDslRouteBuilder.java"));
- CamelNodeDetails details = AdvancedRouteBuilderParser.parseRouteBuilder(clazz, true);
+ CamelNodeDetails details = AdvancedRouteBuilderParser.parseRouteBuilder(clazz, ".",
+ "src/test/java/org/apache/camel/parser/java/MyJavaDslRouteBuilder.java",true);
String tree = details.dump(0);
LOG.info("\n" + tree);
+ String name = details.getFileName();
+ System.out.println(name);
System.out.println(tree);
}
[09/13] camel git commit: Add RouteCoverage annotation to
camel-test-spring to make it easy to turn on
Posted by da...@apache.org.
Add RouteCoverage annotation to camel-test-spring to make it easy to turn on
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/4cf26c0a
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/4cf26c0a
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/4cf26c0a
Branch: refs/heads/parser2
Commit: 4cf26c0ab9d281b794a0aad4b83f72deac2ad360
Parents: 702f4a9
Author: Claus Ibsen <da...@apache.org>
Authored: Sat Oct 7 21:26:12 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat Oct 7 21:26:12 2017 +0200
----------------------------------------------------------------------
.../test/spring/CamelAnnotationsHandler.java | 18 ++++++++++++++----
.../spring/CamelSpringBootExecutionListener.java | 12 ++++++++----
.../CamelSpringDelegatingTestContextLoader.java | 14 +++++++++++++-
.../test/spring/CamelSpringTestContextLoader.java | 5 ++++-
.../test/spring/RouteCoverageEventNotifier.java | 2 ++
.../sample/camel/SampleCamelApplicationTest.java | 2 ++
6 files changed, 43 insertions(+), 10 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/4cf26c0a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelAnnotationsHandler.java
----------------------------------------------------------------------
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 c27f7783..4b41e23 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
@@ -23,6 +23,7 @@ import java.util.LinkedList;
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;
@@ -30,6 +31,7 @@ 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;
@@ -75,12 +77,20 @@ public final class CamelAnnotationsHandler {
* @param context the initialized Spring context
* @param testClass the test class being executed
*/
- public static void handleRouteCoverage(ConfigurableApplicationContext context, Class<?> testClass) {
+ public static void handleRouteCoverage(ConfigurableApplicationContext context, Class<?> testClass, Function testMethod) throws Exception {
if (testClass.isAnnotationPresent(RouteCoverage.class)) {
- LOGGER.info("Enabling RouteCoverage");
- // JMX must be enabled to turn on route coverage
- System.clearProperty(JmxSystemPropertyKeys.DISABLED);
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);
+ }
+ });
}
}
http://git-wip-us.apache.org/repos/asf/camel/blob/4cf26c0a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringBootExecutionListener.java
----------------------------------------------------------------------
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 f693011..195f57d 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
@@ -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
@@ -42,7 +42,6 @@ public class CamelSpringBootExecutionListener extends AbstractTestExecutionListe
ConfigurableApplicationContext context = (ConfigurableApplicationContext) testContext.getApplicationContext();
// Post CamelContext(s) instantiation but pre CamelContext(s) start setup
- CamelAnnotationsHandler.handleRouteCoverage(context, testClass);
CamelAnnotationsHandler.handleProvidesBreakpoint(context, testClass);
CamelAnnotationsHandler.handleShutdownTimeout(context, testClass);
CamelAnnotationsHandler.handleMockEndpoints(context, testClass);
@@ -57,15 +56,20 @@ public class CamelSpringBootExecutionListener extends AbstractTestExecutionListe
@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);
}
http://git-wip-us.apache.org/repos/asf/camel/blob/4cf26c0a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringDelegatingTestContextLoader.java
----------------------------------------------------------------------
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 9595d8a..aa76c15 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
@@ -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;
@@ -77,7 +79,7 @@ public class CamelSpringDelegatingTestContextLoader extends DelegatingSmartConte
AnnotationConfigUtils.registerAnnotationConfigProcessors((BeanDefinitionRegistry) context);
// Post CamelContext(s) instantiation but pre CamelContext(s) start setup
- CamelAnnotationsHandler.handleRouteCoverage(context, testClass);
+ CamelAnnotationsHandler.handleRouteCoverage(context, testClass, (String) -> getTestMethod().getName());
CamelAnnotationsHandler.handleProvidesBreakpoint(context, testClass);
CamelAnnotationsHandler.handleShutdownTimeout(context, testClass);
CamelAnnotationsHandler.handleMockEndpoints(context, testClass);
@@ -120,4 +122,14 @@ public class CamelSpringDelegatingTestContextLoader extends DelegatingSmartConte
return CamelSpringTestHelper.getTestClass();
}
+ /**
+ * Returns the test method under test.
+ *
+ * @return the method that is being executed
+ * @see CamelSpringTestHelper
+ */
+ protected Method getTestMethod() {
+ return CamelSpringTestHelper.getTestMethod();
+ }
+
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/camel/blob/4cf26c0a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java
----------------------------------------------------------------------
diff --git a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java
index 939a57c..3323b9f 100644
--- a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java
+++ b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java
@@ -32,6 +32,7 @@ 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.apache.camel.test.ExcludingPackageScanClassResolver;
import org.apache.camel.test.spring.CamelSpringTestHelper.DoToSpringCamelContextsStrategy;
@@ -289,7 +290,9 @@ public class CamelSpringTestContextLoader extends AbstractContextLoader {
@Override
public void execute(String contextName, SpringCamelContext camelContext) throws Exception {
LOG.info("Enabling RouteCoverage");
- camelContext.getManagementStrategy().addEventNotifier(new RouteCoverageEventNotifier(testClass.getName(), (String) -> getTestMethod().getName()));
+ EventNotifier notifier = new RouteCoverageEventNotifier(testClass.getName(), (String) -> getTestMethod().getName());
+ camelContext.addService(notifier, true);
+ camelContext.getManagementStrategy().addEventNotifier(notifier);
}
});
}
http://git-wip-us.apache.org/repos/asf/camel/blob/4cf26c0a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/RouteCoverageEventNotifier.java
----------------------------------------------------------------------
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 7be9370..17b7064 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
@@ -42,6 +42,8 @@ public class RouteCoverageEventNotifier extends EventNotifierSupport {
public RouteCoverageEventNotifier(String testClassName, Function testMethodName) {
this.testClassName = testClassName;
this.testMethodName = testMethodName;
+ setIgnoreCamelContextEvents(false);
+ setIgnoreExchangeEvents(true);
}
@Override
http://git-wip-us.apache.org/repos/asf/camel/blob/4cf26c0a/examples/camel-example-spring-boot/src/test/java/sample/camel/SampleCamelApplicationTest.java
----------------------------------------------------------------------
diff --git a/examples/camel-example-spring-boot/src/test/java/sample/camel/SampleCamelApplicationTest.java b/examples/camel-example-spring-boot/src/test/java/sample/camel/SampleCamelApplicationTest.java
index 7d53276..94dc75b 100644
--- a/examples/camel-example-spring-boot/src/test/java/sample/camel/SampleCamelApplicationTest.java
+++ b/examples/camel-example-spring-boot/src/test/java/sample/camel/SampleCamelApplicationTest.java
@@ -21,6 +21,7 @@ import java.util.concurrent.TimeUnit;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.NotifyBuilder;
import org.apache.camel.test.spring.CamelSpringBootRunner;
+import org.apache.camel.test.spring.RouteCoverage;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@@ -30,6 +31,7 @@ import static org.junit.Assert.assertTrue;
@RunWith(CamelSpringBootRunner.class)
@SpringBootTest(classes = SampleCamelApplication.class)
+@RouteCoverage
public class SampleCamelApplicationTest {
@Autowired
[12/13] camel git commit: Lets rename @RouteCoverage to
@EnableRouteCoverage as its easier to remember
Posted by da...@apache.org.
Lets rename @RouteCoverage to @EnableRouteCoverage as its easier to remember
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/797b9f01
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/797b9f01
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/797b9f01
Branch: refs/heads/parser2
Commit: 797b9f0188e6e71209d74e7dec54ddee7daeae85
Parents: 75f2ba0
Author: Claus Ibsen <da...@apache.org>
Authored: Sun Oct 8 13:38:50 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun Oct 8 13:38:50 2017 +0200
----------------------------------------------------------------------
.../test/spring/CamelAnnotationsHandler.java | 2 +-
.../spring/CamelSpringTestContextLoader.java | 4 +-
.../camel/test/spring/EnableRouteCoverage.java | 39 ++++++++++++++++++++
.../apache/camel/test/spring/RouteCoverage.java | 39 --------------------
...ringRouteProcessorDumpRouteCoverageTest.java | 2 +-
.../java/sample/camel/FooApplicationTest.java | 5 +--
.../camel/SampleCamelApplicationTest.java | 4 +-
7 files changed, 47 insertions(+), 48 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/797b9f01/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelAnnotationsHandler.java
----------------------------------------------------------------------
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 4b41e23..071891f 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
@@ -78,7 +78,7 @@ public final class CamelAnnotationsHandler {
* @param testClass the test class being executed
*/
public static void handleRouteCoverage(ConfigurableApplicationContext context, Class<?> testClass, Function testMethod) throws Exception {
- if (testClass.isAnnotationPresent(RouteCoverage.class)) {
+ if (testClass.isAnnotationPresent(EnableRouteCoverage.class)) {
System.setProperty("CamelTestRouteCoverage", "true");
CamelSpringTestHelper.doToSpringCamelContexts(context, new CamelSpringTestHelper.DoToSpringCamelContextsStrategy() {
http://git-wip-us.apache.org/repos/asf/camel/blob/797b9f01/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java
----------------------------------------------------------------------
diff --git a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java
index 3323b9f..434f188 100644
--- a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java
+++ b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java
@@ -268,7 +268,7 @@ public class CamelSpringTestContextLoader extends AbstractContextLoader {
LOG.info("Enabling Camel JMX as DisableJmx annotation was found and disableJmx is set to false.");
System.clearProperty(JmxSystemPropertyKeys.DISABLED);
}
- } else if (!testClass.isAnnotationPresent(RouteCoverage.class)) {
+ } 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");
@@ -282,7 +282,7 @@ public class CamelSpringTestContextLoader extends AbstractContextLoader {
* @param testClass the test class being executed
*/
private void handleRouteCoverage(GenericApplicationContext context, Class<?> testClass) throws Exception {
- if (testClass.isAnnotationPresent(RouteCoverage.class)) {
+ if (testClass.isAnnotationPresent(EnableRouteCoverage.class)) {
System.setProperty("CamelTestRouteCoverage", "true");
CamelSpringTestHelper.doToSpringCamelContexts(context, new DoToSpringCamelContextsStrategy() {
http://git-wip-us.apache.org/repos/asf/camel/blob/797b9f01/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/EnableRouteCoverage.java
----------------------------------------------------------------------
diff --git a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/EnableRouteCoverage.java b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/EnableRouteCoverage.java
new file mode 100644
index 0000000..13c8514
--- /dev/null
+++ b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/EnableRouteCoverage.java
@@ -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 {
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/797b9f01/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/RouteCoverage.java
----------------------------------------------------------------------
diff --git a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/RouteCoverage.java b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/RouteCoverage.java
deleted file mode 100644
index df92663..0000000
--- a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/RouteCoverage.java
+++ /dev/null
@@ -1,39 +0,0 @@
-/**
- * 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 RouteCoverage {
-
-}
http://git-wip-us.apache.org/repos/asf/camel/blob/797b9f01/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringRouteProcessorDumpRouteCoverageTest.java
----------------------------------------------------------------------
diff --git a/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringRouteProcessorDumpRouteCoverageTest.java b/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringRouteProcessorDumpRouteCoverageTest.java
index b910e7b..9374d2f 100644
--- a/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringRouteProcessorDumpRouteCoverageTest.java
+++ b/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringRouteProcessorDumpRouteCoverageTest.java
@@ -27,7 +27,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
-@RouteCoverage
+@EnableRouteCoverage
public class CamelSpringRouteProcessorDumpRouteCoverageTest extends CamelSpringRunnerPlainTest {
@BeforeClass
http://git-wip-us.apache.org/repos/asf/camel/blob/797b9f01/examples/camel-example-spring-boot/src/test/java/sample/camel/FooApplicationTest.java
----------------------------------------------------------------------
diff --git a/examples/camel-example-spring-boot/src/test/java/sample/camel/FooApplicationTest.java b/examples/camel-example-spring-boot/src/test/java/sample/camel/FooApplicationTest.java
index 0b76960..48aaf29 100644
--- a/examples/camel-example-spring-boot/src/test/java/sample/camel/FooApplicationTest.java
+++ b/examples/camel-example-spring-boot/src/test/java/sample/camel/FooApplicationTest.java
@@ -21,20 +21,19 @@ import java.util.concurrent.TimeUnit;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.NotifyBuilder;
import org.apache.camel.test.spring.CamelSpringBootRunner;
-import org.apache.camel.test.spring.RouteCoverage;
+import org.apache.camel.test.spring.EnableRouteCoverage;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.context.annotation.Configuration;
import static org.junit.Assert.assertTrue;
@RunWith(CamelSpringBootRunner.class)
@SpringBootTest(classes = SampleCamelApplication.class,
properties = "greeting = Hell foo")
-@RouteCoverage
+@EnableRouteCoverage
@Ignore
public class FooApplicationTest {
http://git-wip-us.apache.org/repos/asf/camel/blob/797b9f01/examples/camel-example-spring-boot/src/test/java/sample/camel/SampleCamelApplicationTest.java
----------------------------------------------------------------------
diff --git a/examples/camel-example-spring-boot/src/test/java/sample/camel/SampleCamelApplicationTest.java b/examples/camel-example-spring-boot/src/test/java/sample/camel/SampleCamelApplicationTest.java
index 94dc75b..f4c2fc5 100644
--- a/examples/camel-example-spring-boot/src/test/java/sample/camel/SampleCamelApplicationTest.java
+++ b/examples/camel-example-spring-boot/src/test/java/sample/camel/SampleCamelApplicationTest.java
@@ -21,7 +21,7 @@ import java.util.concurrent.TimeUnit;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.NotifyBuilder;
import org.apache.camel.test.spring.CamelSpringBootRunner;
-import org.apache.camel.test.spring.RouteCoverage;
+import org.apache.camel.test.spring.EnableRouteCoverage;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@@ -31,7 +31,7 @@ import static org.junit.Assert.assertTrue;
@RunWith(CamelSpringBootRunner.class)
@SpringBootTest(classes = SampleCamelApplication.class)
-@RouteCoverage
+@EnableRouteCoverage
public class SampleCamelApplicationTest {
@Autowired
[08/13] camel git commit: Add RouteCoverage annotation to
camel-test-spring to make it easy to turn on
Posted by da...@apache.org.
Add RouteCoverage annotation to camel-test-spring to make it easy to turn on
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/702f4a98
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/702f4a98
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/702f4a98
Branch: refs/heads/parser2
Commit: 702f4a98a608f63ac15f4d49c0542a7daa190556
Parents: b05a46d
Author: Claus Ibsen <da...@apache.org>
Authored: Sat Oct 7 20:48:24 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat Oct 7 20:48:24 2017 +0200
----------------------------------------------------------------------
.../test/spring/CamelAnnotationsHandler.java | 15 +
.../CamelSpringBootExecutionListener.java | 1 +
.../CamelSpringDelegatingTestContextLoader.java | 1 +
.../spring/CamelSpringTestContextLoader.java | 42 ++-
...gTestContextLoaderTestExecutionListener.java | 3 +-
.../test/spring/CamelSpringTestHelper.java | 14 +-
.../apache/camel/test/spring/RouteCoverage.java | 39 +++
.../test/spring/RouteCoverageEventNotifier.java | 96 ++++++
...ringRouteProcessorDumpRouteCoverageTest.java | 55 ++++
.../test/spring/CamelSpringRunnerPlainTest.java | 5 +
.../camel/test/junit4/CamelTestSupport.java | 4 +-
.../java/sample/camel/SampleCamelRouter.java | 2 +-
.../apache/camel/parser/graph/RenderRoute.java | 4 +
.../java/RoasterJavaDslTwoRoutesTest.java | 2 +-
.../org/apache/camel/maven/CoverageMojo.java | 323 +++++++++++++++++++
15 files changed, 594 insertions(+), 12 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/702f4a98/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelAnnotationsHandler.java
----------------------------------------------------------------------
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 8902f4f..c27f7783 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
@@ -69,6 +69,21 @@ public final class CamelAnnotationsHandler {
}
}
+ /**
+ * 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) {
+ if (testClass.isAnnotationPresent(RouteCoverage.class)) {
+ LOGGER.info("Enabling RouteCoverage");
+ // JMX must be enabled to turn on route coverage
+ System.clearProperty(JmxSystemPropertyKeys.DISABLED);
+ System.setProperty("CamelTestRouteCoverage", "true");
+ }
+ }
+
public static void handleProvidesBreakpoint(ConfigurableApplicationContext context, Class<?> testClass) throws Exception {
Collection<Method> methods = getAllMethods(testClass);
final List<Breakpoint> breakpoints = new LinkedList<Breakpoint>();
http://git-wip-us.apache.org/repos/asf/camel/blob/702f4a98/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringBootExecutionListener.java
----------------------------------------------------------------------
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 546462d..f693011 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
@@ -42,6 +42,7 @@ public class CamelSpringBootExecutionListener extends AbstractTestExecutionListe
ConfigurableApplicationContext context = (ConfigurableApplicationContext) testContext.getApplicationContext();
// Post CamelContext(s) instantiation but pre CamelContext(s) start setup
+ CamelAnnotationsHandler.handleRouteCoverage(context, testClass);
CamelAnnotationsHandler.handleProvidesBreakpoint(context, testClass);
CamelAnnotationsHandler.handleShutdownTimeout(context, testClass);
CamelAnnotationsHandler.handleMockEndpoints(context, testClass);
http://git-wip-us.apache.org/repos/asf/camel/blob/702f4a98/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringDelegatingTestContextLoader.java
----------------------------------------------------------------------
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 380fac0..9595d8a 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
@@ -77,6 +77,7 @@ public class CamelSpringDelegatingTestContextLoader extends DelegatingSmartConte
AnnotationConfigUtils.registerAnnotationConfigProcessors((BeanDefinitionRegistry) context);
// Post CamelContext(s) instantiation but pre CamelContext(s) start setup
+ CamelAnnotationsHandler.handleRouteCoverage(context, testClass);
CamelAnnotationsHandler.handleProvidesBreakpoint(context, testClass);
CamelAnnotationsHandler.handleShutdownTimeout(context, testClass);
CamelAnnotationsHandler.handleMockEndpoints(context, testClass);
http://git-wip-us.apache.org/repos/asf/camel/blob/702f4a98/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java
----------------------------------------------------------------------
diff --git a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java
index b595ce3..939a57c 100644
--- a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java
+++ b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoader.java
@@ -107,7 +107,7 @@ public class CamelSpringTestContextLoader extends AbstractContextLoader {
public ApplicationContext loadContext(String... locations) throws Exception {
Class<?> testClass = getTestClass();
-
+
if (LOG.isDebugEnabled()) {
LOG.debug("Loading ApplicationContext for locations [" + StringUtils.arrayToCommaDelimitedString(locations) + "].");
}
@@ -153,6 +153,7 @@ public class CamelSpringTestContextLoader extends AbstractContextLoader {
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);
@@ -204,7 +205,6 @@ public class CamelSpringTestContextLoader extends AbstractContextLoader {
if (mergedConfig != null) {
parentContext = mergedConfig.getParentApplicationContext();
-
}
if (testClass.isAnnotationPresent(ExcludeRoutes.class)) {
@@ -258,7 +258,7 @@ public class CamelSpringTestContextLoader extends AbstractContextLoader {
*/
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.");
@@ -267,12 +267,34 @@ public class CamelSpringTestContextLoader extends AbstractContextLoader {
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(RouteCoverage.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(RouteCoverage.class)) {
+ System.setProperty("CamelTestRouteCoverage", "true");
+
+ CamelSpringTestHelper.doToSpringCamelContexts(context, new DoToSpringCamelContextsStrategy() {
+
+ @Override
+ public void execute(String contextName, SpringCamelContext camelContext) throws Exception {
+ LOG.info("Enabling RouteCoverage");
+ camelContext.getManagementStrategy().addEventNotifier(new RouteCoverageEventNotifier(testClass.getName(), (String) -> getTestMethod().getName()));
+ }
+ });
+ }
+ }
+
/**
* Handles the processing of the {@link ProvidesBreakpoint} annotation on a test class. Exists here
* as it is needed in
@@ -504,4 +526,14 @@ public class CamelSpringTestContextLoader extends AbstractContextLoader {
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();
+ }
}
http://git-wip-us.apache.org/repos/asf/camel/blob/702f4a98/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoaderTestExecutionListener.java
----------------------------------------------------------------------
diff --git a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoaderTestExecutionListener.java b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoaderTestExecutionListener.java
index d8533d1..100b998 100644
--- a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoaderTestExecutionListener.java
+++ b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestContextLoaderTestExecutionListener.java
@@ -45,5 +45,6 @@ public class CamelSpringTestContextLoaderTestExecutionListener extends AbstractT
@Override
public void prepareTestInstance(TestContext testContext) throws Exception {
CamelSpringTestHelper.setTestClass(testContext.getTestClass());
- }
+ CamelSpringTestHelper.setTestContext(testContext);
+ }
}
http://git-wip-us.apache.org/repos/asf/camel/blob/702f4a98/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestHelper.java
----------------------------------------------------------------------
diff --git a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestHelper.java b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestHelper.java
index aa75c0e..1c426b4 100644
--- a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestHelper.java
+++ b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/CamelSpringTestHelper.java
@@ -27,6 +27,7 @@ import java.util.Set;
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
@@ -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() {
}
@@ -60,7 +62,15 @@ public final class CamelSpringTestHelper {
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.
*/
http://git-wip-us.apache.org/repos/asf/camel/blob/702f4a98/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/RouteCoverage.java
----------------------------------------------------------------------
diff --git a/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/RouteCoverage.java b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/RouteCoverage.java
new file mode 100644
index 0000000..df92663
--- /dev/null
+++ b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/RouteCoverage.java
@@ -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 RouteCoverage {
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/702f4a98/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/RouteCoverageEventNotifier.java
----------------------------------------------------------------------
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
new file mode 100644
index 0000000..7be9370
--- /dev/null
+++ b/components/camel-test-spring/src/main/java/org/apache/camel/test/spring/RouteCoverageEventNotifier.java
@@ -0,0 +1,96 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.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;
+
+ public RouteCoverageEventNotifier(String testClassName, Function testMethodName) {
+ this.testClassName = testClassName;
+ this.testMethodName = testMethodName;
+ }
+
+ @Override
+ public boolean isEnabled(EventObject event) {
+ return event instanceof CamelContextStoppingEvent;
+ }
+
+ @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();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/702f4a98/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringRouteProcessorDumpRouteCoverageTest.java
----------------------------------------------------------------------
diff --git a/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringRouteProcessorDumpRouteCoverageTest.java b/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringRouteProcessorDumpRouteCoverageTest.java
new file mode 100644
index 0000000..b910e7b
--- /dev/null
+++ b/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringRouteProcessorDumpRouteCoverageTest.java
@@ -0,0 +1,55 @@
+/**
+ * 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.io.File;
+
+import org.apache.camel.management.ManagedManagementStrategy;
+import org.apache.camel.test.junit4.TestSupport;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+@RouteCoverage
+public class CamelSpringRouteProcessorDumpRouteCoverageTest extends CamelSpringRunnerPlainTest {
+
+ @BeforeClass
+ public static void prepareFiles() throws Exception {
+ TestSupport.deleteDirectory("target/camel-route-coverage");
+ }
+
+ @Test
+ public void testJmx() throws Exception {
+ // JMX is enabled with route coverage
+ assertEquals(ManagedManagementStrategy.class, camelContext.getManagementStrategy().getClass());
+ }
+
+ @Override
+ public void testRouteCoverage() throws Exception{
+ camelContext.stop();
+ camelContext2.stop();
+
+ // there should be files
+ String[] names = new File("target/camel-route-coverage").list();
+ assertNotNull(names);
+ assertTrue(names.length > 0);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/702f4a98/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringRunnerPlainTest.java
----------------------------------------------------------------------
diff --git a/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringRunnerPlainTest.java b/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringRunnerPlainTest.java
index 17fa2d6..39b2d88 100644
--- a/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringRunnerPlainTest.java
+++ b/components/camel-test-spring/src/test/java/org/apache/camel/test/spring/CamelSpringRunnerPlainTest.java
@@ -118,6 +118,11 @@ public class CamelSpringRunnerPlainTest {
assertNull(camelContext2.getDebugger());
}
+ @Test
+ public void testRouteCoverage() throws Exception {
+ // noop
+ }
+
}
// end::example[]
// END SNIPPET: e1
http://git-wip-us.apache.org/repos/asf/camel/blob/702f4a98/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 632b8b0..2206822 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
@@ -391,7 +391,8 @@ public abstract class CamelTestSupport extends TestSupport {
log.info("Took: " + TimeUtils.printDuration(time) + " (" + time + " millis)");
// if we should dump route stats, then write that to a file
- if (isDumpRouteCoverage()) {
+ boolean coverage = System.getProperty("CamelTestRouteCoverage", "false").equalsIgnoreCase("true") || isDumpRouteCoverage();
+ if (coverage) {
String className = this.getClass().getSimpleName();
String dir = "target/camel-route-coverage";
String name = className + "-" + getTestMethodName() + ".xml";
@@ -485,7 +486,6 @@ public abstract class CamelTestSupport extends TestSupport {
builder.append(routesSummary);
log.info(builder.toString());
-
}
/**
http://git-wip-us.apache.org/repos/asf/camel/blob/702f4a98/examples/camel-example-spring-boot/src/main/java/sample/camel/SampleCamelRouter.java
----------------------------------------------------------------------
diff --git a/examples/camel-example-spring-boot/src/main/java/sample/camel/SampleCamelRouter.java b/examples/camel-example-spring-boot/src/main/java/sample/camel/SampleCamelRouter.java
index 4e0422e..20cf957 100644
--- a/examples/camel-example-spring-boot/src/main/java/sample/camel/SampleCamelRouter.java
+++ b/examples/camel-example-spring-boot/src/main/java/sample/camel/SampleCamelRouter.java
@@ -29,7 +29,7 @@ public class SampleCamelRouter extends RouteBuilder {
@Override
public void configure() throws Exception {
- from("timer:hello?period={{timer.period}}")
+ from("timer:hello?period={{timer.period}}").routeId("hello")
.transform(method("myBean", "saySomething"))
.to("stream:out");
}
http://git-wip-us.apache.org/repos/asf/camel/blob/702f4a98/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/graph/RenderRoute.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/graph/RenderRoute.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/graph/RenderRoute.java
index 2b1cd1c..a1f4807 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/graph/RenderRoute.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/graph/RenderRoute.java
@@ -24,6 +24,10 @@ import javax.imageio.ImageIO;
import org.apache.camel.parser.model.CamelNodeDetails;
+/**
+ * @deprecated experiment to render a route via Java image
+ */
+@Deprecated
public class RenderRoute {
public static void main(String[] args) {
http://git-wip-us.apache.org/repos/asf/camel/blob/702f4a98/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTwoRoutesTest.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTwoRoutesTest.java b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTwoRoutesTest.java
index 4e555a9..23ccef6 100644
--- a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTwoRoutesTest.java
+++ b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTwoRoutesTest.java
@@ -51,7 +51,7 @@ public class RoasterJavaDslTwoRoutesTest extends CamelTestSupport {
assertEquals("src/test/java/org/apache/camel/parser/java/TwoRoutesRouteBuilder.java", details2.getFileName());
assertEquals("foo", details.getRouteId());
- assertEquals("bar", details.getRouteId());
+ assertEquals("bar", details2.getRouteId());
String tree = details.dump(0);
LOG.info("\n" + tree);
http://git-wip-us.apache.org/repos/asf/camel/blob/702f4a98/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/CoverageMojo.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/CoverageMojo.java b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/CoverageMojo.java
new file mode 100644
index 0000000..e529587
--- /dev/null
+++ b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/CoverageMojo.java
@@ -0,0 +1,323 @@
+/**
+ * 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.maven;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import org.apache.camel.maven.helper.EndpointHelper;
+import org.apache.camel.parser.RouteBuilderParser;
+import org.apache.camel.parser.model.CamelEndpointDetails;
+import org.apache.camel.parser.model.CamelNodeDetails;
+import org.apache.camel.parser.model.CamelRouteDetails;
+import org.apache.camel.parser.model.CamelSimpleExpressionDetails;
+import org.apache.maven.model.Resource;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.mojo.exec.AbstractExecMojo;
+import org.jboss.forge.roaster.Roaster;
+import org.jboss.forge.roaster.model.JavaType;
+import org.jboss.forge.roaster.model.source.JavaClassSource;
+
+/**
+ * Performs route coverage reports after running Camel unit tests with camel-test modules
+ *
+ * @goal coverage
+ * @threadSafe
+ */
+public class CoverageMojo extends AbstractExecMojo {
+
+ /**
+ * The maven project.
+ *
+ * @parameter property="project"
+ * @required
+ * @readonly
+ */
+ protected MavenProject project;
+
+ /**
+ * Whether to fail if a route was not fully covered
+ *
+ * @parameter property="camel.failOnError"
+ * default-value="false"
+ */
+ private boolean failOnError;
+
+ /**
+ * Whether to include test source code
+ *
+ * @parameter property="camel.includeTest"
+ * default-value="false"
+ */
+ private boolean includeTest;
+
+ /**
+ * To filter the names of java and xml files to only include files matching any of the given list of patterns (wildcard and regular expression).
+ * Multiple values can be separated by comma.
+ *
+ * @parameter property="camel.includes"
+ */
+ private String includes;
+
+ /**
+ * To filter the names of java and xml files to exclude files matching any of the given list of patterns (wildcard and regular expression).
+ * Multiple values can be separated by comma.
+ *
+ * @parameter property="camel.excludes"
+ */
+ private String excludes;
+
+ // CHECKSTYLE:OFF
+ @Override
+ public void execute() throws MojoExecutionException, MojoFailureException {
+
+ List<CamelEndpointDetails> endpoints = new ArrayList<>();
+ List<CamelSimpleExpressionDetails> simpleExpressions = new ArrayList<>();
+ List<CamelRouteDetails> routeIds = new ArrayList<>();
+ Set<File> javaFiles = new LinkedHashSet<File>();
+ Set<File> xmlFiles = new LinkedHashSet<File>();
+
+ // find all java route builder classes
+ List list = project.getCompileSourceRoots();
+ for (Object obj : list) {
+ String dir = (String) obj;
+ findJavaFiles(new File(dir), javaFiles);
+ }
+ // find all xml routes
+ list = project.getResources();
+ for (Object obj : list) {
+ Resource dir = (Resource) obj;
+ findXmlFiles(new File(dir.getDirectory()), xmlFiles);
+ }
+
+ if (includeTest) {
+ list = project.getTestCompileSourceRoots();
+ for (Object obj : list) {
+ String dir = (String) obj;
+ findJavaFiles(new File(dir), javaFiles);
+ }
+ list = project.getTestResources();
+ for (Object obj : list) {
+ Resource dir = (Resource) obj;
+ findXmlFiles(new File(dir.getDirectory()), xmlFiles);
+ }
+ }
+
+ List<CamelNodeDetails> routeTrees = new ArrayList<>();
+
+ for (File file : javaFiles) {
+ if (matchFile(file)) {
+ try {
+
+ // parse the java source code and find Camel RouteBuilder classes
+ String fqn = file.getPath();
+ String baseDir = ".";
+ JavaType out = Roaster.parse(file);
+ // we should only parse java classes (not interfaces and enums etc)
+ if (out != null && out instanceof JavaClassSource) {
+ JavaClassSource clazz = (JavaClassSource) out;
+ List<CamelNodeDetails> result = RouteBuilderParser.parseRouteBuilderTree(clazz, baseDir, fqn, true);
+ routeTrees.addAll(result);
+ }
+ } catch (Exception e) {
+ getLog().warn("Error parsing java file " + file + " code due " + e.getMessage(), e);
+ }
+ }
+ }
+ for (File file : xmlFiles) {
+ if (matchFile(file)) {
+ try {
+ // TODO: implement me
+ } catch (Exception e) {
+ getLog().warn("Error parsing xml file " + file + " code due " + e.getMessage(), e);
+ }
+ }
+ }
+
+ getLog().info("Discovered " + routeTrees.size() + " routes");
+
+ // skip any routes which has no route id assigned
+
+ long anonymous = routeTrees.stream().filter(t -> t.getRouteId() == null).count();
+ if (anonymous > 0) {
+ getLog().warn("Discovered " + anonymous + " anonymous routes. Add route ids to these routes for route coverage support");
+ }
+
+ routeTrees = routeTrees.stream().filter(t -> t.getRouteId() != null).collect(Collectors.toList());
+
+ routeTrees.forEach(t -> {
+ String routeId = t.getRouteId();
+ String fileName = asRelativeFile(t.getFileName());
+ String tree = t.dump(4);
+
+ getLog().info("Route " + routeId + " discovered in file " + fileName);
+ getLog().info("\n" + tree + "\n");
+ });
+
+ int notCovered = 0;
+
+ if (failOnError && (notCovered > 0)) {
+ throw new MojoExecutionException("Some routes are not fully covered");
+ }
+ }
+ // CHECKSTYLE:ON
+
+ private static int countRouteId(List<CamelRouteDetails> details, String routeId) {
+ int answer = 0;
+ for (CamelRouteDetails detail : details) {
+ if (routeId.equals(detail.getRouteId())) {
+ answer++;
+ }
+ }
+ return answer;
+ }
+
+ private void findJavaFiles(File dir, Set<File> javaFiles) {
+ File[] files = dir.isDirectory() ? dir.listFiles() : null;
+ if (files != null) {
+ for (File file : files) {
+ if (file.getName().endsWith(".java")) {
+ javaFiles.add(file);
+ } else if (file.isDirectory()) {
+ findJavaFiles(file, javaFiles);
+ }
+ }
+ }
+ }
+
+ private void findXmlFiles(File dir, Set<File> xmlFiles) {
+ File[] files = dir.isDirectory() ? dir.listFiles() : null;
+ if (files != null) {
+ for (File file : files) {
+ if (file.getName().endsWith(".xml")) {
+ xmlFiles.add(file);
+ } else if (file.isDirectory()) {
+ findXmlFiles(file, xmlFiles);
+ }
+ }
+ }
+ }
+
+ private boolean matchFile(File file) {
+ if (excludes == null && includes == null) {
+ return true;
+ }
+
+ // exclude take precedence
+ if (excludes != null) {
+ for (String exclude : excludes.split(",")) {
+ exclude = exclude.trim();
+ // try both with and without directory in the name
+ String fqn = stripRootPath(asRelativeFile(file.getAbsolutePath()));
+ boolean match = EndpointHelper.matchPattern(fqn, exclude) || EndpointHelper.matchPattern(file.getName(), exclude);
+ if (match) {
+ return false;
+ }
+ }
+ }
+
+ // include
+ if (includes != null) {
+ for (String include : includes.split(",")) {
+ include = include.trim();
+ // try both with and without directory in the name
+ String fqn = stripRootPath(asRelativeFile(file.getAbsolutePath()));
+ boolean match = EndpointHelper.matchPattern(fqn, include) || EndpointHelper.matchPattern(file.getName(), include);
+ if (match) {
+ return true;
+ }
+ }
+ // did not match any includes
+ return false;
+ }
+
+ // was not excluded nor failed include so its accepted
+ return true;
+ }
+
+ private String asRelativeFile(String name) {
+ String answer = name;
+
+ String base = project.getBasedir().getAbsolutePath();
+ if (name.startsWith(base)) {
+ answer = name.substring(base.length());
+ // skip leading slash for relative path
+ if (answer.startsWith(File.separator)) {
+ answer = answer.substring(1);
+ }
+ }
+ return answer;
+ }
+
+ private String stripRootPath(String name) {
+ // strip out any leading source / resource directory
+
+ List list = project.getCompileSourceRoots();
+ for (Object obj : list) {
+ String dir = (String) obj;
+ dir = asRelativeFile(dir);
+ if (name.startsWith(dir)) {
+ return name.substring(dir.length() + 1);
+ }
+ }
+ list = project.getTestCompileSourceRoots();
+ for (Object obj : list) {
+ String dir = (String) obj;
+ dir = asRelativeFile(dir);
+ if (name.startsWith(dir)) {
+ return name.substring(dir.length() + 1);
+ }
+ }
+ List resources = project.getResources();
+ for (Object obj : resources) {
+ Resource resource = (Resource) obj;
+ String dir = asRelativeFile(resource.getDirectory());
+ if (name.startsWith(dir)) {
+ return name.substring(dir.length() + 1);
+ }
+ }
+ resources = project.getTestResources();
+ for (Object obj : resources) {
+ Resource resource = (Resource) obj;
+ String dir = asRelativeFile(resource.getDirectory());
+ if (name.startsWith(dir)) {
+ return name.substring(dir.length() + 1);
+ }
+ }
+
+ return name;
+ }
+
+ private static String asPackageName(String name) {
+ return name.replace(File.separator, ".");
+ }
+
+ private static String asSimpleClassName(String className) {
+ int dot = className.lastIndexOf('.');
+ if (dot > 0) {
+ return className.substring(dot + 1);
+ } else {
+ return className;
+ }
+ }
+}
[06/13] camel git commit: Camel java dsl parser prototype
Posted by da...@apache.org.
Camel java dsl parser prototype
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/e9d6f0bf
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/e9d6f0bf
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/e9d6f0bf
Branch: refs/heads/parser2
Commit: e9d6f0bf843718b4099fd2af1a631ad6d5a3d11d
Parents: cb55f5a
Author: Claus Ibsen <da...@apache.org>
Authored: Sat Oct 7 18:27:47 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat Oct 7 18:27:47 2017 +0200
----------------------------------------------------------------------
.../apache/camel/parser/RouteBuilderParser.java | 9 ++-
.../helper/CamelJavaTreeParserHelper.java | 34 +++++----
.../camel/parser/java/RoasterJavaDslTest.java | 4 -
.../java/RoasterJavaDslTwoRoutesTest.java | 79 ++++++++++++++++++++
.../parser/java/TwoRoutesRouteBuilder.java | 33 ++++++++
5 files changed, 138 insertions(+), 21 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/e9d6f0bf/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java
index aa54cb3..f525a56 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java
@@ -20,6 +20,7 @@ import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import org.apache.camel.parser.helper.CamelJavaTreeParserHelper;
@@ -75,10 +76,12 @@ public final class RouteBuilderParser {
CamelJavaTreeParserHelper parser = new CamelJavaTreeParserHelper();
List<CamelNodeDetails> list = new ArrayList<>();
for (MethodSource<JavaClassSource> configureMethod : methods) {
- // TODO: list of details, on per route
- CamelNodeDetails details = parser.parseCamelRoute(clazz, baseDir, fullyQualifiedFileName, configureMethod);
- list.add(details);
+ // there may be multiple route builder configure methods
+ List<CamelNodeDetails> details = parser.parseCamelRouteTree(clazz, baseDir, fullyQualifiedFileName, configureMethod);
+ list.addAll(details);
}
+ // we end up parsing bottom->up so reverse list
+ Collections.reverse(list);
return list;
}
http://git-wip-us.apache.org/repos/asf/camel/blob/e9d6f0bf/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaTreeParserHelper.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaTreeParserHelper.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaTreeParserHelper.java
index af19acd..2f81d80 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaTreeParserHelper.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaTreeParserHelper.java
@@ -19,13 +19,13 @@ package org.apache.camel.parser.helper;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
+import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.camel.catalog.CamelCatalog;
import org.apache.camel.catalog.DefaultCamelCatalog;
import org.apache.camel.catalog.JSonSchemaHelper;
-import org.apache.camel.parser.RouteBuilderParser;
import org.apache.camel.parser.model.CamelNodeDetails;
import org.apache.camel.parser.model.CamelNodeDetailsFactory;
import org.apache.camel.parser.roaster.StatementFieldSource;
@@ -66,10 +66,8 @@ public final class CamelJavaTreeParserHelper {
private CamelCatalog camelCatalog = new DefaultCamelCatalog(true);
- // TODO: add support for multiple routes
- // TODO: rename this method
- public CamelNodeDetails parseCamelRoute(JavaClassSource clazz, String baseDir, String fullyQualifiedFileName,
- MethodSource<JavaClassSource> method) {
+ public List<CamelNodeDetails> parseCamelRouteTree(JavaClassSource clazz, String baseDir, String fullyQualifiedFileName,
+ MethodSource<JavaClassSource> method) {
// find any from which is the start of the route
CamelNodeDetailsFactory nodeFactory = CamelNodeDetailsFactory.newInstance();
@@ -91,24 +89,32 @@ public final class CamelJavaTreeParserHelper {
}
}
+ List<CamelNodeDetails> answer = new ArrayList<>();
+
+ if (route.getOutputs().isEmpty()) {
+ // okay no routes found
+ return answer;
+ }
+
// now parse the route node and build the correct model/tree structure of the EIPs
// re-create factory as we rebuild the tree
nodeFactory = CamelNodeDetailsFactory.newInstance();
+ CamelNodeDetails parent = route.getOutputs().get(0);
- CamelNodeDetails from = route.getOutputs().get(0);
- CamelNodeDetails answer = nodeFactory.copyNode(null, "from", from);
- answer.setFileName(fullyQualifiedFileName);
-
- CamelNodeDetails parent = answer;
- for (int i = 1; i < route.getOutputs().size(); i++) {
-
+ for (int i = 0; i < route.getOutputs().size(); i++) {
CamelNodeDetails node = route.getOutputs().get(i);
String name = node.getName();
// TODO: use camel catalog to know about these types and when to do what
- // special for some EIPs
- if ("choice".equals(name)) {
+ // special for some EIPs such as choice
+
+ if ("from".equals(name)) {
+ CamelNodeDetails from = nodeFactory.copyNode(null, "from", node);
+ from.setFileName(fullyQualifiedFileName);
+ answer.add(from);
+ parent = from;
+ } else if ("choice".equals(name)) {
CamelNodeDetails output = nodeFactory.copyNode(parent, name, node);
parent.addOutput(output);
parent = output;
http://git-wip-us.apache.org/repos/asf/camel/blob/e9d6f0bf/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
index 9041e2f..6378f41 100644
--- a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
+++ b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
@@ -55,10 +55,6 @@ public class RoasterJavaDslTest extends CamelTestSupport {
assertTrue(tree.contains("30\t toD"));
assertTrue(tree.contains("33\t otherwise"));
assertTrue(tree.contains("36\t to"));
-
- String name = details.getFileName();
- System.out.println(name);
- System.out.println(tree);
}
@Test
http://git-wip-us.apache.org/repos/asf/camel/blob/e9d6f0bf/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTwoRoutesTest.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTwoRoutesTest.java b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTwoRoutesTest.java
new file mode 100644
index 0000000..6c80003
--- /dev/null
+++ b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTwoRoutesTest.java
@@ -0,0 +1,79 @@
+/**
+ * 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.parser.java;
+
+import java.io.File;
+import java.util.List;
+
+import org.apache.camel.parser.RouteBuilderParser;
+import org.apache.camel.parser.model.CamelNodeDetails;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.jboss.forge.roaster.Roaster;
+import org.jboss.forge.roaster.model.source.JavaClassSource;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class RoasterJavaDslTwoRoutesTest extends CamelTestSupport {
+
+ private static final Logger LOG = LoggerFactory.getLogger(RoasterJavaDslTwoRoutesTest.class);
+
+ @Override
+ public boolean isDumpRouteCoverage() {
+ return true;
+ }
+
+ @Test
+ public void parseTree() throws Exception {
+ JavaClassSource clazz = (JavaClassSource) Roaster.parse(new File("src/test/java/org/apache/camel/parser/java/TwoRoutesRouteBuilder.java"));
+
+ List<CamelNodeDetails> list = RouteBuilderParser.parseRouteBuilderTree(clazz, ".",
+ "src/test/java/org/apache/camel/parser/java/TwoRoutesRouteBuilder.java",true);
+ assertEquals(2, list.size());
+
+ CamelNodeDetails details = list.get(0);
+ CamelNodeDetails details2 = list.get(1);
+ assertEquals("src/test/java/org/apache/camel/parser/java/TwoRoutesRouteBuilder.java", details.getFileName());
+ assertEquals("src/test/java/org/apache/camel/parser/java/TwoRoutesRouteBuilder.java", details2.getFileName());
+
+ String tree = details.dump(0);
+ LOG.info("\n" + tree);
+
+ String tree2 = details2.dump(0);
+ LOG.info("\n" + tree2);
+
+ assertTrue(tree.contains("25\tfrom"));
+ assertTrue(tree.contains("26\t log"));
+ assertTrue(tree.contains("27\t to"));
+
+ assertTrue(tree2.contains("29\tfrom"));
+ assertTrue(tree2.contains("30\t transform"));
+ assertTrue(tree2.contains("31\t to"));
+ }
+
+ @Test
+ public void testRouteCoverage() throws Exception {
+ context.addRoutes(new TwoRoutesRouteBuilder());
+
+ getMockEndpoint("mock:foo").expectedMessageCount(1);
+
+ template.sendBody("direct:foo", "Hello World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/e9d6f0bf/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/TwoRoutesRouteBuilder.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/TwoRoutesRouteBuilder.java b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/TwoRoutesRouteBuilder.java
new file mode 100644
index 0000000..55a8b08
--- /dev/null
+++ b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/TwoRoutesRouteBuilder.java
@@ -0,0 +1,33 @@
+/**
+ * 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.parser.java;
+
+import org.apache.camel.builder.RouteBuilder;
+
+public class TwoRoutesRouteBuilder extends RouteBuilder {
+
+ @Override
+ public void configure() throws Exception {
+ from("direct:foo").routeId("foo")
+ .log("I was here")
+ .to("mock:foo");
+
+ from("direct:bar").routeId("bar")
+ .transform(simple("Someone was here"))
+ .to("mock:bar");
+ }
+}
[07/13] camel git commit: Camel java dsl parser prototype
Posted by da...@apache.org.
Camel java dsl parser prototype
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/b05a46d9
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/b05a46d9
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/b05a46d9
Branch: refs/heads/parser2
Commit: b05a46d9a9b36273d23d48a192ef80218790fd2d
Parents: e9d6f0b
Author: Claus Ibsen <da...@apache.org>
Authored: Sat Oct 7 18:44:55 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat Oct 7 18:44:55 2017 +0200
----------------------------------------------------------------------
.../parser/helper/CamelJavaTreeParserHelper.java | 19 ++++++++++++++++++-
.../camel/parser/model/CamelNodeDetails.java | 10 ++++++++++
.../camel/parser/java/RoasterJavaDslTest.java | 1 +
.../parser/java/RoasterJavaDslTwoRoutesTest.java | 3 +++
4 files changed, 32 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/b05a46d9/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaTreeParserHelper.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaTreeParserHelper.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaTreeParserHelper.java
index 2f81d80..2dd06b3 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaTreeParserHelper.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaTreeParserHelper.java
@@ -114,6 +114,9 @@ public final class CamelJavaTreeParserHelper {
from.setFileName(fullyQualifiedFileName);
answer.add(from);
parent = from;
+ } else if ("routeId".equals(name)) {
+ // should be set on the parent
+ parent.setRouteId(node.getRouteId());
} else if ("choice".equals(name)) {
CamelNodeDetails output = nodeFactory.copyNode(parent, name, node);
parent.addOutput(output);
@@ -199,9 +202,10 @@ public final class CamelJavaTreeParserHelper {
// special for Java DSL having some endXXX
boolean isEnd = "end".equals(name) || "endChoice".equals(name) || "endDoTry".equals(name);
+ boolean isRouteId = "routeId".equals(name);
// only include if its a known Camel model
- if (isEnd || camelCatalog.findModelNames().contains(name)) {
+ if (isEnd || isRouteId || camelCatalog.findModelNames().contains(name)) {
CamelNodeDetails newNode = nodeFactory.newNode(node, name);
// include source code details
@@ -212,6 +216,19 @@ public final class CamelJavaTreeParserHelper {
}
newNode.setFileName(fullyQualifiedFileName);
+ if (isRouteId) {
+ // grab the route id
+ List args = mi.arguments();
+ if (args != null && args.size() > 0) {
+ // the first argument has the route id
+ Expression exp = (Expression) args.get(0);
+ String routeId = getLiteralValue(clazz, block, exp);
+ if (routeId != null) {
+ newNode.setRouteId(routeId);
+ }
+ }
+ }
+
node.addPreliminaryOutput(newNode);
return node;
}
http://git-wip-us.apache.org/repos/asf/camel/blob/b05a46d9/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetails.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetails.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetails.java
index 7167eb8..d058b5b 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetails.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetails.java
@@ -31,11 +31,13 @@ public class CamelNodeDetails {
private final String name;
private final int order;
private List<CamelNodeDetails> outputs;
+ private String routeId;
public CamelNodeDetails(CamelNodeDetails parent, String name, int order, CamelNodeDetails copy) {
this.parent = parent;
this.name = name;
this.order = order;
+ this.routeId = copy.getRouteId();
this.fileName = copy.getFileName();
this.lineNumber = copy.getLineNumber();
this.lineNumberEnd = copy.getLineNumberEnd();
@@ -78,6 +80,14 @@ public class CamelNodeDetails {
return outputs;
}
+ public String getRouteId() {
+ return routeId;
+ }
+
+ public void setRouteId(String routeId) {
+ this.routeId = routeId;
+ }
+
public String getFileName() {
return fileName;
}
http://git-wip-us.apache.org/repos/asf/camel/blob/b05a46d9/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
index 6378f41..24f3c81 100644
--- a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
+++ b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
@@ -46,6 +46,7 @@ public class RoasterJavaDslTest extends CamelTestSupport {
assertEquals(1, list.size());
CamelNodeDetails details = list.get(0);
assertEquals("src/test/java/org/apache/camel/parser/java/MyJavaDslRouteBuilder.java", details.getFileName());
+ assertEquals("bar", details.getRouteId());
String tree = details.dump(0);
LOG.info("\n" + tree);
http://git-wip-us.apache.org/repos/asf/camel/blob/b05a46d9/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTwoRoutesTest.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTwoRoutesTest.java b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTwoRoutesTest.java
index 6c80003..4e555a9 100644
--- a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTwoRoutesTest.java
+++ b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTwoRoutesTest.java
@@ -50,6 +50,9 @@ public class RoasterJavaDslTwoRoutesTest extends CamelTestSupport {
assertEquals("src/test/java/org/apache/camel/parser/java/TwoRoutesRouteBuilder.java", details.getFileName());
assertEquals("src/test/java/org/apache/camel/parser/java/TwoRoutesRouteBuilder.java", details2.getFileName());
+ assertEquals("foo", details.getRouteId());
+ assertEquals("bar", details.getRouteId());
+
String tree = details.dump(0);
LOG.info("\n" + tree);
[11/13] camel git commit: Camel route coverage maven plugin
Posted by da...@apache.org.
Camel route coverage maven plugin
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/75f2ba0b
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/75f2ba0b
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/75f2ba0b
Branch: refs/heads/parser2
Commit: 75f2ba0b5c081ff0bbc7da467a0bf160561deecf
Parents: 760f05c
Author: Claus Ibsen <da...@apache.org>
Authored: Sun Oct 8 13:24:58 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun Oct 8 13:35:23 2017 +0200
----------------------------------------------------------------------
examples/camel-example-spring-boot/pom.xml | 11 ++++++
.../org/apache/camel/maven/CoverageMojo.java | 40 +++++++++++---------
.../apache/camel/maven/model/CoverageNode.java | 12 +++---
3 files changed, 40 insertions(+), 23 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/75f2ba0b/examples/camel-example-spring-boot/pom.xml
----------------------------------------------------------------------
diff --git a/examples/camel-example-spring-boot/pom.xml b/examples/camel-example-spring-boot/pom.xml
index 051c1d3..205cfb7 100644
--- a/examples/camel-example-spring-boot/pom.xml
+++ b/examples/camel-example-spring-boot/pom.xml
@@ -115,6 +115,17 @@
</execution>
</executions>
</plugin>
+
+ <plugin>
+ <groupId>org.apache.camel</groupId>
+ <artifactId>camel-maven-plugin</artifactId>
+ <version>${project.version}</version>
+ <!-- allows to fail if not all routes are fully covered during testing
+ <configuration>
+ <failOnError>true</failOnError>
+ </configuration>
+ -->
+ </plugin>
</plugins>
</build>
http://git-wip-us.apache.org/repos/asf/camel/blob/75f2ba0b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/CoverageMojo.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/CoverageMojo.java b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/CoverageMojo.java
index ef1c8cd..82392f6 100644
--- a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/CoverageMojo.java
+++ b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/CoverageMojo.java
@@ -170,38 +170,39 @@ public class CoverageMojo extends AbstractExecMojo {
getLog().warn("Discovered " + anonymous + " anonymous routes. Add route ids to these routes for route coverage support");
}
- routeTrees = routeTrees.stream().filter(t -> t.getRouteId() != null).collect(Collectors.toList());
+ final AtomicInteger notCovered = new AtomicInteger();
- routeTrees.forEach(t -> {
+ routeTrees = routeTrees.stream().filter(t -> t.getRouteId() != null).collect(Collectors.toList());
+ for (CamelNodeDetails t : routeTrees) {
String routeId = t.getRouteId();
String fileName = asRelativeFile(t.getFileName());
// grab dump data for the route
try {
List<KeyValueHolder<String, Integer>> coverageData = CoverageHelper.parseDumpRouteCoverageByRouteId("target/camel-route-coverage", routeId);
-
- List<CoverageNode> coverage = gatherRouteCoverageSummary(t, coverageData);
-
- String out = templateCoverageData(fileName, routeId, coverage);
- getLog().info("Route coverage summary:\n\n" + out);
- getLog().info("");
+ if (coverageData.isEmpty()) {
+ getLog().warn("No route coverage data found for route: " + routeId
+ + ". Make sure to enable route coverage in your unit tests and assign unique route ids to your routes. Also remember to run unit tests first.");
+ } else {
+ List<CoverageNode> coverage = gatherRouteCoverageSummary(t, coverageData);
+ String out = templateCoverageData(fileName, routeId, coverage, notCovered);
+ getLog().info("Route coverage summary:\n\n" + out);
+ getLog().info("");
+ }
} catch (Exception e) {
- e.printStackTrace();
- // ignore
+ throw new MojoExecutionException("Error during gathering route coverage data for route: " + routeId, e);
}
- });
-
- int notCovered = 0;
+ }
- if (failOnError && (notCovered > 0)) {
- throw new MojoExecutionException("Some routes are not fully covered");
+ if (failOnError && notCovered.get() > 0) {
+ throw new MojoExecutionException("There are " + notCovered.get() + " route(s) not fully covered!");
}
}
// CHECKSTYLE:ON
@SuppressWarnings("unchecked")
- private String templateCoverageData(String fileName, String routeId, List<CoverageNode> model) throws MojoExecutionException {
+ private String templateCoverageData(String fileName, String routeId, List<CoverageNode> model, AtomicInteger notCovered) throws MojoExecutionException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PrintStream sw = new PrintStream(bos);
@@ -220,6 +221,11 @@ public class CoverageMojo extends AbstractExecMojo {
sw.println(String.format("%8s %8s %s", node.getLineNumber(), node.getCount(), pad + node.getName()));
}
+ if (covered != model.size()) {
+ // okay here is a route that was not fully covered
+ notCovered.incrementAndGet();
+ }
+
// calculate percentage of route coverage (must use double to have decimals)
double percentage = ((double) covered / (double) model.size()) * 100;
sw.println();
@@ -241,7 +247,7 @@ public class CoverageMojo extends AbstractExecMojo {
private static void gatherRouteCoverageSummary(CamelNodeDetails node, Iterator<KeyValueHolder<String, Integer>> it, AtomicInteger level, List<CoverageNode> answer) {
CoverageNode data = new CoverageNode();
data.setName(node.getName());
- data.setLineNumber(node.getLineNumber());
+ data.setLineNumber(Integer.valueOf(node.getLineNumber()));
data.setLevel(level.get());
// add data
http://git-wip-us.apache.org/repos/asf/camel/blob/75f2ba0b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/model/CoverageNode.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/model/CoverageNode.java b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/model/CoverageNode.java
index 1e749cb..9b26dd7 100644
--- a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/model/CoverageNode.java
+++ b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/model/CoverageNode.java
@@ -19,8 +19,8 @@ package org.apache.camel.maven.model;
public final class CoverageNode {
private String name;
- private String lineNumber;
- private Integer count;
+ private int lineNumber;
+ private int count;
private int level;
public String getName() {
@@ -31,19 +31,19 @@ public final class CoverageNode {
this.name = name;
}
- public String getLineNumber() {
+ public int getLineNumber() {
return lineNumber;
}
- public void setLineNumber(String lineNumber) {
+ public void setLineNumber(int lineNumber) {
this.lineNumber = lineNumber;
}
- public Integer getCount() {
+ public int getCount() {
return count;
}
- public void setCount(Integer count) {
+ public void setCount(int count) {
this.count = count;
}
[05/13] camel git commit: Camel java dsl parser prototype
Posted by da...@apache.org.
Camel java dsl parser prototype
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/cb55f5a1
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/cb55f5a1
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/cb55f5a1
Branch: refs/heads/parser2
Commit: cb55f5a11fbe0656de2e981a4504a43ed707fa7d
Parents: 84cb66b
Author: Claus Ibsen <da...@apache.org>
Authored: Sat Oct 7 18:16:56 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat Oct 7 18:16:56 2017 +0200
----------------------------------------------------------------------
.../parser/AdvancedRouteBuilderParser.java | 57 ---
.../parser/RouteBuilderNodeDetailsParser.java | 24 +
.../apache/camel/parser/RouteBuilderParser.java | 36 ++
.../helper/AdvancedCamelJavaParserHelper.java | 440 ------------------
.../helper/CamelJavaTreeParserHelper.java | 443 +++++++++++++++++++
.../camel/parser/java/RoasterJavaDslTest.java | 17 +-
6 files changed, 517 insertions(+), 500 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/cb55f5a1/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/AdvancedRouteBuilderParser.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/AdvancedRouteBuilderParser.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/AdvancedRouteBuilderParser.java
deleted file mode 100644
index 9159203..0000000
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/AdvancedRouteBuilderParser.java
+++ /dev/null
@@ -1,57 +0,0 @@
-/**
- * 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
- * <p>
- * http://www.apache.org/licenses/LICENSE-2.0
- * <p>
- * 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.parser;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.camel.parser.helper.AdvancedCamelJavaParserHelper;
-import org.apache.camel.parser.helper.CamelJavaParserHelper;
-import org.apache.camel.parser.model.CamelNodeDetails;
-import org.jboss.forge.roaster.model.source.JavaClassSource;
-import org.jboss.forge.roaster.model.source.MethodSource;
-
-/**
- * TODO: Merge this to {@link RouteBuilderParser}
- */
-public class AdvancedRouteBuilderParser {
-
- // TODO: list of details, on per route
- public static CamelNodeDetails parseRouteBuilder(JavaClassSource clazz, String baseDir, String fullyQualifiedFileName,
- boolean includeInlinedRouteBuilders) {
- AdvancedCamelJavaParserHelper parser = new AdvancedCamelJavaParserHelper();
-
- List<MethodSource<JavaClassSource>> methods = new ArrayList<>();
- MethodSource<JavaClassSource> method = CamelJavaParserHelper.findConfigureMethod(clazz);
- if (method != null) {
- methods.add(method);
- }
- if (includeInlinedRouteBuilders) {
- List<MethodSource<JavaClassSource>> inlinedMethods = CamelJavaParserHelper.findInlinedConfigureMethods(clazz);
- if (!inlinedMethods.isEmpty()) {
- methods.addAll(inlinedMethods);
- }
- }
-
- for (MethodSource<JavaClassSource> configureMethod : methods) {
- CamelNodeDetails details = parser.parseCamelRoute(clazz, baseDir, fullyQualifiedFileName, configureMethod);
- return details;
- }
-
- return null;
- }
-}
http://git-wip-us.apache.org/repos/asf/camel/blob/cb55f5a1/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderNodeDetailsParser.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderNodeDetailsParser.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderNodeDetailsParser.java
new file mode 100644
index 0000000..cb8591b
--- /dev/null
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderNodeDetailsParser.java
@@ -0,0 +1,24 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.parser;
+
+/**
+ * TODO: Merge this to {@link RouteBuilderParser}
+ */
+public class RouteBuilderNodeDetailsParser {
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/cb55f5a1/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java
index d38e1d6..aa54cb3 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java
@@ -22,8 +22,10 @@ import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
+import org.apache.camel.parser.helper.CamelJavaTreeParserHelper;
import org.apache.camel.parser.helper.CamelJavaParserHelper;
import org.apache.camel.parser.model.CamelEndpointDetails;
+import org.apache.camel.parser.model.CamelNodeDetails;
import org.apache.camel.parser.model.CamelRouteDetails;
import org.apache.camel.parser.model.CamelSimpleExpressionDetails;
import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ASTNode;
@@ -48,6 +50,40 @@ public final class RouteBuilderParser {
}
/**
+ * Parses the java source class and build a route model (tree) of the discovered routes in the java source class.
+ *
+ * @param clazz the java source class
+ * @param baseDir the base of the source code
+ * @param fullyQualifiedFileName the fully qualified source code file name
+ * @return a list of route model (tree) of each discovered route
+ */
+ public static List<CamelNodeDetails> parseRouteBuilderTree(JavaClassSource clazz, String baseDir, String fullyQualifiedFileName,
+ boolean includeInlinedRouteBuilders) {
+
+ List<MethodSource<JavaClassSource>> methods = new ArrayList<>();
+ MethodSource<JavaClassSource> method = CamelJavaParserHelper.findConfigureMethod(clazz);
+ if (method != null) {
+ methods.add(method);
+ }
+ if (includeInlinedRouteBuilders) {
+ List<MethodSource<JavaClassSource>> inlinedMethods = CamelJavaParserHelper.findInlinedConfigureMethods(clazz);
+ if (!inlinedMethods.isEmpty()) {
+ methods.addAll(inlinedMethods);
+ }
+ }
+
+ CamelJavaTreeParserHelper parser = new CamelJavaTreeParserHelper();
+ List<CamelNodeDetails> list = new ArrayList<>();
+ for (MethodSource<JavaClassSource> configureMethod : methods) {
+ // TODO: list of details, on per route
+ CamelNodeDetails details = parser.parseCamelRoute(clazz, baseDir, fullyQualifiedFileName, configureMethod);
+ list.add(details);
+ }
+
+ return list;
+ }
+
+ /**
* Parses the java source class to discover Camel endpoints.
*
* @param clazz the java source class
http://git-wip-us.apache.org/repos/asf/camel/blob/cb55f5a1/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java
deleted file mode 100644
index e57a09b..0000000
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java
+++ /dev/null
@@ -1,440 +0,0 @@
-/**
- * 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.parser.helper;
-
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileReader;
-import java.util.List;
-import java.util.Map;
-
-import org.apache.camel.catalog.CamelCatalog;
-import org.apache.camel.catalog.DefaultCamelCatalog;
-import org.apache.camel.catalog.JSonSchemaHelper;
-import org.apache.camel.parser.RouteBuilderParser;
-import org.apache.camel.parser.model.CamelNodeDetails;
-import org.apache.camel.parser.model.CamelNodeDetailsFactory;
-import org.apache.camel.parser.roaster.StatementFieldSource;
-import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ASTNode;
-import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
-import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Block;
-import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.BooleanLiteral;
-import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Expression;
-import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ExpressionStatement;
-import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.FieldDeclaration;
-import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.InfixExpression;
-import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MemberValuePair;
-import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodDeclaration;
-import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodInvocation;
-import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.NormalAnnotation;
-import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.NumberLiteral;
-import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ParenthesizedExpression;
-import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.QualifiedName;
-import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SimpleName;
-import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SingleMemberAnnotation;
-import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.StringLiteral;
-import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Type;
-import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.VariableDeclarationFragment;
-import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.VariableDeclarationStatement;
-import org.jboss.forge.roaster.model.Annotation;
-import org.jboss.forge.roaster.model.source.FieldSource;
-import org.jboss.forge.roaster.model.source.JavaClassSource;
-import org.jboss.forge.roaster.model.source.MethodSource;
-
-/**
- * A Camel Java parser that only depends on the Roaster API.
- * <p/>
- * This implementation is lower level details. For a higher level parser see {@link RouteBuilderParser}.
- */
-// TODO: rename this class
-public final class AdvancedCamelJavaParserHelper {
-
- private CamelCatalog camelCatalog = new DefaultCamelCatalog(true);
-
- public CamelNodeDetails parseCamelRoute(JavaClassSource clazz, String baseDir, String fullyQualifiedFileName,
- MethodSource<JavaClassSource> method) {
-
- // find any from which is the start of the route
- CamelNodeDetailsFactory nodeFactory = CamelNodeDetailsFactory.newInstance();
-
- CamelNodeDetails route = nodeFactory.newNode(null, "route");
-
- if (method != null) {
- MethodDeclaration md = (MethodDeclaration) method.getInternal();
- Block block = md.getBody();
- if (block != null) {
- for (Object statement : md.getBody().statements()) {
- // must be a method call expression
- if (statement instanceof ExpressionStatement) {
- ExpressionStatement es = (ExpressionStatement) statement;
- Expression exp = es.getExpression();
- parseExpression(nodeFactory, fullyQualifiedFileName, clazz, block, exp, route);
- }
- }
- }
- }
-
- // now parse the route node and build the correct model/tree structure of the EIPs
-
- // re-create factory as we rebuild the tree
- nodeFactory = CamelNodeDetailsFactory.newInstance();
-
- CamelNodeDetails from = route.getOutputs().get(0);
- CamelNodeDetails answer = nodeFactory.copyNode(null, "from", from);
- answer.setFileName(fullyQualifiedFileName);
-
- CamelNodeDetails parent = answer;
- for (int i = 1; i < route.getOutputs().size(); i++) {
-
- CamelNodeDetails node = route.getOutputs().get(i);
- String name = node.getName();
-
- // TODO: use camel catalog to know about these types and when to do what
- // special for some EIPs
- if ("choice".equals(name)) {
- CamelNodeDetails output = nodeFactory.copyNode(parent, name, node);
- parent.addOutput(output);
- parent = output;
- } else if ("when".equals(name)) {
- parent = grandParent(parent, "choice");
- CamelNodeDetails output = nodeFactory.copyNode(parent, name, node);
- parent.addOutput(output);
- parent = output;
- } else if ("otherwise".equals(name)) {
- parent = grandParent(parent, "choice");
- CamelNodeDetails output = nodeFactory.copyNode(parent, name, node);
- parent.addOutput(output);
- parent = output;
- } else if ("end".equals(name) || "endChoice".equals(name) || "endDoTry".equals(name)) {
- // special for ending otherwise, as we end it automatic in Java DSL so do a double end then
- if ("otherwise".equals(parent.getName())) {
- parent = parent.getParent();
- }
- // parent should be grand parent
- parent = parent.getParent();
- } else {
- boolean hasOutput = hasOutput(name);
- if (hasOutput) {
- // has output so add as new child node
- CamelNodeDetails output = nodeFactory.copyNode(parent, name, node);
- parent.addOutput(output);
- parent = output;
- } else {
- // add straight to itself
- CamelNodeDetails output = nodeFactory.copyNode(parent, name, node);
- parent.addOutput(output);
- }
- }
- }
-
- return answer;
- }
-
- private boolean hasOutput(String name) {
- String json = camelCatalog.modelJSonSchema(name);
- List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("model", json, false);
- return isModelOutput(rows);
- }
-
- private static boolean isModelOutput(List<Map<String, String>> rows) {
- for (Map<String, String> row : rows) {
- if (row.containsKey("output")) {
- return "true".equals(row.get("output"));
- }
- }
- return false;
- }
-
- private static CamelNodeDetails grandParent(CamelNodeDetails node, String parentName) {
- if (node == null) {
- return null;
- }
- if (parentName.equals(node.getName())) {
- return node;
- } else {
- return grandParent(node.getParent(), parentName);
- }
- }
-
- private void parseExpression(CamelNodeDetailsFactory nodeFactory, String fullyQualifiedFileName,
- JavaClassSource clazz, Block block, Expression exp, CamelNodeDetails node) {
- if (exp == null) {
- return;
- }
- if (exp instanceof MethodInvocation) {
- MethodInvocation mi = (MethodInvocation) exp;
- node = doParseCamelModels(nodeFactory, fullyQualifiedFileName, clazz, block, mi, node);
- // if the method was called on another method, then recursive
- exp = mi.getExpression();
- parseExpression(nodeFactory, fullyQualifiedFileName, clazz, block, exp, node);
- }
- }
-
- private CamelNodeDetails doParseCamelModels(CamelNodeDetailsFactory nodeFactory, String fullyQualifiedFileName,
- JavaClassSource clazz, Block block, MethodInvocation mi, CamelNodeDetails node) {
- String name = mi.getName().getIdentifier();
-
- // special for Java DSL having some endXXX
- boolean isEnd = "end".equals(name) || "endChoice".equals(name) || "endDoTry".equals(name);
-
- // only include if its a known Camel model
- if (isEnd || camelCatalog.findModelNames().contains(name)) {
- CamelNodeDetails newNode = nodeFactory.newNode(node, name);
-
- // include source code details
- int pos = mi.getName().getStartPosition();
- int line = findLineNumber(fullyQualifiedFileName, pos);
- if (line > -1) {
- newNode.setLineNumber("" + line);
- }
- newNode.setFileName(fullyQualifiedFileName);
-
- node.addPreliminaryOutput(newNode);
- return node;
- }
-
- return node;
- }
-
- @SuppressWarnings("unchecked")
- private static FieldSource<JavaClassSource> getField(JavaClassSource clazz, Block block, SimpleName ref) {
- String fieldName = ref.getIdentifier();
- if (fieldName != null) {
- // find field in class
- FieldSource field = clazz != null ? clazz.getField(fieldName) : null;
- if (field == null) {
- field = findFieldInBlock(clazz, block, fieldName);
- }
- return field;
- }
- return null;
- }
-
- @SuppressWarnings("unchecked")
- private static FieldSource<JavaClassSource> findFieldInBlock(JavaClassSource clazz, Block block, String fieldName) {
- for (Object statement : block.statements()) {
- // try local statements first in the block
- if (statement instanceof VariableDeclarationStatement) {
- final Type type = ((VariableDeclarationStatement) statement).getType();
- for (Object obj : ((VariableDeclarationStatement) statement).fragments()) {
- if (obj instanceof VariableDeclarationFragment) {
- VariableDeclarationFragment fragment = (VariableDeclarationFragment) obj;
- SimpleName name = fragment.getName();
- if (name != null && fieldName.equals(name.getIdentifier())) {
- return new StatementFieldSource(clazz, fragment, type);
- }
- }
- }
- }
-
- // okay the field may be burried inside an anonymous inner class as a field declaration
- // outside the configure method, so lets go back to the parent and see what we can find
- ASTNode node = block.getParent();
- if (node instanceof MethodDeclaration) {
- node = node.getParent();
- }
- if (node instanceof AnonymousClassDeclaration) {
- List declarations = ((AnonymousClassDeclaration) node).bodyDeclarations();
- for (Object dec : declarations) {
- if (dec instanceof FieldDeclaration) {
- FieldDeclaration fd = (FieldDeclaration) dec;
- final Type type = fd.getType();
- for (Object obj : fd.fragments()) {
- if (obj instanceof VariableDeclarationFragment) {
- VariableDeclarationFragment fragment = (VariableDeclarationFragment) obj;
- SimpleName name = fragment.getName();
- if (name != null && fieldName.equals(name.getIdentifier())) {
- return new StatementFieldSource(clazz, fragment, type);
- }
- }
- }
- }
- }
- }
- }
- return null;
- }
-
- /**
- * @deprecated currently not in use
- */
- @Deprecated
- public static String getLiteralValue(JavaClassSource clazz, Block block, Expression expression) {
- // unwrap parenthesis
- if (expression instanceof ParenthesizedExpression) {
- expression = ((ParenthesizedExpression) expression).getExpression();
- }
-
- if (expression instanceof StringLiteral) {
- return ((StringLiteral) expression).getLiteralValue();
- } else if (expression instanceof BooleanLiteral) {
- return "" + ((BooleanLiteral) expression).booleanValue();
- } else if (expression instanceof NumberLiteral) {
- return ((NumberLiteral) expression).getToken();
- }
-
- // if it a method invocation then add a dummy value assuming the method invocation will return a valid response
- if (expression instanceof MethodInvocation) {
- String name = ((MethodInvocation) expression).getName().getIdentifier();
- return "{{" + name + "}}";
- }
-
- // if its a qualified name (usually a constant field in another class)
- // then add a dummy value as we cannot find the field value in other classes and maybe even outside the
- // source code we have access to
- if (expression instanceof QualifiedName) {
- QualifiedName qn = (QualifiedName) expression;
- String name = qn.getFullyQualifiedName();
- return "{{" + name + "}}";
- }
-
- if (expression instanceof SimpleName) {
- FieldSource<JavaClassSource> field = getField(clazz, block, (SimpleName) expression);
- if (field != null) {
- // is the field annotated with a Camel endpoint
- if (field.getAnnotations() != null) {
- for (Annotation ann : field.getAnnotations()) {
- boolean valid = "org.apache.camel.EndpointInject".equals(ann.getQualifiedName()) || "org.apache.camel.cdi.Uri".equals(ann.getQualifiedName());
- if (valid) {
- Expression exp = (Expression) ann.getInternal();
- if (exp instanceof SingleMemberAnnotation) {
- exp = ((SingleMemberAnnotation) exp).getValue();
- } else if (exp instanceof NormalAnnotation) {
- List values = ((NormalAnnotation) exp).values();
- for (Object value : values) {
- MemberValuePair pair = (MemberValuePair) value;
- if ("uri".equals(pair.getName().toString())) {
- exp = pair.getValue();
- break;
- }
- }
- }
- if (exp != null) {
- return getLiteralValue(clazz, block, exp);
- }
- }
- }
- }
- // is the field an org.apache.camel.Endpoint type?
- if ("Endpoint".equals(field.getType().getSimpleName())) {
- // then grab the uri from the first argument
- VariableDeclarationFragment vdf = (VariableDeclarationFragment) field.getInternal();
- expression = vdf.getInitializer();
- if (expression instanceof MethodInvocation) {
- MethodInvocation mi = (MethodInvocation) expression;
- List args = mi.arguments();
- if (args != null && args.size() > 0) {
- // the first argument has the endpoint uri
- expression = (Expression) args.get(0);
- return getLiteralValue(clazz, block, expression);
- }
- }
- } else {
- // no annotations so try its initializer
- VariableDeclarationFragment vdf = (VariableDeclarationFragment) field.getInternal();
- expression = vdf.getInitializer();
- if (expression == null) {
- // its a field which has no initializer, then add a dummy value assuming the field will be initialized at runtime
- return "{{" + field.getName() + "}}";
- } else {
- return getLiteralValue(clazz, block, expression);
- }
- }
- } else {
- // we could not find the field in this class/method, so its maybe from some other super class, so insert a dummy value
- final String fieldName = ((SimpleName) expression).getIdentifier();
- return "{{" + fieldName + "}}";
- }
- } else if (expression instanceof InfixExpression) {
- String answer = null;
- // is it a string that is concat together?
- InfixExpression ie = (InfixExpression) expression;
- if (InfixExpression.Operator.PLUS.equals(ie.getOperator())) {
-
- String val1 = getLiteralValue(clazz, block, ie.getLeftOperand());
- String val2 = getLiteralValue(clazz, block, ie.getRightOperand());
-
- // if numeric then we plus the values, otherwise we string concat
- boolean numeric = isNumericOperator(clazz, block, ie.getLeftOperand()) && isNumericOperator(clazz, block, ie.getRightOperand());
- if (numeric) {
- Long num1 = val1 != null ? Long.valueOf(val1) : 0;
- Long num2 = val2 != null ? Long.valueOf(val2) : 0;
- answer = "" + (num1 + num2);
- } else {
- answer = (val1 != null ? val1 : "") + (val2 != null ? val2 : "");
- }
-
- if (!answer.isEmpty()) {
- // include extended when we concat on 2 or more lines
- List extended = ie.extendedOperands();
- if (extended != null) {
- for (Object ext : extended) {
- String val3 = getLiteralValue(clazz, block, (Expression) ext);
- if (numeric) {
- Long num3 = val3 != null ? Long.valueOf(val3) : 0;
- Long num = Long.valueOf(answer);
- answer = "" + (num + num3);
- } else {
- answer += val3 != null ? val3 : "";
- }
- }
- }
- }
- }
- return answer;
- }
-
- return null;
- }
-
- private static boolean isNumericOperator(JavaClassSource clazz, Block block, Expression expression) {
- if (expression instanceof NumberLiteral) {
- return true;
- } else if (expression instanceof SimpleName) {
- FieldSource field = getField(clazz, block, (SimpleName) expression);
- if (field != null) {
- return field.getType().isType("int") || field.getType().isType("long")
- || field.getType().isType("Integer") || field.getType().isType("Long");
- }
- }
- return false;
- }
-
- private static int findLineNumber(String fullyQualifiedFileName, int position) {
- int lines = 0;
-
- try {
- int current = 0;
- try (BufferedReader br = new BufferedReader(new FileReader(new File(fullyQualifiedFileName)))) {
- String line;
- while ((line = br.readLine()) != null) {
- lines++;
- current += line.length() + 1; // add 1 for line feed
- if (current >= position) {
- return lines;
- }
- }
- }
- } catch (Exception e) {
- // ignore
- return -1;
- }
-
- return lines;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/camel/blob/cb55f5a1/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaTreeParserHelper.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaTreeParserHelper.java b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaTreeParserHelper.java
new file mode 100644
index 0000000..af19acd
--- /dev/null
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelJavaTreeParserHelper.java
@@ -0,0 +1,443 @@
+/**
+ * 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.parser.helper;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.camel.catalog.CamelCatalog;
+import org.apache.camel.catalog.DefaultCamelCatalog;
+import org.apache.camel.catalog.JSonSchemaHelper;
+import org.apache.camel.parser.RouteBuilderParser;
+import org.apache.camel.parser.model.CamelNodeDetails;
+import org.apache.camel.parser.model.CamelNodeDetailsFactory;
+import org.apache.camel.parser.roaster.StatementFieldSource;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ASTNode;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Block;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.BooleanLiteral;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Expression;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ExpressionStatement;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.FieldDeclaration;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.InfixExpression;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MemberValuePair;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodDeclaration;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.MethodInvocation;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.NormalAnnotation;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.NumberLiteral;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.ParenthesizedExpression;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.QualifiedName;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SimpleName;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.SingleMemberAnnotation;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.StringLiteral;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.Type;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.VariableDeclarationFragment;
+import org.jboss.forge.roaster._shade.org.eclipse.jdt.core.dom.VariableDeclarationStatement;
+import org.jboss.forge.roaster.model.Annotation;
+import org.jboss.forge.roaster.model.source.FieldSource;
+import org.jboss.forge.roaster.model.source.JavaClassSource;
+import org.jboss.forge.roaster.model.source.MethodSource;
+
+/**
+ * A Camel Java tree parser that only depends on the Roaster API.
+ * <p/>
+ * This implement is used for parsing the Camel routes and build a tree structure of the EIP nodes.
+ *
+ * @see CamelJavaParserHelper for parser that can discover endpoints and simple expressions
+ */
+public final class CamelJavaTreeParserHelper {
+
+ private CamelCatalog camelCatalog = new DefaultCamelCatalog(true);
+
+ // TODO: add support for multiple routes
+ // TODO: rename this method
+ public CamelNodeDetails parseCamelRoute(JavaClassSource clazz, String baseDir, String fullyQualifiedFileName,
+ MethodSource<JavaClassSource> method) {
+
+ // find any from which is the start of the route
+ CamelNodeDetailsFactory nodeFactory = CamelNodeDetailsFactory.newInstance();
+
+ CamelNodeDetails route = nodeFactory.newNode(null, "route");
+
+ if (method != null) {
+ MethodDeclaration md = (MethodDeclaration) method.getInternal();
+ Block block = md.getBody();
+ if (block != null) {
+ for (Object statement : md.getBody().statements()) {
+ // must be a method call expression
+ if (statement instanceof ExpressionStatement) {
+ ExpressionStatement es = (ExpressionStatement) statement;
+ Expression exp = es.getExpression();
+ parseExpression(nodeFactory, fullyQualifiedFileName, clazz, block, exp, route);
+ }
+ }
+ }
+ }
+
+ // now parse the route node and build the correct model/tree structure of the EIPs
+
+ // re-create factory as we rebuild the tree
+ nodeFactory = CamelNodeDetailsFactory.newInstance();
+
+ CamelNodeDetails from = route.getOutputs().get(0);
+ CamelNodeDetails answer = nodeFactory.copyNode(null, "from", from);
+ answer.setFileName(fullyQualifiedFileName);
+
+ CamelNodeDetails parent = answer;
+ for (int i = 1; i < route.getOutputs().size(); i++) {
+
+ CamelNodeDetails node = route.getOutputs().get(i);
+ String name = node.getName();
+
+ // TODO: use camel catalog to know about these types and when to do what
+ // special for some EIPs
+ if ("choice".equals(name)) {
+ CamelNodeDetails output = nodeFactory.copyNode(parent, name, node);
+ parent.addOutput(output);
+ parent = output;
+ } else if ("when".equals(name)) {
+ parent = grandParent(parent, "choice");
+ CamelNodeDetails output = nodeFactory.copyNode(parent, name, node);
+ parent.addOutput(output);
+ parent = output;
+ } else if ("otherwise".equals(name)) {
+ parent = grandParent(parent, "choice");
+ CamelNodeDetails output = nodeFactory.copyNode(parent, name, node);
+ parent.addOutput(output);
+ parent = output;
+ } else if ("end".equals(name) || "endChoice".equals(name) || "endDoTry".equals(name)) {
+ // special for ending otherwise, as we end it automatic in Java DSL so do a double end then
+ if ("otherwise".equals(parent.getName())) {
+ parent = parent.getParent();
+ }
+ // parent should be grand parent
+ parent = parent.getParent();
+ } else {
+ boolean hasOutput = hasOutput(name);
+ if (hasOutput) {
+ // has output so add as new child node
+ CamelNodeDetails output = nodeFactory.copyNode(parent, name, node);
+ parent.addOutput(output);
+ parent = output;
+ } else {
+ // add straight to itself
+ CamelNodeDetails output = nodeFactory.copyNode(parent, name, node);
+ parent.addOutput(output);
+ }
+ }
+ }
+
+ return answer;
+ }
+
+ private boolean hasOutput(String name) {
+ String json = camelCatalog.modelJSonSchema(name);
+ List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("model", json, false);
+ return isModelOutput(rows);
+ }
+
+ private static boolean isModelOutput(List<Map<String, String>> rows) {
+ for (Map<String, String> row : rows) {
+ if (row.containsKey("output")) {
+ return "true".equals(row.get("output"));
+ }
+ }
+ return false;
+ }
+
+ private static CamelNodeDetails grandParent(CamelNodeDetails node, String parentName) {
+ if (node == null) {
+ return null;
+ }
+ if (parentName.equals(node.getName())) {
+ return node;
+ } else {
+ return grandParent(node.getParent(), parentName);
+ }
+ }
+
+ private void parseExpression(CamelNodeDetailsFactory nodeFactory, String fullyQualifiedFileName,
+ JavaClassSource clazz, Block block, Expression exp, CamelNodeDetails node) {
+ if (exp == null) {
+ return;
+ }
+ if (exp instanceof MethodInvocation) {
+ MethodInvocation mi = (MethodInvocation) exp;
+ node = doParseCamelModels(nodeFactory, fullyQualifiedFileName, clazz, block, mi, node);
+ // if the method was called on another method, then recursive
+ exp = mi.getExpression();
+ parseExpression(nodeFactory, fullyQualifiedFileName, clazz, block, exp, node);
+ }
+ }
+
+ private CamelNodeDetails doParseCamelModels(CamelNodeDetailsFactory nodeFactory, String fullyQualifiedFileName,
+ JavaClassSource clazz, Block block, MethodInvocation mi, CamelNodeDetails node) {
+ String name = mi.getName().getIdentifier();
+
+ // special for Java DSL having some endXXX
+ boolean isEnd = "end".equals(name) || "endChoice".equals(name) || "endDoTry".equals(name);
+
+ // only include if its a known Camel model
+ if (isEnd || camelCatalog.findModelNames().contains(name)) {
+ CamelNodeDetails newNode = nodeFactory.newNode(node, name);
+
+ // include source code details
+ int pos = mi.getName().getStartPosition();
+ int line = findLineNumber(fullyQualifiedFileName, pos);
+ if (line > -1) {
+ newNode.setLineNumber("" + line);
+ }
+ newNode.setFileName(fullyQualifiedFileName);
+
+ node.addPreliminaryOutput(newNode);
+ return node;
+ }
+
+ return node;
+ }
+
+ @SuppressWarnings("unchecked")
+ private static FieldSource<JavaClassSource> getField(JavaClassSource clazz, Block block, SimpleName ref) {
+ String fieldName = ref.getIdentifier();
+ if (fieldName != null) {
+ // find field in class
+ FieldSource field = clazz != null ? clazz.getField(fieldName) : null;
+ if (field == null) {
+ field = findFieldInBlock(clazz, block, fieldName);
+ }
+ return field;
+ }
+ return null;
+ }
+
+ @SuppressWarnings("unchecked")
+ private static FieldSource<JavaClassSource> findFieldInBlock(JavaClassSource clazz, Block block, String fieldName) {
+ for (Object statement : block.statements()) {
+ // try local statements first in the block
+ if (statement instanceof VariableDeclarationStatement) {
+ final Type type = ((VariableDeclarationStatement) statement).getType();
+ for (Object obj : ((VariableDeclarationStatement) statement).fragments()) {
+ if (obj instanceof VariableDeclarationFragment) {
+ VariableDeclarationFragment fragment = (VariableDeclarationFragment) obj;
+ SimpleName name = fragment.getName();
+ if (name != null && fieldName.equals(name.getIdentifier())) {
+ return new StatementFieldSource(clazz, fragment, type);
+ }
+ }
+ }
+ }
+
+ // okay the field may be burried inside an anonymous inner class as a field declaration
+ // outside the configure method, so lets go back to the parent and see what we can find
+ ASTNode node = block.getParent();
+ if (node instanceof MethodDeclaration) {
+ node = node.getParent();
+ }
+ if (node instanceof AnonymousClassDeclaration) {
+ List declarations = ((AnonymousClassDeclaration) node).bodyDeclarations();
+ for (Object dec : declarations) {
+ if (dec instanceof FieldDeclaration) {
+ FieldDeclaration fd = (FieldDeclaration) dec;
+ final Type type = fd.getType();
+ for (Object obj : fd.fragments()) {
+ if (obj instanceof VariableDeclarationFragment) {
+ VariableDeclarationFragment fragment = (VariableDeclarationFragment) obj;
+ SimpleName name = fragment.getName();
+ if (name != null && fieldName.equals(name.getIdentifier())) {
+ return new StatementFieldSource(clazz, fragment, type);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * @deprecated currently not in use
+ */
+ @Deprecated
+ public static String getLiteralValue(JavaClassSource clazz, Block block, Expression expression) {
+ // unwrap parenthesis
+ if (expression instanceof ParenthesizedExpression) {
+ expression = ((ParenthesizedExpression) expression).getExpression();
+ }
+
+ if (expression instanceof StringLiteral) {
+ return ((StringLiteral) expression).getLiteralValue();
+ } else if (expression instanceof BooleanLiteral) {
+ return "" + ((BooleanLiteral) expression).booleanValue();
+ } else if (expression instanceof NumberLiteral) {
+ return ((NumberLiteral) expression).getToken();
+ }
+
+ // if it a method invocation then add a dummy value assuming the method invocation will return a valid response
+ if (expression instanceof MethodInvocation) {
+ String name = ((MethodInvocation) expression).getName().getIdentifier();
+ return "{{" + name + "}}";
+ }
+
+ // if its a qualified name (usually a constant field in another class)
+ // then add a dummy value as we cannot find the field value in other classes and maybe even outside the
+ // source code we have access to
+ if (expression instanceof QualifiedName) {
+ QualifiedName qn = (QualifiedName) expression;
+ String name = qn.getFullyQualifiedName();
+ return "{{" + name + "}}";
+ }
+
+ if (expression instanceof SimpleName) {
+ FieldSource<JavaClassSource> field = getField(clazz, block, (SimpleName) expression);
+ if (field != null) {
+ // is the field annotated with a Camel endpoint
+ if (field.getAnnotations() != null) {
+ for (Annotation ann : field.getAnnotations()) {
+ boolean valid = "org.apache.camel.EndpointInject".equals(ann.getQualifiedName()) || "org.apache.camel.cdi.Uri".equals(ann.getQualifiedName());
+ if (valid) {
+ Expression exp = (Expression) ann.getInternal();
+ if (exp instanceof SingleMemberAnnotation) {
+ exp = ((SingleMemberAnnotation) exp).getValue();
+ } else if (exp instanceof NormalAnnotation) {
+ List values = ((NormalAnnotation) exp).values();
+ for (Object value : values) {
+ MemberValuePair pair = (MemberValuePair) value;
+ if ("uri".equals(pair.getName().toString())) {
+ exp = pair.getValue();
+ break;
+ }
+ }
+ }
+ if (exp != null) {
+ return getLiteralValue(clazz, block, exp);
+ }
+ }
+ }
+ }
+ // is the field an org.apache.camel.Endpoint type?
+ if ("Endpoint".equals(field.getType().getSimpleName())) {
+ // then grab the uri from the first argument
+ VariableDeclarationFragment vdf = (VariableDeclarationFragment) field.getInternal();
+ expression = vdf.getInitializer();
+ if (expression instanceof MethodInvocation) {
+ MethodInvocation mi = (MethodInvocation) expression;
+ List args = mi.arguments();
+ if (args != null && args.size() > 0) {
+ // the first argument has the endpoint uri
+ expression = (Expression) args.get(0);
+ return getLiteralValue(clazz, block, expression);
+ }
+ }
+ } else {
+ // no annotations so try its initializer
+ VariableDeclarationFragment vdf = (VariableDeclarationFragment) field.getInternal();
+ expression = vdf.getInitializer();
+ if (expression == null) {
+ // its a field which has no initializer, then add a dummy value assuming the field will be initialized at runtime
+ return "{{" + field.getName() + "}}";
+ } else {
+ return getLiteralValue(clazz, block, expression);
+ }
+ }
+ } else {
+ // we could not find the field in this class/method, so its maybe from some other super class, so insert a dummy value
+ final String fieldName = ((SimpleName) expression).getIdentifier();
+ return "{{" + fieldName + "}}";
+ }
+ } else if (expression instanceof InfixExpression) {
+ String answer = null;
+ // is it a string that is concat together?
+ InfixExpression ie = (InfixExpression) expression;
+ if (InfixExpression.Operator.PLUS.equals(ie.getOperator())) {
+
+ String val1 = getLiteralValue(clazz, block, ie.getLeftOperand());
+ String val2 = getLiteralValue(clazz, block, ie.getRightOperand());
+
+ // if numeric then we plus the values, otherwise we string concat
+ boolean numeric = isNumericOperator(clazz, block, ie.getLeftOperand()) && isNumericOperator(clazz, block, ie.getRightOperand());
+ if (numeric) {
+ Long num1 = val1 != null ? Long.valueOf(val1) : 0;
+ Long num2 = val2 != null ? Long.valueOf(val2) : 0;
+ answer = "" + (num1 + num2);
+ } else {
+ answer = (val1 != null ? val1 : "") + (val2 != null ? val2 : "");
+ }
+
+ if (!answer.isEmpty()) {
+ // include extended when we concat on 2 or more lines
+ List extended = ie.extendedOperands();
+ if (extended != null) {
+ for (Object ext : extended) {
+ String val3 = getLiteralValue(clazz, block, (Expression) ext);
+ if (numeric) {
+ Long num3 = val3 != null ? Long.valueOf(val3) : 0;
+ Long num = Long.valueOf(answer);
+ answer = "" + (num + num3);
+ } else {
+ answer += val3 != null ? val3 : "";
+ }
+ }
+ }
+ }
+ }
+ return answer;
+ }
+
+ return null;
+ }
+
+ private static boolean isNumericOperator(JavaClassSource clazz, Block block, Expression expression) {
+ if (expression instanceof NumberLiteral) {
+ return true;
+ } else if (expression instanceof SimpleName) {
+ FieldSource field = getField(clazz, block, (SimpleName) expression);
+ if (field != null) {
+ return field.getType().isType("int") || field.getType().isType("long")
+ || field.getType().isType("Integer") || field.getType().isType("Long");
+ }
+ }
+ return false;
+ }
+
+ private static int findLineNumber(String fullyQualifiedFileName, int position) {
+ int lines = 0;
+
+ try {
+ int current = 0;
+ try (BufferedReader br = new BufferedReader(new FileReader(new File(fullyQualifiedFileName)))) {
+ String line;
+ while ((line = br.readLine()) != null) {
+ lines++;
+ current += line.length() + 1; // add 1 for line feed
+ if (current >= position) {
+ return lines;
+ }
+ }
+ }
+ } catch (Exception e) {
+ // ignore
+ return -1;
+ }
+
+ return lines;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/cb55f5a1/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
index 928ac87..9041e2f 100644
--- a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
+++ b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
@@ -17,8 +17,9 @@
package org.apache.camel.parser.java;
import java.io.File;
+import java.util.List;
-import org.apache.camel.parser.AdvancedRouteBuilderParser;
+import org.apache.camel.parser.RouteBuilderParser;
import org.apache.camel.parser.model.CamelNodeDetails;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.jboss.forge.roaster.Roaster;
@@ -37,14 +38,24 @@ public class RoasterJavaDslTest extends CamelTestSupport {
}
@Test
- public void parse() throws Exception {
+ public void parseTree() throws Exception {
JavaClassSource clazz = (JavaClassSource) Roaster.parse(new File("src/test/java/org/apache/camel/parser/java/MyJavaDslRouteBuilder.java"));
- CamelNodeDetails details = AdvancedRouteBuilderParser.parseRouteBuilder(clazz, ".",
+ List<CamelNodeDetails> list = RouteBuilderParser.parseRouteBuilderTree(clazz, ".",
"src/test/java/org/apache/camel/parser/java/MyJavaDslRouteBuilder.java",true);
+ assertEquals(1, list.size());
+ CamelNodeDetails details = list.get(0);
+ assertEquals("src/test/java/org/apache/camel/parser/java/MyJavaDslRouteBuilder.java", details.getFileName());
+
String tree = details.dump(0);
LOG.info("\n" + tree);
+ assertTrue(tree.contains("25\tfrom"));
+ assertTrue(tree.contains("27\t setHeader"));
+ assertTrue(tree.contains("30\t toD"));
+ assertTrue(tree.contains("33\t otherwise"));
+ assertTrue(tree.contains("36\t to"));
+
String name = details.getFileName();
System.out.println(name);
System.out.println(tree);
[10/13] camel git commit: Camel route coverage maven plugin
Posted by da...@apache.org.
Camel route coverage maven plugin
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/760f05cf
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/760f05cf
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/760f05cf
Branch: refs/heads/parser2
Commit: 760f05cf90280a6819546001e49fbd024d53202e
Parents: 4cf26c0
Author: Claus Ibsen <da...@apache.org>
Authored: Sun Oct 8 13:05:17 2017 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun Oct 8 13:05:17 2017 +0200
----------------------------------------------------------------------
.../java/sample/camel/SampleCamelRouter.java | 3 +
.../java/sample/camel/FooApplicationTest.java | 53 +++++++++
.../org/apache/camel/maven/CoverageMojo.java | 98 ++++++++++++++--
.../camel/maven/helper/CoverageHelper.java | 114 +++++++++++++++++++
.../apache/camel/maven/model/CoverageNode.java | 58 ++++++++++
5 files changed, 318 insertions(+), 8 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/760f05cf/examples/camel-example-spring-boot/src/main/java/sample/camel/SampleCamelRouter.java
----------------------------------------------------------------------
diff --git a/examples/camel-example-spring-boot/src/main/java/sample/camel/SampleCamelRouter.java b/examples/camel-example-spring-boot/src/main/java/sample/camel/SampleCamelRouter.java
index 20cf957..d8e7d2e 100644
--- a/examples/camel-example-spring-boot/src/main/java/sample/camel/SampleCamelRouter.java
+++ b/examples/camel-example-spring-boot/src/main/java/sample/camel/SampleCamelRouter.java
@@ -31,6 +31,9 @@ public class SampleCamelRouter extends RouteBuilder {
public void configure() throws Exception {
from("timer:hello?period={{timer.period}}").routeId("hello")
.transform(method("myBean", "saySomething"))
+ .filter(simple("${body} contains 'foo'"))
+ .to("log:foo")
+ .end()
.to("stream:out");
}
http://git-wip-us.apache.org/repos/asf/camel/blob/760f05cf/examples/camel-example-spring-boot/src/test/java/sample/camel/FooApplicationTest.java
----------------------------------------------------------------------
diff --git a/examples/camel-example-spring-boot/src/test/java/sample/camel/FooApplicationTest.java b/examples/camel-example-spring-boot/src/test/java/sample/camel/FooApplicationTest.java
new file mode 100644
index 0000000..0b76960
--- /dev/null
+++ b/examples/camel-example-spring-boot/src/test/java/sample/camel/FooApplicationTest.java
@@ -0,0 +1,53 @@
+/**
+ * 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 sample.camel;
+
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.NotifyBuilder;
+import org.apache.camel.test.spring.CamelSpringBootRunner;
+import org.apache.camel.test.spring.RouteCoverage;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.annotation.Configuration;
+
+import static org.junit.Assert.assertTrue;
+
+@RunWith(CamelSpringBootRunner.class)
+@SpringBootTest(classes = SampleCamelApplication.class,
+ properties = "greeting = Hell foo")
+@RouteCoverage
+@Ignore
+public class FooApplicationTest {
+
+ @Autowired
+ private CamelContext camelContext;
+
+ @Test
+ public void shouldSayFoo() throws Exception {
+ // we expect that one or more messages is automatic done by the Camel
+ // route as it uses a timer to trigger
+ NotifyBuilder notify = new NotifyBuilder(camelContext).whenDone(1).create();
+
+ assertTrue(notify.matches(10, TimeUnit.SECONDS));
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/760f05cf/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/CoverageMojo.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/CoverageMojo.java b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/CoverageMojo.java
index e529587..ef1c8cd 100644
--- a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/CoverageMojo.java
+++ b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/CoverageMojo.java
@@ -16,19 +16,26 @@
*/
package org.apache.camel.maven;
+import java.io.ByteArrayOutputStream;
import java.io.File;
+import java.io.PrintStream;
import java.util.ArrayList;
+import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
+import org.apache.camel.maven.helper.CoverageHelper;
import org.apache.camel.maven.helper.EndpointHelper;
+import org.apache.camel.maven.model.CoverageNode;
import org.apache.camel.parser.RouteBuilderParser;
import org.apache.camel.parser.model.CamelEndpointDetails;
import org.apache.camel.parser.model.CamelNodeDetails;
import org.apache.camel.parser.model.CamelRouteDetails;
import org.apache.camel.parser.model.CamelSimpleExpressionDetails;
+import org.apache.camel.util.KeyValueHolder;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
@@ -168,10 +175,21 @@ public class CoverageMojo extends AbstractExecMojo {
routeTrees.forEach(t -> {
String routeId = t.getRouteId();
String fileName = asRelativeFile(t.getFileName());
- String tree = t.dump(4);
- getLog().info("Route " + routeId + " discovered in file " + fileName);
- getLog().info("\n" + tree + "\n");
+ // grab dump data for the route
+ try {
+ List<KeyValueHolder<String, Integer>> coverageData = CoverageHelper.parseDumpRouteCoverageByRouteId("target/camel-route-coverage", routeId);
+
+ List<CoverageNode> coverage = gatherRouteCoverageSummary(t, coverageData);
+
+ String out = templateCoverageData(fileName, routeId, coverage);
+ getLog().info("Route coverage summary:\n\n" + out);
+ getLog().info("");
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ // ignore
+ }
});
int notCovered = 0;
@@ -182,16 +200,80 @@ public class CoverageMojo extends AbstractExecMojo {
}
// CHECKSTYLE:ON
- private static int countRouteId(List<CamelRouteDetails> details, String routeId) {
- int answer = 0;
- for (CamelRouteDetails detail : details) {
- if (routeId.equals(detail.getRouteId())) {
- answer++;
+ @SuppressWarnings("unchecked")
+ private String templateCoverageData(String fileName, String routeId, List<CoverageNode> model) throws MojoExecutionException {
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ PrintStream sw = new PrintStream(bos);
+
+ sw.println("File: " + fileName);
+ sw.println("Route: " + routeId);
+ sw.println();
+ sw.println(String.format("%8s %8s %s", "Line #", "Count", "Route"));
+ sw.println(String.format("%8s %8s %s", "------", "-----", "-----"));
+
+ int covered = 0;
+ for (CoverageNode node : model) {
+ if (node.getCount() > 0) {
+ covered++;
}
+ String pad = padString(node.getLevel());
+ sw.println(String.format("%8s %8s %s", node.getLineNumber(), node.getCount(), pad + node.getName()));
}
+
+ // calculate percentage of route coverage (must use double to have decimals)
+ double percentage = ((double) covered / (double) model.size()) * 100;
+ sw.println();
+ sw.println("Coverage: " + covered + " out of " + model.size() + " (" + String.format("%.1f", percentage) + "%)");
+ sw.println();
+
+ return bos.toString();
+ }
+
+ private static List<CoverageNode> gatherRouteCoverageSummary(CamelNodeDetails route, List<KeyValueHolder<String, Integer>> coverageData) {
+ List<CoverageNode> answer = new ArrayList<>();
+
+ Iterator<KeyValueHolder<String, Integer>> it = coverageData.iterator();
+ AtomicInteger level = new AtomicInteger();
+ gatherRouteCoverageSummary(route, it, level, answer);
return answer;
}
+ private static void gatherRouteCoverageSummary(CamelNodeDetails node, Iterator<KeyValueHolder<String, Integer>> it, AtomicInteger level, List<CoverageNode> answer) {
+ CoverageNode data = new CoverageNode();
+ data.setName(node.getName());
+ data.setLineNumber(node.getLineNumber());
+ data.setLevel(level.get());
+
+ // add data
+ answer.add(data);
+
+ // find count
+ boolean found = false;
+ while (!found && it.hasNext()) {
+ KeyValueHolder<String, Integer> holder = it.next();
+ found = holder.getKey().equals(node.getName());
+ if (found) {
+ data.setCount(holder.getValue());
+ }
+ }
+
+ if (node.getOutputs() != null) {
+ level.addAndGet(1);
+ for (CamelNodeDetails child : node.getOutputs()) {
+ gatherRouteCoverageSummary(child, it, level, answer);
+ }
+ level.addAndGet(-1);
+ }
+ }
+
+ private static String padString(int level) {
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < level; i++) {
+ sb.append(" ");
+ }
+ return sb.toString();
+ }
+
private void findJavaFiles(File dir, Set<File> javaFiles) {
File[] files = dir.isDirectory() ? dir.listFiles() : null;
if (files != null) {
http://git-wip-us.apache.org/repos/asf/camel/blob/760f05cf/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/helper/CoverageHelper.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/helper/CoverageHelper.java b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/helper/CoverageHelper.java
new file mode 100644
index 0000000..33913d4
--- /dev/null
+++ b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/helper/CoverageHelper.java
@@ -0,0 +1,114 @@
+/**
+ * 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.maven.helper;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import org.apache.camel.catalog.CamelCatalog;
+import org.apache.camel.catalog.DefaultCamelCatalog;
+import org.apache.camel.util.IOHelper;
+import org.apache.camel.util.KeyValueHolder;
+import org.apache.camel.util.XmlLineNumberParser;
+
+public final class CoverageHelper {
+
+ private CoverageHelper() {
+ }
+
+ public static List<KeyValueHolder<String, Integer>> parseDumpRouteCoverageByRouteId(String directory, String routeId) throws Exception {
+ List<KeyValueHolder<String, Integer>> answer = new ArrayList<>();
+
+ File[] files = new File(directory).listFiles(f -> f.getName().endsWith(".xml"));
+ if (files == null) {
+ return answer;
+ }
+
+ CamelCatalog catalog = new DefaultCamelCatalog(true);
+
+ for (File file : files) {
+ FileInputStream fis = new FileInputStream(file);
+ Document dom = XmlLineNumberParser.parseXml(fis);
+ IOHelper.close(fis);
+ NodeList routes = dom.getElementsByTagName("route");
+ for (int i = 0; i < routes.getLength(); i++) {
+ Node route = routes.item(i);
+ String id = route.getAttributes().getNamedItem("id").getNodeValue();
+ // must be the target route
+ if (routeId.equals(id)) {
+ // parse each route and build a Map<String, Integer> with the no of messages processed
+ // where String is the EIP name
+ AtomicInteger counter = new AtomicInteger();
+ parseRouteData(catalog, route, answer, counter);
+ }
+ }
+ }
+
+ return answer;
+ }
+
+ private static void parseRouteData(CamelCatalog catalog, Node node, List<KeyValueHolder<String, Integer>> data, AtomicInteger counter) {
+ // must be a known EIP model
+ String key = node.getNodeName();
+ boolean valid = catalog.findModelNames().contains(key); // skip route as we use from instead
+ if (!valid) {
+ return;
+ }
+
+ // only calculate for elements within the route
+ if (!"route".equals(key)) {
+ Integer count = 0;
+ Node total = node.getAttributes().getNamedItem("exchangesTotal");
+ if (total != null) {
+ count = Integer.valueOf(total.getNodeValue());
+ }
+ KeyValueHolder<String, Integer> holder = data.size() > counter.get() ? data.get(counter.get()) : null;
+ if (holder != null && holder.getKey().equals(key)) {
+ count += holder.getValue();
+ }
+ if (holder == null) {
+ // add new
+ data.add(counter.get(), new KeyValueHolder<>(key, count));
+ } else {
+ // replace existing
+ data.set(counter.get(), new KeyValueHolder<>(key, count));
+ }
+ // advance counter
+ counter.incrementAndGet();
+ }
+
+ // any children
+ NodeList children = node.getChildNodes();
+ if (children != null) {
+ for (int i = 0; i < children.getLength(); i++) {
+ Node child = children.item(i);
+ if (child instanceof Element) {
+ parseRouteData(catalog, child, data, counter);
+ }
+ }
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/760f05cf/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/model/CoverageNode.java
----------------------------------------------------------------------
diff --git a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/model/CoverageNode.java b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/model/CoverageNode.java
new file mode 100644
index 0000000..1e749cb
--- /dev/null
+++ b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/model/CoverageNode.java
@@ -0,0 +1,58 @@
+/**
+ * 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.maven.model;
+
+public final class CoverageNode {
+
+ private String name;
+ private String lineNumber;
+ private Integer count;
+ private int level;
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getLineNumber() {
+ return lineNumber;
+ }
+
+ public void setLineNumber(String lineNumber) {
+ this.lineNumber = lineNumber;
+ }
+
+ public Integer getCount() {
+ return count;
+ }
+
+ public void setCount(Integer count) {
+ this.count = count;
+ }
+
+ public int getLevel() {
+ return level;
+ }
+
+ public void setLevel(int level) {
+ this.level = level;
+ }
+
+}