You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2022/03/23 10:39:40 UTC
[camel] branch main updated: (chores) camel-report-maven-plugin: consolidate route/validation mojo code
This is an automated email from the ASF dual-hosted git repository.
orpiske 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 7679e5b (chores) camel-report-maven-plugin: consolidate route/validation mojo code
7679e5b is described below
commit 7679e5bc100a356c1809890bb6d80a81ab96cd8c
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Wed Mar 23 09:24:08 2022 +0100
(chores) camel-report-maven-plugin: consolidate route/validation mojo code
---
.../org/apache/camel/maven/ReportPluginCommon.java | 164 +++++++++++++++++++++
.../org/apache/camel/maven/RouteCoverageMojo.java | 153 ++-----------------
.../java/org/apache/camel/maven/ValidateMojo.java | 152 +++----------------
3 files changed, 194 insertions(+), 275 deletions(-)
diff --git a/catalog/camel-report-maven-plugin/src/main/java/org/apache/camel/maven/ReportPluginCommon.java b/catalog/camel-report-maven-plugin/src/main/java/org/apache/camel/maven/ReportPluginCommon.java
new file mode 100644
index 0000000..98746a1
--- /dev/null
+++ b/catalog/camel-report-maven-plugin/src/main/java/org/apache/camel/maven/ReportPluginCommon.java
@@ -0,0 +1,164 @@
+/*
+ * 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.Set;
+
+import org.apache.camel.support.PatternHelper;
+import org.apache.maven.model.Resource;
+import org.apache.maven.project.MavenProject;
+
+final class ReportPluginCommon {
+
+ private ReportPluginCommon() {
+
+ }
+
+ public static String asRelativeFile(String name, MavenProject project) {
+ 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;
+ }
+
+ public static String stripRootPath(String name, MavenProject project) {
+ // strip out any leading source / resource directory
+
+ for (String dir : project.getCompileSourceRoots()) {
+ dir = asRelativeFile(dir, project);
+ if (name.startsWith(dir)) {
+ return name.substring(dir.length() + 1);
+ }
+ }
+ for (String dir : project.getTestCompileSourceRoots()) {
+ dir = asRelativeFile(dir, project);
+ if (name.startsWith(dir)) {
+ return name.substring(dir.length() + 1);
+ }
+ }
+ for (Resource resource : project.getResources()) {
+ String dir = asRelativeFile(resource.getDirectory(), project);
+ if (name.startsWith(dir)) {
+ return name.substring(dir.length() + 1);
+ }
+ }
+ for (Resource resource : project.getTestResources()) {
+ String dir = asRelativeFile(resource.getDirectory(), project);
+ if (name.startsWith(dir)) {
+ return name.substring(dir.length() + 1);
+ }
+ }
+
+ return name;
+ }
+
+ public static boolean fileListMatchesPattern(String fileList, File file, MavenProject project) {
+ for (String exclude : fileList.split(",")) {
+ exclude = exclude.trim();
+ // try both with and without directory in the name
+ String fqn = stripRootPath(asRelativeFile(file.getAbsolutePath(), project), project);
+ boolean match = PatternHelper.matchPattern(fqn, exclude) || PatternHelper.matchPattern(file.getName(), exclude);
+ if (match) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public static 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);
+ }
+ }
+ }
+ }
+
+ public static 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);
+ }
+ }
+ }
+ }
+
+ public static boolean matchRouteFile(File file, String excludes, String includes, MavenProject project) {
+ if (excludes == null && includes == null) {
+ return true;
+ }
+
+ // exclude take precedence
+ if (excludes != null) {
+ if (fileListMatchesPattern(excludes, file, project)) {
+ return false;
+ }
+ }
+
+ // include
+ if (includes != null) {
+ return fileListMatchesPattern(includes, file, project);
+ }
+
+ // was not excluded nor failed include so its accepted
+ return true;
+ }
+
+ public static void findJavaRouteBuilderClasses(
+ Set<File> javaFiles, boolean includeJava, boolean includeTest, MavenProject project) {
+ if (includeJava) {
+ for (String dir : project.getCompileSourceRoots()) {
+ findJavaFiles(new File(dir), javaFiles);
+ }
+ if (includeTest) {
+ for (String dir : project.getTestCompileSourceRoots()) {
+ findJavaFiles(new File(dir), javaFiles);
+ }
+ }
+ }
+ }
+
+ public static void findXmlRouters(Set<File> xmlFiles, boolean includeXml, boolean includeTest, MavenProject project) {
+ if (includeXml) {
+ for (Resource dir : project.getResources()) {
+ findXmlFiles(new File(dir.getDirectory()), xmlFiles);
+ }
+ if (includeTest) {
+ for (Resource dir : project.getTestResources()) {
+ findXmlFiles(new File(dir.getDirectory()), xmlFiles);
+ }
+ }
+ }
+ }
+}
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 04738f6..f8ddce8 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
@@ -54,9 +54,7 @@ import org.apache.camel.parser.XmlRouteParser;
import org.apache.camel.parser.helper.RouteCoverageHelper;
import org.apache.camel.parser.model.CamelNodeDetails;
import org.apache.camel.parser.model.CoverageData;
-import org.apache.camel.support.PatternHelper;
import org.apache.camel.util.FileUtil;
-import org.apache.maven.model.Resource;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
@@ -66,6 +64,12 @@ import org.jboss.forge.roaster.Roaster;
import org.jboss.forge.roaster.model.JavaType;
import org.jboss.forge.roaster.model.source.JavaClassSource;
+import static org.apache.camel.maven.ReportPluginCommon.asRelativeFile;
+import static org.apache.camel.maven.ReportPluginCommon.findJavaRouteBuilderClasses;
+import static org.apache.camel.maven.ReportPluginCommon.findXmlRouters;
+import static org.apache.camel.maven.ReportPluginCommon.matchRouteFile;
+import static org.apache.camel.maven.ReportPluginCommon.stripRootPath;
+
/**
* Performs route coverage reports after running Camel unit tests with camel-test modules
*/
@@ -147,30 +151,9 @@ public class RouteCoverageMojo extends AbstractExecMojo {
Set<File> xmlFiles = new LinkedHashSet<>();
// find all java route builder classes
- List list = project.getCompileSourceRoots();
- for (Object obj : list) {
- String dir = (String) obj;
- findJavaFiles(new File(dir), javaFiles);
- }
+ findJavaRouteBuilderClasses(javaFiles, true, includeTest, project);
// 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);
- }
- }
+ findXmlRouters(xmlFiles, true, includeTest, project);
List<CamelNodeDetails> routeTrees = new ArrayList<>();
@@ -248,7 +231,7 @@ public class RouteCoverageMojo extends AbstractExecMojo {
// favor strict matching on route ids
for (CamelNodeDetails t : routeIdTrees) {
String routeId = t.getRouteId();
- String fileName = stripRootPath(asRelativeFile(t.getFileName()));
+ String fileName = stripRootPath(asRelativeFile(t.getFileName(), project), project);
String sourceFileName = new File(fileName).getName();
String packageName = new File(fileName).getParent();
Element pack = null;
@@ -320,7 +303,7 @@ public class RouteCoverageMojo extends AbstractExecMojo {
if (!coverage.isEmpty()) {
totalNumberOfNodes += coverage.size();
- String fileName = stripRootPath(asRelativeFile(t.getValue().get(0).getFileName()));
+ String fileName = stripRootPath(asRelativeFile(t.getValue().get(0).getFileName(), project), project);
String out = templateCoverageData(fileName, null, coverage, notCovered, coveredNodes);
getLog().info("Route coverage summary:\n\n" + out);
getLog().info("");
@@ -349,7 +332,7 @@ public class RouteCoverageMojo extends AbstractExecMojo {
Map<String, List<CamelNodeDetails>> answer = new LinkedHashMap<>();
for (CamelNodeDetails t : anonymousRouteTrees) {
- String fileName = asRelativeFile(t.getFileName());
+ String fileName = asRelativeFile(t.getFileName(), project);
String className = FileUtil.stripExt(FileUtil.stripPath(fileName));
List<CamelNodeDetails> list = answer.computeIfAbsent(className, k -> new ArrayList<>());
list.add(t);
@@ -527,120 +510,8 @@ public class RouteCoverageMojo extends AbstractExecMojo {
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 = PatternHelper.matchPattern(fqn, exclude) || PatternHelper.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 = PatternHelper.matchPattern(fqn, include) || PatternHelper.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;
+ return matchRouteFile(file, excludes, includes, project);
}
private void appendSourcefileNode(
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 d203cc9..ab959c6 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
@@ -55,6 +55,13 @@ import org.jboss.forge.roaster.Roaster;
import org.jboss.forge.roaster.model.JavaType;
import org.jboss.forge.roaster.model.source.JavaClassSource;
+import static org.apache.camel.maven.ReportPluginCommon.asRelativeFile;
+import static org.apache.camel.maven.ReportPluginCommon.findJavaFiles;
+import static org.apache.camel.maven.ReportPluginCommon.findJavaRouteBuilderClasses;
+import static org.apache.camel.maven.ReportPluginCommon.findXmlRouters;
+import static org.apache.camel.maven.ReportPluginCommon.matchRouteFile;
+import static org.apache.camel.maven.ReportPluginCommon.stripRootPath;
+
/**
* Parses the source code and validates the Camel routes has valid endpoint uris and simple expressions, and validates
* configuration files such as application.properties.
@@ -371,17 +378,17 @@ public class ValidateMojo extends AbstractExecMojo {
Set<File> xmlFiles = new LinkedHashSet<>();
// find all java route builder classes
- findJavaRouteBuilderClasses(javaFiles);
+ findJavaRouteBuilderClasses(javaFiles, includeJava, includeTest, project);
// find all xml routes
- findXmlRouters(xmlFiles);
+ findXmlRouters(xmlFiles, includeXml, includeTest, project);
for (File file : javaFiles) {
- if (matchRouteFile(file)) {
+ if (matchFile(file)) {
parseJavaRouteFile(endpoints, simpleExpressions, routeIds, file);
}
}
for (File file : xmlFiles) {
- if (matchRouteFile(file)) {
+ if (matchFile(file)) {
parseXmlRouteFile(endpoints, simpleExpressions, routeIds, file);
}
}
@@ -530,7 +537,7 @@ public class ValidateMojo extends AbstractExecMojo {
sb.append(detail.getLineNumber()).append(")");
} else if (detail.getLineNumber() != null) {
// this is from xml
- String fqn = stripRootPath(asRelativeFile(detail.getFileName()));
+ String fqn = stripRootPath(asRelativeFile(detail.getFileName(), project), project);
if (fqn.endsWith(".xml")) {
fqn = fqn.substring(0, fqn.length() - 4);
fqn = asPackageName(fqn);
@@ -619,31 +626,6 @@ public class ValidateMojo extends AbstractExecMojo {
}
}
- private void findXmlRouters(Set<File> xmlFiles) {
- if (includeXml) {
- for (Resource dir : project.getResources()) {
- findXmlFiles(new File(dir.getDirectory()), xmlFiles);
- }
- if (includeTest) {
- for (Resource dir : project.getTestResources()) {
- findXmlFiles(new File(dir.getDirectory()), xmlFiles);
- }
- }
- }
- }
-
- private void findJavaRouteBuilderClasses(Set<File> javaFiles) {
- if (includeJava) {
- for (String dir : project.getCompileSourceRoots()) {
- findJavaFiles(new File(dir), javaFiles);
- }
- if (includeTest) {
- for (String dir : project.getTestCompileSourceRoots()) {
- findJavaFiles(new File(dir), javaFiles);
- }
- }
- }
- }
private int countEndpointPairs(List<CamelEndpointDetails> endpoints, String scheme) {
int pairs = 0;
@@ -703,7 +685,7 @@ public class ValidateMojo extends AbstractExecMojo {
sb.append(lineNumber).append(")");
} else if (lineNumber != null) {
// this is from xml
- String fqn = stripRootPath(asRelativeFile(fileName));
+ String fqn = stripRootPath(asRelativeFile(fileName, project), project);
if (fqn.endsWith(".xml")) {
fqn = fqn.substring(0, fqn.length() - 4);
fqn = asPackageName(fqn);
@@ -733,7 +715,7 @@ public class ValidateMojo extends AbstractExecMojo {
sb.append(detail.getLineNumber()).append(")");
} else if (detail.getLineNumber() != null) {
// this is from xml
- String fqn = stripRootPath(asRelativeFile(detail.getFileName()));
+ String fqn = stripRootPath(asRelativeFile(detail.getFileName(), project), project);
if (fqn.endsWith(".xml")) {
fqn = fqn.substring(0, fqn.length() - 4);
fqn = asPackageName(fqn);
@@ -794,7 +776,7 @@ public class ValidateMojo extends AbstractExecMojo {
sb.append(detail.getLineNumber()).append(")");
} else if (detail.getLineNumber() != null) {
// this is from xml
- String fqn = stripRootPath(asRelativeFile(detail.getFileName()));
+ String fqn = stripRootPath(asRelativeFile(detail.getFileName(), project), project);
if (fqn.endsWith(".xml")) {
fqn = fqn.substring(0, fqn.length() - 4);
fqn = asPackageName(fqn);
@@ -897,36 +879,10 @@ public class ValidateMojo extends AbstractExecMojo {
}
}
- 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 matchPropertiesFile(File file) {
for (String part : configurationFiles.split(",")) {
part = part.trim();
- String fqn = stripRootPath(asRelativeFile(file.getAbsolutePath()));
+ String fqn = stripRootPath(asRelativeFile(file.getAbsolutePath(), project), project);
boolean match = PatternHelper.matchPattern(fqn, part);
if (match) {
return true;
@@ -935,80 +891,8 @@ public class ValidateMojo extends AbstractExecMojo {
return false;
}
- private boolean matchRouteFile(File file) {
- if (excludes == null && includes == null) {
- return true;
- }
-
- // exclude take precedence
- if (excludes != null) {
- if (fileListMatchesPattern(excludes, file)) {
- return false;
- }
- }
-
- // include
- if (includes != null) {
- return fileListMatchesPattern(includes, file);
- }
-
- // was not excluded nor failed include so its accepted
- return true;
- }
-
- private boolean fileListMatchesPattern(String fileList, File file) {
- for (String exclude : fileList.split(",")) {
- exclude = exclude.trim();
- // try both with and without directory in the name
- String fqn = stripRootPath(asRelativeFile(file.getAbsolutePath()));
- return PatternHelper.matchPattern(fqn, exclude) || PatternHelper.matchPattern(file.getName(), exclude);
- }
- return false;
- }
-
- 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
-
- for (String dir : project.getCompileSourceRoots()) {
- dir = asRelativeFile(dir);
- if (name.startsWith(dir)) {
- return name.substring(dir.length() + 1);
- }
- }
- for (String dir : project.getTestCompileSourceRoots()) {
- dir = asRelativeFile(dir);
- if (name.startsWith(dir)) {
- return name.substring(dir.length() + 1);
- }
- }
- for (Resource resource : project.getResources()) {
- String dir = asRelativeFile(resource.getDirectory());
- if (name.startsWith(dir)) {
- return name.substring(dir.length() + 1);
- }
- }
- for (Resource resource : project.getTestResources()) {
- String dir = asRelativeFile(resource.getDirectory());
- if (name.startsWith(dir)) {
- return name.substring(dir.length() + 1);
- }
- }
-
- return name;
+ private boolean matchFile(File file) {
+ return matchRouteFile(file, excludes, includes, project);
}
private static String asPackageName(String name) {