This is an automated email from the ASF dual-hosted git repository.
cziegeler pushed a commit to branch master
in repository
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature.git
The following commit(s) were added to refs/heads/master by this push:
new f201105 SLING-9137 : Add method to ArtifactId to create an id based
on the mvn path
f201105 is described below
commit f20110525ccdf46219c339e9e6301d7ec9a9a1fd
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Mon Feb 24 14:27:38 2020 +0100
SLING-9137 : Add method to ArtifactId to create an id based on the mvn path
---
.../java/org/apache/sling/feature/ArtifactId.java | 37 ++++++++++++++++++++++
.../org/apache/sling/feature/ArtifactIdTest.java | 30 ++++++++++++++++++
2 files changed, 67 insertions(+)
diff --git a/src/main/java/org/apache/sling/feature/ArtifactId.java
b/src/main/java/org/apache/sling/feature/ArtifactId.java
index 8b8f87d..1408838 100644
--- a/src/main/java/org/apache/sling/feature/ArtifactId.java
+++ b/src/main/java/org/apache/sling/feature/ArtifactId.java
@@ -180,7 +180,44 @@ public class ArtifactId implements Comparable<ArtifactId> {
}
/**
+ * Create a new artifact id from a maven path The schema is
+ * {@code
groupIdPath/artifactId/version/artifactId-version[-classifier].type}
+ *
+ * @param path The maven path
+ * @return A new artifact id
+ * @throws IllegalArgumentException If the path is not valid
+ * @since 1.3.0
+ */
+ public static ArtifactId fromMvnPath(final String path) {
+ final String[] parts = path.startsWith("/") ?
path.substring(1).split("/") : path.split("/");
+ if (parts.length < 4) {
+ throw new IllegalArgumentException("Invalid mvn path: " + path);
+ }
+ final String gId = String.join(".", Arrays.copyOfRange(parts, 0,
parts.length - 3));
+ final String aId = parts[parts.length - 3];
+ final String version = parts[parts.length - 2];
+ final String prefix = aId.concat("-").concat(version);
+ if (!parts[parts.length - 1].startsWith(prefix)) {
+ throw new IllegalArgumentException("Invalid mvn path: " + path);
+ }
+ final int pos = parts[parts.length - 1].lastIndexOf(".");
+ final String type = parts[parts.length - 1].substring(pos + 1);
+ final String classifier;
+ if (pos > prefix.length()) {
+ if (parts[parts.length - 1].charAt(prefix.length()) != '-') {
+ throw new IllegalArgumentException("Invalid mvn path: " +
path);
+ }
+ classifier = parts[parts.length - 1].substring(prefix.length() +
1, pos);
+ } else {
+ classifier = null;
+ }
+
+ return new ArtifactId(gId, aId, version, classifier, type);
+ }
+
+ /**
* Return a mvn url
+ *
* @return A mvn url
* @see #fromMvnUrl(String)
*/
diff --git a/src/test/java/org/apache/sling/feature/ArtifactIdTest.java
b/src/test/java/org/apache/sling/feature/ArtifactIdTest.java
index 7e6cd2d..cd6b528 100644
--- a/src/test/java/org/apache/sling/feature/ArtifactIdTest.java
+++ b/src/test/java/org/apache/sling/feature/ArtifactIdTest.java
@@ -21,6 +21,9 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import java.util.ArrayList;
+import java.util.List;
+
import org.junit.Test;
import org.osgi.framework.Version;
@@ -236,4 +239,31 @@ public class ArtifactIdTest {
assertEquals("group/a/artifact/1.0/artifact-1.0-foo.json",
a2.toMvnPath());
assertEquals("group/a/artifact/1.0/artifact-1.0-foo.jar",
a3.toMvnPath());
}
+
+ @Test
+ public void testFromMvnPath() {
+ final List<ArtifactId> ids = new ArrayList<>();
+ ids.add(new ArtifactId("group", "artifact", "1.0", null, "zip"));
+ ids.add(new ArtifactId("group", "artifact", "1.0", "foo", "zip"));
+ ids.add(new ArtifactId("group", "artifact", "1.0", "a-classifier",
"zip"));
+ ids.add(new ArtifactId("group", "artifact", "1.alhpa", null, "zip"));
+ ids.add(new ArtifactId("group", "my-artifact", "1.0", null, "zip"));
+ ids.add(new ArtifactId("group", "my-artifact", "1.0", "a-classifier",
"zip"));
+ ids.add(new ArtifactId("group", "my-artifact", "1.alhpa", null,
"zip"));
+
+ ids.add(new ArtifactId("c.a.group", "artifact", "1.0", null, "zip"));
+ ids.add(new ArtifactId("c.a.group", "artifact", "1.0", "foo", "zip"));
+ ids.add(new ArtifactId("c.a.group", "artifact", "1.0", "a-classifier",
"zip"));
+ ids.add(new ArtifactId("c.a.group", "artifact", "1.alhpa", null,
"zip"));
+ ids.add(new ArtifactId("c.a.group", "my-artifact", "1.0", null,
"zip"));
+ ids.add(new ArtifactId("c.a.group", "my-artifact", "1.0",
"a-classifier", "zip"));
+ ids.add(new ArtifactId("c.a.group", "my-artifact", "1.alhpa", null,
"zip"));
+
+ for (final ArtifactId id : ids) {
+ final String path = id.toMvnPath();
+ final ArtifactId newId = ArtifactId.fromMvnPath(path);
+
+ assertEquals(id, newId);
+ }
+ }
}