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 2023/12/13 12:15:25 UTC

(camel) branch main updated: CAMEL-20206: split overly complex methods in catalog components (#12439)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new b9800b379bf CAMEL-20206: split overly complex methods in catalog components (#12439)
b9800b379bf is described below

commit b9800b379bf5e17c5171aa7cc5a9d7aafd005567
Author: Otavio Rodolfo Piske <or...@users.noreply.github.com>
AuthorDate: Wed Dec 13 09:15:18 2023 -0300

    CAMEL-20206: split overly complex methods in catalog components (#12439)
---
 .../org/apache/camel/maven/RouteCoverageMojo.java  |  52 ++++++-----
 .../java/org/apache/camel/maven/ValidateMojo.java  | 104 +++++++++++++--------
 .../apache/camel/parser/RouteBuilderParser.java    |  60 ++++++------
 3 files changed, 128 insertions(+), 88 deletions(-)

diff --git a/catalog/camel-report-maven-plugin/src/main/java/org/apache/camel/maven/RouteCoverageMojo.java b/catalog/camel-report-maven-plugin/src/main/java/org/apache/camel/maven/RouteCoverageMojo.java
index be0ecafed9c..3095ed7365a 100644
--- a/catalog/camel-report-maven-plugin/src/main/java/org/apache/camel/maven/RouteCoverageMojo.java
+++ b/catalog/camel-report-maven-plugin/src/main/java/org/apache/camel/maven/RouteCoverageMojo.java
@@ -223,27 +223,9 @@ public class RouteCoverageMojo extends AbstractExecMojo {
             }
 
             // grab dump data for the route
-            try {
-                List<CoverageData> coverageData = RouteCoverageHelper
-                        .parseDumpRouteCoverageByRouteId(project.getBasedir() + "/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(List.of(t), coverageData);
-                    totalNumberOfNodes += coverage.size();
-                    String out = templateCoverageData(fileName, routeId, coverage, notCovered, coveredNodes);
-                    getLog().info("Route coverage summary:\n\n" + out);
-                    getLog().info("");
-
-                    if (generateJacocoXmlReport && report != null) {
-                        appendSourcefileNode(document, sourceFileName, pack, coverage);
-                    }
-                }
-
-            } catch (Exception e) {
-                throw new MojoExecutionException("Error during gathering route coverage data for route: " + routeId, e);
-            }
+            totalNumberOfNodes =
+                    grabDumpData(t, routeId, totalNumberOfNodes, fileName, notCovered, coveredNodes, report, document,
+                            sourceFileName, pack);
         }
 
         if (generateJacocoXmlReport && report != null) {
@@ -271,6 +253,34 @@ public class RouteCoverageMojo extends AbstractExecMojo {
         }
     }
 
+    private int grabDumpData(
+            CamelNodeDetails t, String routeId, int totalNumberOfNodes, String fileName, AtomicInteger notCovered,
+            AtomicInteger coveredNodes, Element report, Document document, String sourceFileName, Element pack)
+            throws MojoExecutionException {
+        try {
+            List<CoverageData> coverageData = RouteCoverageHelper
+                    .parseDumpRouteCoverageByRouteId(project.getBasedir() + "/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(List.of(t), coverageData);
+                totalNumberOfNodes += coverage.size();
+                String out = templateCoverageData(fileName, routeId, coverage, notCovered, coveredNodes);
+                getLog().info("Route coverage summary:\n\n" + out);
+                getLog().info("");
+
+                if (generateJacocoXmlReport && report != null) {
+                    appendSourcefileNode(document, sourceFileName, pack, coverage);
+                }
+            }
+
+        } catch (Exception e) {
+            throw new MojoExecutionException("Error during gathering route coverage data for route: " + routeId, e);
+        }
+        return totalNumberOfNodes;
+    }
+
     private void doGenerateJacocoReport(File file, Document document) {
         try {
             getLog().info("Generating Jacoco XML report: " + file + "\n\n");
diff --git a/catalog/camel-report-maven-plugin/src/main/java/org/apache/camel/maven/ValidateMojo.java b/catalog/camel-report-maven-plugin/src/main/java/org/apache/camel/maven/ValidateMojo.java
index 4b0acf9c374..08fc1fdfb36 100644
--- a/catalog/camel-report-maven-plugin/src/main/java/org/apache/camel/maven/ValidateMojo.java
+++ b/catalog/camel-report-maven-plugin/src/main/java/org/apache/camel/maven/ValidateMojo.java
@@ -243,36 +243,13 @@ public class ValidateMojo extends AbstractExecMojo {
         List<ConfigurationPropertiesValidationResult> results = new ArrayList<>();
 
         for (File file : propertiesFiles) {
-            if (matchPropertiesFile(file)) {
-                try (InputStream is = new FileInputStream(file)) {
-                    Properties prop = new OrderedProperties();
-                    prop.load(is);
-
-                    // validate each line
-                    for (String name : prop.stringPropertyNames()) {
-                        String value = prop.getProperty(name);
-                        if (value != null) {
-                            String text = name + "=" + value;
-                            ConfigurationPropertiesValidationResult result = catalog.validateConfigurationProperty(text);
-                            // only include lines that camel can accept (as there may be non camel properties too)
-                            if (result.isAccepted()) {
-                                // try to find line number
-                                int lineNumber = findLineNumberInPropertiesFile(file, name);
-                                if (lineNumber != -1) {
-                                    result.setLineNumber(lineNumber);
-                                }
-                                results.add(result);
-                                result.setText(text);
-                                result.setFileName(file.getName());
-                            }
-                        }
-                    }
-                } catch (Exception e) {
-                    getLog().warn("Error parsing file " + file + " code due " + e.getMessage(), e);
-                }
-            }
+            parseProperties(catalog, file, results);
         }
 
+        validateResults(results);
+    }
+
+    private void validateResults(List<ConfigurationPropertiesValidationResult> results) throws MojoExecutionException {
         int configurationErrors = 0;
         int unknownComponents = 0;
         int incapableErrors = 0;
@@ -355,6 +332,37 @@ public class ValidateMojo extends AbstractExecMojo {
         }
     }
 
+    private void parseProperties(CamelCatalog catalog, File file, List<ConfigurationPropertiesValidationResult> results) {
+        if (matchPropertiesFile(file)) {
+            try (InputStream is = new FileInputStream(file)) {
+                Properties prop = new OrderedProperties();
+                prop.load(is);
+
+                // validate each line
+                for (String name : prop.stringPropertyNames()) {
+                    String value = prop.getProperty(name);
+                    if (value != null) {
+                        String text = name + "=" + value;
+                        ConfigurationPropertiesValidationResult result = catalog.validateConfigurationProperty(text);
+                        // only include lines that camel can accept (as there may be non camel properties too)
+                        if (result.isAccepted()) {
+                            // try to find line number
+                            int lineNumber = findLineNumberInPropertiesFile(file, name);
+                            if (lineNumber != -1) {
+                                result.setLineNumber(lineNumber);
+                            }
+                            results.add(result);
+                            result.setText(text);
+                            result.setFileName(file.getName());
+                        }
+                    }
+                }
+            } catch (Exception e) {
+                getLog().warn("Error parsing file " + file + " code due " + e.getMessage(), e);
+            }
+        }
+    }
+
     private int findLineNumberInPropertiesFile(File file, String name) {
         name = name.trim();
         // try to find the line number
@@ -402,6 +410,12 @@ public class ValidateMojo extends AbstractExecMojo {
         }
 
         // endpoint uris
+        validateResults(catalog, endpoints, simpleExpressions, routeIds);
+    }
+
+    private void validateResults(
+            CamelCatalog catalog, List<CamelEndpointDetails> endpoints, List<CamelSimpleExpressionDetails> simpleExpressions,
+            List<CamelRouteDetails> routeIds) throws MojoExecutionException {
         int endpointErrors = 0;
         int unknownComponents = 0;
         int incapableErrors = 0;
@@ -488,25 +502,35 @@ public class ValidateMojo extends AbstractExecMojo {
         int duplicateRouteIdErrors = validateDuplicateRouteId(routeIds);
         String routeIdSummary = "";
         if (duplicateRouteId) {
-            if (duplicateRouteIdErrors == 0) {
-                routeIdSummary = String.format("Duplicate route id validation success: (%s = ids)", routeIds.size());
-            } else {
-                routeIdSummary = String.format("Duplicate route id validation error: (%s = ids, %s = duplicates)",
-                        routeIds.size(), duplicateRouteIdErrors);
-            }
-            if (duplicateRouteIdErrors > 0) {
-                getLog().warn(routeIdSummary);
-            } else {
-                getLog().info(routeIdSummary);
-            }
+            routeIdSummary = handleDuplicateRouteId(duplicateRouteIdErrors, routeIds);
         }
 
-        if (failOnError && (endpointErrors > 0 || simpleErrors > 0 || duplicateRouteIdErrors > 0) || sedaDirectErrors > 0) {
+        if (failOnError && hasErrors(endpointErrors, simpleErrors, duplicateRouteIdErrors) || sedaDirectErrors > 0) {
             throw new MojoExecutionException(
                     endpointSummary + "\n" + simpleSummary + "\n" + routeIdSummary + "\n" + sedaDirectSummary);
         }
     }
 
+    private static boolean hasErrors(int endpointErrors, int simpleErrors, int duplicateRouteIdErrors) {
+        return endpointErrors > 0 || simpleErrors > 0 || duplicateRouteIdErrors > 0;
+    }
+
+    private String handleDuplicateRouteId(int duplicateRouteIdErrors, List<CamelRouteDetails> routeIds) {
+        String routeIdSummary;
+        if (duplicateRouteIdErrors == 0) {
+            routeIdSummary = String.format("Duplicate route id validation success: (%s = ids)", routeIds.size());
+        } else {
+            routeIdSummary = String.format("Duplicate route id validation error: (%s = ids, %s = duplicates)",
+                    routeIds.size(), duplicateRouteIdErrors);
+        }
+        if (duplicateRouteIdErrors > 0) {
+            getLog().warn(routeIdSummary);
+        } else {
+            getLog().info(routeIdSummary);
+        }
+        return routeIdSummary;
+    }
+
     private String buildSimpleSummaryMessage(List<CamelSimpleExpressionDetails> simpleExpressions, int simpleErrors) {
         String simpleSummary;
         if (simpleErrors == 0) {
diff --git a/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java b/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java
index e2889c5086c..0fad0239fcc 100644
--- a/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java
+++ b/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/RouteBuilderParser.java
@@ -329,38 +329,44 @@ public final class RouteBuilderParser {
             List<ParserResult> expressions = CamelJavaParserHelper.parseCamelLanguageExpressions(method, "csimple");
             for (ParserResult result : expressions) {
                 if (result.isParsed()) {
-                    String fileName = parseFileName(baseDir, fullyQualifiedFileName);
-
-                    CamelCSimpleExpressionDetails detail = new CamelCSimpleExpressionDetails();
-                    detail.setFileName(fileName);
-                    detail.setClassName(clazz.getQualifiedName());
-                    detail.setMethodName("configure");
-                    int line = findLineNumber(clazz.toUnformattedString(), result.getPosition());
-                    if (line > -1) {
-                        detail.setLineNumber(Integer.toString(line));
-                    }
-                    int endLine = findLineNumber(clazz.toUnformattedString(), result.getPosition() + result.getLength());
-                    if (endLine > -1) {
-                        detail.setLineNumberEnd(Integer.toString(endLine));
-                    }
-                    detail.setAbsolutePosition(result.getPosition());
-                    int linePos = findLinePosition(clazz.toUnformattedString(), result.getPosition());
-                    if (linePos > -1) {
-                        detail.setLinePosition(linePos);
-                    }
-                    detail.setCsimple(result.getElement());
-
-                    boolean predicate = result.getPredicate() != null ? result.getPredicate() : false;
-                    boolean expression = !predicate;
-                    detail.setPredicate(predicate);
-                    detail.setExpression(expression);
-
-                    csimpleExpressions.add(detail);
+                    checkParsedResult(clazz, baseDir, fullyQualifiedFileName, csimpleExpressions, result);
                 }
             }
         }
     }
 
+    private static void checkParsedResult(
+            JavaClassSource clazz, String baseDir, String fullyQualifiedFileName,
+            List<CamelCSimpleExpressionDetails> csimpleExpressions, ParserResult result) {
+        String fileName = parseFileName(baseDir, fullyQualifiedFileName);
+
+        CamelCSimpleExpressionDetails detail = new CamelCSimpleExpressionDetails();
+        detail.setFileName(fileName);
+        detail.setClassName(clazz.getQualifiedName());
+        detail.setMethodName("configure");
+        int line = findLineNumber(clazz.toUnformattedString(), result.getPosition());
+        if (line > -1) {
+            detail.setLineNumber(Integer.toString(line));
+        }
+        int endLine = findLineNumber(clazz.toUnformattedString(), result.getPosition() + result.getLength());
+        if (endLine > -1) {
+            detail.setLineNumberEnd(Integer.toString(endLine));
+        }
+        detail.setAbsolutePosition(result.getPosition());
+        int linePos = findLinePosition(clazz.toUnformattedString(), result.getPosition());
+        if (linePos > -1) {
+            detail.setLinePosition(linePos);
+        }
+        detail.setCsimple(result.getElement());
+
+        boolean predicate = result.getPredicate() != null ? result.getPredicate() : false;
+        boolean expression = !predicate;
+        detail.setPredicate(predicate);
+        detail.setExpression(expression);
+
+        csimpleExpressions.add(detail);
+    }
+
     /**
      * Parses the java source class to discover Camel routes with id's assigned.
      *