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:54 UTC

[11/13] camel git commit: Camel route coverage maven plugin

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;
     }