You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by kw...@apache.org on 2024/09/20 12:04:15 UTC

(jackrabbit-filevault) branch master updated: JCRVLT-774 improve javadoc on AbstractDependencyResolver

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

kwin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jackrabbit-filevault.git


The following commit(s) were added to refs/heads/master by this push:
     new 9f4f6275 JCRVLT-774 improve javadoc on AbstractDependencyResolver
9f4f6275 is described below

commit 9f4f627596f276d3112899d09aab138fcfba7372
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Fri Sep 20 14:04:02 2024 +0200

    JCRVLT-774 improve javadoc on AbstractDependencyResolver
    
    Increase test coverage
---
 src/site/markdown/properties.md                    |  2 +-
 .../context/AbstractDependencyResolver.java        | 50 ++++++++++++++++------
 .../context/AbstractDependencyResolverTest.java    |  4 +-
 3 files changed, 40 insertions(+), 16 deletions(-)

diff --git a/src/site/markdown/properties.md b/src/site/markdown/properties.md
index e9330ce8..af9f6baf 100644
--- a/src/site/markdown/properties.md
+++ b/src/site/markdown/properties.md
@@ -66,7 +66,7 @@ Example:
 | lastModifiedBy | A user name indicating who last modified this package | no | empty
 | buildCount | An integer indicating how often this package has been built | no | empty
 | dependencies | Comma-separated list of dependencies. Each dependency has the format `<group>:<name>:<version or versionrange>`. See [Dependencies][api.Dependency]. | no | empty
-| dependencies-locations | Optional comma-separated list of dependencies' locations. Each item has the format `<package-id>=<uri>`. Currently [FileVault Package Maven Plugin](https://jackrabbit.apache.org/filevault-package-maven-plugin/index.html) is using a URI scheme for Maven coordinates like `maven:<groupId>:<artifactId>:<version>[[:<classifier>]:packaging]`. | no | empty
+| dependencies-locations | Optional comma-separated list of dependencies' locations. Each item has the format `<package-id>=<uri>`. Currently [FileVault Package Maven Plugin](https://jackrabbit.apache.org/filevault-package-maven-plugin/index.html) is using a URI scheme for Maven coordinates like `maven:<groupId>:<artifactId>:<version>[:packaging[:<classifier>]]`. | no | empty
 | created | A date string in the format `±YYYY-MM-DDThh:mm:ss.SSSTZD` specifying when the package has been created initially (see also [ISO8601][api.ISO8601]) | no | empty
 | createdBy | A user name indicating who initially created this package | no | empty
 | generator | Information about the generator of the package | no | for packages created by filevault: `org.apache.jackrabbit.vault:version`
diff --git a/vault-validation/src/main/java/org/apache/jackrabbit/vault/validation/context/AbstractDependencyResolver.java b/vault-validation/src/main/java/org/apache/jackrabbit/vault/validation/context/AbstractDependencyResolver.java
index 9bbe693d..13da5c46 100644
--- a/vault-validation/src/main/java/org/apache/jackrabbit/vault/validation/context/AbstractDependencyResolver.java
+++ b/vault-validation/src/main/java/org/apache/jackrabbit/vault/validation/context/AbstractDependencyResolver.java
@@ -35,10 +35,11 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Abstract resolver supporting Maven repository dependency location URIs (starting with {@code maven:}).
+ * Abstract package dependency resolver supporting Maven repository dependency location URIs (starting with {@code maven:}).
  * All package dependencies are mapped to Maven coordinates by this class and then resolved via {@link #resolvePackageInfo(MavenCoordinates)}.
- * It comes with a cache so that the same package dependency is not resolved more than once.
+ * The resolver comes with a cache so that the same package dependency is not resolved more than once.
  * This class is not thread-safe.
+ * @see PackageInfo
  */
 public abstract class AbstractDependencyResolver implements DependencyResolver {
 
@@ -50,6 +51,10 @@ public abstract class AbstractDependencyResolver implements DependencyResolver {
     public static final String MAVEN_REPOSITORY_SCHEME = "maven";
     private final Collection<PackageInfo> packageInfoCache;
 
+    protected AbstractDependencyResolver() {
+        this.packageInfoCache = new LinkedList<>();
+    }
+
     protected AbstractDependencyResolver(@NotNull Collection<PackageInfo> packageInfoCache) {
         this.packageInfoCache = new LinkedList<>(packageInfoCache);
     }
@@ -90,8 +95,16 @@ public abstract class AbstractDependencyResolver implements DependencyResolver {
     }
 
     /**
-     * Use some heuristics to map the package dependency to Maven coordinates and try to resolve them then via {@link #resolvePackageInfo(MavenCoordinates)}.
-     * @param dependency
+     * Uses some heuristics to map the package dependency to Maven coordinates.
+     * It tries to resolve the package version ranges in the following order:
+     * <ol>
+     * <li>trying lower bound version</li>
+     * <li>trying higher bound version</li>
+     * <li>trying LATEST version</li>
+     * </ol>
+     * Each one is triggering {@link #resolvePackageInfo(MavenCoordinates)}.
+     * It doesn't leverage any Maven API for version range resolution, therefore this method should be overwritten if there are better ways to resolve version ranges.
+     * @param dependency the dependency from which to retrieve the package info
      * @return the resolved package info or {@code null}
      * @throws IOException
      */
@@ -117,22 +130,29 @@ public abstract class AbstractDependencyResolver implements DependencyResolver {
         return info;
     }
 
-    public abstract @Nullable PackageInfo resolvePackageInfo(MavenCoordinates mavenCoordinates) throws IOException;
+    /**
+     * Resolves the main meta information of a package dependency given via Maven coordinates.
+     * @param mavenCoordinates the coordinates of the package dependency
+     * @return the resolved package info or {@code null}
+     * @throws IOException
+     */
+    public abstract @Nullable PackageInfo resolvePackageInfo(@NotNull MavenCoordinates mavenCoordinates) throws IOException;
 
+    /** Encapsulates Maven coordinates groupId, artifactId, version, packaging (default {@code zip}), classifier (optional) */
     public static final class MavenCoordinates {
         @NotNull private final String groupId;
         @NotNull private final String artifactId;
-        @Nullable private final String version;
+        @NotNull private final String version;
         @NotNull private final String packaging;
         @Nullable private final String classifier;
 
         private static final String DEFAULT_PACKAGING = "zip";
 
-        public MavenCoordinates(@NotNull String groupId, @NotNull String artifactId, String version) {
+        public MavenCoordinates(@NotNull String groupId, @NotNull String artifactId, @NotNull String version) {
             this(groupId, artifactId, version, DEFAULT_PACKAGING, null);
         }
 
-        public MavenCoordinates(@NotNull String groupId, @NotNull String artifactId, String version,@NotNull String packaging, String classifier) {
+        public MavenCoordinates(@NotNull String groupId, @NotNull String artifactId, @NotNull String version, @NotNull String packaging, String classifier) {
             super();
             this.groupId = groupId;
             this.artifactId = artifactId;
@@ -141,6 +161,10 @@ public abstract class AbstractDependencyResolver implements DependencyResolver {
             this.classifier = classifier;
         }
 
+        /** Parses a Maven URI of the format {@code maven:<groupId>:<artifactId>:<version>[:<packaging>[:<classifier]]} and returns the extracted Maven coordinates.
+         * @param uri
+         * @return the Maven coordinates or {@code null} if the URI is not a Maven URI
+         */
         public static @Nullable MavenCoordinates parse(URI uri) {
             if (!MAVEN_REPOSITORY_SCHEME.equals(uri.getScheme())) {
                 return null;
@@ -151,15 +175,13 @@ public abstract class AbstractDependencyResolver implements DependencyResolver {
             // support groupId, artifactId, packaging and classifier (format like https://maven.apache.org/plugins/maven-dependency-plugin/get-mojo.html#artifact)
             // extract group id and artifact id
             String[] parts = uri.getSchemeSpecificPart().split(":");
-            if (parts.length < 2) {
-                throw new IllegalArgumentException("At least group id and artifact id need to be given separated by ':'");
+            if (parts.length < 3) {
+                throw new IllegalArgumentException("At least group id, artifact id and version need to be given separated by ':'");
             }
             String groupId = parts[0];
             String artifactId = parts[1];
-            String version = null;
-            if (parts.length > 2) {
-                version = parts[2];
-            }
+            String version = parts[2];
+            
             String packaging = DEFAULT_PACKAGING;
             if (parts.length > 3) {
                 packaging = parts[3];
diff --git a/vault-validation/src/test/java/org/apache/jackrabbit/vault/validation/context/AbstractDependencyResolverTest.java b/vault-validation/src/test/java/org/apache/jackrabbit/vault/validation/context/AbstractDependencyResolverTest.java
index ba39742a..cd15abdb 100644
--- a/vault-validation/src/test/java/org/apache/jackrabbit/vault/validation/context/AbstractDependencyResolverTest.java
+++ b/vault-validation/src/test/java/org/apache/jackrabbit/vault/validation/context/AbstractDependencyResolverTest.java
@@ -27,8 +27,10 @@ public class AbstractDependencyResolverTest {
 
     @Test
     public void testUriToMavenCoordinates() throws URISyntaxException {
-        Assert.assertEquals(new MavenCoordinates("group1", "name1", null), MavenCoordinates.parse(new URI("maven", "group1:name1", null)));
+        Assert.assertEquals(new MavenCoordinates("group1", "name1", "1.0.0"), MavenCoordinates.parse(new URI("maven", "group1:name1:1.0.0", null)));
         Assert.assertEquals(new MavenCoordinates("group1", "name1", "1.0.0", "test", "myclassifier"), MavenCoordinates.parse(new URI("maven", "group1:name1:1.0.0:test:myclassifier", null)));
+        Assert.assertThrows(IllegalArgumentException.class, () -> MavenCoordinates.parse(new URI("maven", "group:name", null)));
+        Assert.assertNull(MavenCoordinates.parse(new URI("http://example.com")));
     }
 
 }