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/sling-org-apache-sling-installer-hc.git


The following commit(s) were added to refs/heads/master by this push:
     new 4c3d75e  SLING-7618 allow multiple whitelisted entries with the same 
id (but different versions)
4c3d75e is described below

commit 4c3d75ed0829e2204b9e69ad8e656931e2a65815
Author: Konrad Windszus <[email protected]>
AuthorDate: Mon Oct 15 21:00:58 2018 +0200

    SLING-7618 allow multiple whitelisted entries with the same id (but
    different versions)
---
 pom.xml                                            | 12 ++++++
 .../installer/hc/OsgiInstallerHealthCheck.java     | 48 +++++++++++++++++-----
 .../installer/hc/OsgiInstallerHealthCheckTest.java | 29 +++++++++++++
 3 files changed, 79 insertions(+), 10 deletions(-)

diff --git a/pom.xml b/pom.xml
index 5dc889f..1902e7c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -111,5 +111,17 @@
             <version>1.0.4</version>
             <scope>provided</scope>
         </dependency>
+        <!-- test dependencies -->
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+                   <groupId>org.hamcrest</groupId>
+            <artifactId>hamcrest-junit</artifactId>
+            <version>2.0.0.0</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
diff --git 
a/src/main/java/org/apache/sling/installer/hc/OsgiInstallerHealthCheck.java 
b/src/main/java/org/apache/sling/installer/hc/OsgiInstallerHealthCheck.java
index c6f34c8..4210358 100644
--- a/src/main/java/org/apache/sling/installer/hc/OsgiInstallerHealthCheck.java
+++ b/src/main/java/org/apache/sling/installer/hc/OsgiInstallerHealthCheck.java
@@ -17,7 +17,10 @@
  */
 package org.apache.sling.installer.hc;
 
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import org.apache.commons.lang3.StringUtils;
@@ -52,7 +55,7 @@ public class OsgiInstallerHealthCheck implements HealthCheck {
     private static final Logger LOG = 
LoggerFactory.getLogger(OsgiInstallerHealthCheck.class);
 
     private Configuration configuration;
-    private Map<String, Version> skipEntityIdsWithVersions;
+    private Map<String, List<Version>> skipEntityIdsWithVersions;
     
     private final static String DOCUMENTATION_URL = 
"https://sling.apache.org/documentation/bundles/osgi-installer.html#health-check";;
 
@@ -84,11 +87,16 @@ public class OsgiInstallerHealthCheck implements 
HealthCheck {
     @Activate
     protected void activate(Configuration configuration) {
         this.configuration = configuration;
-        skipEntityIdsWithVersions = 
parseEntityIdsWithVersions(configuration.skipEntityIds());
+        try {
+            skipEntityIdsWithVersions = 
parseEntityIdsWithVersions(configuration.skipEntityIds());
+        } catch (IllegalArgumentException e) {
+            throw new IllegalStateException("Invalid configuration in 
'skipEntityIds': " + e.getLocalizedMessage(), e);
+        }
+        
     }
 
-    private Map<String, Version> parseEntityIdsWithVersions(String[] 
entityIdsAndVersions) throws IllegalArgumentException {
-        Map<String, Version> entityIdsWithVersions = new HashMap<>();
+    static Map<String, List<Version>> parseEntityIdsWithVersions(String[] 
entityIdsAndVersions) throws IllegalArgumentException {
+        Map<String, List<Version>> entityIdsWithVersions = new HashMap<>();
         if (entityIdsAndVersions != null) {
             for (String entityIdAndVersion : entityIdsAndVersions) {
                 String[] parts = entityIdAndVersion.split(" ", 2);
@@ -99,7 +107,25 @@ public class OsgiInstallerHealthCheck implements 
HealthCheck {
                 } else {
                     version = null;
                 }
-                entityIdsWithVersions.put(entityId, version);
+                // does an entry with the same id already exist?
+                if (entityIdsWithVersions.containsKey(entityId)) {
+                    
+                    List<Version> versions = 
entityIdsWithVersions.get(entityId);
+                    // previous entry contained no version?
+                    if (versions == null) {
+                        throw new IllegalArgumentException("One entry with 
'id' " + entityId + " contained no version limitation and there was another 
entry with the same id. This is an invalid combination. Please only list the 
same id more than once if different versions are given as well.");
+                    }
+                    versions.add(version);
+                } else {
+                    final List<Version> versions;
+                    if (version == null) {
+                        versions = null;
+                    } else {
+                        versions = new 
ArrayList<>(Collections.singletonList(version));
+                    }
+                    entityIdsWithVersions.put(entityId, versions);
+                }
+                
             }
         }
         return entityIdsWithVersions;
@@ -201,11 +227,13 @@ public class OsgiInstallerHealthCheck implements 
HealthCheck {
 
     private void reportInvalidResource(Resource invalidResource, String 
resourceType, FormattingResultLog hcLog) {
         if 
(skipEntityIdsWithVersions.containsKey(invalidResource.getEntityId())) {
-            Version version = 
skipEntityIdsWithVersions.get(invalidResource.getEntityId());
-            if (version != null) {
-                if (version.equals(invalidResource.getVersion())) {
-                    LOG.debug("Skipping not installed resource '{}' as its 
entity id and version is in the skip list", invalidResource);
-                    return;
+            List<Version> versions = 
skipEntityIdsWithVersions.get(invalidResource.getEntityId());
+            if (versions != null) {
+                for (Version version : versions) {
+                    if (version.equals(invalidResource.getVersion())) {
+                        LOG.debug("Skipping not installed resource '{}' as its 
entity id and version is in the skip list", invalidResource);
+                        return;
+                    }
                 }
             } else {
                 LOG.debug("Skipping not installed resource '{}' as its entity 
id is in the skip list", invalidResource);
diff --git 
a/src/test/java/org/apache/sling/installer/hc/OsgiInstallerHealthCheckTest.java 
b/src/test/java/org/apache/sling/installer/hc/OsgiInstallerHealthCheckTest.java
new file mode 100644
index 0000000..b561851
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/installer/hc/OsgiInstallerHealthCheckTest.java
@@ -0,0 +1,29 @@
+package org.apache.sling.installer.hc;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+import org.hamcrest.Matchers;
+import org.junit.Assert;
+import org.junit.Test;
+import org.osgi.framework.Version;
+
+public class OsgiInstallerHealthCheckTest {
+
+    @Test
+    public void testParseEntityIdsWithVersions() {
+        String[] entityIdsAndVersions = new String[] { "idA 1.0.0", "idA 
2.0.0", "idB" };
+        Map<String, List<Version>> map = 
OsgiInstallerHealthCheck.parseEntityIdsWithVersions(entityIdsAndVersions);
+        Assert.assertThat(map, Matchers.allOf(
+                Matchers.aMapWithSize(2),
+                Matchers.hasEntry("idA", Arrays.asList(new Version("1.0.0"), 
new Version("2.0.0"))),
+                Matchers.hasEntry("idB", null)));
+    }
+
+    @Test(expected=IllegalArgumentException.class)
+    public void testParseEntityIdsWithVersionsAndConflictingVersions() {
+        String[] entityIdsAndVersions = new String[] { "idA", "idA 2.0.0", 
"idB" };
+        
OsgiInstallerHealthCheck.parseEntityIdsWithVersions(entityIdsAndVersions);
+    }
+}

Reply via email to