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

jbonofre pushed a commit to branch karaf-4.2.x
in repository https://gitbox.apache.org/repos/asf/karaf.git


The following commit(s) were added to refs/heads/karaf-4.2.x by this push:
     new c37146e  [KARAF-6951] Add regex support in AddToRepository goal
c37146e is described below

commit c37146eaee43ee6506a2a551f3256d7ba226d9c8
Author: jbonofre <[email protected]>
AuthorDate: Sun Dec 13 17:14:49 2020 +0100

    [KARAF-6951] Add regex support in AddToRepository goal
    
    (cherry picked from commit 378d1e1ea2016aaca2be882d6cb23d14688c01fa)
---
 .../tooling/features/AbstractFeatureMojo.java      |  17 +++-
 .../tooling/features/AddToRepositoryMojoTest.java  | 113 +++++++++++++++++++++
 2 files changed, 127 insertions(+), 3 deletions(-)

diff --git 
a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/AbstractFeatureMojo.java
 
b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/AbstractFeatureMojo.java
index 7c29fe3..f933fb0 100644
--- 
a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/AbstractFeatureMojo.java
+++ 
b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/features/AbstractFeatureMojo.java
@@ -24,6 +24,8 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.apache.felix.utils.version.VersionRange;
 import org.apache.felix.utils.version.VersionTable;
@@ -195,15 +197,23 @@ public abstract class AbstractFeatureMojo extends 
MojoSupport {
 
     private Feature getMatchingFeature(Map<String, Feature> featuresMap, 
String feature, String version) {
         Feature f = null;
+        Pattern namePattern = Pattern.compile(feature);
         if (version != null && !version.equals(Feature.DEFAULT_VERSION)) {
             // looking for a specific feature with name and version
-            f = featuresMap.get(feature + "/" + version);
+            for (String key : featuresMap.keySet()) {
+                String[] nameAndVersion = key.split("/");
+                Matcher matcher = namePattern.matcher(nameAndVersion[0]);
+                if (matcher.matches() && version.equals(nameAndVersion[1])) {
+                    f = featuresMap.get(key);
+                }
+            }
             if (f == null) {
                 //it's probably is a version range so try to use VersionRange 
Utils
                 VersionRange versionRange = new VersionRange(version);
                 for (String key : featuresMap.keySet()) {
                     String[] nameVersion = key.split("/");
-                    if (feature.equals(nameVersion[0])) {
+                    Matcher matcher = namePattern.matcher(nameVersion[0]);
+                    if (matcher.matches()) {
                         String verStr = featuresMap.get(key).getVersion();
                         Version ver = VersionTable.getVersion(verStr);
                         if (versionRange.contains(ver)) {
@@ -218,7 +228,8 @@ public abstract class AbstractFeatureMojo extends 
MojoSupport {
             // looking for the first feature name (whatever the version is)
             for (String key : featuresMap.keySet()) {
                 String[] nameVersion = key.split("/");
-                if (feature.equals(nameVersion[0])) {
+                Matcher matcher = namePattern.matcher(nameVersion[0]);
+                if (matcher.matches()) {
                     f = featuresMap.get(key);
                     break;
                 }
diff --git 
a/tooling/karaf-maven-plugin/src/test/java/org/apache/karaf/tooling/features/AddToRepositoryMojoTest.java
 
b/tooling/karaf-maven-plugin/src/test/java/org/apache/karaf/tooling/features/AddToRepositoryMojoTest.java
new file mode 100644
index 0000000..de84ce1
--- /dev/null
+++ 
b/tooling/karaf-maven-plugin/src/test/java/org/apache/karaf/tooling/features/AddToRepositoryMojoTest.java
@@ -0,0 +1,113 @@
+/*
+ * 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.karaf.tooling.features;
+
+import org.apache.karaf.features.internal.model.Feature;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.*;
+
+public class AddToRepositoryMojoTest {
+
+    @Test
+    public void test() throws Exception {
+        AddToRepositoryMojo mojo = new AddToRepositoryMojo();
+
+        List<String> featuresNames = new ArrayList<>();
+        featuresNames.add("test");
+
+        Set<Feature> features = new HashSet<>();
+
+        Feature testFeature = new Feature();
+        testFeature.setName("test");
+        testFeature.setVersion("1.0.0");
+
+        Map<String, Feature> featuresMap = new HashMap<>();
+        featuresMap.put("test/1.0.0", testFeature);
+
+        Feature otherFeature = new Feature();
+        otherFeature.setName("other");
+        otherFeature.setVersion("2.0.0");
+        featuresMap.put("other/2.0.0", otherFeature);
+
+        mojo.addFeatures(featuresNames, features, featuresMap, false);
+
+        Assert.assertEquals(1, features.size());
+        Iterator<Feature> iterator = features.iterator();
+        Feature check = iterator.next();
+        Assert.assertEquals("test", check.getName());
+        Assert.assertEquals("1.0.0", check.getVersion());
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testNotMatch() throws Exception {
+        AddToRepositoryMojo mojo = new AddToRepositoryMojo();
+
+        List<String> featuresNames = new ArrayList<>();
+        featuresNames.add("fo.*");
+
+        Set<Feature> features = new HashSet<>();
+
+        Feature testFeature = new Feature();
+        testFeature.setName("test");
+        testFeature.setVersion("1.0.0");
+
+        Map<String, Feature> featuresMap = new HashMap<>();
+        featuresMap.put("test/1.0.0", testFeature);
+
+        Feature otherFeature = new Feature();
+        otherFeature.setName("other");
+        otherFeature.setVersion("2.0.0");
+        featuresMap.put("other/2.0.0", otherFeature);
+
+        mojo.addFeatures(featuresNames, features, featuresMap, false);
+    }
+
+    @Test
+    public void testRegex() throws Exception {
+        AddToRepositoryMojo mojo = new AddToRepositoryMojo();
+
+        List<String> featuresNames = new ArrayList<>();
+        featuresNames.add("te.*");
+
+        Set<Feature> features = new HashSet<>();
+
+        Feature testFeature = new Feature();
+        testFeature.setName("test");
+        testFeature.setVersion("1.0.0");
+
+        Map<String, Feature> featuresMap = new HashMap<>();
+        featuresMap.put("test/1.0.0", testFeature);
+
+        Feature otherFeature = new Feature();
+        otherFeature.setName("other");
+        otherFeature.setVersion("2.0.0");
+        featuresMap.put("other/2.0.0", otherFeature);
+
+        mojo.addFeatures(featuresNames, features, featuresMap, false);
+
+        Assert.assertEquals(1, features.size());
+        Iterator<Feature> iterator = features.iterator();
+        Feature check = iterator.next();
+        Assert.assertEquals("test", check.getName());
+        Assert.assertEquals("1.0.0", check.getVersion());
+    }
+
+}

Reply via email to